Package sim.display

Class SimpleController

java.lang.Object
sim.display.SimpleController
All Implemented Interfaces:
Controller

public class SimpleController extends Object implements Controller
  • Field Details

    • PS_STOPPED

      public static final int PS_STOPPED
      The play thread is presently stopped.
      See Also:
    • PS_PLAYING

      public static final int PS_PLAYING
      The play thread is presently playing.
      See Also:
    • PS_PAUSED

      public static final int PS_PAUSED
      The play thread is presently paused.
      See Also:
  • Constructor Details

    • SimpleController

      public SimpleController(GUIState simulation)
    • SimpleController

      public SimpleController(GUIState simulation, boolean displayInspectors)
  • Method Details

    • getSimulation

      public GUIState getSimulation()
    • getPlayState

      public int getPlayState()
      Gets whether or not the current thread is PS_PLAYING, PS_STOPPED, or PS_PAUSED.
    • doClose

      public void doClose()
      Closes the Controller and shuts down the simulation. Quits the program only if other simulations are not running in the same program. Called when the user clicks on the close button of the Console, or during a program-wide doQuit() process. Can also be called programmatically.
    • setIncrementSeedOnPlay

      public void setIncrementSeedOnPlay(boolean val)
      Deprecated.
      renamed to setIncrementSeedOnStop
    • getIncrementSeedOnPlay

      public boolean getIncrementSeedOnPlay()
      Deprecated.
      renamed to getIncrementSeedOnStop
    • setIncrementSeedOnStop

      public void setIncrementSeedOnStop(boolean val)
    • getIncrementSeedOnStop

      public boolean getIncrementSeedOnStop()
    • pressStop

      public void pressStop()
      Called when the user presses the stop button. You can call this as well to simulate the same.
    • pressPause

      public void pressPause()
      Called when the user presses the pause button. You can call this as well to simulate the same. Keep in mind that pause is a toggle.
    • pressPlay

      public void pressPlay()
      Called when the user presses the play button. You can call this as well to simulate the same. Keep in mind that play will change to step if pause is down.
    • registerFrame

      public boolean registerFrame(JFrame frame)
      Simulations can call this to add a frame to be listed in the "Display list" of the console
      Specified by:
      registerFrame in interface Controller
    • unregisterFrame

      public boolean unregisterFrame(JFrame frame)
      Simulations can call this to remove a frame from the "Display list" of the console
      Specified by:
      unregisterFrame in interface Controller
    • unregisterAllFrames

      public boolean unregisterAllFrames()
      Simulations can call this to clear out the "Display list" of the console
      Specified by:
      unregisterAllFrames in interface Controller
    • doChangeCode

      public void doChangeCode(Runnable r)
      Deprecated.
      Description copied from interface: Controller
      This method will interrupt the simulation (pause it), call your runnable, then continue (uninterrupt) the simulation. This allows you to guarantee a way to change the model from a separate thread -- for example, the Swing event thread -- in a synchronous, blocking fashion.

      You have other options for updating the model from external threads. One option is to add a Steppable to GUIState's scheduleImmediate(...) queue. When the Steppable is stepped, it will be done so inside the model's thread. This is asynchronous (non-blocking), however.

      Alternatively, you can synchronize on state.schedule and run your code. This is synchronous.

      Specified by:
      doChangeCode in interface Controller
    • refresh

      public void refresh()
      Description copied from interface: Controller
      Lazily updates and redraws all the displays and inspectors. Do not call this method from the model thread -- only from the Swing event thread. This is an expensive procedure and should not be done unless necessary. Typically it's done in response to some event (a button press etc.) rather than in the model itself.
      Specified by:
      refresh in interface Controller
    • setInspectors

      public void setInspectors(Bag inspectors, Bag names)
      Adds new inspectors to the Console's list, given the provided inspectors, their portrayals, and appropriate names for them. These bags must match in size, else an exception will be thrown.
      Specified by:
      setInspectors in interface Controller
    • registerInspector

      public void registerInspector(Inspector inspector, Stoppable stopper)
      Registers an inspector to be Stopped if necessary in the future. This automatically happens if you call setInspectors(...).
      Specified by:
      registerInspector in interface Controller
    • stopAllInspectors

      public void stopAllInspectors(boolean killDraggedOutWindowsToo)
      Stops all inspectors. If killDraggedOutWindowsToo is true, then the detatched inspectors are stopped as well.
    • removeAllInspectors

      public void removeAllInspectors(boolean killDraggedOutWindowsToo)
      Stops and removes all inspectors. If killDraggedOutWindowsToo is true, then all inspector windows will be closed; else only the inspectors presently embedded in the console will be stopped and removed.
    • doNew

      public boolean doNew()
      Calls forth the "New Simulation" window.
    • getAllInspectors

      public ArrayList getAllInspectors()
      Returns a list of all current inspectors. Some of these inspectors may be stored in the SimpleController itself, and others may have been dragged out into their own JFrames. You will need to distinguish between these two on your own. Note that some of these inspectors are stored as weak keys in the SimpleController, so holding onto this list will prevent them from getting garbage collected. As a result, you should only use this list for temporary scans.
      Specified by:
      getAllInspectors in interface Controller
    • getAllFrames

      public ArrayList getAllFrames()
      Returns a list of all displays. You own the resulting list and can do what you like with it.
      Specified by:
      getAllFrames in interface Controller