KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > AddressBookEditorPane


1 package net.suberic.pooka.gui.propedit;
2 import net.suberic.pooka.*;
3 import net.suberic.util.*;
4 import net.suberic.util.gui.propedit.*;
5 import net.suberic.pooka.gui.propedit.*;
6 import net.suberic.util.gui.*;
7 import javax.swing.*;
8 import java.awt.event.*;
9 import java.awt.Component JavaDoc;
10 import java.awt.Container JavaDoc;
11 import java.awt.Cursor JavaDoc;
12 import javax.swing.Action JavaDoc;
13
14 /**
15  * A property editor which edits an AddressBook.
16  */

17 public class AddressBookEditorPane extends MultiEditorPane {
18
19   AddressBook book;
20   String JavaDoc bookName;
21   JTextField searchEntryField;
22   JButton searchButton;
23
24   ConfigurablePopupMenu popupMenu;
25
26   /**
27    * @param propertyName The property to be edited.
28    * @param template The property that will define the layout of the
29    * editor.
30    * @param manager The PropertyEditorManager that will manage the
31    * changes.
32    */

33   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
34     configureBasic(propertyName, template, propertyBaseName, newManager);
35
36     // we're going to have "AddressBook." at the beginning, so remove that.
37
bookName = propertyBase.substring(12, propertyBase.length());
38     book = Pooka.getAddressBookManager().getAddressBook(bookName);
39
40     JPanel searchEntryPanel = createSearchEntryPanel();
41     createAddressTable();
42     buttonPanel = createButtonPanel();
43
44     JPanel addressPanel = new JPanel();
45     addressPanel.setLayout(new BoxLayout(addressPanel, BoxLayout.Y_AXIS));
46
47     addressPanel.add(searchEntryPanel);
48     JScrollPane addressScrollPane = new JScrollPane(optionTable);
49     try {
50       addressScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(Integer.parseInt(manager.getProperty("Pooka.addressBookEditor.hsize", "300")), Integer.parseInt(manager.getProperty("Pooka.addressBookEditor.vsize", "100"))));
51     } catch (Exception JavaDoc e) {
52       addressScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(300, 100));
53     }
54     addressPanel.add(addressScrollPane);
55
56     doEditorPaneLayout(addressPanel, buttonPanel);
57
58     popupMenu = new ConfigurablePopupMenu();
59     popupMenu.configureComponent("AddressBookEditor.popupMenu", manager.getFactory().getSourceBundle());
60     popupMenu.setActive(getActions());
61
62     updateEditorEnabled();
63   }
64
65   /**
66    * Creates the panel which has the entry fields -- i.e., "Enter string to
67    * match", an entry field, and a search button.
68    */

69   public JPanel createSearchEntryPanel() {
70     JPanel searchEntryPanel = new JPanel();
71     searchEntryPanel.add(new JLabel(manager.getProperty("AddressBookEditor.matchString", "Match String: ")));
72
73     searchEntryField = new JTextField(30);
74     searchEntryPanel.add(searchEntryField);
75
76     Action JavaDoc a = new SearchAction();
77
78     searchButton = new JButton(manager.getProperty("AddressBookEditor.title.Search", "Search"));
79     searchButton.addActionListener(a);
80     searchEntryPanel.add(searchButton);
81
82     return searchEntryPanel;
83   }
84
85   /**
86    * Creates the AddressTable.
87    */

