home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / Controller.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  5.6 KB  |  191 lines

  1. " ------------------------------------------------------------
  2. * A Controller coordinates a model, and user actions.  It
  3. * provides scheduling (control) behavior to determine when the
  4. * user wants to communicate with the model or view.
  5. * ------------------------------------------------------------ 
  6. "
  7. Class Controller :Object ! myModel myWindow !
  8. [
  9.    initialize
  10.  
  11.       " Initialize the state of the receiver. Subclasses should 
  12.       * include 'super initialize' when redefining this message 
  13.       * to insure proper initialization.
  14.       "
  15.  
  16.       "Controller initialize"
  17.       ^ self
  18. |
  19.    window
  20.  
  21.       ^ myWindow
  22. |
  23.    window: newWindow
  24.  
  25.       myWindow <- newWindow
  26. |
  27.    release
  28.  
  29.      " Breaks the cycle between the receiver and its model. 
  30.       * It is usually not necessary to send release provided 
  31.       * the receiver's model has been properly 
  32.       * released independently.
  33.       "
  34.       myModel <- nil.
  35. |
  36.    isInWorld
  37.  
  38.      ^ (myWindow ~~ nil)
  39. |
  40.    model
  41.  
  42.       " Answer the receiver's model which is the same as the 
  43.       * model of the receiver's view.
  44.       "
  45.       ^ myModel
  46. |
  47.    model: aModel 
  48.  
  49.       " Controller/model: is sent in 
  50.       * order to coordinate the links between the model, and 
  51.       * controller.  In ordinary usage, the receiver is created 
  52.       * and passed as the parameter to Model/addDependent: 
  53.       * so that the receiver's model and controller links 
  54.       * can be set up by the model.
  55.       "
  56.       myModel <- aModel
  57. |
  58.    refreshView
  59.       (myModel notNil)
  60.         ifTrue: [ myModel value ].
  61. |
  62.    controlInitialize
  63.  
  64.       " Sent by Controller | startUp as part of the 
  65.       * standard control sequence, it provides a place in 
  66.       * the standard control sequence for initializing the 
  67.       * receiver (taking into account the current state of 
  68.       * its model and controller).  It should be redefined 
  69.       * in subclasses to perform some specific action.
  70.       "
  71.       ^ self
  72. |
  73.    controlLoop 
  74.  
  75.       " Sent by Controller|startUp as part of the standard 
  76.       * control sequence.  Controller|controlLoop sends the 
  77.       * message Controller|isControlActive to test for loop 
  78.       * termination.  As long as true is returned, the loop 
  79.       * continues.  When false is returned, the loop ends. 
  80.       * Each time through the loop, the 
  81.       * message Controller|controlActivity is sent.
  82.       "
  83.       [self isControlActive] 
  84.          whileTrue: [ self controlActivity ]
  85. |
  86.    controlTerminate
  87.  
  88.       " Provide a place in the standard control sequence 
  89.       * for terminating the receiver (taking into account 
  90.       * the current state of its model and controller).  It 
  91.       * should be redefined in subclasses to perform some 
  92.       * specific action.
  93.       "
  94.       ^ self
  95. |
  96.    startUp
  97.  
  98.       " Give control to the receiver.  The default control 
  99.       * sequence is to initialize (see 
  100.       * Controller|controlInitialize), to loop 
  101.       * (see Controller|controlLoop), and then to terminate 
  102.       * (see Controller|controlTerminate).  After this 
  103.       * sequence, control is returned to the sender of 
  104.       * Control|startUp.  The receiver's control sequence is 
  105.       * used to coordinate the interaction of its controller 
  106.       * and model.  In general, this consists of waiting for 
  107.       * user input, testing the input with respect to the 
  108.       * current display of the controller, and updating the 
  109.       * model to reflect intended changes.
  110.       "
  111.       self controlInitialize.
  112.       self controlLoop.
  113.       self controlTerminate
  114. |
  115.    terminateAndInitializeAround: aBlock
  116.  
  117.       self controlTerminate.
  118.  
  119.       aBlock value.
  120.  
  121.       self controlInitialize
  122. |
  123.    controlActivity
  124.  
  125.       " Pass control to the next control level (that is, 
  126.       * to the Controller of a subView of the receiver's 
  127.       * view) if possible. It is sent by 
  128.       * Controller/controlLoop each time through the main 
  129.       * control loop.
  130.       "
  131.       self checkControls.
  132. |
  133.    checkControls 
  134.       
  135.       " This must be redefined by subclasses to do something
  136.       * useful, including telling the Model that changes 
  137.       * have been made by the User.
  138.       "
  139.       ^ nil
  140. |
  141.    isControlActive
  142.  
  143.       " Answer whether receiver wishes to continue evaluating 
  144.       * its controlLoop method.  It is sent by 
  145.       * Controller/controlLoop in order to determine when the 
  146.       * receiver's control loop should terminate, and should 
  147.       * be redefined in a subclass if some special condition 
  148.       * for terminating the main control loop is needed.
  149.       "
  150.       ^ self viewHasMouse
  151. |
  152.    refreshYourself
  153.  
  154.      " Since the User clicks on GUI elements & enters text 
  155.      * into GUI elements, the Controller already has placed
  156.      * the View Data into the View & sent the changes to 
  157.      * the Model so all we have to do is:
  158.      "
  159.      myWindow refreshYourself   " Into Window.st on 09-Nov-2002 "
  160. |
  161.    viewHasMouse ! mousePt tl br xCoord yCoord !
  162.  
  163.       " Answer whether the mouse pointer is in the 
  164.       * receiver's view. Controller/viewHasMouse is normally
  165.       * used in internal methods.
  166.       "
  167.      (myWindow isNil)
  168.        ifFalse: [ mousePt <- myWindow getMouseCoords.
  169.                   tl      <- myWindow getOrigin.
  170.                   br      <- myWindow getWindowSize.
  171.                   xCoord  <- Interval new.
  172.                   yCoord  <- Interval new.
  173.                   
  174.                   xCoord from: tl x to: br x.
  175.                   yCoord from: tl y to: br y.
  176.                   
  177.                   (xCoord inRange: (mousePt x)
  178.                      and: [yCoord inRange: (mousePt y)])
  179.                    
  180.                      ifTrue:  [ ^ true ] ].
  181.      ^ false
  182. |
  183.    superController
  184.  
  185.      ^ nil  " SubControllers should override this. "
  186. |
  187.    new
  188.  
  189.      ^ super new  " initialize "
  190. ]
  191.