/Users/lyon/j4p/src/addBk/addressBook/AddressBookMediator.java

1    package addBk.addressBook; 
2     
3    //AddressDisplay.java 
4     
5     
6    public class AddressBookMediator { 
7        private static AddressBookRecord storageRecord; 
8        private static boolean editFlag = false; 
9        private static boolean emptyDb = true; 
10    
11       private static AddressBookFrame adf = 
12               new AddressBookFrame(); 
13       private static AddressBookDatabase abd = 
14               new AddressBookDatabase(); 
15       private static AddressBookMediator display = 
16               getAddressDisplay(); 
17       private final AddressBookFrame addressBookFrame = 
18               new AddressBookFrame(); 
19    
20       public static void setTextFieldValues( 
21               AddressBookRecord abr) { 
22           getAdf().getTf().setText(abr.name); 
23           getAdf().getAp().t0.setText(abr.address); 
24           getAdf().getAp().t1.setText(abr.notes); 
25           getAdf().getTbp().tfp.t0.setText( 
26                   abr.dial_1); 
27           getAdf().getTbp().tfp.t1.setText( 
28                   abr.dial_2); 
29           getAdf().getTbp().tfp.t2.setText( 
30                   abr.dial_3); 
31           getAdf().getEditPanel().tfp.t0.setText( 
32                   abr.dial_1); 
33           getAdf().getEditPanel().tfp.t1.setText( 
34                   abr.dial_2); 
35           getAdf().getEditPanel().tfp.t2.setText( 
36                   abr.dial_3); 
37       } 
38    
39       public AddressBookRecord getTextFieldValues() { 
40           AddressBookRecord abr = 
41                   new AddressBookRecord(); 
42           abr.name = getAdf().getTf().getText(); 
43           abr.address = 
44           getAdf().getAp().t0.getText(); 
45           abr.notes = 
46           getAdf().getAp().t1.getText(); 
47           if (!AddressBookFrame.isEditMode()) { 
48               abr.dial_1 = 
49               getAdf().getTbp().tfp.t0.getText(); 
50               abr.dial_2 = 
51               getAdf().getTbp().tfp.t1.getText(); 
52               abr.dial_3 = 
53               getAdf().getTbp().tfp.t2.getText(); 
54           } else { 
55               abr.dial_1 = 
56               getAdf().getEditPanel().tfp.t0.getText(); 
57               abr.dial_2 = 
58               getAdf().getEditPanel().tfp.t1.getText(); 
59               abr.dial_3 = 
60               getAdf().getEditPanel().tfp.t2.getText(); 
61           } 
62           return (abr); 
63       } 
64    
65       public void nextRecord() { 
66           getAbd().incrementRecord(); 
67           setStorageRecord((getAbd().readRecord())); 
68           AddressBookMediator.setTextFieldValues( 
69                   getAbd().readRecord()); 
70       } 
71    
72       public void prevRecord() { 
73           getAbd().decrementRecord(); 
74           setStorageRecord((getAbd().readRecord())); 
75           AddressBookMediator.setTextFieldValues( 
76                   getAbd().readRecord()); 
77       } 
78    
79       public void addRecord() { 
80           AddressBookRecord abr = getAddressDisplay() 
81                   .getTextFieldValues(); 
82           getAbd().addRecord(abr); 
83           setStorageRecord((getAbd().readRecord())); 
84           getAdf().getIndexPanel().indexList.add( 
85                   abr.name); 
86           getAddressDisplay().setIndexItems(); 
87           AddressBookMediator.setEditFlag(false); 
88       } 
89    
90       public void deleteCurrentRecord() { 
91           getAbd().deleteCurrentRecord(); 
92           getAddressDisplay().setIndexItems(); 
93           AddressBookMediator.setEditFlag(false); 
94           setStorageRecord((getAbd().readRecord())); 
95       } 
96    
97       public void saveCurrentRecord() { 
98           System.out.println( 
99                   "saving current record"); 
100          getAbd().saveCurrentRecord( 
101                  getTextFieldValues()); 
102          getAbd().sort(); 
103          getAddressDisplay().setIndexItems(); 
104          setStorageRecord((getAbd().readRecord())); 
105          AddressBookMediator.setEditFlag(false); 
106      } 
107   
108      public void revertRecord() { 
109          //System.out.println("reverting current record"); 
110          getAbd().saveCurrentRecord( 
111                  getStorageRecord()); 
112          setStorageRecord((getAbd().readRecord())); 
113          AddressBookMediator.setTextFieldValues( 
114                  getAbd().readRecord()); 
115          AddressBookMediator.setEditFlag(false); 
116      } 
117   
118      public void setEditFlag() { 
119          AddressBookMediator.setEditFlag(true); 
120      } 
121   
122      public void toggleIndexDisplay() { 
123          if (AddressBookFrame.isIndexToggle() == 
124              true) { 
125              AddressBookFrame.setIndexToggle( 
126                      false); 
127              getAdf().init(); 
128          } else { 
129              AddressBookFrame.setIndexToggle(true); 
130              getAdf().init(); 
131          } 
132      } 
133   
134      public void setIndexItems() { 
135          getAdf().getIndexPanel().indexList.removeAll(); 
136          for (int i = 0; i < 
137                          (getAbd().getNumRecords()); i++) { 
138              AddressBookRecord abr = getAbd() 
139                      .readRecord(i); 
140              getAdf().getIndexPanel().indexList.add( 
141                      abr.name); 
142          } // end for 
143      } 
144   
145      public void setRecord(int i) { 
146          getAbd().setRecordNumber(i); 
147          AddressBookRecord abr = getAbd() 
148                  .readRecord(); 
149          AddressBookMediator.setTextFieldValues( 
150                  abr); 
151          setStorageRecord((getAbd().readRecord())); 
152      } 
153   
154      public void toggleEditPanel() { 
155          if (AddressBookFrame.isEditMode() == 
156              true) { 
157              AddressBookFrame.setEditMode(false); 
158              getAdf().getTf().setEditable(false); 
159              getAdf().getAp().t0.setEditable( 
160                      false); 
161              getAdf().getAp().t1.setEditable( 
162                      false); 
163              getAdf().init(); 
164              System.out.println("Edit mode OFF"); 
165          } else { 
166              AddressBookFrame.setEditMode(true); 
167              getAdf().getTf().setEditable(true); 
168              getAdf().getAp().t0.setEditable(true); 
169              getAdf().getAp().t1.setEditable(true); 
170              getAdf().init(); 
171              System.out.println("Edit mode ON"); 
172   
173              if (isEmptyDb()) { 
174                  // auto add record in edit mode when none 
175                  AddressBookRecord abr = 
176                          new AddressBookRecord(); 
177                  abr.name = "new record"; 
178                  AddressBookMediator.setTextFieldValues( 
179                          abr); 
180                  getAddressDisplay().addRecord(); 
181                  setEmptyDb(false); 
182              } 
183   
184          } 
185      } 
186   
187      public void dbMerge() { 
188          new CSVMerge(getAbd()); 
189          getAbd().sort(); 
190          getAddressDisplay().setRecord(0); 
191          getAddressDisplay().setIndexItems(); 
192          setEmptyDb(false); 
193      } 
194   
195      public void dbExport() { 
196          new CSVExport(getAbd()); 
197      } 
198   
199      public void dbOpen() { 
200          getAbd().openDb(); 
201          AddressBookMediator.setTextFieldValues( 
202                  getAbd().readRecord()); 
203          getAddressDisplay().setRecord(0); 
204          getAddressDisplay().setIndexItems(); 
205          setEmptyDb(false); 
206      } 
207   
208      public void dbSave() { 
209          getAbd().saveDb(); 
210      } 
211   
212      public static AddressBookRecord getStorageRecord() { 
213          return storageRecord; 
214      } 
215   
216      public static void setStorageRecord( 
217              AddressBookRecord storageRecord) { 
218          AddressBookMediator.storageRecord = 
219          storageRecord; 
220      } 
221   
222      public static boolean isEditFlag() { 
223          return editFlag; 
224      } 
225   
226      public static void setEditFlag( 
227              boolean editFlag) { 
228          AddressBookMediator.editFlag = editFlag; 
229      } 
230   
231      public static boolean isEmptyDb() { 
232          return emptyDb; 
233      } 
234   
235      public static void setEmptyDb( 
236              boolean emptyDb) { 
237          AddressBookMediator.emptyDb = emptyDb; 
238      } 
239   
240      public static AddressBookFrame getAdf() { 
241          return adf; 
242      } 
243   
244      public static void setAdf( 
245              AddressBookFrame adf) { 
246          AddressBookMediator.adf = adf; 
247      } 
248   
249      public static AddressBookDatabase getAbd() { 
250          return abd; 
251      } 
252   
253      public static void setAbd( 
254              AddressBookDatabase abd) { 
255          AddressBookMediator.abd = abd; 
256      } 
257   
258      public static AddressBookMediator getAddressDisplay() { 
259          if (display == null) display = 
260                               new AddressBookMediator(); 
261          return display; 
262      } 
263   
264      public AddressBookFrame getAddressBookFrame() { 
265          return addressBookFrame; 
266      } 
267   
268   
269  } 
270