OOP2

AbstractView.java

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