mvp
Class View

java.lang.Object
  |
  +--mvp.View
All Implemented Interfaces:
java.util.Observer, java.io.Serializable

public abstract class View
extends java.lang.Object
implements java.util.Observer, java.io.Serializable

Class View is an abstract parent class for view classes in an MVP design. See Fisher SE lecture notes for further discussion of the MVP design methodology.

View implements Observer since it is often convenient for a view to observe its companion model, particularly when a model has multiple views that all need to change simultaneously when the model changes. View implements Serializable as a convenience for serializing models that refer to views. Typically, view data are not themselves worthy of serialization, but when a model refers to a view, that view needs to be Serializable in principle in order for serialization to proceed without problems.

See Also:
Serialized Form

Field Summary
protected  java.awt.event.WindowAdapter closeAdapter
          The exit-on-close listener
protected  boolean editable
          True if this is editable
protected  Model model
          The unique companion model for this
protected  Screen screen
          The physical display screen for this.
protected  boolean shown
          True if the window is displayed
protected  java.awt.Component widget
          The outermost interactive element (i.e., "widget") for this.
protected  Window window
          The physical UI window for this.
 
Constructor Summary
View()
          Construct this with a null screen and model.
View(Screen screen, Model model)
          Construct a view with the given Screen and Model.
 
Method Summary
 java.awt.Component compose()
          Compose the interface components of this, setting the window and/or widget fields to the top-level of the composition.
 Model getModel()
          Return the model of this.
 java.awt.Component getWidget()
          Return the widget of this.
 Window getWindow()
          Return the window of this.
 void hide()
          Remove the window of this from the screen, thereby physically undisplaying it.
 boolean isEditable()
          Return true if this is currently editable.
 boolean isShown()
          Return true if this is currently displayed on the screen.
 void run()
          Run this by entering the screen's event loop, which will in turn display the window of this and any widgets within the window.
 void setEditable(boolean editable)
          Set the editability of this to the given boolean value.
 void setExitOnClose(boolean exitOnClose)
          Perform the necessary set up to call or not to call the companion model's exit method when the user closes this' window.
 void setModel(Model model)
          Set the model of this to the given model, if the model is not already set.
 void show()
          Insert the window of this into the screen, thereby physically displaying it.
 void show(int x, int y)
          Same specs as Show(), except the window is shown at the given x,y coordinate.
 void update(java.util.Observable o, java.lang.Object arg)
          Update the displayed data in this.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

model

protected Model model
The unique companion model for this


screen

protected Screen screen
The physical display screen for this. Only views that have a non-null Window (see below) need a value for the Screen. Otherwise, the value of the Screen member is null.


window

protected Window window
The physical UI window for this. The Window is the stand-alone top-level window for the view, displayed on the screen by the underlying window manager. For views that do not have a managed window, e.g., pulldown menu or dialog contained in another view, the Window member is null.


widget

protected java.awt.Component widget
The outermost interactive element (i.e., "widget") for this. In some toolkits, Window and Widget are the same type. In such cases, if a view has an non-null Window, then it does not need a value for Widget. In toolkits such as Java Swing, where Window and Widget are distinct types, a view may need both a value for the Window and Widget.


shown

protected boolean shown
True if the window is displayed


editable

protected boolean editable
True if this is editable


closeAdapter

protected java.awt.event.WindowAdapter closeAdapter
The exit-on-close listener

Constructor Detail

View

public View(Screen screen,
            Model model)
