2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.beaninfo.editors;
48
import javax.swing.border.*;
49
import javax.swing.event.*;
50
import javax.swing.tree.*;
53
import org.openide.explorer.*;
54
import org.openide.explorer.propertysheet.PropertyEnv;
55
import org.openide.explorer.propertysheet.editors.*;
56
import org.openide.explorer.view.*;
57
import org.openide.loaders.*;
58
import org.openide.nodes.*;
59
import org.openide.util.*;
62
* Component that displays an explorer that displays only certain
63
* nodes. Similar to the node selector (retrieved from the TopManager)
64
* but arranged a bit differently, plus allows the user to set the
65
* currently selected node.
66
* @author Joe Warzecha
68
public class DataObjectPanel extends JPanel {
70
final static int DEFAULT_INSET = 10;
72
protected DataFilter folderFilter;
73
protected DataFilter dataFilter;
74
protected NodeAcceptor nodeFilter;
75
protected Insets insets;
76
protected String subTitle;
77
protected String description;
78
protected DataObject rootObject;
80
protected Node rootNode;
81
protected DataObject dObj;
82
/** Set to true when panel is used by DataObjectArrayEditor. Relevant only
83
* for list view. Tree view allows only single selection. */
84
protected boolean multiSelection;
85
protected int selectionMode = JFileChooser.FILES_ONLY;
87
protected PropertyEditorSupport myEditor;
89
private PropertyEnv env;
91
public DataObjectPanel(PropertyEditorSupport my, PropertyEnv env) {
96
/** Allows the panel to be redisplayed in a new dialog controlled
97
* by a new PropertyEnv. */
98
public void setEnv(PropertyEnv env) {
103
* Set the data filter used to filter the nodes displayed.
104
* An example of a DataFilter is one that only returns 'true'
105
* for JavaDataObjects so that only nodes representing those
106
* objects are displayed in the Explorer.
108
* @param df The DataFilter used to filter nodes
110
public void setDataFilter(DataFilter df) {
115
* Set the Node filter used to filter nodes. For example,
116
* if a user wants to display only JavaDataObjects, but not
117
* any subnodes under those DataNodes, then set the node
118
* filter to not accept any sub nodes.
120
* @param acceptor The NodeAcceptor used to filter subnodes.
122
public void setNodeFilter(NodeAcceptor acceptor) {
123
nodeFilter = acceptor;
127
* Set the insets of the Explorer panel.
129
* @param insetVal The value used for all of the insets (top,
130
* bottom, left, right).
132
public void setInsetValue(int insetVal) {
133
insets = new Insets(insetVal, insetVal, insetVal, insetVal);
137
* Set explanation text displayed above the Explorer. If
138
* not set, no text is displayed.
140
* @param text Text displayed on the GUI above the Explorer.
142
public void setText(String text) {
147
* Sets the root object displayed on the Explorer. If
148
* not set, then the normal 'FileSystems' node is displayed
151
* @param obj The DataObject used as the root node on the
154
public void setRootObject(DataObject obj) {
158
public void setRootNode(Node n) {
163
* This filter can be used to filter folders.
164
* It is applied before the data filter.
166
public void setFolderFilter(DataFilter f) {
171
* This filter can be used to filter folders.
172
* It is applied before the data filter.
174
public DataFilter getFolderFilter() {
179
* Sets the currently selected DataObject.
181
* @param d The DataObject to be selected in the Explorer.
183
public void setDataObject(DataObject d) {
188
* Sets selection mode for dialog. It is valid only for list view GUI (JFileChooser).
189
* It is set to false when used by DataObjectEditor and to true when used by
190
* DataObjectArrayEditor.
192
* @param multiSelection True if multiple object selection is enabled.
194
public void setMultiSelection (boolean multiSelection) {
195
this.multiSelection = multiSelection;
199
* Sets selection mode for JFileChooser. It is valid only for list view GUI (JFileChooser).
201
* JFileChooser.FILES_ONLY
202
* JFileChooser.DIRECTORIES_ONLY
203
* JFileChooser.FILES_AND_DIRECTORIES
205
* @param selectionMode integer value controling if files, directories or both can be
208
public void setSelectionMode (int selectionMode) {
209
this.selectionMode = selectionMode;
213
* Sets description of the panel.
215
* @param desc Desciption of the panel.
217
public void setDescription(String desc) {
220
protected Node findNode(Node parent, DataObject val) {
221
Children children = parent.getChildren();
222
Node theNode = children.findChild(val.getName());
223
if (theNode == null) {
224
Node [] allNodes = children.getNodes();
225
if ((allNodes != null) && (allNodes.length > 0)) {
227
(i < allNodes.length) && (theNode == null); i++) {
228
DataObject dObj = (DataObject)
229
allNodes [i].getCookie(DataObject.class);
230
if ((dObj != null) && (dObj == val)) {
231
theNode = allNodes [i];
240
protected Node findNodeForObj(Node rootNode, DataObject dObj) {
242
DataFolder df = dObj.getFolder();
243
Vector<DataFolder> v = new Vector<DataFolder>();
250
Node parent = findParentNode(v, rootNode.getChildren());
251
if (parent != null) {
252
node = findNode(parent, dObj);
254
node = findNode(rootNode, dObj);
257
node = findNode(rootNode, dObj);
263
protected Node findParentNode(Vector<DataFolder> v, Children children) {
264
DataFolder df = v.lastElement();
266
//Node n = children.findChild (df.getPrimaryFile ().getName ());
267
Node n = children.findChild(df.getNodeDelegate().getName());
269
Node [] nodes = children.getNodes();
270
for (int i = 0; (i < nodes.length) && (n == null); i++) {
272
(DataFolder) nodes [i].getCookie(DataFolder.class);
273
if ((folder != null) && (folder == df)) {
282
return findParentNode(v, n.getChildren());
284
// Didn't find it, try next folder anyway
285
return findParentNode(v, children);
292
* Return the currently selected DataObject.
293
* @return The currently selected DataObject or null if there is no node seleted
295
public DataObject getDataObject() {
300
* Return the currently selected Node.
301
* @return The currently selected Node or null if there is no node seleted
303
public Node getNode() {
307
/** Get the customized property value.
308
* @return the property value
309
* @exception InvalidStateException when the custom property editor does not contain a valid property value
310
* (and thus it should not be set)
312
public Object getPropertyValue() throws IllegalStateException {
313
return getDataObject();
316
protected void setOkButtonEnabled (boolean b) {
318
env.setState(b ? env.STATE_VALID : env.STATE_INVALID);
322
static class FilteredChildren extends FilterNode.Children {
323
private NodeAcceptor nodeAcceptor;
324
private DataFilter dFilter;
326
FilteredChildren(Node n, NodeAcceptor acceptor, DataFilter filter) {
328
nodeAcceptor = acceptor;
332
private Node [] makeFilterNode(Node n) {
333
FilteredChildren children =
334
new FilteredChildren(n, nodeAcceptor, dFilter);
335
return new Node [] { new FilterNode(n, children) };
339
protected Node[] createNodes(Node key) {
341
Node[] n = new Node[] {key};
342
if (dFilter != null) {
344
(DataObject) n [0].getCookie(DataObject.class);
345
if ((dObj != null) && (dFilter.acceptDataObject(dObj))) {
346
return makeFilterNode(n [0]);
350
if (nodeAcceptor.acceptNodes(n)) {
351
return makeFilterNode(n [0]);