KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > AddressBookSelectionPanel


1 package net.suberic.pooka.gui;
2 import net.suberic.util.gui.IconManager;
3 import net.suberic.pooka.*;
4
5 import javax.swing.*;
6 import javax.swing.table.AbstractTableModel JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.net.URL JavaDoc;
10
11 import java.util.*;
12
13 /**
14  * This shows a dialog which allows you to select addresses from an
15  * AddressBook.
16  */

17 public class AddressBookSelectionPanel extends JPanel {
18
19   // the list of addresses available
20
JTable addressTable;
21   
22   // the list of addresses selected
23
JTable confirmedTable;
24
25   // the filter entry
26
JTextField filterField;
27
28   // the AddressEntryTextArea that we're using.
29
AddressEntryTextArea entryArea;
30
31   // the parent frame.
32
Component JavaDoc parentFrame;
33
34   /**
35    * Creates a new AddressBookSelectionPanel using the given
36    * AddressEntryTextArea.
37    */

38   public AddressBookSelectionPanel(AddressEntryTextArea entryTextArea, Component JavaDoc newParentFrame) {
39     entryArea = entryTextArea;
40     parentFrame = newParentFrame;
41     configurePanel();
42   }
43
44   /**
45    * Sets up the panel. The entryArea should be set already.
46    */

47   void configurePanel() {
48     this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
49
50     this.add(createFilterPanel());
51
52     Box addressBox = new Box(BoxLayout.X_AXIS);
53
54     createAddressTable();
55
56     JScrollPane addressPane = new JScrollPane(addressTable);
57     addressPane.setPreferredSize(new java.awt.Dimension JavaDoc(300,200));
58     addressBox.add(addressPane);
59
60     JPanel selectionPanel = createSelectionPanel();
61     addressBox.add(selectionPanel);
62
63     createConfirmedTable();
64
65     JScrollPane confirmedPane = new JScrollPane(confirmedTable);
66     confirmedPane.setPreferredSize(new java.awt.Dimension JavaDoc(300,200));
67     addressBox.add(confirmedPane);
68
69     this.add(addressBox);
70
71     Box buttonBox = createButtonBox();
72
73     this.add(buttonBox);
74   }
75
76   /**
77    * Creates the filter panel.
78    */

79   JPanel createFilterPanel() {
80     JPanel returnValue = new JPanel();
81     filterField = new JTextField(20);
82     JButton filterButton = new JButton(Pooka.getProperty("AddressBookEditor.title.Search", "Search"));
83     filterButton.addActionListener(new AbstractAction() {
84     public void actionPerformed(ActionEvent JavaDoc e) {
85       doFilter(filterField.getText());
86     }
87       });
88
89     returnValue.add(filterField);
90     returnValue.add(filterButton);
91     return returnValue;
92   }
93
94   /**
95    * Creates the address table.
96    */

97   void createAddressTable() {
98     AddressBookTableModel addressModel = new AddressBookTableModel();
99     addressTable = new JTable(addressModel);
100     addressTable.getColumnModel().getColumn(0).setPreferredWidth(100);
101     addressTable.getColumnModel().getColumn(1).setPreferredWidth(100);
102     addressTable.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
103     public void mouseClicked(java.awt.event.MouseEvent JavaDoc e) {
104       if (e.getClickCount() == 2) {
105         int rowIndex = addressTable.rowAtPoint(e.getPoint());
106         if (rowIndex != -1) {
107           addressTable.setRowSelectionInterval(rowIndex, rowIndex);
108           confirmSelectedAddresses();
109         }
110       }
111     }
112       });
113
114     doFilter("");
115   }
116
117   /**
118    * Creates the confirmed table.
119    */

120   void createConfirmedTable() {
121     AddressBookTableModel confirmedModel = new AddressBookTableModel();
122     confirmedTable = new JTable(confirmedModel);
123     confirmedTable.getColumnModel().getColumn(0).setPreferredWidth(100);
124     confirmedTable.getColumnModel().getColumn(1).setPreferredWidth(100);
125   }
126
127   /**
128    * Creates the Selection Panel.
129    */

