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