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 ; 7 import java.awt.Component ; 8 import java.awt.event.ActionEvent ; 9 import java.net.URL ; 10 11 import java.util.*; 12 13 17 public class AddressBookSelectionPanel extends JPanel { 18 19 JTable addressTable; 21 22 JTable confirmedTable; 24 25 JTextField filterField; 27 28 AddressEntryTextArea entryArea; 30 31 Component parentFrame; 33 34 38 public AddressBookSelectionPanel(AddressEntryTextArea entryTextArea, Component newParentFrame) { 39 entryArea = entryTextArea; 40 parentFrame = newParentFrame; 41 configurePanel(); 42 } 43 44 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 (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 (300,200)); 67 addressBox.add(confirmedPane); 68 69 this.add(addressBox); 70 71 Box buttonBox = createButtonBox(); 72 73 this.add(buttonBox); 74 } 75 76 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 e) { 85 doFilter(filterField.getText()); 86 } 87 }); 88 89 returnValue.add(filterField); 90 returnValue.add(filterButton); 91 return returnValue; 92 } 93 94 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 () { 103 public void mouseClicked(java.awt.event.MouseEvent 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 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 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 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 e) { 155 removeSelectedAddresses(); 156 } 157 }); 158 returnValue.add(removeButton); 159 160 returnValue.add(Box.createVerticalGlue()); 161 162 return returnValue; 163 } 164 165 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 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 e) { 184 closePanel(); 185 } 186 }); 187 returnValue.add(cancelButton); 188 189 return returnValue; 190 } 191 192 196 public void doFilter(String filterValue) { 197 AddressBookEntry[] matchingValues = getAddressMatcher().match(filterValue); 198 AddressBookTableModel model = (AddressBookTableModel) addressTable.getModel(); 199 model.setEntries(matchingValues); 200 } 201 202 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 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 240 public void confirmSelectedAddresses() { 241 AddressBookEntry[] selectedValues = getSelectedAddresses(); 242 AddressBookTableModel model = (AddressBookTableModel) confirmedTable.getModel(); 243 model.addEntries(selectedValues); 244 } 245 246 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 263 public void copySelectionsToEntry() { 264 AddressBookEntry[] confirmedEntries = getConfirmedAddresses(); 265 entryArea.addAddresses(confirmedEntries); 266 } 267 268 271 public void closePanel() { 272 if (parentFrame instanceof JInternalFrame) { 273 try { 274 ((JInternalFrame) parentFrame).setClosed(true); 275 } catch (java.beans.PropertyVetoException e) { 276 } 277 } else { 278 ((JFrame) parentFrame).dispose(); 279 } 280 } 281 282 285 public AddressMatcher getAddressMatcher() { 286 return entryArea.getNewMessageUI().getSelectedProfile().getAddressMatcher(); 287 } 288 289 292 class AddressBookTableModel extends AbstractTableModel { 293 294 List addressList = new ArrayList(); 295 296 299 public AddressBookTableModel() { 300 301 } 302 303 306 public int getRowCount() { 307 return addressList.size(); 308 } 309 310 313 public int getColumnCount() { 314 return 2; 315 } 316 317 318 321 public Object 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 340 public AddressBookEntry getEntryAt(int row) { 341 return (AddressBookEntry) addressList.get(row); 342 } 343 344 347 public void addEntries(AddressBookEntry[] newEntries) { 348 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 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 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 390 public String 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 |