130   JPanel createSelectionPanel() {
131     JPanel returnValue = new JPanel();
132     returnValue.setLayout(new BoxLayout(returnValue, BoxLayout.Y_AXIS));
133
134     returnValue.add(Box.createVerticalGlue());
135
136     IconManager iconManager = Pooka.getUIFactory().getIconManager();
137     
138     ImageIcon addButtonIcon = iconManager.getIcon(Pooka.getProperty("NewMessage.selectionPanel.button.add", "Right"));
139
140     JButton addButton = new JButton(addButtonIcon);
141     addButton.addActionListener(new AbstractAction() {
142     public void actionPerformed(ActionEvent JavaDoc e) {
143       confirmSelectedAddresses();
144     }
145       });
146     returnValue.add(addButton);
147
148     returnValue.add(Box.createVerticalGlue());
149
150     ImageIcon removeButtonIcon = iconManager.getIcon(Pooka.getProperty("NewMessage.selectionPanel.button.remove", "Left"));
151
152     JButton removeButton = new JButton(removeButtonIcon);
153     removeButton.addActionListener(new AbstractAction() {
154     public void actionPerformed(ActionEvent JavaDoc e) {
155       removeSelectedAddresses();
156     }
157       });
158     returnValue.add(removeButton);
159
160     returnValue.add(Box.createVerticalGlue());
161
162     return returnValue;
163   }
164
165   /**
166    * Creates the box with the ok and cancel buttons.
167    */

168   Box createButtonBox() {
169     Box returnValue = Box.createHorizontalBox();
170
171     JButton okButton = new JButton(Pooka.getProperty("button.ok", "Ok"));
172     okButton.addActionListener(new AbstractAction() {
173     public void actionPerformed(ActionEvent JavaDoc e) {
174       copySelectionsToEntry();
175       closePanel();
176     }
177       });
178     returnValue.add(okButton);
179
180
181     JButton cancelButton = new JButton(Pooka.getProperty("button.cancel", "Cancel"));
182     cancelButton.addActionListener(new AbstractAction() {
183     public void actionPerformed(ActionEvent JavaDoc e) {
184       closePanel();
185     }
186       });
187     returnValue.add(cancelButton);
188
189     return returnValue;
190   }
191
192   /**
193    * Updates the addressTable using the results of the filter from the
194    * filterField.
195    */

196   public void doFilter(String JavaDoc filterValue) {
197     AddressBookEntry[] matchingValues = getAddressMatcher().match(filterValue);
198     AddressBookTableModel model = (AddressBookTableModel) addressTable.getModel();
199     model.setEntries(matchingValues);
200   }
201
202   /**
203    * Gets the currently selected addresses in the addressTable.
204    */

205   public AddressBookEntry[] getSelectedAddresses() {
206     if (addressTable != null) {
207       AddressBookTableModel model = (AddressBookTableModel) addressTable.getModel();
208       int[] selectedRows = addressTable.getSelectedRows();
209       if (selectedRows != null) {
210     AddressBookEntry[] returnValue = new AddressBookEntry[selectedRows.length];
211     for (int i = 0; i < selectedRows.length; i++) {
212       returnValue[i] = model.getEntryAt(selectedRows[i]);
213     }
214     return returnValue;
215       }
216     }
217     return new AddressBookEntry[0];
218   }
219
220   /**
221    * Gets all of the confirmed list of addresses.
222    */

223   public AddressBookEntry[] getConfirmedAddresses() {
224     if (confirmedTable != null) {
225       AddressBookTableModel model = (AddressBookTableModel) confirmedTable.getModel();
226       AddressBookEntry[] returnValue = new AddressBookEntry[model.getRowCount()];
227       for (int i = 0; i < returnValue.length; i++) {
228     returnValue[i] = model.getEntryAt(i);
229       }
230       return returnValue;
231     }
232
233     return new AddressBookEntry[0];
234   }
235
236   /**
237    * Adds the currently selected address(es) in the addressTable to the
238    * confirmedTable.
239    */

240   public void confirmSelectedAddresses() {
241     AddressBookEntry[] selectedValues = getSelectedAddresses();
242     AddressBookTableModel model = (AddressBookTableModel) confirmedTable.getModel();
243     model.addEntries(selectedValues);
244   }
245
246   /**
247    * Removed the currently selected address(es) in the confirmedTable from the
248    * confirmedTable.
249    */