88   public void createAddressTable() {
89     optionTable = new JTable();
90     optionTable.setCellSelectionEnabled(false);
91     optionTable.setColumnSelectionAllowed(false);
92     optionTable.setRowSelectionAllowed(true);
93
94     optionTable.addMouseListener(new MouseAdapter() {
95         public void mouseClicked(MouseEvent e) {
96           if (e.getClickCount() == 2) {
97             int rowIndex = optionTable.rowAtPoint(e.getPoint());
98             if (rowIndex != -1) {
99               optionTable.setRowSelectionInterval(rowIndex, rowIndex);
100               AddressBookEntry selectedEntry = getSelectedEntry();
101               if (selectedEntry != null) {
102                 editEntry(selectedEntry);
103               }
104             }
105           }
106         }
107
108         public void mousePressed(MouseEvent e) {
109           if (e.isPopupTrigger()) {
110             // see if anything is selected
111
int rowIndex = optionTable.rowAtPoint(e.getPoint());
112             if (rowIndex == -1 || !optionTable.isRowSelected(rowIndex) ) {
113               optionTable.setRowSelectionInterval(rowIndex, rowIndex);
114             }
115
116             showPopupMenu(optionTable, e);
117           }
118         }
119
120         public void mouseReleased(MouseEvent e) {
121           if (e.isPopupTrigger()) {
122             // see if anything is selected
123
int rowIndex = optionTable.rowAtPoint(e.getPoint());
124             if (rowIndex == -1 || !optionTable.isRowSelected(rowIndex) ) {
125               optionTable.setRowSelectionInterval(rowIndex, rowIndex);
126             }
127
128             showPopupMenu(optionTable, e);
129           }
130         }
131       });
132
133     updateTableModel(new AddressBookEntry[0]);
134
135   }
136
137   /**
138    * Performs a search using the string value in the searchEntryField. Updates
139    * the optionTable with the results.
140    */

141   public void performSearch() {
142     AddressBookEntry[] matchingEntries = book.getAddressMatcher().match(searchEntryField.getText());
143     updateTableModel(matchingEntries);
144   }
145
146   /**
147    * Adds a new entry.
148    */

149   public void performAdd() {
150     AddressBookEntry newEntry = new net.suberic.pooka.vcard.Vcard(new java.util.Properties JavaDoc());
151     try {
152       newEntry.setAddresses(new javax.mail.internet.InternetAddress JavaDoc[] { new javax.mail.internet.InternetAddress JavaDoc("example@example.com") });
153     } catch (Exception JavaDoc e) { }
154     if (newEntry.getAddresses() != null) {
155       book.addAddress(newEntry);
156       ((AddressBookTableModel)optionTable.getModel()).addEntry(newEntry);
157     }
158     editEntry(newEntry);
159   }
160
161   /**
162    * Edits the current entry.
163    */

164   protected void editSelectedValue(Container JavaDoc container) {
165     AddressBookEntry e = getSelectedEntry();
166     if (e != null) {
167       String JavaDoc newValueTemplate = manager.getProperty(editorTemplate + "._addValueTemplate", "");
168       if (newValueTemplate.length() > 0) {
169         PropertyEditorUI editor = manager.getFactory().createEditor(newValueTemplate, newValueTemplate, manager);
170         AddressEntryController aec = null;
171         if (editor instanceof WizardEditorPane && ((WizardEditorPane)editor).getController() instanceof AddressEntryController) {
172           aec = (AddressEntryController) ((WizardEditorPane) editor).getController();
173           aec.setAddressBook(book);
174           aec.loadEntry(e);
175         }
176         manager.getFactory().showNewEditorWindow(manager.getProperty(newValueTemplate + ".label", newValueTemplate), editor, getPropertyEditorPane().getContainer());
177
178         if (aec != null) {
179           AddressBookEntry editedEntry = aec.getEntry();
180           if (editedEntry == e) {
181             ((AddressBookTableModel)optionTable.getModel()).updateEntry(editedEntry);
182           } else {
183             book.addAddress(editedEntry);
184             ((AddressBookTableModel)optionTable.getModel()).addEntry(editedEntry);
185           }
186         }
187       } else {
188         editEntry(e);
189       }
190     }
191   }
192
193   /**
194    * Deletes the current entry.
195    */

196   public void removeSelectedValue() {
197     AddressBookEntry e = getSelectedEntry();
198     if (e != null) {
199       book.removeAddress(e);
200       ((AddressBookTableModel)optionTable.getModel()).removeEntry(e);
201     }
202   }
203
204   /**
205    * Gets the currently selected entry.
206    */

207   public AddressBookEntry getSelectedEntry() {
208     int index = optionTable.getSelectedRow();
209     if (index > -1)
210       return ((AddressBookTableModel)optionTable.getModel()).getEntryAt(index);
211     else
212       return null;
213   }
214
215   /**
216    * Brings up an editor for the current entry.
217    */

