KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.util.*;
4 import java.awt.event.ActionListener JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import java.awt.CardLayout JavaDoc;
7
8 /**
9  * This will made a panel which can change depending on
10  * exact properties which are then edited will depend on the value of
11  * another propery.
12  *
13  * Special settings: keyProperty is the property that will be used
14  * to define this editor.
15  */

16 public class VariableEditorPane extends CompositeSwingPropertyEditor {
17
18   HashMap<String JavaDoc, PropertyEditorUI> idToEditorMap = new HashMap<String JavaDoc, PropertyEditorUI>();
19   String JavaDoc keyProperty;
20   String JavaDoc currentKeyValue = null;
21
22   /**
23    * This configures this editor with the following values.
24    *
25    * @param propertyName The property to be edited.
26    * @param template The property that will define the layout of the
27    * editor.
28    * @param manager The PropertyEditorManager that will manage the
29    * changes.
30    */

31   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
32     configureBasic(propertyName, template, propertyBaseName, newManager);
33     debug = manager.getProperty("editors.debug", "false").equalsIgnoreCase("true");
34
35     getLogger().fine("VEP: editorTemplate=" + editorTemplate + ", getProperty template.keyProperty = '" + manager.getProperty(editorTemplate + ".keyProperty", "") + "'");
36     keyProperty = createSubProperty(manager.getProperty(editorTemplate + ".keyProperty", ""));
37     getLogger().fine("VEP: keying off property " + keyProperty);
38
39     editors = new Vector();
40
41     manager.addPropertyEditorListener(keyProperty, new PropertyEditorAdapter() {
42         public void propertyChanged(PropertyEditorUI ui, String JavaDoc prop, String JavaDoc newValue) {
43           showPanel(newValue);
44         }
45       });
46
47     this.setLayout(new java.awt.CardLayout JavaDoc());
48
49     String JavaDoc currentValue = manager.getProperty(keyProperty, "");
50     getLogger().fine("VEP: currentValue for " + keyProperty + " = " + currentValue);
51     if (currentValue == "") {
52       // check the editor for this, if any.
53
PropertyEditorUI keyEditor = manager.getPropertyEditor(keyProperty);
54       if (keyEditor != null) {
55         currentValue = keyEditor.getValue().getProperty(keyProperty, "");
56       }
57     }
58
59     showPanel(currentValue);
60
61     manager.registerPropertyEditor(property, this);
62   }
63
64   /**
65    * This shows the editor window for the configured value.
66    */

67   public void showPanel(String JavaDoc selectedId) {
68     boolean enableMe = true;
69     if (selectedId == null || selectedId.equals("")) {
70       enableMe = false;
71     }
72
73     CardLayout JavaDoc layout = (CardLayout JavaDoc) getLayout();
74
75     PropertyEditorUI newSelected = idToEditorMap.get(selectedId);
76     if (newSelected == null) {
77       // we'll have to make a new window.
78
if (selectedId == null || selectedId.equals("")) {
79         JPanel jp = new JPanel();
80         this.add(selectedId, jp);
81       } else {
82         SwingPropertyEditor spe = createEditorPane(selectedId);
83
84         // save reference to new pane in hash table
85
idToEditorMap.put(selectedId, spe);
86         editors.add(spe);
87
88         if (enableMe && isEditorEnabled()) {
89           spe.removeDisableMask(this);
90         } else {
91           spe.addDisableMask(this);
92         }
93         this.add(selectedId, spe);
94       }
95     }
96     layout.show(this, selectedId);
97     currentKeyValue = selectedId;
98   }
99
100   /**
101    * Creates a SwingPropertyEditor for the given subproperty.
102    */

103   public SwingPropertyEditor createEditorPane(String JavaDoc selectedId) {
104
105     String JavaDoc editValue = createSubTemplate("." + selectedId);
106
107     SwingPropertyEditor returnValue = (SwingPropertyEditor)manager.getFactory().createEditor(property, editValue, propertyBase, manager);
108     return returnValue;
109   }
110
111
112   /**
113    * Returns the helpId for this editor.
114    */

115   public String JavaDoc getHelpID() {
116     String JavaDoc subProperty = manager.getProperty(editorTemplate + ".helpController", "");
117     if (subProperty.length() == 0) {
118       if (currentKeyValue != null) {
119         PropertyEditorUI selectedEditor = idToEditorMap.get(currentKeyValue);
120         if (selectedEditor == null) {
121           return super.getHelpID();
122         } else {
123           return selectedEditor.getHelpID();
124         }
125       } else {
126         return super.getHelpID();
127       }
128     } else {
129       return super.getHelpID();
130     }
131   }
132
133   /**
134    * This writes the currently configured values in the PropertyEditorUI
135    * to the source VariableBundle.
136    */

137   public void setValue() throws PropertyValueVetoException {
138     validateProperty();
139     if (currentKeyValue != null) {
140       PropertyEditorUI selectedEditor = idToEditorMap.get(currentKeyValue);
141       selectedEditor.setValue();
142     }
143   }
144
145   /**
146    * Validates the currently selected editor.
147    */

148   public void validateProperty() throws PropertyValueVetoException {
149     if (currentKeyValue != null) {
150       PropertyEditorUI selectedEditor = idToEditorMap.get(currentKeyValue);
151       selectedEditor.validateProperty();
152     }
153   }
154
155   /**
156    * Returns the current values of the edited properties as a
157    * java.util.Properties object.
158    */

159   public java.util.Properties JavaDoc getValue() {
160     java.util.Properties JavaDoc currentRetValue = new java.util.Properties JavaDoc();
161     if (currentKeyValue != null) {
162       PropertyEditorUI selectedEditor = idToEditorMap.get(currentKeyValue);
163       currentRetValue.putAll(selectedEditor.getValue());
164     }
165     return currentRetValue;
166   }
167
168 }
169
170
171
172
Popular Tags