KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.awt.Component JavaDoc;
4 import java.awt.Container JavaDoc;
5 import java.util.Vector JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 /**
12  * <p>This will make an editor for a list of properties.</p>
13  *
14  * <p>Note that CompositeSwingPropertyEditors generally will append
15  * subProperties that start with "." to the source template and property.
16  * In addition, CompositeSwingPropertyEditors have two properties
17  * that can vary this behavior: if "propertyScoped" is set to true and
18  * the subproperty does not start with ".", then the original property is
19  * passed through. If "addSubProperty" is set to false, then the original
20  * property will be passed through even if the subProperty starts with "."
21  * </p>
22  *
23  * <p>For template properties, the value 'templateBase" can be set
24  * to indicate the template base to use for scoped subtemplates, so that
25  * you can use the same set of subproperty definitions for multiple templated
26  * locations (i.e. using Store.editor.main.server as the key for .server
27  * for both Store.editor.main.imap and Store.editor.main.pop3).</p>
28  *
29  * <p>In addition, for a templated subproperty you can set
30  * '.appendProperty=false', which will result in that subproperty not
31  * being added to the property itself.</p>
32  */

33 public abstract class CompositeSwingPropertyEditor extends SwingPropertyEditor {
34   protected List JavaDoc<SwingPropertyEditor> editors;
35   protected Logger JavaDoc mLogger = Logger.getLogger("editors.debug");
36
37   /**
38    * This writes the currently configured values in the PropertyEditorUI
39    * to the source VariableBundle.
40    */

41   public void setValue() throws PropertyValueVetoException {
42     if (isEditorEnabled()) {
43       List JavaDoc<PropertyValueVetoException> exceptionList = new ArrayList JavaDoc<PropertyValueVetoException>();
44       for (int i = 0; i < editors.size() ; i++) {
45         try {
46           editors.get(i).setValue();
47         } catch (PropertyValueVetoException pvve) {
48           exceptionList.add(pvve);
49         }
50       }
51       if (exceptionList.size() > 0) {
52         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
53         Iterator JavaDoc<PropertyValueVetoException> iter = exceptionList.iterator();
54         while (iter.hasNext()) {
55           PropertyValueVetoException pvve = iter.next();
56           builder.append(pvve.getMessage());
57           if (iter.hasNext())
58             builder.append("\r\n");
59         }
60         throw new PropertyValueVetoException(builder.toString());
61       }
62     }
63   }
64
65   public void validateProperty() throws PropertyValueVetoException {
66
67     if (isEditorEnabled()) {
68       List JavaDoc<PropertyValueVetoException> exceptionList = new ArrayList JavaDoc<PropertyValueVetoException>();
69       for (int i = 0; i < editors.size() ; i++) {
70         try {
71           editors.get(i).validateProperty();
72         } catch (PropertyValueVetoException pvve) {
73           exceptionList.add(pvve);
74         }
75       }
76       if (exceptionList.size() > 0) {
77         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
78         Iterator JavaDoc<PropertyValueVetoException> iter = exceptionList.iterator();
79         while (iter.hasNext()) {
80           PropertyValueVetoException pvve = iter.next();
81           builder.append(pvve.getMessage());
82           if (iter.hasNext())
83             builder.append("\r\n");
84         }
85         throw new PropertyValueVetoException(builder.toString());
86       }
87     }
88   }
89
90   /**
91    * This resets the editor to the original (or latest set, if setValue()
92    * has been called) value of the edited property.
93    */

94     public void resetDefaultValue() throws PropertyValueVetoException {
95     if (isEditorEnabled()) {
96       for (int i = 0; i < editors.size() ; i++) {
97         editors.get(i).resetDefaultValue();
98       }
99     }
100   }
101
102   /**
103    * Returns the current values of the edited properties as a
104    * java.util.Properties object.
105    */

106   public java.util.Properties JavaDoc getValue() {
107     java.util.Properties JavaDoc currentRetValue = new java.util.Properties JavaDoc();
108     java.util.Iterator JavaDoc<SwingPropertyEditor> iter = editors.iterator();
109     while (iter.hasNext()) {
110       currentRetValue.putAll(iter.next().getValue());
111     }
112
113     return currentRetValue;
114   }
115
116   /**
117    * Returns the appropriate property for this source property.
118    */

119   public String JavaDoc createSubProperty(String JavaDoc pSource) {
120     if (pSource.startsWith(".")) {
121       if (manager.getProperty(editorTemplate + ".addSubProperty", "").equalsIgnoreCase("false") || manager.getProperty(createSubTemplate(pSource) + ".appendProperty", "true").equalsIgnoreCase("false")) {
122         return property;
123       } else {
124         return property + pSource;
125       }
126     } else {
127       if (manager.getProperty(editorTemplate + ".propertyScoped", "").equalsIgnoreCase("true")) {
128         return property;
129       } else {
130         return pSource;
131       }
132     }
133   }
134
135   /**
136    * Returns the appropriate tempate for this source property.
137    */

138   public String JavaDoc createSubTemplate(String JavaDoc pSource) {
139     if (pSource.startsWith(".")) {
140       return manager.getProperty(editorTemplate + ".templateBase", editorTemplate) + pSource;
141     } else {
142       return pSource;
143     }
144   }
145
146   /**
147    * Returns the appropriate propertyBase for this source property.
148    */

149   public String JavaDoc createSubPropertyBase(String JavaDoc pSource) {
150     if (! pSource.startsWith(".") && ! manager.getProperty(editorTemplate + ".propertyScoped", "").equalsIgnoreCase("true")) {
151       return pSource;
152     } else {
153       return propertyBase;
154     }
155   }
156
157   /**
158    * Lays out the composite property editor in a grid.
159    */

160   protected void layoutGrid(Container JavaDoc parent, Component JavaDoc[] labelComponents, Component JavaDoc[] valueComponents, int initialX, int initialY, int xPad, int yPad, boolean nested) {
161     SpringLayout layout;
162     try {
163       layout = (SpringLayout)parent.getLayout();
164     } catch (ClassCastException JavaDoc exc) {
165       System.err.println("The first argument to layoutGrid must use SpringLayout.");
166       return;
167     }
168
169     if (labelComponents == null || labelComponents.length < 1) {
170       System.err.println("Attempt to layoutGrid with no components.");
171       return;
172     }
173
174     // go through both columns.
175
Spring labelWidth = Spring.constant(0);
176     Spring valueWidth = Spring.constant(0);
177     Spring fullWidth = Spring.constant(0);
178
179     Spring labelValueXOffset = Spring.constant(initialX, initialX, 32000);
180     Spring xOffset = Spring.constant(initialX, initialX, initialX);
181     Spring fullXOffset = Spring.constant(initialX, initialX, 32000);
182
183     for (int i = 0; i < labelComponents.length; i++) {
184       // for components with a label and a value, add to labelWidth and
185
// valueWidth.
186
if (valueComponents[i] != null) {
187         labelWidth = Spring.max(labelWidth, layout.getConstraints(labelComponents[i]).getWidth());
188         valueWidth = Spring.max(valueWidth, layout.getConstraints(valueComponents[i]).getWidth());
189       } else {
190         // otherwise just add to fullWidth.
191
fullWidth = Spring.max(fullWidth, layout.getConstraints(labelComponents[i]).getWidth());
192       }
193     }
194
195     // make sure fullWidth and labelWidth + valueWidth match.
196
if (fullWidth.getValue() <= labelWidth.getValue() + xPad + valueWidth.getValue()) {
197       fullWidth = Spring.sum(labelWidth, Spring.sum(Spring.constant(xPad), valueWidth));
198     } else {
199       valueWidth = Spring.sum(fullWidth, Spring.minus(Spring.sum(Spring.constant(xPad), labelWidth)));
200     }
201
202     for (int i = 0; i < labelComponents.length; i++) {
203       if (valueComponents[i] != null) {
204         SpringLayout.Constraints constraints = layout.getConstraints(labelComponents[i]);
205         //layout.putConstraint(SpringLayout.WEST, labelComponents[i], labelValueXOffset, SpringLayout.WEST, parent);
206
layout.putConstraint(SpringLayout.WEST, labelComponents[i], xOffset, SpringLayout.WEST, parent);
207         constraints.setWidth(labelWidth);
208
209         constraints = layout.getConstraints(valueComponents[i]);
210         layout.putConstraint(SpringLayout.WEST, valueComponents[i], xPad, SpringLayout.EAST, labelComponents[i]);
211         constraints.setWidth(valueWidth);
212         if (i == 0) {
213           layout.putConstraint(SpringLayout.EAST, parent, fullXOffset, SpringLayout.EAST, valueComponents[i]);
214         }
215       } else {
216         // set for the full width.
217
SpringLayout.Constraints constraints = layout.getConstraints(labelComponents[i]);
218         //layout.putConstraint(SpringLayout.WEST, labelComponents[i], fullXOffset, SpringLayout.WEST, parent);
219
layout.putConstraint(SpringLayout.WEST, labelComponents[i], xOffset, SpringLayout.WEST, parent);
220         constraints.setWidth(fullWidth);
221         if (i == 0) {
222           layout.putConstraint(SpringLayout.EAST, parent, fullXOffset, SpringLayout.EAST, labelComponents[i]);
223         }
224       }
225     }
226
227     //Align all cells in each row and make them the same height.
228
for (int i = 0; i < labelComponents.length; i++) {
229       Spring height = Spring.constant(0);
230       if (valueComponents[i] != null) {
231         height = Spring.max(layout.getConstraints(labelComponents[i]).getHeight(), layout.getConstraints(valueComponents[i]).getHeight());
232         if (i == 0) {
233           layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.NORTH, parent);
234         } else {
235           layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.SOUTH, labelComponents[i - 1]);
236         }
237         layout.putConstraint(SpringLayout.NORTH, valueComponents[i], 0, SpringLayout.NORTH, labelComponents[i]);
238         layout.putConstraint(SpringLayout.SOUTH, valueComponents[i], 0, SpringLayout.SOUTH, labelComponents[i]);
239
240         layout.getConstraints(labelComponents[i]).setHeight(height);
241         layout.getConstraints(valueComponents[i]).setHeight(height);
242       } else {
243         if (i == 0) {
244           layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.NORTH, parent);
245         } else {
246           layout.putConstraint(SpringLayout.NORTH, labelComponents[i], yPad, SpringLayout.SOUTH, labelComponents[i - 1]);
247         }
248       }
249     }
250
251     Spring southBoundary = Spring.constant(yPad, yPad, 32000);
252     if (nested) {
253       southBoundary = Spring.constant(yPad);
254     }
255
256     layout.putConstraint(SpringLayout.SOUTH, parent, southBoundary, SpringLayout.SOUTH, labelComponents[labelComponents.length - 1]);
257     //Set the parent's size.
258
//pCons.setConstraint(SpringLayout.EAST, Spring.sum(fullWidth, Spring.constant(initialX)));
259
}
260
261   /**
262    * Adds a disable mask to the PropertyEditorUI.
263    */