218   public void editEntry(AddressBookEntry entry) {
219     /*
220       AddressEntryEditor editor = new AddressEntryEditor(manager, entry);
221       manager.getFactory().showNewEditorWindow(manager.getProperty("AddressEntryEditor.title", "Address Entry"), editor);
222     */

223   }
224
225   /**
226    * Brings up the current popup menu.
227    */

228   public void showPopupMenu(JComponent component, MouseEvent e) {
229     popupMenu.show(component, e.getX(), e.getY());
230   }
231
232   /**
233    * Updates the TableModel with the new entries.
234    */

235   public void updateTableModel(AddressBookEntry[] entries) {
236     AddressBookTableModel newTableModel = new AddressBookTableModel(entries);
237     optionTable.setModel(newTableModel);
238   }
239
240   public void setValue() {
241     if (book != null) {
242       try {
243         book.saveAddressBook();
244       } catch (Exception JavaDoc e) {
245         Pooka.getUIFactory().showError(Pooka.getProperty("error.AddressBook.saveAddressBook", "Error saving Address Book: ") + e.getMessage());
246         e.printStackTrace();
247       }
248     } else {
249       // if we're setting the value on this editor, then we might also be
250
// creating the edited AddressBook. see if that's true.
251
SwingUtilities.invokeLater(new Runnable JavaDoc() {
252           public void run() {
253             AddressBook newBook = Pooka.getAddressBookManager().getAddressBook(bookName);
254             if (newBook != null) {
255               book = newBook;
256               updateEditorEnabled();
257             }
258           }
259         });
260     }
261   }
262
263   public void validateProperty() {
264
265   }
266
267   public java.util.Properties JavaDoc getValue() {
268     return new java.util.Properties JavaDoc();
269   }
270
271   public void resetDefaultValue() {
272     try {
273       book.loadAddressBook();
274     } catch (Exception JavaDoc e) {
275       Pooka.getUIFactory().showError(Pooka.getProperty("error.AddressBook.loadAddressBook", "Error reloading Address Book: ") + e.getMessage());
276       e.printStackTrace();
277     }
278     performSearch();
279   }
280
281   public boolean isChanged() {
282     return false;
283   }
284
285   /**
286    * Run when the PropertyEditor may have changed enabled states.
287    */

