KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > editors > TypeIdentPE


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 package org.netbeans.modules.java.ui.editors;
21
22 import java.beans.*;
23 import java.util.Collection JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27
28 import org.openide.explorer.propertysheet.ExPropertyEditor;
29 import org.openide.explorer.propertysheet.PropertyEnv;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34
35
36 import org.openide.src.*;
37
38 /**
39  * A specialized PropertyEditor for {@link org.openide.src.Identifier} data type
40  * for type name selection. The property editor can be further customized or
41  * constrained by various atributes in the FeatureDescriptor for the given property.
42  *
43  * @author sdedic
44  * @version
45  */

46 public class TypeIdentPE extends PropertyEditorSupport implements ExPropertyEditor {
47
48     private static final String JavaDoc ATTR_CUSTOM_TITLE = "title"; // NOI18N
49
private static final String JavaDoc ATTR_CUSTOM_INPUT = "userInput"; // NOI18N
50
private static final String JavaDoc ATTR_CLASSPATH_ROOTS = "classPathRoots"; // NOI18N
51
private static final String JavaDoc ATTR_ACCEPT_CLASSES = "acceptClasses"; // NOI18N
52
private static final String JavaDoc ATTR_ACCEPT_INTERFACES = "acceptInterfaces"; // NOI18N
53
private static final String JavaDoc ATTR_DATA_FILTER = "dataFilter"; // NOI18N
54

55     /**
56      * Environment for the property editor.
57      */

58     PropertyEnv environment;
59     
60     /**
61      * True, if a class name can be entered. Default = <CODE>true</CODE>
62      */

63     boolean acceptClasses = true;
64     
65     /**
66      * True, if an interface name can be entered. Default = <CODE>true</CODE>
67      */

68     boolean acceptInterfaces = true;
69     
70     /**
71      * True, if only validated input should be accepted. Unknown names
72      * are treated as invalid in that case. Default = <CODE>false</CODE>
73      */

74     boolean validNamesOnly = false;
75     
76     PropertyChangeSupport supp;
77     
78     ClassChooserPanel customEditor;
79     
80     /**
81      * Custom acceptor for values presented by the property editor.
82     PropertyValueAcceptor valueAcceptor;
83      */

84     
85     /**
86      * The value itself.
87      */

88     Identifier identifier;
89     
90     public java.lang.String JavaDoc getAsText() {
91         if (identifier == null)
92             return ""; // NOI18N
93

94         // return the source form of the identifier.
95
return identifier.getSourceName();
96     }
97     
98     /**
99      * Accepts a single class or interface name - depending on the feature
100      * descriptor passed.
101      * @param str name of a class, or an interface, or an empty string.
102      *
103      * @throw IllegalArgumentException if
104      * <UL>
105      * <LI><STRONG>str</STRONG> is not a valid Java identifier
106      * <LI><STRONG>str</STRONG> identifies a class although the Editor was
107      * instructed to accept only interfaces or vice versa.
108      * </UL>
109      */

110     public void setAsText(java.lang.String JavaDoc str) throws IllegalArgumentException JavaDoc {
111         Identifier i;
112         
113         if (str == null || "".equals(str)) {
114             i = null;
115         } else {
116             Type t;
117
118             // try to parse the type. This can throw IllegalArgumentException,
119
// if the string does not form a valid java type specification.
120
t = Type.parse(str);
121
122             // if primitive or array type, deny the input (IAE)
123
if (t.isPrimitive()) {
124                 denyPrimitiveType(t);
125             } else if (t.isArray()) {
126                 denyArrayType(t);
127             }
128
129             ClassElement c = loadRepresentation(str);
130
131             // checks constraints imposed on this property editor and
132
// throws IAE if necessary
133
checkInputConstraints(str, c);
134             
135             i = t.getClassName();
136         }
137
138         // record the new value & fire a property change.
139
super.setValue(i);
140     }
141
142     public void setValue(java.lang.Object JavaDoc obj) throws IllegalArgumentException JavaDoc {
143         if (!(obj instanceof Identifier))
144             throw new IllegalArgumentException JavaDoc("Invalid value type: " + obj.getClass()); // NOI18N
145
super.setValue(obj);
146     }
147
148     /**
149      * Does not support a custom property editor (yet)
150      */

151     public boolean supportsCustomEditor() {
152         return true;
153     }
154     
155     /**
156      * Attaches itself to a PropertyEnv. The implementation scans for
157      * some attributes and sets the editor's properties according to them.
158      * <UL>
159      * <LI>{@link #ATTR_ACCEPT_CLASSES} for acceptClasses property
160      * <LI>{@link #ATTR_ACCEPT_INTEFACES} for acceptInterfaces property
161      * </UL>
162      */

163     public void attachEnv(PropertyEnv env) {
164         FeatureDescriptor fd = env.getFeatureDescriptor();
165         
166         Boolean JavaDoc b;
167         
168         b = (Boolean JavaDoc)fd.getValue(ATTR_ACCEPT_CLASSES);
169         if (b != null)
170             setAcceptClasses(b.booleanValue());
171
172         b = (Boolean JavaDoc)fd.getValue(ATTR_ACCEPT_INTERFACES);
173         if (b != null)
174             setAcceptInterfaces(b.booleanValue());
175         
176         /*
177         Object filter = ds.getValue(ATTR_DATA_FILTER);
178         if (filter instanceof PropertyValueAcceptor) {
179             acceptor = (PropertyValueAcceptor)filter;
180         }
181          */

182     }
183     
184     public void addPropertyChangeListener(PropertyChangeListener l) {
185         super.addPropertyChangeListener(l);
186         if (supp == null) {
187             synchronized (this) {
188                 if (supp == null)
189                     supp = new PropertyChangeSupport(this);
190             }
191         }
192         synchronized (supp) {
193             supp.addPropertyChangeListener(l);
194         }
195     }
196     
197     public void removePropertyChangeListener(PropertyChangeListener l) {
198         if (supp != null) {
199             synchronized (supp) {
200                 supp.removePropertyChangeListener(l);
201             }
202         }
203         super.removePropertyChangeListener(l);
204     }
205     
206     void enableOK(boolean enable) {
207         if (supp == null) {
208             return;
209         }
210         supp.firePropertyChange(ExPropertyEditor.PROP_VALUE_VALID, null,
211             enable ? Boolean.TRUE : Boolean.FALSE);
212     }
213     
214     /**
215      * Configures the Editor so that it accepts class names.
216      * This can be combined with {@link #setAcceptInterfaces} to allow both
217      * interface and class names to be entered.
218      */

219     public void setAcceptClasses(boolean accept) {
220         this.acceptClasses = accept;
221     }
222     
223     /**
224      * Configures the Editor so that it accepts interface names.
225      * This can be combined with {@link #setAcceptClasses} to allow both
226      * interface and class names to be entered.
227      */

228     public void setAcceptInterfaces(boolean accept) {
229         this.acceptInterfaces = accept;
230     }
231     
232     public void setValidNamesOnly(boolean accept) {
233         this.validNamesOnly = accept;
234     }
235     
236     public boolean getAcceptClasses() {
237         return this.acceptClasses;
238     }
239     
240     public boolean getAcceptInterfaces() {
241         return this.acceptInterfaces;
242     }
243     
244     public boolean getValidNamesOnly() {
245         return this.validNamesOnly;
246     }
247     
248     /*
249      * The boring non-UI stuff...
250      */

251     
252     /**
253      * This method tries to locate a class, given the user-supplied className.
254      * Note that because resolver functions are absent, it can't get any context
255      * information. Pity.
256      */

257     private ClassElement loadRepresentation(String JavaDoc className) {
258         //TODO: !!!!! Don't know what to do with this
259
return ClassElement.forName(className);
260     }
261     
262     
263     private void denyPrimitiveType(Type t) {
264         throw new IllegalArgumentException JavaDoc(
265             t.toString() + " is a primitive type. Simple class or interface is required" // NOI18N
266
);
267     }
268     
269     private void denyArrayType(Type t) {
270         throw new IllegalArgumentException JavaDoc(
271             t.toString() + " is an array. Simple class or interface is required" // NOI18N
272
);
273     }
274
275     /**
276      * Checks whether the name entered and the entity denoted by that name
277      * matches the configured constraints on this editor.
278      */

279     void checkInputConstraints(String JavaDoc input, ClassElement result)
280         throws IllegalArgumentException JavaDoc {
281         // the first case is, that the result is null -> input was not found.
282

283         if (result == null) {
284             if (!getValidNamesOnly()) {
285                 // cannot verify that the name conforms to constraints,
286
// but can't reject it either.
287
return;
288             }
289             throw new IllegalArgumentException JavaDoc("Cannot find class or interface " + input); // NOI18N
290
}
291         
292         boolean properClass = result.isClassOrInterface();
293         
294         if (properClass && !getAcceptClasses()) {
295             throw new IllegalArgumentException JavaDoc("Class name is not permitted here"); // NOI18N
296
}
297         
298         if (!properClass && !getAcceptInterfaces()) {
299             throw new IllegalArgumentException JavaDoc("Interface name is not permitted here"); // NOI18N
300
}
301     }
302
303     public java.awt.Component JavaDoc getCustomEditor() {
304         Thread.dumpStack();
305         // XXX needs to be rewritten somehow
306
return new javax.swing.JPanel JavaDoc();
307         /*
308         if (customEditor == null) {
309             Enumeration en = FileSystemCapability.COMPILE.fileSystems();
310             Collection roots = new LinkedList();
311             
312             while (en.hasMoreElements()) {
313                 FileObject f = ((FileSystem)en.nextElement()).getRoot();
314                 try {
315                     Object o = DataObject.find(f).getCookie(DataObject.Container.class);
316                     roots.add(o);
317                 } catch (DataObjectNotFoundException ex) {
318                 }
319             }
320             customEditor = new ClassChooserPanel(this, roots);
321         }
322         return customEditor;
323          */

324     }
325     
326     /*
327     PropertyValueAcceptor getAcceptor() {
328     }
329     
330     static final class AllAcceptor extends PropertyValueAcceptor {
331     }
332      */

333 }
334
Popular Tags