001: package net.suberic.util.gui.propedit;
002:
003: import net.suberic.util.*;
004: import javax.swing.*;
005: import java.awt.Dimension;
006: import java.util.List;
007: import java.util.LinkedList;
008:
009: /**
010: * A Swing implementation of the PropertyEditorUI.
011: */
012: public abstract class SwingPropertyEditor extends JPanel implements
013: PropertyEditorUI {
014: // debug flag
015: protected boolean debug = false;
016:
017: // shows whether or not this component is enabled.
018: protected boolean enabled;
019:
020: // the property being edited.
021: protected String property;
022:
023: // the template to use
024: protected String editorTemplate;
025:
026: // the original value of the property.
027: protected String originalValue;
028:
029: // the PorpertyEditorManager for this instance.
030: protected PropertyEditorManager manager;
031:
032: // the label component. this is used for a default implementation
033: // of the sizing code we have below.
034: protected java.awt.Container labelComponent;
035:
036: // the value component. this is used for a default implementation
037: // of the sizing code we have below.
038: protected java.awt.Container valueComponent;
039:
040: // the listener list.
041: protected List listenerList = new LinkedList();
042:
043: /**
044: * Creates a new SwingPropertyEditor, in this case a JPanel with a
045: * GridBagLayout. Note that configureEditor() will need to get called
046: * on this component in order to make it useful.
047: */
048: public SwingPropertyEditor() {
049: super ();
050: this .setLayout(new java.awt.GridBagLayout());
051: }
052:
053: /**
054: * Creates a SwingPropertyEditor using the given property and manager.
055: *
056: * @param propertyName The property to be edited. This property will
057: * also be used to define the layout of this Editor.
058: * @param newManager The PropertyEditorManager that will manage the
059: * changes.
060: * @param isEnabled Whether or not this editor is enabled by default.
061: */
062: public SwingPropertyEditor(String propertyName,
063: PropertyEditorManager newManager, boolean isEnabled) {
064: configureEditor(propertyName, propertyName, newManager,
065: isEnabled);
066: }
067:
068: /**
069: * Creates a SwingPropertyEditor using the given property and manager.
070: *
071: * @param propertyName The property to be edited.
072: * @param template The property that will define the layout of the
073: * editor.
074: * @param newManager The PropertyEditorManager that will manage the
075: * changes.
076: */
077: public SwingPropertyEditor(String propertyName, String template,
078: PropertyEditorManager newManager) {
079: configureEditor(propertyName, template, newManager, true);
080: }
081:
082: /**
083: * Creates a SwingPropertyEditor using the given property and manager.
084: *
085: * @param propertyName The property to be edited. This property will
086: * also be used to define the layout of this Editor.
087: * @param newManager The PropertyEditorManager that will manage the
088: * changes.
089: */
090: public void configureEditor(String propertyName,
091: PropertyEditorManager newManager) {
092: configureEditor(propertyName, propertyName, newManager, true);
093: }
094:
095: /**
096: * Creates a SwingPropertyEditor using the given property and manager.
097: *
098: * @param propertyName The property to be edited. This property will
099: * also be used to define the layout of this Editor.
100: * @param newManager The PropertyEditorManager that will manage the
101: * changes.
102: * @param isEnabled Whether or not this editor is enabled by default.
103: */
104: public void configureEditor(String propertyName,
105: PropertyEditorManager newManager, boolean isEnabled) {
106: configureEditor(propertyName, propertyName, newManager,
107: isEnabled);
108: }
109:
110: /**
111: * A default implementation of setEnabled. This simply sets the
112: * enabled flag to the newValue. If the labelComponent and
113: * valueComponent attributes are set, it will also call setEnabled
114: * on those.
115: *
116: * Subclasses which do not use the default labelComponent and
117: * valueComponent attributes, or which require additional functionality,
118: * should override this method.
119: */
120: public void setEnabled(boolean newValue) {
121: enabled = newValue;
122: }
123:
124: /**
125: * Returns the enabled flag.
126: */
127: public boolean isEnabled() {
128: return enabled;
129: }
130:
131: /**
132: * Creates a JLabel for this component.
133: */
134: public JLabel createLabel() {
135: String defaultLabel;
136: int dotIndex = property.lastIndexOf(".");
137: if (dotIndex == -1)
138: defaultLabel = new String(property);
139: else
140: defaultLabel = property.substring(dotIndex + 1);
141:
142: JLabel returnValue = new JLabel(manager.getProperty(
143: editorTemplate + ".label", defaultLabel));
144:
145: return returnValue;
146: }
147:
148: /**
149: * Gets the minimum size for the labelComponent.
150: */
151: public Dimension getMinimumLabelSize() {
152: if (labelComponent != null) {
153: return labelComponent.getMinimumSize();
154: } else {
155: return new Dimension(0, 0);
156: }
157: }
158:
159: /**
160: * Gets the minimum size for the valueComponent.
161: */
162: public Dimension getMinimumValueSize() {
163: if (valueComponent != null) {
164: return valueComponent.getMinimumSize();
165: } else {
166: return new Dimension(0, 0);
167: }
168: }
169:
170: /**
171: * Returns the calculated minimum size for this component.
172: */
173: public Dimension getMinimumTotalSize() {
174: return this .getMinimumSize();
175: }
176:
177: /**
178: * Sets the size for the label component and the value component.
179: */
180: public void setSizes(Dimension labelSize, Dimension valueSize) {
181: if (labelComponent != null)
182: labelComponent.setSize(labelSize);
183: if (valueComponent != null)
184: valueComponent.setSize(valueSize);
185: }
186:
187: /**
188: * Sets the widths for the label component and the value component.
189: */
190: public void setWidths(int labelWidth, int valueWidth) {
191: if (labelComponent != null)
192: labelComponent.setSize(new Dimension(labelWidth,
193: labelComponent.getSize().height));
194: if (valueComponent != null)
195: valueComponent.setSize(new Dimension(valueWidth,
196: valueComponent.getSize().height));
197: }
198:
199: /**
200: * Sets the heights for the label component and the value component.
201: */
202: public void setHeights(int labelHeight, int valueHeight) {
203: if (labelComponent != null)
204: labelComponent.setSize(new Dimension(labelComponent
205: .getSize().width, labelHeight));
206: if (valueComponent != null)
207: valueComponent.setSize(new Dimension(valueComponent
208: .getSize().width, valueHeight));
209: }
210:
211: /**
212: * Gets the current valueComponent.
213: */
214: public java.awt.Container getValueComponent() {
215: return valueComponent;
216: }
217:
218: /**
219: * Gets the current labelComponent.
220: */
221: public java.awt.Container getLabelComponent() {
222: return valueComponent;
223: }
224:
225: /**
226: * Gets the PropertyEditorManager
227: */
228: public PropertyEditorManager getManager() {
229: return manager;
230: }
231:
232: /**
233: * Adds a PropertyEditorListener to the ListenerList.
234: */
235: public void addPropertyEditorListener(PropertyEditorListener pel) {
236: if (pel != null && !listenerList.contains(pel))
237: listenerList.add(pel);
238: }
239:
240: /**
241: * Removes a PropertyEditorListener from the ListenerList.
242: */
243: public void removePropertyEditorListener(PropertyEditorListener pel) {
244: if (pel != null && listenerList.contains(pel))
245: listenerList.remove(pel);
246: }
247:
248: /**
249: * Fires a propertyChanging event to all of the PropertyEditorListeners.
250: * If any of the listeners veto the new value, then this returns false.
251: * Otherwise, returns true.
252: */
253: public void firePropertyChangingEvent(String newValue)
254: throws PropertyValueVetoException {
255: for (int i = 0; i < listenerList.size(); i++) {
256: PropertyEditorListener current = (PropertyEditorListener) listenerList
257: .get(i);
258: current.propertyChanging(this , property, newValue);
259: }
260: }
261:
262: /**
263: * Fires a propertyChanged event to all of the PropertyEditorListeners.
264: */
265: public void firePropertyChangedEvent(String newValue) {
266: for (int i = 0; i < listenerList.size(); i++) {
267: PropertyEditorListener current = (PropertyEditorListener) listenerList
268: .get(i);
269: current.propertyChanged(this , property, newValue);
270: }
271: }
272:
273: /**
274: * Gets the parent PropertyEditorPane for the current valueComponent.
275: */
276: public PropertyEditorPane getPropertyEditorPane() {
277: try {
278: Class pepClass = Class
279: .forName("net.suberic.util.gui.propedit.PropertyEditorPane");
280: if (pepClass != null) {
281: PropertyEditorPane pep = (PropertyEditorPane) SwingUtilities
282: .getAncestorOfClass(pepClass, valueComponent);
283: return pep;
284: }
285: } catch (Exception e) {
286: }
287:
288: return null;
289: }
290:
291: /**
292: * Does a resize on the parent PropertyEditorPane, if any.
293: */
294: public void doResize() {
295: PropertyEditorPane pep = getPropertyEditorPane();
296: if (pep != null) {
297: pep.resizeEditor();
298: }
299: }
300:
301: /**
302: * Adds the appropriate listeners.
303: */
304: public void addDefaultListeners() {
305: List propertyListenerList = manager.getPropertyAsList(
306: editorTemplate + "._listeners", "");
307: java.util.Iterator it = propertyListenerList.iterator();
308: while (it.hasNext()) {
309: String current = (String) it.next();
310: PropertyEditorListener pel = manager
311: .createListener(current);
312: if (pel != null) {
313: addPropertyEditorListener(pel);
314: }
315: }
316: }
317: }
|