home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RB3641.ZIP / EX2 / TODOITEM.CLS < prev    next >
Text File  |  1991-12-16  |  5KB  |  176 lines

  1.  
  2. Object subclass: #ToDoItem
  3.   instanceVariableNames: 
  4.     'type description priority deadline completed '
  5.   classVariableNames: ''
  6.   poolDictionaries: 
  7.     'CharacterConstants ' !
  8.  
  9.  
  10. !ToDoItem class methods !
  11.  
  12. new
  13.     
  14.     ^super new! !
  15.  
  16.  
  17.  
  18. !ToDoItem methods !
  19.    
  20. asParameter
  21.  
  22.     ^(' ' asAsciiZ)!
  23.  
  24. asString
  25.  
  26.     | compStr |
  27.     compStr := ''.
  28.     (completed = true)  ifTrue: [ compStr := 'true' ].
  29.     (completed = false) ifTrue: [ compStr := 'false' ].
  30.     ^type,description,deadline,priority,compStr!
  31.  
  32. compareTo: aToDoItem using: aSortKey
  33.  
  34.     "Returns true if the receiver ToDoItem is less than or equal
  35.      to the argument ToDoItem, using the SortKey as the element
  36.      to be compared.
  37.  
  38.      This method was originally four IF statements. After working
  39.      with Smalltalk for a while longer it was obvious that this
  40.      case statement-like processing wasn't necessary in OO, if we
  41.      could work out how to do it otherwise.
  42.  
  43.      What happens here is that aSortKey (which has the value priority
  44.      or deadline etc, is converted into a symbol (unique occurance in
  45.      the system, like PM Atoms) which can then be used as the name of
  46.      a method. The perform: method executes the argument it is given
  47.      by sending it as a message to self (ie the item). So this one-liner
  48.      sets up the sort block as being a comparison between the two items'
  49.      values of one of their instance variables (like type or description).
  50.  
  51.      If aSortKey was equal to type then the expression would be
  52.  
  53.      ^self type <= aToDoItem type                                      "
  54.  
  55. ^(self perform: (aSortKey asSymbol)) <= (aToDoItem perform: (aSortKey asSymbol))!
  56.   
  57. completed
  58.     "returns the value of completed"
  59.  
  60.     ^completed!
  61.   
  62. completed: aValue
  63.     "Sets the value of completed"
  64.  
  65.     completed := aValue.!
  66.    
  67. deadline
  68.  
  69.     "Returns the value of deadline. Provided so that the instance
  70.      variables values can be seen by other objects."
  71.  
  72. ^deadline!
  73.    
  74. deadline: aDeadline
  75.  
  76.     "Sets the value of deadline to the argument aDeadline. Allows
  77.      other objects to alter the value of deadline. In the ideal OO
  78.      world this is the only place where a change in the structure
  79.      of deadline means any changes have to be made to the access
  80.      routines. Every object that is interested in setting deadline
  81.      has to come through here so one change is all you need."
  82.  
  83. deadline := aDeadline.!
  84.  
  85. description
  86.     "returns the value of description"
  87.  
  88.     ^description!
  89.  
  90. description: aDescription
  91.     "sets the value of description"
  92.  
  93.     description := aDescription.!
  94.  
  95. getItemText
  96.    "*********** Added 12/9 ochiai ************"
  97.         "Return a string containing all the fields
  98.          of a ToDoItem ( type, decription, deadline and priority).
  99.          Priority value is replaced by corresponding value in
  100.          ToDoPriorityList dictionary. A new instance of this
  101.          dictionary is created before using it."
  102.  
  103.          | answer       compStr |
  104.  
  105.         "Here a new string is initialised and accessed as a stream
  106.          so that nextPutAll can be used. This is similar to writing
  107.          to a file."
  108.  
  109.         answer := WriteStream on: (String new: 120).
  110.  
  111.         "The string is filled with the instance variables' values.
  112.          The leftOf: method is an addition to String class. We should
  113.          not really have hardcoded numbers here, they are dependant on
  114.          the text limits set in ToDoAddDialogBox and in textHeader."
  115.  
  116.     compStr := ''.
  117.     (completed = true)  ifTrue: [ compStr := 'Done' ].
  118.     (completed = false) ifTrue: [ compStr := '        ' ].
  119.  
  120.         answer   nextPutAll: ((self deadline) leftOf:8);
  121.             nextPutAll: '  ';
  122.             nextPutAll: ((self type) leftOf: 8);
  123.             nextPutAll: '  ';
  124.             nextPutAll: ((self description) leftOf: 30);
  125.             nextPutAll: '  '; 
  126.             nextPutAll: (((ToDoPriorityList new class init) keyAtValue:  priority) leftOf: 4); 
  127.            " nextPutAll: ((self priority) leftOf: 4); "
  128.             nextPutAll: '  '; 
  129.             nextPutAll: ( compStr).
  130.  
  131.  
  132. "The contents of the string are returned."
  133.  
  134. ^answer contents!
  135.  
  136. printYourself
  137.  
  138.         "Print the text lines."
  139.     | aHandle |
  140.     CursorManager execute change.
  141.     aHandle := FileHandle openDevice: 'LPT1'.
  142.     aHandle deviceWrite: '***************************'.
  143.     aHandle deviceWrite: (String with: Lf with: Cr).
  144.     aHandle deviceWrite: (deadline, '  ',  type, '  ', description, '  ', priority).
  145.     aHandle deviceWrite: (String with: Lf with: Cr).
  146. "    aHandle deviceWrite: 'ToDo item printed'."
  147.     aHandle deviceWrite: (String with: Lf with: Cr).
  148.     aHandle close.
  149.     CursorManager normal change!
  150.    
  151. priority
  152.     "returns the value of priority"
  153.     ^priority!
  154.    
  155. priority: aPriority
  156.     "sets the value of priority"
  157.  
  158.     priority := aPriority!
  159.  
  160. template
  161.         "Give a template"
  162.  
  163.                    self type: 'Personal'.
  164.                 self deadline: '99.12.31'.
  165.                 self description: 'Celebrate!!'.
  166.                 self priority: 'High'.
  167.                 self completed: false.!
  168.  
  169. type
  170.     "returns the value of type"
  171.     ^type!
  172.    
  173. type: aType
  174.     "sets the value of type"
  175.     type := aType.! !
  176.