264   public void addDisableMask(Object JavaDoc key) {
265     disableMaskSet.add(key);
266     updateEditorEnabled();
267   }
268
269   /**
270    * Removes the disable mask keyed by this Object.
271    */

272   public void removeDisableMask(Object JavaDoc key) {
273     disableMaskSet.remove(key);
274     updateEditorEnabled();
275   }
276
277   /**
278    * Run when the PropertyEditor may have changed enabled states.
279    */

280   protected void updateEditorEnabled() {
281     if (isEditorEnabled()) {
282       for(PropertyEditorUI editor: editors) {
283         editor.removeDisableMask(this);
284       }
285     } else {
286       for(PropertyEditorUI editor: editors) {
287         editor.addDisableMask(this);
288       }
289     }
290   }
291
292   /**
293    * Gets the parent PropertyEditorPane for the given component.
294    */

295   public PropertyEditorPane getPropertyEditorPane() {
296     return getPropertyEditorPane(this);
297   }
298
299   /**
300    * Returns the helpId for this editor.
301    */

302   public String JavaDoc getHelpID() {
303     String JavaDoc subProperty = manager.getProperty(editorTemplate + ".helpController", "");
304     if (subProperty.length() == 0)
305       return getEditorTemplate();
306     else {
307       String JavaDoc controllerProperty = createSubTemplate(subProperty);
308       Iterator JavaDoc<SwingPropertyEditor> iter = editors.iterator();
309       while(iter.hasNext()) {
310         PropertyEditorUI ui = iter.next();
311         if (ui.getEditorTemplate().equals(controllerProperty)) {
312           return ui.getHelpID();
313         }
314       }
315     }
316
317     return getEditorTemplate();
318   }
319
320   /**
321    * Returns the display value for this property.
322    */

323   public String JavaDoc getDisplayValue() {
324     return getProperty();
325   }
326
327   /**
328    * Removes the PropertyEditor.
329    */

330   public void remove() {
331     manager.removePropertyEditorListeners(getProperty());
332     for (PropertyEditorUI editor: editors) {
333       editor.remove();
334     }
335   }
336
337   /**
338    * Accepts or rejects the initial focus for this component.
339    */

340   public boolean acceptDefaultFocus() {
341     if (editors != null) {
342       for (SwingPropertyEditor editor: editors) {
343         if (editor.acceptDefaultFocus()) {
344           return true;
345         }
346       }
347       return false;
348     } else {
349       return false;
350     }
351   }
352
353
354 }
355
356
357
358
Popular Tags