288   protected void updateEditorEnabled() {
289     super.updateEditorEnabled();
290     searchButton.setEnabled(isEditorEnabled());
291     searchEntryField.setEnabled(isEditorEnabled());
292   }
293
294
295   public void setBusy(boolean newValue) {
296     if (newValue)
297       this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
298     else
299       this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
300   }
301
302   public class AddressBookTableModel extends javax.swing.table.AbstractTableModel JavaDoc {
303
304     AddressBookEntry[] entries;
305
306     public AddressBookTableModel(AddressBookEntry[] newEntries) {
307       entries = newEntries;
308     }
309
310     public int getRowCount() {
311       return entries.length;
312     }
313
314     public int getColumnCount() {
315       return 4;
316     }
317
318     public String JavaDoc getColumnName(int index) {
319       if (index == 0) {
320         return Pooka.getProperty("AddressBookTable.personalName", "Name");
321       } else if (index == 1) {
322         return Pooka.getProperty("AddressBookTable.firstName", "First Name");
323       } else if (index == 2) {
324         return Pooka.getProperty("AddressBookTable.lastName", "Last Name");
325       } else if (index == 3) {
326         return Pooka.getProperty("AddressBookTable.address", "Email Address");
327       } else {
328         return null;
329       }
330     }
331
332     public Object JavaDoc getValueAt(int row, int column) {
333       if (row < 0 || column < 0 || row >= getRowCount() || column >= getColumnCount())
334         return null;
335
336       AddressBookEntry currentEntry = entries[row];
337
338       if (column == 0) {
339         return currentEntry.getID();
340       }
341       if (column == 1) {
342         return currentEntry.getFirstName();
343       }
344       if (column == 2) {
345         return currentEntry.getLastName();
346       }
347       if (column == 3) {
348         return currentEntry.getAddressString();
349       }
350
351       return null;
352     }
353
354     /**
355      * Returns the AddressBookEntry at the given index.
356      */

357     public AddressBookEntry getEntryAt(int index) {
358       return entries[index];
359     }
360
361     /**
362      * Adds the given AddressBookEntry to the end of the table.
363      */

364     public void addEntry(AddressBookEntry e) {
365       AddressBookEntry[] newEntries;
366       int length;
367
368       if (entries != null) {
369         length = entries.length;
370         newEntries = new AddressBookEntry[length + 1];
371         System.arraycopy(entries, 0, newEntries, 1, length);
372       } else {
373         length = 0;
374         newEntries = new AddressBookEntry[1];
375       }
376       newEntries[0] = e;
377
378       entries = newEntries;
379
380       fireTableRowsInserted(length, length);
381
382       optionTable.clearSelection();
383       optionTable.addRowSelectionInterval(0,0);
384       optionTable.scrollRectToVisible(optionTable.getCellRect(0, 1, true));
385   }
386
387     /**
388      * Removes the given AddressBookEntry from the table, if present.
389      */

390     public void removeEntry(AddressBookEntry e) {
391       boolean found = false;
392
393       for (int i = 0; !found && i < entries.length; i++) {
394         if (e == entries[i]) {
395           found = true;
396           int removedRow = i;
397           AddressBookEntry[] newEntries = new AddressBookEntry[entries.length - 1];
398           if (removedRow != 0)
399             System.arraycopy(entries, 0, newEntries, 0, removedRow);
400
401           if (removedRow != entries.length -1)
402             System.arraycopy(entries, removedRow + 1, newEntries, removedRow, entries.length - removedRow - 1);
403
404           entries = newEntries;
405           fireTableRowsDeleted(removedRow, removedRow);
406         }
407       }
408     }
409
410     /**
411      * Updates the given AddressBookEntry in the table.
412      */

413     public void updateEntry(AddressBookEntry e) {
414       boolean found = false;
415
416       for (int i = 0; !found && i < entries.length; i++) {
417         if (e == entries[i]) {
418           found = true;
419           fireTableRowsUpdated(i,i);
420         }
421       }
422     }
423   }
424
425   public class SearchAction extends AbstractAction {
426     public SearchAction() {
427       super("address-search");
428     }
429
430     public void actionPerformed(ActionEvent e) {
431       setBusy(true);
432       performSearch();
433       setBusy(false);
434     }
435   }
436
437   /**
438    * Gets the parent PropertyEditorPane for the given component.
439    */

440   public PropertyEditorPane getPropertyEditorPane() {
441     return getPropertyEditorPane(this);
442   }
443
444   /**
445    * Returns the display value for this property.
446    */

447   public String JavaDoc getDisplayValue() {
448     return bookName;
449   }
450
451   /**
452    * Creates the actions for this editor.
453    */

454   protected void createActions() {
455     mDefaultActions = new Action JavaDoc[] {
456       new SearchAction(),
457       new AddAddressAction(),
458       new EditAction(),
459       new DeleteAction()
460     };
461   }
462
463   public class AddAddressAction extends AbstractAction {
464     public AddAddressAction() {
465       //super("address-add");
466
super("editor-add");
467     }
468
469     public void actionPerformed(ActionEvent e) {
470       // check to see if we want to add a new value using a
471
// wizard
472
String JavaDoc newValueTemplate = manager.getProperty(editorTemplate + "._addValueTemplate", "");
473       if (newValueTemplate.length() > 0) {
474         PropertyEditorUI editor = manager.getFactory().createEditor(newValueTemplate, newValueTemplate, manager);
475         AddressEntryController aec = null;
476         if (editor instanceof WizardEditorPane && ((WizardEditorPane)editor).getController() instanceof AddressEntryController) {
477           aec = (AddressEntryController) ((WizardEditorPane) editor).getController();
478           aec.setAddressBook(book);
479         }
480         manager.getFactory().showNewEditorWindow(manager.getProperty(newValueTemplate + ".label", newValueTemplate), editor, getPropertyEditorPane().getContainer());
481
482         if (aec != null) {
483           AddressBookEntry newEntry = aec.getEntry();
484           if (newEntry != null) {
485             ((AddressBookTableModel)optionTable.getModel()).addEntry(newEntry);
486           }
487         }
488       } else {
489         addNewValue(getNewValueName(), getPropertyEditorPane().getContainer());
490       }
491
492     }
493   }
494
495
496
497 }
498
Popular Tags