250   public void removeSelectedAddresses() {
251     AddressBookTableModel model = (AddressBookTableModel) confirmedTable.getModel();
252     int[] selectedRows = confirmedTable.getSelectedRows();
253     AddressBookEntry[] removedEntries = new AddressBookEntry[selectedRows.length];
254     for (int i = 0; i < selectedRows.length; i++) {
255       removedEntries[i] = model.getEntryAt(selectedRows[i]);
256     }
257     model.removeEntries(removedEntries);
258   }
259
260   /**
261    * Copies the entries from the selection list to the AddressEntryTextArea.
262    */

263   public void copySelectionsToEntry() {
264     AddressBookEntry[] confirmedEntries = getConfirmedAddresses();
265     entryArea.addAddresses(confirmedEntries);
266   }
267
268   /**
269    * Closes this panel.
270    */

271   public void closePanel() {
272     if (parentFrame instanceof JInternalFrame) {
273       try {
274     ((JInternalFrame) parentFrame).setClosed(true);
275       } catch (java.beans.PropertyVetoException JavaDoc e) {
276       }
277     } else {
278       ((JFrame) parentFrame).dispose();
279     }
280   }
281   
282   /**
283    * Gets the appropriate AddressMatcher.
284    */

285   public AddressMatcher getAddressMatcher() {
286     return entryArea.getNewMessageUI().getSelectedProfile().getAddressMatcher();
287   }
288
289   /**
290    * A TableModel for Address Books.
291    */

292   class AddressBookTableModel extends AbstractTableModel JavaDoc {
293     
294     List addressList = new ArrayList();
295
296     /**
297      * Creates an AddressBookTableModel.
298      */

299     public AddressBookTableModel() {
300       
301     }
302
303     /**
304      * Returns the row count.
305      */

306     public int getRowCount() {
307       return addressList.size();
308     }
309
310     /**
311      * Returns the column count.
312      */

313     public int getColumnCount() {
314       return 2;
315     }
316
317     
318     /**
319      * Returns the value at the given row and column.
320      */

321     public Object JavaDoc getValueAt(int row, int column) {
322       if (row < 0 || row >= getRowCount() || column < 0 || column >= 2)
323     return null;
324
325       AddressBookEntry entry = getEntryAt(row);
326       if (column == 0) {
327     return entry.getID();
328       }
329
330       if (column == 1) {
331     return entry.getAddressString();
332       }
333
334       return null;
335     }
336
337     /**
338      * Returns the AddressBookEntry for the given row.
339      */

340     public AddressBookEntry getEntryAt(int row) {
341       return (AddressBookEntry) addressList.get(row);
342     }
343
344     /**
345      * Adds the given AddressBookEntries to the table.
346      */

347     public void addEntries(AddressBookEntry[] newEntries) {
348       // make sure that each entry is unique.
349
if (newEntries != null && newEntries.length > 0) {
350     int firstNew = addressList.size();
351     for (int i = 0 ; i < newEntries.length; i++) {
352       if ( ! addressList.contains(newEntries[i])) {
353         addressList.add(newEntries[i]);
354       }
355     }
356     
357     if (firstNew != addressList.size())
358       fireTableRowsInserted(firstNew, addressList.size() -1);
359       }
360     }
361
362     /**
363      * Removes the given AddressBookEntries from the table.
364      */

365     public void removeEntries(AddressBookEntry[] removedEntries) {
366       for (int i = 0; i < removedEntries.length; i++) {
367     int index = addressList.indexOf(removedEntries[i]);
368     if (index >=0) {
369       addressList.remove(index);
370       fireTableRowsDeleted(index, index);
371     }
372       }
373     }
374
375     /**
376      * Sets the given AddressBookEntries as the values for the table.
377      */

378     public void setEntries(AddressBookEntry[] newEntries) {
379       ArrayList newList = new ArrayList();
380       for (int i = 0; i < newEntries.length; i++) {
381     newList.add(newEntries[i]);
382       }
383       addressList=newList;
384       fireTableDataChanged();
385     }
386
387     /**
388      * Returns the name for the given column.
389      */

390     public String JavaDoc getColumnName(int col) {
391       if (col == 0)
392     return Pooka.getProperty("AddressBookTable.personalName", "Name");
393       
394       if (col == 1)
395     return Pooka.getProperty("AddressBookTable.address", "Email Address");
396
397       return "";
398     }
399   }
400 }
401
Popular Tags