KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > treeeditor > TreeEditorView


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.treeeditor;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyVetoException JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.swing.ActionMap JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.text.DefaultEditorKit JavaDoc;
33
34 import org.netbeans.modules.xml.schema.model.SchemaComponent;
35 import org.netbeans.modules.xml.wsdl.model.Definitions;
36 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
37 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
38 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.UIUtilities;
39 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
40 import org.openide.explorer.ExplorerManager;
41 import org.openide.explorer.ExplorerUtils;
42 import org.openide.explorer.view.BeanTreeView;
43 import org.openide.nodes.Node;
44 import org.openide.util.Lookup;
45 import org.openide.windows.TopComponent;
46 /**
47  * @author radval
48  *
49  * To change the template for this generated type comment go to
50  * Window - Preferences - Java - Code Generation - Code and Comments
51  */

52 public class TreeEditorView extends JPanel JavaDoc
53         implements ExplorerManager.Provider, Lookup.Provider,
54         PropertyChangeListener JavaDoc/*, HelpCtx.Provider*/ {
55     
56     /**
57      *
58      */

59     private static final long serialVersionUID = -6844839168489591934L;
60     
61     private BeanTreeView btv;
62     
63     private ExplorerManager explorerManager;
64     private transient Lookup lookup;
65     
66     public static final String JavaDoc PROP_VALID_NODE_SELECTED = "PROP_VALID_NODE_SELECTED";//NOI18N
67

68     public static final String JavaDoc PROP_DUPLICATE_NODE_SELECTED = "PROP_DUPLICATE_NODE_SELECTED";//NOI18N
69

70     private Node mRootNode;
71     
72     private WSDLModel mModel;
73
74     public TreeEditorView(WSDLModel model) {
75         this.mModel = model;
76         initGUI();
77     }
78     
79     private void initGUI() {
80         setLayout(new BorderLayout JavaDoc());
81         // Create the templates view
82
btv = new BeanTreeView();
83         btv.setRootVisible( true );
84         btv.setSelectionMode( javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION );
85         btv.setPopupAllowed( true );
86         add(btv, BorderLayout.CENTER);
87
88         explorerManager = new ExplorerManager();
89         explorerManager.addPropertyChangeListener(this);
90         ActionMap JavaDoc map = getActionMap();
91         map.put(DefaultEditorKit.copyAction,
92                 ExplorerUtils.actionCopy(explorerManager));
93         map.put(DefaultEditorKit.cutAction,
94                 ExplorerUtils.actionCut(explorerManager));
95         map.put(DefaultEditorKit.pasteAction,
96                 ExplorerUtils.actionPaste(explorerManager));
97         map.put("delete", //NOI18N
98
ExplorerUtils.actionDelete(explorerManager, false));
99         lookup = ExplorerUtils.createLookup(explorerManager, map);
100
101         // Must do this when the component is in the UI tree.
102
EventQueue.invokeLater(new Runnable JavaDoc() {
103             public void run() {
104                 //mRootNode = new AbstractNode(new Children.Array());
105
populateRootNode(mModel.getDefinitions());
106                 //Initially expand root node and the folder nodes below it.
107
btv.expandNode(mRootNode);
108                 Utility.expandNodes(btv, 2, mRootNode);
109                 try {
110                     explorerManager.setSelectedNodes(new Node[] {mRootNode});
111                 } catch (PropertyVetoException JavaDoc pve) {
112                 }
113             }
114         });
115     }
116     
117     public ExplorerManager getExplorerManager() {
118         return explorerManager;
119     }
120
121     public Lookup getLookup() {
122         return lookup;
123     }
124     
125     private void populateRootNode(Definitions definitions) {
126         if (definitions != null) {
127             if (mRootNode == null) {
128                 TopComponent tc = findParentTopComponent();
129                 DefinitionsNode dNode = new DefinitionsNode(
130                         definitions, getExplorerManager(), tc);
131                 mRootNode = dNode;
132             }
133             explorerManager.setRootContext( mRootNode );
134         }
135     }
136     
137     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
138         if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
139             Node[] nodes = (Node[]) evt.getNewValue();
140             if (nodes.length > 0) {
141                 nodes[0].getChildren().getNodes(true);
142 // getTreeView().expandNode(nodes[0]);
143
//manager.setExploredContextAndSelection()
144
TopComponent tc = findParentTopComponent();
145                 // We cannot assume that we are visible, so check for null.
146
if (tc != null) {
147                     tc.setActivatedNodes(nodes);
148                 }
149             }
150         }/* else if (evt.getPropertyName().equals(ExplorerManager.PROP_EXPLORED_CONTEXT)) {
151             Node node = (Node) evt.getNewValue();
152             if(node != null) {
153             getTreeView().expandNode(node);
154             }
155             }*/

156     }
157
158     /**
159      * Finds the TopComponent that contains us.
160      *
161      * @return the parent TopComponent.
162      */

163     private TopComponent findParentTopComponent() {
164         return (TopComponent) SwingUtilities.getAncestorOfClass(
165                 TopComponent.class, this);
166     }
167     
168     public void showComponent(WSDLComponent sc) {
169         List JavaDoc<Node> path = UIUtilities.findPathFromRoot(
170                 getExplorerManager().getRootContext(), sc);
171         if (path == null || path.isEmpty()) {
172             return;
173         }
174         Node node = path.get(path.size() - 1);
175         try {
176             getExplorerManager().setExploredContextAndSelection(
177                     node, new Node[] { node });
178         } catch (PropertyVetoException JavaDoc pve) {
179         }
180     }
181     
182     public void showComponent(SchemaComponent sc) {
183         List JavaDoc<Node> path = UIUtilities.findPathFromRoot(
184                 getExplorerManager().getRootContext(), sc, mModel);
185         if (path == null || path.isEmpty()) {
186             return;
187         }
188         Node node = path.get(path.size() - 1);
189         try {
190             getExplorerManager().setExploredContextAndSelection(
191                     node, new Node[] { node });
192         } catch (PropertyVetoException JavaDoc pve) {
193         }
194     }
195     
196 // IZ 96828: suppress help for nodes, just use WSDL view help topic.
197
// public HelpCtx getHelpCtx() {
198
// HelpCtx ctx = new HelpCtx(TreeEditorView.class);
199
// Node[] selNodes = getExplorerManager().getSelectedNodes();
200
// if (selNodes != null && selNodes.length > 0) {
201
// for (Node node : selNodes) {
202
// if (node.getHelpCtx() != null) {
203
// return node.getHelpCtx();
204
// }
205
// }
206
// }
207
// return ctx;
208
// }
209

210     @Override JavaDoc
211     public void requestFocus() {
212         super.requestFocus();
213         btv.requestFocus();
214     }
215
216     @Override JavaDoc
217     public boolean requestFocusInWindow() {
218         super.requestFocusInWindow();
219         return btv.requestFocusInWindow();
220     }
221 }
222
Popular Tags