1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import net.suberic.util.*; 4 import java.awt.CardLayout ; 5 import javax.swing.event.*; 6 import java.util.*; 7 import javax.swing.*; 8 9 31 32 public class SectionedEditorPane extends CompositeSwingPropertyEditor implements ListSelectionListener { 33 34 JList optionList; 35 JPanel entryPanel; 36 boolean changed = false; 37 DefaultListModel optionListModel; 38 List templates; 39 40 Hashtable<String , SwingPropertyEditor> currentPanels = new Hashtable<String , SwingPropertyEditor>(); 41 42 51 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 52 configureBasic(propertyName, template, propertyBaseName, newManager); 53 54 editors = new Vector(); 56 57 59 List propertyList = manager.getPropertyAsList(propertyName + ".editableFields", ""); 60 61 optionList = createOptionList(propertyList); 62 63 JScrollPane optionScrollPane = new JScrollPane(optionList); 64 SpringLayout layout = new SpringLayout(); 65 this.setLayout(layout); 66 67 this.add(optionScrollPane); 68 layout.putConstraint(SpringLayout.WEST, optionScrollPane, 5, SpringLayout.WEST, this); 69 layout.putConstraint(SpringLayout.NORTH, optionScrollPane, 5, SpringLayout.NORTH, this); 70 layout.putConstraint(SpringLayout.SOUTH, optionScrollPane, -5, SpringLayout.SOUTH, this); 72 73 76 entryPanel = createEntryPanel(propertyList); 77 78 java.awt.Component entryComponent = entryPanel; 79 if (manager.getProperty(template + "._useScrollPane", "false").equalsIgnoreCase("true")) { 80 JScrollPane jsp = new JScrollPane(entryPanel); 81 87 entryComponent = jsp; 88 } 89 90 this.add(entryComponent); 91 92 layout.putConstraint(SpringLayout.WEST, entryComponent, 0 ,SpringLayout.EAST, optionScrollPane); 95 96 layout.putConstraint(SpringLayout.NORTH, entryComponent, 0 ,SpringLayout.NORTH, this); 97 layout.putConstraint(SpringLayout.SOUTH, this, 0 ,SpringLayout.SOUTH, entryComponent); 98 layout.putConstraint(SpringLayout.EAST, this, 0 ,SpringLayout.EAST, entryComponent); 99 100 updateEditorEnabled(); 101 102 manager.registerPropertyEditor(property, this); 103 104 optionList.addListSelectionListener(this); 105 } 106 107 110 private JList createOptionList(List editedProperties) { 111 112 optionListModel = new DefaultListModel(); 113 114 Iterator iter = editedProperties.iterator(); 115 while (iter.hasNext()) { 116 String key = (String ) iter.next(); 117 String iconString = manager.getProperty(key + ".Icon", ""); 118 Icon icon = null; 119 if (iconString != "") { 120 icon = manager.getIconManager().getIcon(iconString); 121 } 122 SEPListEntry listEntry = new SEPListEntry(manager.getProperty(key + ".Label", key), icon, key); 123 optionListModel.addElement(listEntry); 124 } 125 126 JList returnValue = new JList(optionListModel); 127 returnValue.setSelectedIndex(0); 128 returnValue.setCellRenderer(new SEPCellRenderer()); 129 return returnValue; 130 } 131 132 138 private JPanel createEntryPanel (List itemList) { 139 CardLayout entryLayout = new CardLayout (); 140 JPanel panel = new JPanel(entryLayout); 141 142 for (Object o: itemList) { 143 String rootProp = (String ) o; 144 SwingPropertyEditor sep = createEditorPane(rootProp, rootProp); 145 146 getLogger().fine("creating editor for " + rootProp); 147 currentPanels.put(rootProp, sep); 149 editors.add(sep); 150 151 panel.add(rootProp, sep); 152 } 153 String defaultProperty = manager.getProperty(property + "._default", ""); 154 155 if (defaultProperty != "") 156 entryLayout.show(panel, defaultProperty); 157 158 return panel; 159 } 160 161 165 public void valueChanged(ListSelectionEvent e) { 166 167 CardLayout entryLayout = (CardLayout )entryPanel.getLayout(); 168 169 String selectedId = ((SEPListEntry)((JList)e.getSource()).getSelectedValue()).getKey(); 170 171 getLogger().fine("selectedId = " + selectedId); 172 if (selectedId != null) { 173 SwingPropertyEditor newSelected = currentPanels.get(selectedId); 174 getLogger().fine("newSelected = " + newSelected); 175 entryLayout.show(entryPanel, selectedId); 176 } 177 178 } 179 180 183 public void editSelectedValue() { 184 } 185 186 189 public void setValue() throws PropertyValueVetoException { 190 if (isEnabled()) { 191 super.setValue(); 192 } 193 } 194 195 198 public void resetDefaultValue() throws PropertyValueVetoException { 199 200 if (isChanged()) { 201 firePropertyChangingEvent(originalValue); 202 optionListModel.removeAllElements(); 203 entryPanel.removeAll(); 204 205 firePropertyChangedEvent(originalValue); 206 } 207 208 java.awt.Component [] components = entryPanel.getComponents(); 209 for (int i = 0; i < components.length; i++) { 210 ((CompositeEditorPane)components[i]).resetDefaultValue(); 211 } 212 } 213 214 217 public java.util.Properties getValue() { 218 java.util.Properties currentRetValue = super.getValue(); 219 return currentRetValue; 220 } 221 222 226 public boolean isChanged() { 227 return changed; 228 } 229 230 234 public void setChanged(boolean newChanged) { 235 changed=newChanged; 236 } 237 238 241 public JList getOptionList() { 242 return optionList; 243 } 244 245 248 public JPanel getEntryPanel() { 249 return entryPanel; 250 } 251 252 255 public SwingPropertyEditor createEditorPane(String subProperty, String subTemplate) { 256 getLogger().fine("creating editor for " + subProperty + ", template " + subTemplate); 257 return (SwingPropertyEditor) manager.getFactory().createEditor(subProperty, subTemplate, manager); 258 } 259 260 263 protected void updateEditorEnabled() { 264 265 optionList.setEnabled(isEditorEnabled()); 266 267 for (int i = 0; i < editors.size() ; i++) { 268 PropertyEditorUI current = (PropertyEditorUI) editors.get(i); 269 if (isEditorEnabled()) { 270 current.removeDisableMask(this); 271 } else { 272 current.addDisableMask(this); 273 } 274 } 275 } 276 277 280 public String getHelpID() { 281 CardLayout entryLayout = (CardLayout )entryPanel.getLayout(); 282 SEPListEntry selectedValue = (SEPListEntry) optionList.getSelectedValue(); 283 if (selectedValue != null) { 284 String selectedId = selectedValue.getKey(); 285 286 if (selectedId != null) { 287 SwingPropertyEditor newSelected = currentPanels.get(selectedId); 288 return newSelected.getHelpID(); 289 } 290 } 291 return getEditorTemplate(); 292 } 293 294 class SEPCellRenderer extends JLabel implements ListCellRenderer { 295 296 public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 297 SEPListEntry sepValue = (SEPListEntry) value; 298 String label = sepValue.getLabel(); 299 this.setText(label); 300 this.setIcon(sepValue.getIcon()); 301 if (isSelected) { 302 setBackground(list.getSelectionBackground()); 303 setForeground(list.getSelectionForeground()); 304 } else { 305 setBackground(list.getBackground()); 306 setForeground(list.getForeground()); 307 } 308 setEnabled(list.isEnabled()); 309 setFont(list.getFont()); 310 setOpaque(true); 311 return this; 312 } 313 } 314 315 class SEPListEntry { 316 String label; 317 Icon icon; 318 String key; 319 320 public SEPListEntry(String pLabel, Icon pIcon, String pKey) { 321 label = pLabel; 322 icon = pIcon; 323 key = pKey; 324 } 325 326 public String getLabel() { 327 return label; 328 } 329 330 public Icon getIcon() { 331 return icon; 332 } 333 334 public String getKey() { 335 return key; 336 } 337 } 338 339 } 340 | Popular Tags |