KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > DataObjectEditor


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.beaninfo.editors;
21
22 import java.beans.*;
23
24 import org.openide.loaders.DataObject;
25 import org.openide.nodes.Node;
26 import org.openide.nodes.NodeAcceptor;
27 import org.openide.loaders.DataFolder;
28 import org.openide.loaders.DataFilter;
29 import org.openide.explorer.propertysheet.*;
30
31 /**
32  * Property editor for org.openide.loaders.DataObject.
33  * Uses class DataObjectPanel as custom property editor.
34  * @author David Strupl
35  */

36 public class DataObjectEditor extends PropertyEditorSupport implements ExPropertyEditor {
37
38     /** Name of the custom property that can be passed in PropertyEnv. */
39     private static final String JavaDoc PROPERTY_CURRENT_FOLDER = "currentFolder"; // NOI18N
40
/** Name of the custom property that can be passed in PropertyEnv. */
41     private static final String JavaDoc PROPERTY_ROOT_FOLDER = "rootFolder"; // NOI18N
42
/** Name of the custom property that can be passed in PropertyEnv. */
43     private static final String JavaDoc PROPERTY_ROOT_NODE = "rootNode"; // NOI18N
44
/** Name of the custom property that can be passed in PropertyEnv. */
45     private static final String JavaDoc PROPERTY_COOKIES = "cookies"; // NOI18N
46
/** Name of the custom property that can be passed in PropertyEnv. */
47     private static final String JavaDoc PROPERTY_DATA_FILTER = "dataFilter"; // NOI18N
48
/** Name of the custom property that can be passed in PropertyEnv. */
49     private static final String JavaDoc PROPERTY_FOLDER_FILTER = "folderFilter"; // NOI18N
50
/** Name of the custom property that can be passed in PropertyEnv. */
51     private static final String JavaDoc PROPERTY_NODE_ACCEPTOR = "nodeAcceptor"; // NOI18N
52
/** Name of the custom property that can be passed in PropertyEnv. */
53     private static final String JavaDoc PROPERTY_LABEL = "label"; // NOI18N
54
/** Name of the custom property that can be passed in PropertyEnv. */
55     private static final String JavaDoc PROPERTY_TITLE = "title"; // NOI18N
56
/** Name of the custom property that can be passed in PropertyEnv. */
57     private static final String JavaDoc PROPERTY_INSET = "inset"; // NOI18N
58
/** Name of the custom property that can be passed in PropertyEnv. */
59     private static final String JavaDoc PROPERTY_DESCRIPTION = "description"; // NOI18N
60
/** Name of the custom property that can be passed in PropertyEnv. */
61     private static final String JavaDoc PROPERTY_GUI_TYPE = "guitype"; // NOI18N
62
/** Name of the custom property that can be passed in PropertyEnv. */
63     private static final String JavaDoc PROPERTY_SELECTION_MODE = "selectionMode"; // NOI18N
64

65     /** This gets lazy initialized in getDataObjectPanel*/
66     private DataObjectPanel customEditor;
67    
68     /** A property stored between calls to atachEnv and getCustomEditor() */
69     private DataFolder rootFolder;
70     /** A property stored between calls to atachEnv and getCustomEditor() */
71     private Node rootNode;
72     /** A property stored between calls to atachEnv and getCustomEditor() */
73     private DataObject currentFolder;
74     /** A property stored between calls to atachEnv and getCustomEditor() */
75     private Class JavaDoc[] cookies;
76     /** A property stored between calls to atachEnv and getCustomEditor() */
77     private DataFilter dataFilter;
78     /** A property stored between calls to atachEnv and getCustomEditor() */
79     private DataFilter folderFilter;
80     /** A property stored between calls to atachEnv and getCustomEditor() */
81     private NodeAcceptor nodeAcceptor;
82     /** A property stored between calls to atachEnv and getCustomEditor() */
83     private String JavaDoc label;
84     /** A property stored between calls to atachEnv and getCustomEditor() */
85     private String JavaDoc title;
86     /** A property stored between calls to atachEnv and getCustomEditor() */
87     private Integer JavaDoc insets;
88     /** A property stored between calls to atachEnv and getCustomEditor() */
89     private String JavaDoc description;
90     /** A property stored between calls to atachEnv and getCustomEditor()
91      * It can be 'TreeView' or 'ListView'. Default is 'ListView'.
92      */

93     private String JavaDoc guiType;
94     /** A property stored between calls to atachEnv and getCustomEditor()
95      * Valid only for 'ListView' GUI type. It controls selection mode of
96      * JFileChooser.
97      *
98      * Valid values are:
99      * JFileChooser.FILES_ONLY
100      * JFileChooser.DIRECTORIES_ONLY
101      * JFileChooser.FILES_AND_DIRECTORIES
102      */

103     private Integer JavaDoc selectionMode;
104     
105     private PropertyChangeSupport supp = new PropertyChangeSupport(this);
106     
107     private PropertyEnv env;
108
109     /**
110      * This method is called by the IDE to pass
111      * the environment to the property editor.
112      */

113     public void attachEnv(PropertyEnv env) {
114         Object JavaDoc newObj = env.getFeatureDescriptor().getValue(PROPERTY_CURRENT_FOLDER);
115         if (newObj instanceof DataObject) {
116             currentFolder = (DataObject)newObj;
117         }
118         newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_FOLDER);
119         if (newObj instanceof DataFolder) {
120             rootFolder = (DataFolder)newObj;
121         }
122         newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_NODE);
123         if (newObj instanceof Node) {
124             rootNode = (Node)newObj;
125         }
126         newObj = env.getFeatureDescriptor().getValue(PROPERTY_COOKIES);
127         if (newObj instanceof Class JavaDoc[]) {
128             cookies = (Class JavaDoc[])newObj;
129         }
130         newObj = env.getFeatureDescriptor().getValue(PROPERTY_DATA_FILTER);
131         if (newObj instanceof DataFilter) {
132             dataFilter = (DataFilter)newObj;
133         }
134         newObj = env.getFeatureDescriptor().getValue(PROPERTY_FOLDER_FILTER);
135         if (newObj instanceof DataFilter) {
136             folderFilter = (DataFilter)newObj;
137         }
138         newObj = env.getFeatureDescriptor().getValue(PROPERTY_NODE_ACCEPTOR);
139         if (newObj instanceof NodeAcceptor) {
140             nodeAcceptor = (NodeAcceptor)newObj;
141         }
142         newObj = env.getFeatureDescriptor().getValue(PROPERTY_LABEL);
143         if (newObj instanceof String JavaDoc) {
144             label = (String JavaDoc)newObj;
145         }
146         newObj = env.getFeatureDescriptor().getValue(PROPERTY_TITLE);
147         if (newObj instanceof String JavaDoc) {
148             title = (String JavaDoc)newObj;
149         }
150         newObj = env.getFeatureDescriptor().getValue(PROPERTY_INSET);
151         if (newObj instanceof Integer JavaDoc) {
152             insets = (Integer JavaDoc)newObj;
153         }
154         newObj = env.getFeatureDescriptor().getValue(PROPERTY_DESCRIPTION);
155         if (newObj instanceof String JavaDoc) {
156             description = (String JavaDoc)newObj;
157         }
158         newObj = env.getFeatureDescriptor().getValue(PROPERTY_GUI_TYPE);
159         if (newObj instanceof String JavaDoc) {
160             guiType = (String JavaDoc)newObj;
161         }
162         newObj = env.getFeatureDescriptor().getValue(PROPERTY_SELECTION_MODE);
163         if (newObj instanceof Integer JavaDoc) {
164             selectionMode = (Integer JavaDoc)newObj;
165         }
166         // fix of 19318, set canEditAsText to false by default
167
if ( env.getFeatureDescriptor().getValue( "canEditAsText" ) == null ) { // NOI18N
168
env.getFeatureDescriptor().setValue( "canEditAsText", Boolean.FALSE ); // NOI18N
169
}
170         if (env.getClass().equals(PropertyEnv.class)) {
171             if ((customEditor != null && customEditor.isVisible()) ||
172                 customEditor==null) {
173                 //allow reuse of the custom editor - it is probably being
174
//redisplayed, so we need to change the PropertyEnv that
175
//may be controlling the dialog.
176
this.env = env;
177             }
178         }
179     }
180     
181     /**
182      * Calls lazy initialization in getDataObjectpanel().
183      * @return an instanceof DataObjectPanel
184      */

