KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > SectionedEditorPane


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import net.suberic.util.*;
4 import java.awt.CardLayout JavaDoc;
5 import javax.swing.event.*;
6 import java.util.*;
7 import javax.swing.*;
8
9 /**
10  * This class will make an editor for a list of elements, where each
11  * element will be displayed on the left and, by selecting one of these
12  * elements, the editor for that item will appear in the panel to the
13  * right.
14  *
15  * Configuration is as follows:
16  *
17  * Foo.propertyType=Sectioned -- shows this is a property editor that
18  * uses a SectionedEditorPane
19  *
20  * Foo.editableFields=Foo.bar:Foo.baz -- shows which subfields are to be edited
21  *
22  * Foo._default=Foo.bar -- shows that by default, the editor for Foo.bar
23  * is shown. If this is not included or blank,
24  * then no editor is displayed by default.
25  *
26  * The value for Foo itself is not used.
27  *
28  * If your Foo.editableFields=Foo.bar:.baz:Frotz.zork, then the values
29  * edited will be defined by Foo.bar, Foo.baz, and Frotz.zork.
30  */

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 JavaDoc, SwingPropertyEditor> currentPanels = new Hashtable<String JavaDoc, SwingPropertyEditor>();
41
42   /**
43    * This configures this editor with the following values.
44    *
45    * @param propertyName The property to be edited.
46    * @param template The property that will define the layout of the
47    * editor.
48    * @param manager The PropertyEditorManager that will manage the
49    * changes.
50    */

51   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
52     configureBasic(propertyName, template, propertyBaseName, newManager);
53
54     // create the editors list.
55
editors = new Vector();
56
57     // create the list of properties to be edited.
58

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, this, 5, SpringLayout.SOUTH, optionScrollPane);
71
layout.putConstraint(SpringLayout.SOUTH, optionScrollPane, -5, SpringLayout.SOUTH, this);
72
73     // create entryPanels (the panels which show the editors for each
74
// property in the optionList) for each option.
75

76     entryPanel = createEntryPanel(propertyList);
77
78     java.awt.Component JavaDoc entryComponent = entryPanel;
79     if (manager.getProperty(template + "._useScrollPane", "false").equalsIgnoreCase("true")) {
80       JScrollPane jsp = new JScrollPane(entryPanel);
81       /*
82       java.awt.Dimension size = jsp.getPreferredSize();
83       size.height = Math.min(size.height, 400);
84       size.width = Math.min(size.width, 475);
85       jsp.setPreferredSize(size);
86       */

87       entryComponent = jsp;
88     }
89
90     this.add(entryComponent);
91
92     // the entry components should handle their offsets themselves, so
93
// put them on the very edges.
94
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   /**
108    * Creates the list of edited items.
109    */

110   private JList createOptionList(List editedProperties) {
111
112     optionListModel = new DefaultListModel();
113
114     Iterator iter = editedProperties.iterator();
115     while (iter.hasNext()) {
116       String JavaDoc key = (String JavaDoc) iter.next();
117       String JavaDoc 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   /**
133    * This creates a panel for each option. It uses a CardLayout.
134    *
135    * Note that this is also the section of code which determines which
136    * subproperties are to be edited.
137    */

138   private JPanel createEntryPanel (List itemList) {
139     CardLayout JavaDoc entryLayout = new CardLayout JavaDoc();
140     JPanel panel = new JPanel(entryLayout);
141
142     for (Object JavaDoc o: itemList) {
143       String JavaDoc rootProp = (String JavaDoc) o;
144       SwingPropertyEditor sep = createEditorPane(rootProp, rootProp);
145
146       getLogger().fine("creating editor for " + rootProp);
147       // save reference to new pane in hash table
148
currentPanels.put(rootProp, sep);
149       editors.add(sep);
150
151       panel.add(rootProp, sep);
152     }
153     String JavaDoc defaultProperty = manager.getProperty(property + "._default", "");
154
155     if (defaultProperty != "")
156       entryLayout.show(panel, defaultProperty);
157
158     return panel;
159   }
160
161   /**
162    * Called when the selected value changed. Should result in the
163    * entryPane changing.
164    */

165   public void valueChanged(ListSelectionEvent e) {
166
167     CardLayout JavaDoc entryLayout = (CardLayout JavaDoc)entryPanel.getLayout();
168
169     String JavaDoc 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   /**
181    * Edits the currently selected value.
182    */

183   public void editSelectedValue() {
184   }
185
186   /**
187    * Sets the value for this SectionedEditorPane.
188    */

189   public void setValue() throws PropertyValueVetoException {
190     if (isEnabled()) {
191       super.setValue();
192     }
193   }
194
195   /**
196    * Resets the default values.
197    */

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 JavaDoc[] components = entryPanel.getComponents();
209     for (int i = 0; i < components.length; i++) {
210       ((CompositeEditorPane)components[i]).resetDefaultValue();
211     }
212   }
213
214   /**
215    * Returns the currently edited values as a Properties object.
216    */

217   public java.util.Properties JavaDoc getValue() {
218     java.util.Properties JavaDoc currentRetValue = super.getValue();
219     return currentRetValue;
220   }
221
222   /**
223    * Returns whether or not the top-level edited values of this EditorPane
224    * have changed.
225    */

226   public boolean isChanged() {
227     return changed;
228   }
229
230   /**
231    * Sets whether or not the top-level edited values of this EditorPane
232    * have changed.
233    */

234   public void setChanged(boolean newChanged) {
235     changed=newChanged;
236   }
237
238   /**
239    * Returns the optionList.
240    */

241   public JList getOptionList() {
242     return optionList;
243   }
244
245   /**
246    * Returns the entryPanel.
247    */

248   public JPanel getEntryPanel() {
249     return entryPanel;
250   }
251
252   /**
253    * Creates an editor.
254    */

255   public SwingPropertyEditor createEditorPane(String JavaDoc subProperty, String JavaDoc subTemplate) {
256     getLogger().fine("creating editor for " + subProperty + ", template " + subTemplate);
257     return (SwingPropertyEditor) manager.getFactory().createEditor(subProperty, subTemplate, manager);
258   }
259
260   /**
261    * Run when the PropertyEditor may have changed enabled states.
262    */

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   /**
278    * Returns the helpId for this editor.
279    */

280   public String JavaDoc getHelpID() {
281     CardLayout JavaDoc entryLayout = (CardLayout JavaDoc)entryPanel.getLayout();
282     SEPListEntry selectedValue = (SEPListEntry) optionList.getSelectedValue();
283     if (selectedValue != null) {
284       String JavaDoc 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 JavaDoc getListCellRendererComponent(JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
297       SEPListEntry sepValue = (SEPListEntry) value;
298       String JavaDoc 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 JavaDoc label;
317     Icon icon;
318     String JavaDoc key;
319
320     public SEPListEntry(String JavaDoc pLabel, Icon pIcon, String JavaDoc pKey) {
321       label = pLabel;
322       icon = pIcon;
323       key = pKey;
324     }
325
326     public String JavaDoc getLabel() {
327       return label;
328     }
329
330     public Icon getIcon() {
331       return icon;
332     }
333
334     public String JavaDoc getKey() {
335       return key;
336     }
337   }
338
339 }
340
Popular Tags