KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
23 import java.beans.*;
24 import java.util.*;
25 import javax.swing.*;
26 import javax.swing.border.*;
27 import javax.swing.event.*;
28 import javax.swing.tree.*;
29
30 import org.openide.*;
31 import org.openide.explorer.*;
32 import org.openide.explorer.propertysheet.PropertyEnv;
33 import org.openide.explorer.propertysheet.editors.*;
34 import org.openide.explorer.view.*;
35 import org.openide.loaders.*;
36 import org.openide.nodes.*;
37 import org.openide.util.*;
38
39 /**
40  * Component that displays an explorer that displays only certain
41  * nodes. Similar to the node selector (retrieved from the TopManager)
42  * but arranged a bit differently, plus allows the user to set the
43  * currently selected node.
44  * @author Joe Warzecha
45  */

46 public class DataObjectPanel extends JPanel {
47     
48     final static int DEFAULT_INSET = 10;
49     
50     protected DataFilter folderFilter;
51     protected DataFilter dataFilter;
52     protected NodeAcceptor nodeFilter;
53     protected Insets insets;
54     protected String JavaDoc subTitle;
55     protected String JavaDoc description;
56     protected DataObject rootObject;
57     
58     protected Node rootNode;
59     protected DataObject dObj;
60     /** Set to true when panel is used by DataObjectArrayEditor. Relevant only
61      * for list view. Tree view allows only single selection. */

62     protected boolean multiSelection;
63     protected int selectionMode = JFileChooser.FILES_ONLY;
64     
65     protected PropertyEditorSupport myEditor;
66     
67     private PropertyEnv env;
68     
69     public DataObjectPanel(PropertyEditorSupport my, PropertyEnv env) {
70         this.env = env;
71         myEditor = my;
72     }
73
74     /** Allows the panel to be redisplayed in a new dialog controlled
75      * by a new PropertyEnv. */

76     public void setEnv(PropertyEnv env) {
77         this.env = env;
78     }
79     
80     /**
81      * Set the data filter used to filter the nodes displayed.
82      * An example of a DataFilter is one that only returns 'true'
83      * for JavaDataObjects so that only nodes representing those
84      * objects are displayed in the Explorer.
85      *
86      * @param df The DataFilter used to filter nodes
87      */

88     public void setDataFilter(DataFilter df) {
89         dataFilter = df;
90     }
91     
92     /**
93      * Set the Node filter used to filter nodes. For example,
94      * if a user wants to display only JavaDataObjects, but not
95      * any subnodes under those DataNodes, then set the node
96      * filter to not accept any sub nodes.
97      *
98      * @param acceptor The NodeAcceptor used to filter subnodes.
99      */

100     public void setNodeFilter(NodeAcceptor acceptor) {
101         nodeFilter = acceptor;
102     }
103     
104     /**
105      * Set the insets of the Explorer panel.
106      *
107      * @param insetVal The value used for all of the insets (top,
108      * bottom, left, right).
109      */

110     public void setInsetValue(int insetVal) {
111        insets = new Insets(insetVal, insetVal, insetVal, insetVal);
112     }
113     
114     /**
115      * Set explanation text displayed above the Explorer. If
116      * not set, no text is displayed.
117      *
118      * @param text Text displayed on the GUI above the Explorer.
119      */

120     public void setText(String JavaDoc text) {
121         subTitle = text;
122     }
123     
124     /**
125      * Sets the root object displayed on the Explorer. If
126      * not set, then the normal 'FileSystems' node is displayed
127      * as the Root.
128      *
129      * @param obj The DataObject used as the root node on the
130      * Explorer.
131      */

132     public void setRootObject(DataObject obj) {
133         rootObject = obj;
134     }
135
136     public void setRootNode(Node n) {
137         rootNode = n;
138     }
139
140     /**
141      * This filter can be used to filter folders.
142      * It is applied before the data filter.
143      */

144     public void setFolderFilter(DataFilter f) {
145         folderFilter = f;
146     }
147     
148     /**
149      * This filter can be used to filter folders.
150      * It is applied before the data filter.
151      */

152     public DataFilter getFolderFilter() {
153         return folderFilter;
154     }
155     
156     /**
157      * Sets the currently selected DataObject.
158      *
159      * @param d The DataObject to be selected in the Explorer.
160      */

161     public void setDataObject(DataObject d) {
162         dObj = d;
163     }
164     
165     /**
166      * Sets selection mode for dialog. It is valid only for list view GUI (JFileChooser).
167      * It is set to false when used by DataObjectEditor and to true when used by
168      * DataObjectArrayEditor.
169      *
170      * @param multiSelection True if multiple object selection is enabled.
171      */

172     public void setMultiSelection (boolean multiSelection) {
173         this.multiSelection = multiSelection;
174     }
175     
176     /**
177      * Sets selection mode for JFileChooser. It is valid only for list view GUI (JFileChooser).
178      * Valid values are:
179      * JFileChooser.FILES_ONLY
180      * JFileChooser.DIRECTORIES_ONLY
181      * JFileChooser.FILES_AND_DIRECTORIES
182      *
183      * @param selectionMode integer value controling if files, directories or both can be
184      * selected in dialog
185      */

186     public void setSelectionMode (int selectionMode) {
187         this.selectionMode = selectionMode;
188     }
189     
190     /**
191      * Sets description of the panel.
192      *
193      * @param desc Desciption of the panel.
194      */

195     public void setDescription(String JavaDoc desc) {
196     }
197     
198     protected Node findNode(Node parent, DataObject val) {
199         Children children = parent.getChildren();
200         Node theNode = children.findChild(val.getName());
201         if (theNode == null) {
202             Node [] allNodes = children.getNodes();
203             if ((allNodes != null) && (allNodes.length > 0)) {
204                 for (int i = 0;
205                 (i < allNodes.length) && (theNode == null); i++) {
206                     DataObject dObj = (DataObject)
207                     allNodes [i].getCookie(DataObject.class);
208                     if ((dObj != null) && (dObj == val)) {
209                         theNode = allNodes [i];
210                     }
211                 }
212             }
213         }
214         
215         return theNode;
216     }
217     
218     protected Node findNodeForObj(Node rootNode, DataObject dObj) {
219         Node node = null;
220         DataFolder df = dObj.getFolder();
221         Vector<DataFolder> v = new Vector<DataFolder>();
222         while (df != null) {
223             v.addElement(df);
224             df = df.getFolder();
225         }
226         
227         if (! v.isEmpty()) {
228             Node parent = findParentNode(v, rootNode.getChildren());
229             if (parent != null) {
230                 node = findNode(parent, dObj);
231             } else {
232                 node = findNode(rootNode, dObj);
233             }
234         } else {
235             node = findNode(rootNode, dObj);
236         }
237         
238         return node;
239     }
240     
241     protected Node findParentNode(Vector<DataFolder> v, Children children) {
242         DataFolder df = v.lastElement();
243         
244         //Node n = children.findChild (df.getPrimaryFile ().getName ());
245
Node n = children.findChild(df.getNodeDelegate().getName());
246         if (n == null) {
247             Node [] nodes = children.getNodes();
248             for (int i = 0; (i < nodes.length) && (n == null); i++) {
249                 DataFolder folder =
250                 (DataFolder) nodes [i].getCookie(DataFolder.class);
251                 if ((folder != null) && (folder == df)) {
252                     n = nodes [i];
253                 }
254             }
255         }
256         
257         if (v.size() > 1) {
258             v.removeElement(df);
259             if (n != null) {
260                 return findParentNode(v, n.getChildren());
261             } else {
262                 // Didn't find it, try next folder anyway
263
return findParentNode(v, children);
264             }
265         }
266         return n;
267     }
268     
269     /**
270      * Return the currently selected DataObject.
271      * @return The currently selected DataObject or null if there is no node seleted
272      */

273     public DataObject getDataObject() {
274         return null;
275     }
276     
277     /**
278      * Return the currently selected Node.
279      * @return The currently selected Node or null if there is no node seleted
280      */

281     public Node getNode() {
282         return null;
283     }
284     
285     /** Get the customized property value.
286      * @return the property value
287      * @exception InvalidStateException when the custom property editor does not contain a valid property value
288      * (and thus it should not be set)
289      */

290     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
291         return getDataObject();
292     }
293     
294     protected void setOkButtonEnabled (boolean b) {
295         if (env != null) {
296             env.setState(b ? env.STATE_VALID : env.STATE_INVALID);
297         }
298     }
299     
300     static class FilteredChildren extends FilterNode.Children {
301         private NodeAcceptor nodeAcceptor;
302         private DataFilter dFilter;
303         
304         FilteredChildren(Node n, NodeAcceptor acceptor, DataFilter filter) {
305             super (n);
306             nodeAcceptor = acceptor;
307             dFilter = filter;
308         }
309         
310         private Node [] makeFilterNode(Node n) {
311             FilteredChildren children =
312             new FilteredChildren(n, nodeAcceptor, dFilter);
313             return new Node [] { new FilterNode(n, children) };
314         }
315         
316         @Override JavaDoc
317         protected Node[] createNodes(Node key) {
318             if (key != null) {
319                 Node[] n = new Node[] {key};
320                 if (dFilter != null) {
321                     DataObject dObj =
322                     (DataObject) n [0].getCookie(DataObject.class);
323                     if ((dObj != null) && (dFilter.acceptDataObject(dObj))) {
324                         return makeFilterNode(n [0]);
325                     }
326                 }
327                 
328                 if (nodeAcceptor.acceptNodes(n)) {
329                     return makeFilterNode(n [0]);
330                 }
331             }
332             return new Node [0];
333         }
334     }
335     
336 }
337
Popular Tags