185     public java.awt.Component JavaDoc getCustomEditor() {
186         return getDataObjectPanel();
187     }
188     
189     /**
190      * Lazy initializes customEditor (DataObjectPanel).
191      * Passes all parameters gathered in method attachEnv.
192      */

193     private DataObjectPanel getDataObjectPanel() {
194         //XXX we must cache the custom editor because the PropertyPanel usage
195
//will actually re-fetch it if something calls setState() on the env.
196
//The alternative is an endless loop during PropertyPanel initialization,
197
//as the initial OK button state is set, causing the PropertyPanel to
198
//rebuild itself, causing the OK button to be set again...
199
if (customEditor == null) {
200             if (guiType != null) {
201                 if ("TreeView".equals(guiType)) {
202                     customEditor = new DataObjectTreeView(this,env);
203                 } else if ("ListView".equals(guiType)) {
204                     customEditor = new DataObjectListView(this,env);
205                 } else {
206                     customEditor = new DataObjectListView(this,env);
207                 }
208             } else {
209                 customEditor = new DataObjectListView(this,env);
210             }
211         } else {
212             //Check explicitly for the env class - issue 36100
213
//env is changed before dialog invocation
214
customEditor.setEnv(env);
215         }
216         if (cookies != null) {
217             customEditor.setDataFilter(new CookieFilter(cookies, dataFilter));
218         } else {
219             customEditor.setDataFilter(dataFilter);
220         }
221         Object JavaDoc value = getValue();
222         if ( value != null && value instanceof DataObject) {
223             customEditor.setDataObject( (DataObject)value );
224         }
225         else if (currentFolder != null) {
226             customEditor.setDataObject(currentFolder);
227         }
228         if (label != null) {
229             customEditor.setText(label);
230         }
231         if (title != null) {
232             customEditor.putClientProperty("title", title); // NOI18N
233
}
234         if (nodeAcceptor != null) {
235             customEditor.setNodeFilter(nodeAcceptor);
236         }
237         if (folderFilter != null) {
238             customEditor.setFolderFilter(folderFilter);
239         }
240         if (rootFolder != null) {
241             customEditor.setRootObject(rootFolder);
242         }
243         if (rootNode != null) {
244             customEditor.setRootNode(rootNode);
245         }
246         if (insets != null) {
247             customEditor.setInsetValue(insets.intValue());
248         }
249         if (description != null) {
250             customEditor.setDescription(description);
251         }
252         if (selectionMode != null) {
253             customEditor.setSelectionMode(selectionMode.intValue());
254         }
255         customEditor.setMultiSelection(false);
256         return customEditor;
257     }
258     
259     /**
260      * Determines whether the propertyEditor can provide a custom editor.
261      * @return true.
262      */

