1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import net.suberic.util.*; 4 import java.util.*; 5 import javax.swing.event.*; 6 import javax.swing.table.DefaultTableModel ; 7 import javax.swing.table.JTableHeader ; 8 import java.awt.Container ; 9 import java.awt.Component ; 10 import java.awt.event.*; 11 12 34 35 public class MultiEditorPane extends CompositeSwingPropertyEditor implements ListSelectionListener { 36 protected JTable optionTable; 37 JPanel entryPanel; 38 protected JPanel buttonPanel; 39 40 List<JButton> buttonList; 41 boolean changed = false; 42 List<String > removeValues = new ArrayList<String >(); 43 String propertyTemplate; 44 List<String > displayProperties; 45 46 protected Action[] mDefaultActions; 47 48 57 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 58 getLogger().fine("creating MultiEditorPane for property " + propertyName + ", template " + template); 59 configureBasic(propertyName, template, propertyBaseName, newManager); 60 61 64 List<String > optionList = manager.getPropertyAsList(property, ""); 65 66 displayProperties = manager.getPropertyAsList(editorTemplate + "._displayProperties", ""); 67 68 optionTable = createOptionTable(optionList, displayProperties); 69 JScrollPane optionScrollPane = new JScrollPane(optionTable); 70 71 buttonPanel = createButtonPanel(); 72 73 getLogger().fine("MultiEditorPane for property " + propertyName + ", template " + template); 74 75 doEditorPaneLayout(optionScrollPane, buttonPanel); 76 77 updateEditorEnabled(); 78 79 manager.registerPropertyEditor(property, this); 80 81 } 82 83 86 public void doEditorPaneLayout(Component listPanel, Component buttonPanel) { 87 SpringLayout layout = new SpringLayout(); 88 this.setLayout(layout); 89 90 this.add(listPanel); 91 this.add(buttonPanel); 92 93 SpringLayout.Constraints ospConstraints = layout.getConstraints(listPanel); 94 SpringLayout.Constraints buttonConstraints = layout.getConstraints(buttonPanel); 95 96 Spring panelHeight = Spring.constant(0); 97 panelHeight = Spring.max(panelHeight, ospConstraints.getHeight()); 98 panelHeight = Spring.max(panelHeight, buttonConstraints.getHeight()); 99 100 ospConstraints.setHeight(panelHeight); 101 buttonConstraints.setHeight(panelHeight); 102 103 layout.putConstraint(SpringLayout.WEST, listPanel, 5, SpringLayout.WEST, this); 104 layout.putConstraint(SpringLayout.NORTH, listPanel, 5, SpringLayout.NORTH, this); 105 layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, listPanel); 106 107 layout.putConstraint(SpringLayout.WEST, buttonPanel, 5, SpringLayout.EAST, listPanel); 108 layout.putConstraint(SpringLayout.NORTH, buttonPanel, 5, SpringLayout.NORTH, this); 109 layout.putConstraint(SpringLayout.EAST, this, 5 ,SpringLayout.EAST, buttonPanel); 110 111 112 } 113 114 118 private JTable createOptionTable(List<String > optionList, List<String > pDisplayProperties) { 119 Vector columnLabels = new Vector(); 121 columnLabels.add(manager.getProperty(editorTemplate + "._label", editorTemplate)); 123 for (String subProperty: pDisplayProperties) { 124 getLogger().fine("adding label for " + subProperty); 125 126 String label = manager.getProperty(editorTemplate + "._displayProperties." + subProperty + ".label", subProperty); 127 columnLabels.add(label); 128 } 129 130 DefaultTableModel dtm = new DefaultTableModel (columnLabels, 0); 131 132 134 for (String option: optionList) { 135 Vector optionValues = createTableEntry(option, pDisplayProperties); 136 dtm.addRow(optionValues); 137 } 138 139 JTable returnValue = new JTable(dtm); 140 returnValue.setCellSelectionEnabled(false); 141 returnValue.setColumnSelectionAllowed(false); 142 returnValue.setRowSelectionAllowed(true); 143 returnValue.setShowGrid(false); 144 145 returnValue.getSelectionModel().addListSelectionListener(this); 146 if (returnValue.getRowCount() > 0) { 147 returnValue.setRowSelectionInterval(0,0); 148 } 149 return returnValue; 150 } 151 152 155 public Vector createTableEntry(String option, List<String > pDisplayProperties) { 156 Vector optionValues = new Vector(); 157 optionValues.add(option); 159 for (String subProperty: pDisplayProperties) { 160 getLogger().fine("adding display property for " + option + "." + subProperty); 161 optionValues.add(manager.getProperty(property + "." + option + "." + subProperty, subProperty)); 162 } 163 164 return optionValues; 165 } 166 167 170 protected JPanel createButtonPanel() { 171 getLogger().fine("creating buttons."); 172 173 createActions(); 174 175 buttonList = new ArrayList<JButton>(); 176 JPanel returnValue = new JPanel(); 177 SpringLayout layout = new SpringLayout(); 178 returnValue.setLayout(layout); 179 180 JButton addButton = createButton("Add", getAction("editor-add"), true); 182 183 JButton editButton = createButton("Edit", getAction("editor-edit"), true); 184 185 JButton removeButton = createButton("Remove", getAction("editor-delete"), false); 186 187 returnValue.add(addButton); 188 returnValue.add(editButton); 189 returnValue.add(removeButton); 190 191 layout.putConstraint(SpringLayout.NORTH, addButton, 0, SpringLayout.NORTH, returnValue); 192 layout.putConstraint(SpringLayout.WEST, addButton, 5 ,SpringLayout.WEST, returnValue); 193 layout.putConstraint(SpringLayout.EAST, returnValue, 5 ,SpringLayout.EAST, addButton); 194 195 layout.putConstraint(SpringLayout.NORTH, editButton, 5, SpringLayout.SOUTH, addButton); 196 layout.putConstraint(SpringLayout.WEST, editButton, 5 ,SpringLayout.WEST, returnValue); 197 198 layout.putConstraint(SpringLayout.NORTH, removeButton, 5, SpringLayout.SOUTH, editButton); 199 layout.putConstraint(SpringLayout.WEST, removeButton, 5 ,SpringLayout.WEST, returnValue); 200 201 Spring buttonWidth = Spring.constant(0); 202 203 SpringLayout.Constraints addConstraints = layout.getConstraints(addButton); 204 SpringLayout.Constraints editConstraints = layout.getConstraints(editButton); 205 SpringLayout.Constraints removeConstraints = layout.getConstraints(removeButton); 206 207 buttonWidth = Spring.max(buttonWidth, addConstraints.getWidth()); 208 buttonWidth = Spring.max(buttonWidth, editConstraints.getWidth()); 209 buttonWidth = Spring.max(buttonWidth, removeConstraints.getWidth()); 210 211 addConstraints.setWidth(buttonWidth); 212 editConstraints.setWidth(buttonWidth); 213 removeConstraints.setWidth(buttonWidth); 214 215 SpringLayout.Constraints panelConstraints = layout.getConstraints(returnValue); 216 218 return returnValue; 219 } 220 221 224 protected void createActions() { 225 mDefaultActions = new Action[] { 226 new AddAction(), 227 new EditAction(), 228 new DeleteAction() 229 }; 230 } 231 232 236 private JButton createButton(String label, Action e, boolean isDefault) { 237 JButton thisButton; 238 239 thisButton = new JButton(manager.getProperty("label." + label, label)); 240 String mnemonic = manager.getProperty("label." + label + ".mnemonic", ""); 241 if (!mnemonic.equals("")) 242 thisButton.setMnemonic(mnemonic.charAt(0)); 243 244 thisButton.setSelected(isDefault); 245 246 thisButton.addActionListener(e); 247 248 buttonList.add(thisButton); 249 return thisButton; 250 } 251 252 253 257 public void valueChanged(ListSelectionEvent e) { 258 259 } 260 261 264 public void addNewValue(String newValueName) { 265 try { 266 List<String > newValueList = new ArrayList<String >(); 267 for (int i = 0; i < optionTable.getRowCount(); i++) { 268 newValueList.add((String ) optionTable.getValueAt(i, 0)); 269 } 270 newValueList.add(newValueName); 271 String newValue = VariableBundle.convertToString(newValueList); 272 firePropertyChangingEvent(newValue) ; 273 Vector newValueVector = createTableEntry(newValueName, displayProperties); 274 ((DefaultTableModel )optionTable.getModel()).addRow(newValueVector); 275 firePropertyChangedEvent(newValue); 276 this.setChanged(true); 277 278 optionTable.getSelectionModel().setSelectionInterval(optionTable.getModel().getRowCount(), optionTable.getModel().getRowCount() -1); 279 } catch (PropertyValueVetoException pvve) { 280 manager.getFactory().showError(getPropertyEditorPane().getContainer(), "Error adding value " + newValueName + " to " + property + ": " + pvve.getReason()); 281 } 282 } 283 284 287 protected void addNewValue(String newValueName, Container container) { 288 if (newValueName == null || newValueName.length() == 0) 289 return; 290 291 try { 292 List<String > newValueList = new ArrayList<String >(); 293 for (int i = 0; i < optionTable.getRowCount(); i++) { 294 newValueList.add((String ) optionTable.getValueAt(i, 0)); 295 } 296 newValueList.add(newValueName); 297 String newValue = VariableBundle.convertToString(newValueList); 298 firePropertyChangingEvent(newValue) ; 299 Vector newValueVector = new Vector(); 300 newValueVector.add(newValueName); 301 ((DefaultTableModel )optionTable.getModel()).addRow(newValueVector); 302 firePropertyChangedEvent(newValue); 303 this.setChanged(true); 304 305 optionTable.getSelectionModel().setSelectionInterval(optionTable.getModel().getRowCount(), optionTable.getModel().getRowCount() -1); 306 editSelectedValue(container); 307 } catch (PropertyValueVetoException pvve) { 308 manager.getFactory().showError(container, "Error adding value " + newValueName + " to " + property + ": " + pvve.getReason()); 309 } 310 } 311 312 315 public void removeSelectedValue() { 316 int selectedRow = optionTable.getSelectedRow(); 317 String selValue = (String ) optionTable.getValueAt(selectedRow, 0); 318 if (selValue == null) 319 return; 320 321 try { 322 List<String > newValueList = new ArrayList<String >(); 323 for (int i = 0; i < optionTable.getRowCount(); i++) { 324 if (i != selectedRow) { 325 newValueList.add((String ) optionTable.getValueAt(i, 0)); 326 } 327 } 328 String newValue = VariableBundle.convertToString(newValueList); 329 firePropertyChangingEvent(newValue) ; 330 ((DefaultTableModel )optionTable.getModel()).removeRow(selectedRow); 331 firePropertyChangedEvent(newValue); 332 333 removeValues.add(property + "." + selValue); 334 335 this.setChanged(true); 336 } catch (PropertyValueVetoException pvve) { 337 manager.getFactory().showError(this, "Error removing value " + selValue + " from " + property + ": " + pvve.getReason()); 338 } 339 340 } 341 342 345 public void editSelectedValue() { 346 editSelectedValue(this.getPropertyEditorPane().getContainer()); 347 } 348 349 353 protected void editSelectedValue(Container container) { 354 getLogger().fine("calling editSelectedValue()."); 355 int selectedRow = optionTable.getSelectedRow(); 356 if (selectedRow != -1) { 357 String valueToEdit = (String ) optionTable.getValueAt(selectedRow, 0); 358 String editProperty = property + "." + valueToEdit; 359 getLogger().fine("editing " + editProperty); 360 361 manager.getFactory().showNewEditorWindow(manager.getProperty(editorTemplate + ".label", editProperty), manager.getFactory().createEditor(editProperty, editorTemplate + ".editableFields", editProperty, "Composite", manager), container); 362 } else { 363 getLogger().fine("editSelectedValue(): no selected value."); 364 } 365 366 } 367 368 371 public String getNewValueName() { 372 boolean goodValue = false; 373 boolean matchFound = false; 374 375 String newName = null; 376 newName = manager.getFactory().showInputDialog(this, manager.getProperty("MultiEditorPane.renameProperty", "Enter new name.")); 377 378 while (goodValue == false) { 379 matchFound = false; 380 if (newName != null) { 381 382 for (int i = 0; i < optionTable.getRowCount() && matchFound == false; i++) { 383 if (((String )optionTable.getValueAt(i, 0)).equals(newName)) 384 matchFound = true; 385 386 } 387 388 if (matchFound == false) 389 goodValue = true; 390 else 391 newName = manager.getFactory().showInputDialog(this, manager.getProperty("MultiEditorPane.error.duplicateName", "Name already exists:") + " " + newName + "\n" + manager.getProperty("MultiEditorPane.renameProperty", "Enter new name.")); 392 } else { 393 goodValue = true; 394 } 395 } 396 397 return newName; 398 } 399 400 403 public String getStringFromList(DefaultListModel dlm) { 404 405 String retVal; 406 if (dlm.getSize() < 1) 407 return ""; 408 else 409 retVal = new String ((String )dlm.getElementAt(0)); 410 411 for (int i = 1; i < dlm.getSize(); i++) { 412 retVal = retVal.concat(":" + (String )dlm.getElementAt(i)); 413 } 414 415 return retVal; 416 } 417 418 421 public void setValue() throws PropertyValueVetoException { 422 if (isEditorEnabled()) { 423 424 if (isChanged()) { 425 getLogger().fine("setting property. property is " + property + "; value is " + getCurrentValue()); 426 manager.setProperty(property, getCurrentValue()); 427 428 for (String removeProp: removeValues) { 429 Set<String > subProperties = manager.getPropertyNamesStartingWith(removeProp + "."); 431 for (String subProp: subProperties) { 432 manager.removeProperty(subProp); 433 } 434 } 435 removeValues = new ArrayList<String >(); 436 437 } 438 } 439 } 440 441 444 public String getCurrentValue() { 445 List<String > values = new ArrayList<String >(); 446 for (int i = 0; i < optionTable.getRowCount(); i++) { 447 values.add((String ) optionTable.getValueAt(i, 0)); 448 } 449 return VariableBundle.convertToString(values); 450 } 451 452 455 public void resetDefaultValue() throws PropertyValueVetoException { 456 457 throw new UnsupportedOperationException ("reset not yet implemented for MultiEditorPane."); 459 460 467 } 468 469 472 public java.util.Properties getValue() { 473 java.util.Properties currentRetValue = new java.util.Properties (); 474 currentRetValue.setProperty(property, getCurrentValue()); 475 return currentRetValue; 476 } 477 478 482 public boolean isChanged() { 483 return changed; 484 } 485 486 490 public void setChanged(boolean newChanged) { 491 changed=newChanged; 492 } 493 494 497 public JPanel getEntryPanel() { 498 return entryPanel; 499 } 500 501 504 public SwingPropertyEditor createEditorPane(String subProperty, String subTemplate) { 505 return (SwingPropertyEditor) manager.getFactory().createEditor(subProperty, subTemplate, "Composite", manager); 506 } 507 508 511 protected void updateEditorEnabled() { 512 for (int i = 0; i < buttonList.size(); i++) { 513 buttonList.get(i).setEnabled(isEditorEnabled()); 514 } 515 } 516 517 520 public void remove() { 521 manager.removePropertyEditorListeners(getProperty()); 522 } 523 524 527 public Action[] getActions() { 528 return mDefaultActions; 529 } 530 531 534 public Action getAction(String name) { 535 for (Action action: mDefaultActions) { 536 if (action.getValue(Action.NAME) != null && action.getValue(Action.NAME).equals(name)) 537 return action; 538 } 539 540 return null; 541 } 542 543 public class AddAction extends AbstractAction { 544 public AddAction() { 545 super("editor-add"); 547 } 548 549 public void actionPerformed(ActionEvent e) { 550 555 String newValueTemplate = manager.getProperty(editorTemplate + "._addValueTemplate", ""); 558 if (newValueTemplate.length() > 0) { 559 manager.getFactory().showNewEditorWindow(manager.getProperty(newValueTemplate + ".label", newValueTemplate), manager.getFactory().createEditor(newValueTemplate, newValueTemplate, manager), getPropertyEditorPane().getContainer()); 560 561 } else { 562 addNewValue(getNewValueName(), getPropertyEditorPane().getContainer()); 563 } 564 565 } 566 } 567 568 public class EditAction extends AbstractAction { 569 public EditAction() { 570 super("editor-edit"); 571 } 572 573 public void actionPerformed(ActionEvent e) { 574 579 editSelectedValue(); 580 } 581 } 582 583 public class DeleteAction extends AbstractAction { 584 public DeleteAction() { 585 super("editor-delete"); 586 } 587 588 public void actionPerformed(ActionEvent e) { 589 removeSelectedValue(); 590 595 } 596 } 597 598 599 } 600 | Popular Tags |