OOP2

ClockTools.java

1
package be.uhasselt.oo2.mvc.app.clock;
2
3
import be.uhasselt.oo2.mvc.AbstractView;
4
import be.uhasselt.oo2.mvc.Controller;
5
import java.awt.GridLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.util.Observable;
9
import javax.swing.JButton;
10
import javax.swing.JComponent;
11
import javax.swing.JPanel;
12
13
/**
14
 *
15
 * @author jvermeulen
16
 */
17
public class ClockTools extends AbstractView {
18
    // The user interface for this view.
19
    private JPanel mTools;
20
    private JButton mStart;
21
    private JButton mStop;
22
    private JButton mReset;
23
    
24
    public ClockTools(Observable model, ClockController controller) {
25
        super(model, controller);
26
        init();
27
    }
28
29
    private void init() {
30
        mTools = new JPanel();
31
        mTools.setLayout(new GridLayout(1, 3));
32
        mStart = new JButton("Start");
33
        mStart.setEnabled(false);
34
        mStop = new JButton("Stop");
35
        mStop.setEnabled(false);
36
        mReset = new JButton("Reset");
37
        
38
        // handle events: pass to controller
39
        mStart.addActionListener(new ActionListener() {
40
41
            @Override
42
            public void actionPerformed(ActionEvent e) {
43
                ((ClockController)getController()).onStart();
44
            }
45
        });
46
        
47
        mStop.addActionListener(new ActionListener() {
48
49
            @Override
50
            public void actionPerformed(ActionEvent e) {
51
                ((ClockController)getController()).onStop();
52
            }
53
        });
54
                
55
        mReset.addActionListener(new ActionListener() {
56
57
            @Override
58
            public void actionPerformed(ActionEvent e) {
59
                ((ClockController)getController()).onReset();
60
            }
61
        });
62
        
63
        mTools.add(mStart);
64
        mTools.add(mStop);
65
        mTools.add(mReset);       
66
    }
67
    
68
        /**
69
     * Updates the state of the clock tools.
70
     * Invoked automatically by ClockModel.
71
     * @param o The ClockModel object that is broadcasting an update
72
     * @param info A ClockUpdate instance describing the changes that have 
73
     * occurred in the ClockModel
74
     */
75
    @Override
76
    public void update(Observable o, Object info) {    
77
        // Cast info to ClockUpdate type.
78
        ClockUpdate clockInfo = (ClockUpdate) info;
79
        
80
        if (clockInfo.isRunning()) {
81
            mStop.setEnabled(true);
82
            mStart.setEnabled(false);
83
        } else {
84
            mStart.setEnabled(true);
85
            mStop.setEnabled(false);
86
        }
87
    }
88
    
89
    @Override
90
    public Controller defaultController(Observable model) {
91
        return new ClockController(model);        
92
    }
93
    
94
    /**
95
     * Convenience method to return the user interface component. We don't need 
96
     * this if we implement View directly and directly subclass a GUI component.
97
     * @return 
98
     */
99
    public JComponent getUI() {
100
        return mTools;
101
    }
102
}
103