Construct a view with the given Screen and Model. Initialize other data members to null or false, as appropriate. The given screen is that in which the view will insert itself to be physically displayed. The model is the companion model in an MVP design.
 pre: screen != null;

 post: (this'.screen == screen) && (this'.model == model) && 
       (window' == null) && (shown' == false) &&
       (editable' == false) && (closeAdapter == false);
                                                                   


View

public View()
Construct this with a null screen and model. Initialize other data members to null or false, as appropriate.
 post: (this'.screen == null) && (this'.model == null) && 
       (window' == null) && (shown' == false) &&
       (editable' == false) && (closeAdapter == false);
                                                                   

Method Detail

run

public void run()
Run this by entering the screen's event loop, which will in turn display the window of this and any widgets within the window. In a non-modal GUI, only the topmost view will be run. In a modal GUI, run is specialized in View subclasses to contain a modal event loop that locks out events from other view components until the event loop has been completed, e.g., by the user selecting 'OK' or 'Cancel'.
 pre: not screen.isRunning();

 post: screen'.isRunning() &&
       forall (Component c | c in screen.getComponents())
           c.IsDisplayed();
                                                                   
where isRunning is an assumed predicate of Screen that returns true if the Screen's event-handling loop is running.


compose

public java.awt.Component compose()
Compose the interface components of this, setting the window and/or widget fields to the top-level of the composition. Note that compose does not physically display this, it only constructs its components so it may subsequently be displayed with the run and show methods. As a convenience to callers, compose returns the widget field of this, since it is frequently accessed after composition.
 post: return == widget;  // Compose must be specialized in subclasses.
                                                                   


show

public void show()
Insert the window of this into the screen, thereby physically displaying it. Upon insertion, the window will be the frontmost of all other screen windows. If this is already displayed, Show has the effect of moving its window to the front of all other windows.

To move the window to some position other than the front, extract the window with this.getWindow and manipulate it with methods available in the Window class.

The screen position of the window will be selected by the underlying window manager. To show the window of this at a specific screen position, use the overloaded version of Show specified below.

 pre: 
         
 post: if (window != null)
       then screen'.IsDisplayed(window) &&
            screen'.IsFrontmost(window) &&
            shown' == true;
       else System.out.println("error");
                                                                   


show

public void show(int x,
                 int y)
Same specs as Show(), except the window is shown at the given x,y coordinate. The x,y coordinate system is in units of screen pixels, with the 0,0 origin in the lower left corner of the screen. The given x,y coordinates refer to the lower left corder of the window.


hide

public void hide()
Remove the window of this from the screen, thereby physically undisplaying it.
 pre: ; // Note that the window may or may not be currently displayed
         
 post: not screen'.IsDisplayed(window) &&
       (shown' == false);
                                                                   


update

public void update(java.util.Observable o,
                   java.lang.Object arg)
Update the displayed data in this. The processing involved in this update is strictly view specific. Generally, the view will call one or more methods in the companion model to obtain the necessary data to perform the update.
 post: ;   // Update must be specialized in subclasses.
                                                                   

Specified by:
update in interface java.util.Observer

getModel

public Model getModel()
Return the model of this. post: return == model;


setModel

public void setModel(Model model)
Set the model of this to the given model, if the model is not already set. This setModel method is used if this must be constructed before its companion model is constructed, and therefore the model will not be available to pass to the constructor.
 pre: this.model == null;

 post: this.model' == model;
                                                                   


getWindow

public Window getWindow()
Return the window of this.
 post: return == window;
                                                                   


getWidget

public java.awt.Component getWidget()
Return the widget of this.
 post: return == widget;
                                                                   


isShown

public boolean isShown()
Return true if this is currently displayed on the screen.
 post: return == shown;
                                                                   


isEditable

public boolean isEditable()
Return true if this is currently editable. The editability of a view dictates whether the user is allowed to enter values in the view's components that are normally editable, e.g., string or text editors.
 post: return == editable;
                                                                   


setEditable

public void setEditable(boolean editable)
Set the editability of this to the given boolean value.
 post: this'.editable == editable;
                                                                   


setExitOnClose

public void setExitOnClose(boolean exitOnClose)
Perform the necessary set up to call or not to call the companion model's exit method when the user closes this' window. When exitOnClose is true, model.exit is called upon window close; when exitOnClose is false, model.exit is not called. From the user's perspective, the close is performed using a command provided by the underlying window manager, e.g., a window close button.
 pre: (window != null)

 post: if (exitOnClose == true)
       then (exists (WindowAdapter wa)
               (wa in window.getListeners(WindowAdapter.class)) &&
               (wa.getClass().getDeclaredMethod(
                 "windowClosing", {WindowEvent.class})).invokes(
                   model.getClass().getDeclaredMethod("exit", null)) &&
               (closeAdapter' == wa))
       else !(exists (WindowAdapter wa)
               (wa in window.getListeners(WindowAdapter.class)) &&
               (wa.getClass().getDeclaredMethod(
                 "windowClosing", {WindowEvent.class})).invokes(
                   model.getClass().getDeclaredMethod("exit", null))) &&
               (closeAdapter' == null)