263     public boolean supportsCustomEditor() {
264         return true;
265     }
266
267     /** Adds the listener also to private support supp.*/
268      public void addPropertyChangeListener(PropertyChangeListener l) {
269          super.addPropertyChangeListener(l);
270          supp.addPropertyChangeListener(l);
271      }
272
273     /** Removes the listener also from private support supp.*/
274      public void removePropertyChangeListener(PropertyChangeListener l) {
275          super.removePropertyChangeListener(l);
276          supp.removePropertyChangeListener(l);
277      }
278     
279     public String JavaDoc getAsText() {
280         Object JavaDoc value = getValue();
281         if (value instanceof DataObject) {
282             return ((DataObject)value).getNodeDelegate().getDisplayName();
283         }
284         return "";
285     }
286
287     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
288         if ((text==null)||("".equals(text))) setValue(null);
289     }
290
291     /** CookieFilter allows you to filter DataObjects
292      * based on presence of specified cookies.
293      */

294     private static class CookieFilter implements DataFilter {
295         private Class JavaDoc[] cookieArray;
296         private DataFilter originalFilter;
297
298         /** Just remember the cookie array and original filter.*/
299         public CookieFilter(Class JavaDoc[] cookieArray, DataFilter originalFilter) {
300             this.cookieArray = cookieArray;
301             this.originalFilter = originalFilter;
302         }
303         /** Should the data object be displayed or not? This implementation
304          * combines the originalFilter with set of cookies supplied
305          * in cookieArray.
306          * @param obj the data object
307          * @return <CODE>true</CODE> if the object should be displayed,
308          * <CODE>false</CODE> otherwise
309          */

310         public boolean acceptDataObject (DataObject obj) {
311             if (cookieArray == null) {
312                 if (originalFilter != null) {
313                     return originalFilter.acceptDataObject(obj);
314                 } else {
315                     return true;
316                 }
317             }
318             for (int i = 0; i < cookieArray.length; i++) {
319                 if (obj.getCookie(cookieArray[i]) == null) {
320                     return false;
321                 }
322             }
323             if (originalFilter != null) {
324                 return originalFilter.acceptDataObject(obj);
325             } else {
326                 return true;
327             }
328         }
329     }
330 }
331
Popular Tags