KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.text.MessageFormat JavaDoc;
24 import org.netbeans.core.UIExceptions;
25
26 import org.openide.loaders.DataObject;
27 import org.openide.nodes.Node;
28 import org.openide.nodes.NodeAcceptor;
29 import org.openide.loaders.DataFolder;
30 import org.openide.loaders.DataFilter;
31 import org.openide.explorer.propertysheet.*;
32 import org.openide.util.NbBundle;
33
34 /**
35  * Property editor for [org.openide.loaders.DataObject].
36  * Uses class DataObjectPanel as custom property editor.
37  * @author David Strupl
38  */

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

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

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

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

116     public void attachEnv(PropertyEnv env) {
117         Object JavaDoc newObj = env.getFeatureDescriptor().getValue(PROPERTY_CURRENT_FOLDER);
118         if (newObj instanceof DataObject) {
119             currentFolder = (DataObject)newObj;
120         }
121         newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_FOLDER);
122         if (newObj instanceof DataFolder) {
123             rootFolder = (DataFolder)newObj;
124         }
125         newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_NODE);
126         if (newObj instanceof Node) {
127             rootNode = (Node)newObj;
128         }
129         newObj = env.getFeatureDescriptor().getValue(PROPERTY_COOKIES);
130         if (newObj instanceof Class JavaDoc[]) {
131             cookies = (Class JavaDoc[])newObj;
132         }
133         newObj = env.getFeatureDescriptor().getValue(PROPERTY_DATA_FILTER);
134         if (newObj instanceof DataFilter) {
135             dataFilter = (DataFilter)newObj;
136         }
137         newObj = env.getFeatureDescriptor().getValue(PROPERTY_FOLDER_FILTER);
138         if (newObj instanceof DataFilter) {
139             folderFilter = (DataFilter)newObj;
140         }
141         newObj = env.getFeatureDescriptor().getValue(PROPERTY_NODE_ACCEPTOR);
142         if (newObj instanceof NodeAcceptor) {
143             nodeAcceptor = (NodeAcceptor)newObj;
144         }
145         newObj = env.getFeatureDescriptor().getValue(PROPERTY_LABEL);
146         if (newObj instanceof String JavaDoc) {
147             label = (String JavaDoc)newObj;
148         }
149         newObj = env.getFeatureDescriptor().getValue(PROPERTY_TITLE);
150         if (newObj instanceof String JavaDoc) {
151             title = (String JavaDoc)newObj;
152         }
153         newObj = env.getFeatureDescriptor().getValue(PROPERTY_INSET);
154         if (newObj instanceof Integer JavaDoc) {
155             insets = (Integer JavaDoc)newObj;
156         }
157         newObj = env.getFeatureDescriptor().getValue(PROPERTY_DESCRIPTION);
158         if (newObj instanceof String JavaDoc) {
159             description = (String JavaDoc)newObj;
160         }
161         newObj = env.getFeatureDescriptor().getValue(PROPERTY_GUI_TYPE);
162         if (newObj instanceof String JavaDoc) {
163             guiType = (String JavaDoc)newObj;
164         }
165         newObj = env.getFeatureDescriptor().getValue(PROPERTY_SELECTION_MODE);
166         if (newObj instanceof Integer JavaDoc) {
167             selectionMode = (Integer JavaDoc)newObj;
168         }
169         // fix of 19318
170
env.getFeatureDescriptor().setValue( "canEditAsText", Boolean.FALSE );
171         
172         if (env.getClass().equals(PropertyEnv.class)) {
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
if ((customEditor != null && customEditor.isVisible()) ||
177                 customEditor==null) {
178                 this.env = env;
179             }
180         }
181     }
182     
183     /**
184      * Calls lazy initialization in getDataObjectpanel().
185      * @return an instanceof DataObjectPanel
186      */

187     public java.awt.Component JavaDoc getCustomEditor() {
188         return getDataObjectPanel();
189     }
190     
191     void setOkButtonEnabled(boolean state) {
192         if (env != null) {
193             env.setState(state ? PropertyEnv.STATE_VALID:PropertyEnv.STATE_INVALID);
194         }
195     }
196     
197     /**
198      * Lazy initializes customEditor (DataObjectPanel).
199      * Passes all parameters gathered in method attachEnv.
200      */

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

272     public boolean supportsCustomEditor() {
273         return true;
274     }
275
276     /** Adds the listener also to private support supp.*/
277      public void addPropertyChangeListener(PropertyChangeListener l) {
278          super.addPropertyChangeListener(l);
279          supp.addPropertyChangeListener(l);
280      }
281
282     /** Removes the listener also from private support supp.*/
283      public void removePropertyChangeListener(PropertyChangeListener l) {
284          super.removePropertyChangeListener(l);
285          supp.removePropertyChangeListener(l);
286      }
287     
288     public String JavaDoc getAsText() {
289         Object JavaDoc value = getValue();
290         if (value instanceof DataObject) {
291             return ((DataObject)value).getNodeDelegate().getDisplayName();
292         }
293         return "";
294     }
295
296     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
297         try {
298             if ((text==null)||("".equals(text))) setValue(null);
299         } catch (Exception JavaDoc e) {
300             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (e.getMessage());
301             String JavaDoc msg = e.getLocalizedMessage();
302             if (msg == null) {
303                 msg = MessageFormat.format(
304                 NbBundle.getMessage(
305                     DataObjectArrayEditor.class,
306                     "FMT_EXC_GENERIC_BAD_VALUE"), new Object JavaDoc[] {text}); //NOI18N
307
}
308             UIExceptions.annotateUser(iae, iae.getMessage(), msg, e,
309                                      new java.util.Date JavaDoc());
310             throw iae;
311         }
312     }
313
314     /** CookieFilter allows you to filter DataObjects
315      * based on presence of specified cookies.
316      */

317     private static class CookieFilter implements DataFilter {
318         private Class JavaDoc[] cookieArray;
319         private DataFilter originalFilter;
320
321         /** Just remember the cookie array and original filter.*/
322         public CookieFilter(Class JavaDoc[] cookieArray, DataFilter originalFilter) {
323             this.cookieArray = cookieArray;
324             this.originalFilter = originalFilter;
325         }
326         /** Should the data object be displayed or not? This implementation
327          * combines the originalFilter with set of cookies supplied
328          * in cookieArray.
329          * @param obj the data object
330          * @return <CODE>true</CODE> if the object should be displayed,
331          * <CODE>false</CODE> otherwise
332          */

333         public boolean acceptDataObject (DataObject obj) {
334             if (cookieArray == null) {
335                 if (originalFilter != null) {
336                     return originalFilter.acceptDataObject(obj);
337                 } else {
338                     return true;
339                 }
340             }
341             for (int i = 0; i < cookieArray.length; i++) {
342                 if (obj.getCookie(cookieArray[i]) == null) {
343                     return false;
344                 }
345             }
346             if (originalFilter != null) {
347                 return originalFilter.acceptDataObject(obj);
348             } else {
349                 return true;
350             }
351         }
352     }
353 }
354
Popular Tags