KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > InplaceEditorFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * InplaceEditorFactory.java
21  *
22  * Created on January 4, 2003, 4:52 PM
23  */

24 package org.openide.explorer.propertysheet;
25
26 import java.awt.Toolkit JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
31 import org.openide.nodes.Node.Property;
32
33 import java.beans.PropertyEditor JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37
38 /** Factory providing inplace editor implementations. Provides appropriate
39  * InplaceEditor implementations, depending on the type of the property, the
40  * results of PropertyEditor.getTags(), or any hinting provided by the property
41  * editor or PropertyEnv to use a custom inplace editor implementation.
42  * Configures the editor returned and attaches it to the property in question.
43   * @author Tim Boudreau
44   */

45 final class InplaceEditorFactory {
46     private InplaceEditor checkbox = null;
47     private InplaceEditor text = null;
48     private InplaceEditor combo = null;
49     private InplaceEditor radio = null;
50     private ReusablePropertyEnv reusableEnv;
51     private boolean tableUI;
52     int radioButtonMax = -1;
53     private boolean useLabels = false;
54     private boolean useRadioBoolean = PropUtils.forceRadioButtons;
55
56     InplaceEditorFactory(boolean tableUI, ReusablePropertyEnv env) {
57         this.tableUI = tableUI;
58         this.reusableEnv = env;
59         
60         //reset editors when windows theme is changing (classic <-> xp)
61
Toolkit.getDefaultToolkit().addPropertyChangeListener( "win.xpstyle.themeActive", new PropertyChangeListener JavaDoc() { //NOI18N
62
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
63                 checkbox = null;
64                 text = null;
65                 combo = null;
66                 radio = null;
67             }
68         });
69
70     }
71
72     /** Set a threshold number of tags below which a radio button, not a
73      * combo box editor should be used */

74     void setRadioButtonMax(int i) {
75         radioButtonMax = i;
76     }
77
78     /** Set whether or not radio and checkbox editors should show the property
79      * name */

80     void setUseLabels(boolean val) {
81         useLabels = val;
82     }
83
84     void setUseRadioBoolean(boolean val) {
85         useRadioBoolean = val;
86     }
87
88     /**Lazily create (or create a new instance of) the radio button editor */
89     private InplaceEditor getRadioEditor(boolean newInstance) {
90         RadioInplaceEditor result;
91
92         if (newInstance) {
93             result = new RadioInplaceEditor(tableUI);
94         } else {
95             if (radio == null) {
96                 radio = new RadioInplaceEditor(tableUI);
97
98                 //Mainly for debugging
99
((JComponent JavaDoc) radio).setName(
100                     "RadioEditor for " + getClass().getName() + "@" + System.identityHashCode(this)
101                 ); //NOI18N
102
}
103
104             result = (RadioInplaceEditor) radio;
105         }
106
107         result.setUseTitle(useLabels);
108
109         return result;
110     }
111
112     /**Lazily create (or create a new instance of) the combo box editor */
113     private InplaceEditor getComboBoxEditor(boolean newInstance) {
114         if (newInstance) {
115             return new ComboInplaceEditor(tableUI);
116         }
117
118         if (combo == null) {
119             combo = new ComboInplaceEditor(tableUI);
120
121             //Mainly for debugging
122
((JComponent JavaDoc) combo).setName(
123                 "ComboInplaceEditor for " + getClass().getName() + "@" + System.identityHashCode(this)
124             ); //NOI18N
125
}
126
127         return combo;
128     }
129
130     /**Lazily create (or create a new instance of) the string editor */
131     private InplaceEditor getStringEditor(boolean newInstance) {
132         if (newInstance) {
133             return new StringInplaceEditor();
134         }
135
136         if (text == null) {
137             text = new StringInplaceEditor();
138
139             //Mainly for debugging
140
((JComponent JavaDoc) text).setName(
141                 "StringEditor for " + getClass().getName() + "@" + System.identityHashCode(this)
142             ); //NOI18N
143
}
144
145         return text;
146     }
147
148     /**Lazily create (or create a new instance of) the checkbox editor */
149     private InplaceEditor getCheckboxEditor(boolean newInstance) {
150         CheckboxInplaceEditor result;
151
152         if (newInstance) {
153             result = new CheckboxInplaceEditor();
154         } else {
155             if (checkbox == null) {
156                 checkbox = new CheckboxInplaceEditor();
157
158                 //Mainly for debugging
159
((JComponent JavaDoc) checkbox).setName(
160                     "CheckboxEditor for " + getClass().getName() + "@" + System.identityHashCode(this)
161                 ); //NOI18N
162
}
163
164             result = (CheckboxInplaceEditor) checkbox;
165         }
166
167         result.setUseTitle(useLabels);
168
169         return (InplaceEditor) result;
170     }
171
172     /** Factory method that returns an appropriate inplace
173      * editor for an object. Special handling is provided for
174      * instances of Node.Property which can provide hints or
175      * even their own legacy inplace editor implementation.
176      * <P>The returned instance will be connected to the
177      * object (the component provided by getComponent() will
178      * render the property object correctly with no additional
179      * intervention needed. If <code>newInstance</code> is
180      * true, will create a new instance of the inplace editor
181      * component (for use with PropertyPanel and other cases
182      * where multiple inplace esditors can be displayed at the
183      * same time); otherwise a shared instance will be configured
184      * and returned.<P> Note that for the case of unknown object
185      * types (non Node.Property objects), the returned InplaceEditor
186      * will have no way of knowing how to update the object with
187      * a new value, and client code must listen for actions on
188      * the InplaceEditor and do this manually - the update method
189      * of the InplaceEditor will do nothing. */

