001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.beaninfo.editors;
043:
044: import java.awt.*;
045: import java.beans.*;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.*;
049: import javax.swing.border.*;
050: import javax.swing.tree.*;
051: import org.openide.explorer.*;
052: import org.openide.explorer.propertysheet.PropertyEnv;
053: import org.openide.explorer.view.*;
054: import org.openide.loaders.*;
055: import org.openide.nodes.*;
056: import org.openide.util.*;
057: import org.netbeans.beaninfo.ExplorerPanel;
058: import org.netbeans.beaninfo.editors.DataObjectPanel.FilteredChildren;
059:
060: /**
061: * Component that displays an explorer that displays only certain
062: * nodes. Similar to the node selector (retrieved from the TopManager)
063: * but arranged a bit differently, plus allows the user to set the
064: * currently selected node.
065: * @author Joe Warzecha
066: */
067: public class DataObjectTreeView extends DataObjectPanel {
068:
069: final static int DEFAULT_INSET = 10;
070:
071: private ExplorerPanel expPanel;
072: private TreeView reposTree;
073:
074: public DataObjectTreeView(PropertyEditorSupport my, PropertyEnv env) {
075: super (my, env);
076: initComponent();
077:
078: reposTree.getAccessibleContext().setAccessibleName(
079: NbBundle.getMessage(DataObjectTreeView.class,
080: "ACSN_DataObjectPanel"));
081: setDescription(NbBundle.getMessage(DataObjectTreeView.class,
082: "ACSD_DataObjectPanel"));
083: }
084:
085: public void addNotify() {
086: completeInitialization();
087: super .addNotify();
088: }
089:
090: /** Called from the constructor. */
091: private void initComponent() {
092: expPanel = new ExplorerPanel();
093: expPanel.setLayout(new BorderLayout());
094: reposTree = new BeanTreeView();
095: reposTree
096: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
097: reposTree.setPopupAllowed(false);
098: reposTree.setDefaultActionAllowed(false);
099: expPanel.add(reposTree, "Center"); // NOI18N
100: }
101:
102: private boolean initialized = false;
103:
104: /** Called from addNotify. */
105: private void completeInitialization() {
106: if (initialized) {
107: //Do not re-initialize if the dialog has already been used,
108: //otherwise we will end up listening to the wrong thing and
109: //the OK button will never be enabled
110: return;
111: }
112: if (insets != null) {
113: setBorder(new EmptyBorder(insets));
114: } else {
115: setBorder(new EmptyBorder(12, 12, 0, 11));
116: }
117: setLayout(new BorderLayout(0, 2));
118:
119: if (subTitle != null) {
120: JLabel l = new JLabel(subTitle);
121: l.setLabelFor(reposTree);
122: add(l, BorderLayout.NORTH);
123: }
124:
125: if (rootNode == null) {
126: if (dataFilter != null) {
127: if (folderFilter != null) {
128: DataFilter dFilter = new DataFilter() {
129: public boolean acceptDataObject(DataObject obj) {
130: if (folderFilter.acceptDataObject(obj)) {
131: return true;
132: }
133: return dataFilter.acceptDataObject(obj);
134: }
135: };
136: rootNode = RepositoryNodeFactory.getDefault()
137: .repository(dFilter);
138: } else {
139: rootNode = RepositoryNodeFactory.getDefault()
140: .repository(dataFilter);
141: }
142: } else {
143: if (folderFilter != null) {
144: rootNode = RepositoryNodeFactory.getDefault()
145: .repository(folderFilter);
146: } else {
147: rootNode = RepositoryNodeFactory.getDefault()
148: .repository(DataFilter.ALL);
149: }
150: }
151: }
152:
153: if (nodeFilter != null) {
154: FilteredChildren children = new FilteredChildren(rootNode,
155: nodeFilter, dataFilter);
156: FilterNode n = new FilterNode(rootNode, children);
157: rootNode = n;
158: }
159:
160: Node rNode = rootNode;
161: if (rootObject != null) {
162: Node n = findNodeForObj(rootNode, rootObject);
163: if (n != null) {
164: NodeAcceptor naccep = nodeFilter;
165: if (naccep == null) {
166: naccep = new NodeAcceptor() {
167: public boolean acceptNodes(Node[] nodes) {
168: return false;
169: }
170: };
171: }
172: FilteredChildren children = new FilteredChildren(n,
173: naccep, dataFilter);
174: FilterNode filtNode = new FilterNode(n, children);
175: rNode = filtNode;
176: }
177: }
178:
179: expPanel.getExplorerManager().setRootContext(rNode);
180:
181: Node theNode = null;
182: if (dObj != null) {
183: theNode = findNodeForObj(rNode, dObj);
184: }
185: if (theNode != null) {
186: try {
187: expPanel.getExplorerManager().setSelectedNodes(
188: new Node[] { theNode });
189: } catch (PropertyVetoException pve) {
190: Logger.getLogger(DataObjectTreeView.class.getName())
191: .log(Level.WARNING, null, pve);
192: } catch (IllegalArgumentException iae) {
193: Logger.getLogger(DataObjectTreeView.class.getName())
194: .log(Level.WARNING, null, iae);
195: }
196: }
197:
198: expPanel.getExplorerManager().addPropertyChangeListener(
199: new PropertyChangeListener() {
200: public void propertyChange(PropertyChangeEvent evt) {
201: if (ExplorerManager.PROP_SELECTED_NODES
202: .equals(evt.getPropertyName())) {
203: Node[] nodes = (Node[]) evt.getNewValue();
204: DataObject d = getDataObject();
205: boolean enableOK = false;
206: if ((nodes != null) && (nodes.length > 0)
207: && (dataFilter != null)
208: && (d != null)) {
209: enableOK = dataFilter
210: .acceptDataObject(d);
211: } else {
212: enableOK = (d != null);
213: }
214: if (enableOK)
215: myEditor.setValue(d);
216: setOkButtonEnabled(enableOK);
217: }
218: }
219: });
220:
221: add(expPanel, BorderLayout.CENTER);
222:
223: if ((dataFilter != null) && (getDataObject() != null)) {
224: setOkButtonEnabled(dataFilter
225: .acceptDataObject(getDataObject()));
226: } else {
227: setOkButtonEnabled(getDataObject() != null);
228: }
229: initialized = true;
230: }
231:
232: /**
233: * Sets description of the panel.
234: *
235: * @param desc Desciption of the panel.
236: */
237: public void setDescription(String desc) {
238: getAccessibleContext().setAccessibleDescription(desc);
239: reposTree.getAccessibleContext().setAccessibleDescription(desc);
240: }
241:
242: /**
243: * Return the currently selected DataObject.
244: * @return The currently selected DataObject or null if there is no node seleted
245: */
246: public DataObject getDataObject() {
247: DataObject retValue = null;
248: Node[] na = expPanel.getExplorerManager().getSelectedNodes();
249: if ((na != null) && (na.length > 0)) {
250: retValue = (DataObject) na[0].getCookie(DataObject.class);
251: }
252: return retValue;
253: }
254:
255: /**
256: * Return the currently selected Node.
257: * @return The currently selected Node or null if there is no node seleted
258: */
259: public Node getNode() {
260: Node retValue = null;
261: Node[] na = expPanel.getExplorerManager().getSelectedNodes();
262: if ((na != null) && (na.length > 0)) {
263: retValue = na[0];
264: }
265: return retValue;
266: }
267:
268: /** Get the customized property value.
269: * @return the property value
270: * @exception InvalidStateException when the custom property editor does not contain a valid property value
271: * (and thus it should not be set)
272: */
273: public Object getPropertyValue() throws IllegalStateException {
274: return getDataObject();
275: }
276: }
|