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