1# 2# Category.py 3# ToDos 4# 5# Converted by u.fiedler on 09.02.05. 6# 7# The original version was written in Objective-C by Malcolm Crawford 8# at http://homepage.mac.com/mmalc/CocoaExamples/controllers.html 9 10from Foundation import * 11import objc 12 13 14class Category(NSObject): 15 title = objc.ivar('title') 16 priority = objc.ivar('priority', 'i') 17 18 @classmethod 19 def allCategories(cls): 20 """Predefined global list of categories""" 21 return categories 22 23 @classmethod 24 def categoryForPriority_(cls, thePriority): 25 for category in categories: 26 if thePriority >= category.priority: 27 return category 28 return None 29 30 @classmethod 31 def categoryWithTitle_andPriority_(cls, aTitle, aValue): 32 """Convenience constructor""" 33 newCategory = Category.alloc().init() 34 newCategory.title = aTitle 35 newCategory.priority = aValue 36 return newCategory 37 38 39 # NSCoding methods 40 # To encode, simply save 'priority'; on decode, replace self with 41 # the existing instance from 'allCategories' with the same priority 42 43 def encodeWithCoder_(self, encoder): 44 if encoder.allowsKeyedCoding(): 45 encoder.encodeInt_forKey_(self.priority, u"priority") 46 else: 47 encoder.encodeObject_(self.priority) 48 49 def initWithCoder_(self, decoder): 50 if decoder.allowsKeyedCoding(): 51 thePriority = decoder.decodeIntForKey_(u"priority") 52 else: 53 thePriority = decoder.decodeObject() 54 return Category.categoryForPriority_(thePriority) 55 56 57categories = [ 58 Category.categoryWithTitle_andPriority_(u"Vital", 11), 59 Category.categoryWithTitle_andPriority_(u"Very Important", 4), 60 Category.categoryWithTitle_andPriority_(u"Important", 3), 61 Category.categoryWithTitle_andPriority_(u"Not Important", 2), 62 Category.categoryWithTitle_andPriority_(u"Whenever", 0) 63] 64