Java Reference
In-Depth Information
19. }
20. currentState = new CleanState();
21. }
22.
23. public void save(){
24. currentState.save();
25. }
26.
27. public void edit(){
28. currentState.edit();
29. }
30.
31. private class DirtyState implements State{
32. private State nextState;
33.
34. public DirtyState(State nextState){
35. this.nextState = nextState;
36. }
37.
38. public void save(){
39. FileLoader.storeData(appointmentFile, appointments);
40. currentState = nextState;
41. }
42. public void edit(){ }
43. }
44.
45. private class CleanState implements State{
46. private State nextState = new DirtyState(this);
47.
48. public void save(){ }
49. public void edit(){ currentState = nextState; }
50. }
51.
52. public ArrayList getAppointments(){
53. return appointments;
54. }
55.
56. public void addAppointment(Appointment appointment){
57. if (!appointments.contains(appointment)){
58. appointments.add(appointment);
59. }
60. }
61. public void removeAppointment(Appointment appointment){
62. appointments.remove(appointment);
63. }
64. }
The class StateGui provides an editing interface for the CalendarEditor's appointments. Notice that the GUI
has a reference to the CalendarEditor , and that it delegates and edit or save actions to the editor. This allows the
editor to perform the required actions and to update its state as appropriate.
Example A.99 StateGui.java
1. import java.awt.Container;
2. import java.awt.BorderLayout;
3. import java.awt.event.ActionListener;
4. import java.awt.event.WindowAdapter;
5. import java.awt.event.ActionEvent;
6. import java.awt.event.WindowEvent;
7. import javax.swing.BoxLayout;
8. import javax.swing.JButton;
9. import javax.swing.JComponent;
10. import javax.swing.JFrame;
11. import javax.swing.JPanel;
12. import javax.swing.JScrollPane;
13. import javax.swing.JTable;
14. import javax.swing.table.AbstractTableModel;
15. import java.util.Date;
16. public class StateGui implements ActionListener{
17. private JFrame mainFrame;
18. private JPanel controlPanel, editPanel;
19. private CalendarEditor editor;
20. private JButton save, exit;
21.
22. public StateGui(CalendarEditor edit){
23. editor = edit;
24. }
Search WWH ::




Custom Search