ASSelectPropertyCell.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // ASIntPropertyCell.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2023/4/17.
  6. //
  7. import Cocoa
  8. class ASSelectPropertyCell: ASPropertyCell {
  9. @IBOutlet var m_titleLbl:NSTextField!
  10. @IBOutlet var m_comboBox:NSComboBox!
  11. override class func shared() -> ASPropertyCell? {
  12. var objects : NSArray!
  13. Bundle.main.loadNibNamed("ASSelectPropertyCell", owner: nil, topLevelObjects: &objects)
  14. for tView in objects {
  15. if let tv = tView as? ASSelectPropertyCell {
  16. return tv
  17. }
  18. }
  19. return nil
  20. }
  21. /// Setter
  22. override func setPropertyInfo(_ info:NSDictionary) {
  23. super.setPropertyInfo(info)
  24. loadComboxItem()
  25. self.m_titleLbl.stringValue = "\(info.value(forKey: "Name") ?? ""):"
  26. setValue((info.value(forKey: "DefaultValue") ?? "0") as AnyObject)
  27. }
  28. override func setValue(_ value:AnyObject) {
  29. super.setValue(value)
  30. let values = self.m_propertyInfo?.value(forKey: "Values") as? [NSDictionary]
  31. if (values != nil) {
  32. for tValue in values! {
  33. if ((tValue.value(forKey: "Value") as! AnyObject).isEqual(to: value)) {
  34. let index = values!.firstIndex(of: tValue) ?? 0
  35. if (index != NSNotFound) {
  36. self.m_comboBox.selectItem(at: index);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. override func value() -> AnyObject {
  43. let index = self.m_comboBox.indexOfSelectedItem
  44. let values = self.m_propertyInfo?.value(forKey: "Values") as! [NSDictionary]
  45. if (values.count > index) {
  46. return ((values[index] as NSDictionary).value(forKey: "Value") as? AnyObject) ?? NSNumber(value: 0)
  47. }
  48. return NSNumber(value: 0)
  49. }
  50. override func setEnabled(_ enabled:Bool) {
  51. super.setEnabled(enabled)
  52. self.alphaValue = enabled ? 1 : 0.6
  53. self.m_comboBox.isEnabled = enabled;
  54. }
  55. /// load Items
  56. func loadComboxItem() {
  57. m_comboBox.removeAllItems()
  58. let values = self.m_propertyInfo?.value(forKey: "Values") as! [NSDictionary]
  59. for value in values {
  60. m_comboBox.addItem(withObjectValue: value.value(forKey: "Name"))
  61. }
  62. }
  63. }