190     public InplaceEditor getInplaceEditor(Property p, boolean newInstance) {
191         PropertyEnv env = new PropertyEnv();
192         env.setBeans(reusableEnv.getBeans());
193
194         return getInplaceEditor(p, env, newInstance);
195     }
196
197     InplaceEditor getInplaceEditor(Property p, PropertyEnv env, boolean newInstance) {
198         PropertyEditor ped = PropUtils.getPropertyEditor(p);
199         InplaceEditor result = (InplaceEditor) p.getValue("inplaceEditor"); //NOI18N
200
env.setFeatureDescriptor(p);
201         env.setEditable(p.canWrite());
202
203         if (ped instanceof ExPropertyEditor) {
204             ExPropertyEditor epe = (ExPropertyEditor) ped;
205
206             //configure the editor/propertyenv
207
epe.attachEnv(env);
208
209             if (result == null) {
210                 result = env.getInplaceEditor();
211             }
212         } else if (ped instanceof EnhancedPropertyEditor) {
213             //handle legacy inplace custom editors
214
EnhancedPropertyEditor enh = (EnhancedPropertyEditor) ped;
215
216             if (enh.hasInPlaceCustomEditor()) {
217                 //Use our wrapper component to handle this
218
result = new WrapperInplaceEditor(enh);
219             }
220         }
221
222         //Okay, the result is null, provide one of the standard inplace editors
223
if (result == null) {
224             Class JavaDoc c = p.getValueType();
225
226             if ((c == Boolean JavaDoc.class) || (c == Boolean.TYPE)) {
227                 if (ped instanceof PropUtils.NoPropertyEditorEditor) {
228                     //platform case
229
result = getStringEditor(newInstance);
230                 } else {
231                     boolean useRadioButtons = useRadioBoolean || (p.getValue("stringValues") != null); //NOI18N
232
result = useRadioButtons ? getRadioEditor(newInstance) : getCheckboxEditor(newInstance);
233                 }
234             } else if (ped.getTags() != null) {
235                 if (ped.getTags().length <= radioButtonMax) {
236                     result = getRadioEditor(newInstance);
237                 } else {
238                     result = getComboBoxEditor(newInstance);
239                 }
240             } else {
241                 result = getStringEditor(newInstance);
242             }
243         }
244
245         if (!tableUI && Boolean.FALSE.equals(p.getValue("canEditAsText"))) { //NOI18N
246
result.getComponent().setEnabled(false);
247         }
248
249         result.clear(); //XXX shouldn't need to do this!
250
result.setPropertyModel(new NodePropertyModel(p, env.getBeans()));
251         result.connect(ped, env);
252
253         //XXX?
254
if (tableUI) {
255             if( result instanceof JTextField JavaDoc )
256                 result.getComponent().setBorder(BorderFactory.createEmptyBorder(0,3,0,0));
257             else
258                 result.getComponent().setBorder(BorderFactory.createEmptyBorder());
259         }
260
261         return result;
262     }
263 }
264
Popular Tags