Java Reference
In-Depth Information
14. try{
15. appointments = (ArrayList)FileLoader.loadData(appointmentFile);
16. }
17. catch (ClassCastException exc){
18. System.err.println("Unable to load information. The file does not contain a list of
appointments.");
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 edit or save actions to the editor. This allows the
editor to perform the required actions and to update its state as appropriate.
Example 2.40 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;
Search WWH ::




Custom Search