AbstractView.java
1 |
|
2 |
import java.util.Observer; |
3 |
|
4 |
/** |
5 |
* |
6 |
* @author jvermeulen |
7 |
*/ |
8 |
public abstract class AbstractView implements View, Observer { |
9 |
|
10 |
private Observable mModel; |
11 |
private Controller mController; |
12 |
|
13 |
/** |
14 |
* Empty constructor so that the model and controller can be set later. |
15 |
*/ |
16 |
public AbstractView() { |
17 |
} |
18 |
|
19 |
public AbstractView(Observable model, Controller controller) { |
20 |
// Set the model. |
21 |
setModel(model); |
22 |
// If a controller was supplied, use it. Otherwise let the first call to |
23 |
// getController() create the default controller. |
24 |
if (controller != null) { |
25 |
setController(controller); |
26 |
} |
27 |
} |
28 |
|
29 |
@Override |
30 |
public void setController(Controller controller) { |
31 |
mController = controller; |
32 |
// Tell the controller this object is its view. |
33 |
getController().setView(this); |
34 |
} |
35 |
|
36 |
@Override |
37 |
public Controller getController() { |
38 |
// If a controller hasn't been defined yet... |
39 |
if (mController == null) { |
40 |
// ...make one. Note that defaultController is normally overriden by |
41 |
// the AbstractView subclass so that it returns the appropriate |
42 |
// controller for the view. |
43 |
setController(defaultController(getModel())); |
44 |
} |
45 |
|
46 |
return mController; |
47 |
} |
48 |
|
49 |
@Override |
50 |
public void setModel(Observable model) { |
51 |
mModel = model; |
52 |
} |
53 |
|
54 |
@Override |
55 |
public Observable getModel() { |
56 |
return mModel; |
57 |
} |
58 |
|
59 |
@Override |
60 |
public Controller defaultController(Observable model) { |
61 |
return null; |
62 |
} |
63 |
|
64 |
/** |
65 |
* A do-nothing implementation of the Observer interface's update method. |
66 |
* Subclasses of AbstractView will provide a concrete implementation for |
67 |
* this method. |
68 |
*/ |
69 |
@Override |
70 |
public void update(Observable o, Object arg) { |
71 |
} |
72 |
|
73 |
} |
74 |