1# 2# AppController.py 3# ManualBindings 4# 5from Cocoa import * 6 7class AppController (NSObject): 8 arrayController = objc.IBOutlet() 9 selectedNameField = objc.IBOutlet() 10 selectedPriceField = objc.IBOutlet() 11 tableView = objc.IBOutlet() 12 totalCountField = objc.IBOutlet() 13 14 def awakeFromNib(self): 15 self.totalCountField.bind_toObject_withKeyPath_options_(u"value", self.arrayController, u"arrangedObjects.@sum.price", None) 16 bindingOptions = {} 17 bindingOptions[u'NSNullPlaceholder'] = u"No Name" 18 self.selectedNameField.bind_toObject_withKeyPath_options_(u"value", self.arrayController, u"selection.name", bindingOptions) 19 # binding for "name" column 20 tableColumn = self.tableView.tableColumnWithIdentifier_(u'name') 21 tableColumn.bind_toObject_withKeyPath_options_(u"value", self.arrayController, u"arrangedObjects.name", bindingOptions) 22 23 # binding options for "price" 24 del bindingOptions[u'NSNullPlaceholder'] 25 bindingOptions[NSValidatesImmediatelyBindingOption] = True 26 27 # binding for selected "price" field 28 self.selectedPriceField.bind_toObject_withKeyPath_options_(u"value", self.arrayController, u"selection.price", bindingOptions) 29 30 #binding for "price" column 31 tableColumn = self.tableView.tableColumnWithIdentifier_(u'price') 32 tableColumn.bind_toObject_withKeyPath_options_(u"value", self.arrayController, u"arrangedObjects.price", bindingOptions) 33 34 # bind array controller to self's itemsArray 35 # we use _k_itemsArray because Python does not have a separate 36 # namespace for instance variables, and we are using accessors. 37 self._k_itemsArray = [] 38 self.arrayController.bind_toObject_withKeyPath_options_(u"contentArray", self, u"self.itemsArray", None) 39 40 @objc.accessor 41 def countOfItemsArray(self): 42 return len(self._k_itemsArray) 43 44 @objc.accessor 45 def objectInItemsArrayAtIndex_(self, index): 46 return self._k_itemsArray[index] 47 48 @objc.accessor 49 def insertObject_inItemsArrayAtIndex_(self, obj, idx): 50 self._k_itemsArray.insert(idx, obj) 51 52 @objc.accessor 53 def removeObjectFromItemsArrayAtIndex_(self, idx): 54 del self._k_itemsArray[idx] 55 56 @objc.accessor 57 def replaceObjectInItemsArrayAtIndex_withObject_(self, idx, obj): 58 self._k_itemsArray[idx] = obj 59 60ITEM_ERROR_DOMAIN = u'ITEM_ERROR_DOMAIN' 61ITEM_NEGATIVE_PRICE = 10001 62 63class Item(NSObject): 64 def price(self): 65 return getattr(self, '_k_price', 0.0) 66 67 def setPrice_(self, aPrice): 68 self._k_price = aPrice 69 70 def name(self): 71 return getattr(self, '_k_name', None) 72 73 def setName_(self, aName): 74 self._k_name = aName 75 76 @objc.accessor 77 def validatePrice_error_(self, value, error): 78 if value >= 0: 79 return True, value, None 80 81 errorString = u'Price cannot be negative' 82 userInfoDict = {NSLocalizedDescriptionKey: errorString} 83 error = NSError.alloc().initWithDomain_code_userInfo_( 84 ITEM_ERROR_DOMAIN, 85 ITEM_NEGATIVE_PRICE, 86 userInfoDict) 87 88 return False, value, error 89