KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 public class DataObjectTreeView extends DataObjectPanel {
46     
47     final static int DEFAULT_INSET = 10;
48     
49     private ExplorerPanel expPanel;
50     private TreeView reposTree;
51     
52     public DataObjectTreeView (PropertyEditorSupport my, PropertyEnv env) {
53         super(my, env);
54         initComponent();
55         
56         reposTree.getAccessibleContext().setAccessibleName( NbBundle.getMessage(DataObjectTreeView.class, "ACSN_DataObjectPanel"));
57         setDescription( NbBundle.getMessage(DataObjectTreeView.class, "ACSD_DataObjectPanel"));
58     }
59     
60     public void addNotify() {
61         completeInitialization();
62         super.addNotify();
63     }
64     
65     /** Called from the constructor. */
66     private void initComponent() {
67         expPanel = new ExplorerPanel();
68         expPanel.setLayout(new BorderLayout());
69         reposTree = new BeanTreeView();
70         reposTree.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
71         reposTree.setPopupAllowed(false);
72         reposTree.setDefaultActionAllowed(false);
73         expPanel.add(reposTree, "Center"); // NOI18N
74
}
75
76     private boolean initialized=false;
77     /** Called from addNotify. */
78     private void completeInitialization() {
79         if (initialized) {
80             //Do not re-initialize if the dialog has already been used,
81
//otherwise we will end up listening to the wrong thing and
82
//the OK button will never be enabled
83
return;
84         }
85         if (insets != null) {
86             setBorder(new EmptyBorder(insets));
87         } else {
88             setBorder(new EmptyBorder(12, 12, 0, 11));
89         }
90         setLayout(new BorderLayout(0, 2));
91         
92         if (subTitle != null) {
93             JLabel l = new JLabel(subTitle);
94             l.setLabelFor(reposTree);
95             add(l, BorderLayout.NORTH);
96         }
97         
98         if (rootNode == null) {
99             if (dataFilter != null) {
100                 if (folderFilter != null) {
101                     DataFilter dFilter = new DataFilter() {
102                         public boolean acceptDataObject(DataObject obj) {
103                             if (folderFilter.acceptDataObject(obj)) {
104                                 return true;
105                             }
106                             return dataFilter.acceptDataObject(obj);
107                         }
108                     };
109                     rootNode = RepositoryNodeFactory.getDefault().repository(dFilter);
110                 } else {
111                     rootNode = RepositoryNodeFactory.getDefault().repository(dataFilter);
112                 }
113             } else {
114                 if (folderFilter != null) {
115                     rootNode = RepositoryNodeFactory.getDefault().repository(folderFilter);
116                 } else {
117                     rootNode = RepositoryNodeFactory.getDefault().repository(DataFilter.ALL);
118                 }
119             }
120         }
121
122         if (nodeFilter != null) {
123             FilteredChildren children =
124                 new FilteredChildren(rootNode, nodeFilter, dataFilter);
125             FilterNode n = new FilterNode(rootNode, children);
126             rootNode = n;
127         }
128         
129         Node rNode = rootNode;
130         if (rootObject != null) {
131             Node n = findNodeForObj(rootNode, rootObject);
132             if (n != null) {
133                 NodeAcceptor naccep = nodeFilter;
134                 if (naccep == null) {
135                     naccep = new NodeAcceptor() {
136                         public boolean acceptNodes(Node [] nodes) {
137                             return false;
138                         }
139                     };
140                 }
141                 FilteredChildren children =
142                     new FilteredChildren(n, naccep, dataFilter);
143                 FilterNode filtNode = new FilterNode(n, children);
144                 rNode = filtNode;
145             }
146         }
147         
148         expPanel.getExplorerManager().setRootContext(rNode);
149         
150         Node theNode = null;
151         if (dObj != null) {
152             theNode = findNodeForObj(rNode, dObj);
153         }
154         if (theNode != null) {
155             try {
156                 expPanel.getExplorerManager().setSelectedNodes
157                 (new Node [] { theNode });
158             } catch (PropertyVetoException pve) {
159                 Logger.getLogger(DataObjectTreeView.class.getName()).log(Level.WARNING, null, pve);
160             } catch (IllegalArgumentException JavaDoc iae) {
161                 Logger.getLogger(DataObjectTreeView.class.getName()).log(Level.WARNING, null, iae);
162             }
163         }
164         
165         expPanel.getExplorerManager().addPropertyChangeListener(
166         new PropertyChangeListener() {
167             public void propertyChange(PropertyChangeEvent evt) {
168                 if (ExplorerManager.PROP_SELECTED_NODES.equals
169                 (evt.getPropertyName())) {
170                     Node [] nodes = (Node []) evt.getNewValue();
171                     DataObject d = getDataObject();
172                     boolean enableOK = false;
173                     if ((nodes != null) && (nodes.length > 0) &&
174                     (dataFilter != null) && (d != null)) {
175                         enableOK = dataFilter.acceptDataObject( d );
176                     } else {
177                         enableOK = ( d != null );
178                     }
179                     if ( enableOK )
180                         myEditor.setValue( d );
181                     setOkButtonEnabled( enableOK );
182                 }
183             }
184         });
185         
186         add(expPanel, BorderLayout.CENTER);
187         
188         if ((dataFilter != null) && (getDataObject() != null)) {
189             setOkButtonEnabled(
190                 dataFilter.acceptDataObject(getDataObject()));
191         } else {
192             setOkButtonEnabled(getDataObject() != null);
193         }
194         initialized=true;
195     }
196     
197     /**
198      * Sets description of the panel.
199      *
200      * @param desc Desciption of the panel.
201      */

202     public void setDescription(String JavaDoc desc) {
203         getAccessibleContext().setAccessibleDescription(desc);
204         reposTree.getAccessibleContext().setAccessibleDescription(desc);
205     }
206     
207     /**
208      * Return the currently selected DataObject.
209      * @return The currently selected DataObject or null if there is no node seleted
210      */

211     public DataObject getDataObject() {
212         DataObject retValue = null;
213         Node[] na = expPanel.getExplorerManager().getSelectedNodes();
214         if ((na != null) && (na.length>0)) {
215             retValue = (DataObject)na[0].getCookie(DataObject.class);
216         }
217         return retValue;
218     }
219     
220     /**
221      * Return the currently selected Node.
222      * @return The currently selected Node or null if there is no node seleted
223      */

224     public Node getNode() {
225         Node retValue = null;
226         Node[] na = expPanel.getExplorerManager().getSelectedNodes();
227         if ((na != null) && (na.length>0)) {
228             retValue = na[0];
229         }
230         return retValue;
231     }
232     
233     /** Get the customized property value.
234      * @return the property value
235      * @exception InvalidStateException when the custom property editor does not contain a valid property value
236      * (and thus it should not be set)
237      */

238     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
239         return getDataObject();
240     }
241 }
242
Popular Tags