KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > navigator > WSDLNavigatorContent


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.navigator;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.EventQueue JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyVetoException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.SwingConstants JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import javax.swing.UIManager JavaDoc;
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.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLModelCookie;
41 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.NodesFactory;
42 import org.netbeans.modules.xml.xam.Model.State;
43 import org.openide.explorer.ExplorerManager;
44 import org.openide.explorer.view.BeanTreeView;
45 import org.openide.explorer.view.TreeView;
46 import org.openide.loaders.DataObject;
47 import org.openide.nodes.Node;
48 import org.openide.util.NbBundle;
49 import org.openide.windows.TopComponent;
50
51 /**
52  * Navigator component containing a tree of WSDL components.
53  *
54  * @author Nathan Fiedler
55  */

56 public class WSDLNavigatorContent extends JPanel JavaDoc
57         implements ExplorerManager.Provider, Runnable JavaDoc, PropertyChangeListener JavaDoc {
58     /** silence compiler warnings */
59     private static final long serialVersionUID = 1L;
60 // /** The lookup for our component tree. */
61
// private static Lookup lookup;
62
/** Explorer manager for the tree view. */
63     private ExplorerManager explorerManager;
64     /** Our component node tree view. */
65     private TreeView treeView;
66     /** Root node of the tree. */
67     private Node rootNode;
68     /** Indicates that the tree view is not in the component hierarchy. */
69     private boolean treeInHierarchy;
70     /** True if currently listening to topcomponent.registry.activatednodes **/
71     private boolean listeningOnActivatedNodes = false;
72     private final JLabel JavaDoc notAvailableLabel = new JLabel JavaDoc(
73             NbBundle.getMessage(WSDLNavigatorContent.class, "MSG_NotAvailable"));
74
75 // static {
76
// // Present a read-only view of the components.
77
// lookup = Lookups.singleton(new ReadOnlyCookie(true));
78
// }
79

80     /**
81      * Creates a new instance of WSDLNavigatorContent.
82      */

83     public WSDLNavigatorContent() {
84         setLayout(new BorderLayout JavaDoc());
85         explorerManager = new ExplorerManager();
86         treeView = new BeanTreeView();
87         explorerManager.addPropertyChangeListener(this);
88         notAvailableLabel.setHorizontalAlignment(SwingConstants.CENTER);
89         notAvailableLabel.setEnabled(false);
90         Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
91
notAvailableLabel.setBackground(usualWindowBkg != null ? usualWindowBkg : Color.white);
92         // to ensure our background color will have effect
93
notAvailableLabel.setOpaque(true);
94     }
95
96 // /**
97
// * Expand the nodes which should be expanded by default.
98
// */
99
// protected void expandDefaultNodes() {
100
// Node rootNode = getExplorerManager().getRootContext();
101
// // Need to prevent looping on malformed trees, so avoid going too
102
// // deep when expanding the children of nodes with only one child.
103
// int depth = 0;
104
// do {
105
// Node[] children = rootNode.getChildren().getNodes();
106
// if (children.length == 1) {
107
// // Expand all nodes that have only a single child.
108
// treeView.expandNode(children[0]);
109
// rootNode = children[0];
110
// depth++;
111
// } else {
112
// // Expand all first-level children that are meant to be shown
113
// // expanded by default.
114
// for (Node child : children) {
115
// DefaultExpandedCookie cookie = (DefaultExpandedCookie)
116
// child.getCookie(DefaultExpandedCookie.class);
117
// if (cookie != null && cookie.isDefaultExpanded()) {
118
// treeView.expandNode(child);
119
// }
120
// }
121
// rootNode = null;
122
// }
123
// } while (rootNode != null && depth < 5);
124
//
125
// // The following code addresses two issues:
126
// //
127
// // 1. When viewing large files, expanding the default set of nodes
128
// // generally means that the contents of the column are so long that
129
// // copious amounts of scrolling are necessary to see it all. This is
130
// // not desirable for the user's first experience with the document.
131
// //
132
// // 2. Because BasicTreeUI essentially ignores the scrollsOnExpand
133
// // setting (or at least it does not work as documented), the tree
134
// // is left scrolled to some random position.
135
// //
136
// // So, if scrolling is necessary, then collapse root's children.
137
// JTree tree = (JTree) treeView.getViewport().getView();
138
// if (tree.getRowCount() > tree.getVisibleRowCount()) {
139
// rootNode = getExplorerManager().getRootContext();
140
// Enumeration kids = rootNode.getChildren().nodes();
141
// while (kids.hasMoreElements()) {
142
// Node kid = (Node) kids.nextElement();
143
// treeView.collapseNode(kid);
144
// }
145
// }
146
// }
147

148     public ExplorerManager getExplorerManager() {
149         return explorerManager;
150     }
151
152     /**
153      * Show the data object in the navigator.
154      *
155      * @param dobj data object to show.
156      */

157     public void navigate(DataObject dobj) {
158         WSDLModel model = null;
159         try {
160             WSDLModelCookie modelCookie =
161                     (WSDLModelCookie) dobj.getCookie(WSDLModelCookie.class);
162             assert modelCookie != null;
163             model = modelCookie.getModel();
164             if (model != null) {
165                 model.removePropertyChangeListener(this);
166                 model.addPropertyChangeListener(this);
167             }
168         } catch (IOException JavaDoc ioe) {
169             // Show a blank page if there is an error.
170
}
171         if (model == null || model.getState() != WSDLModel.State.VALID) {
172             showError();
173         } else {
174             show(model);
175         }
176     }
177
178     public boolean requestFocusInWindow() {
179         return treeView.requestFocusInWindow();
180     }
181
182     public void run() {
183         // Initially expand root node and the folder nodes below it.
184
treeView.expandNode(rootNode);
185         Utility.expandNodes(treeView, 2, rootNode);
186         selectActivatedNodes();
187     }
188
189     public void propertyChange(PropertyChangeEvent JavaDoc event) {
190         String JavaDoc property = event.getPropertyName();
191         if (WSDLModel.STATE_PROPERTY.equals(property)) {
192             State newState = (State) event.getNewValue();
193             if (newState == WSDLModel.State.VALID) {
194                 WSDLModel model = (WSDLModel) event.getSource();
195                 show(model);
196             } else {
197                 showError();
198             }
199             return;
200         }
201         TopComponent tc = (TopComponent) SwingUtilities.
202                 getAncestorOfClass(TopComponent.class, this);
203         if (ExplorerManager.PROP_SELECTED_NODES.equals(property) &&
204                 tc == TopComponent.getRegistry().getActivated()) {
205             Node[] filteredNodes = (Node[])event.getNewValue();
206             if (filteredNodes != null && filteredNodes.length >= 1) {
207                 // Set the active nodes for the parent TopComponent.
208
tc.setActivatedNodes(filteredNodes);
209             }
210         } else if (TopComponent.getRegistry().PROP_ACTIVATED_NODES.equals(property) &&
211                 tc != null && tc != TopComponent.getRegistry().getActivated()) {
212             EventQueue.invokeLater(new Runnable JavaDoc() {
213                 public void run() {
214                     selectActivatedNodes();
215                 }
216             });
217         } else if (TopComponent.getRegistry().PROP_ACTIVATED.equals(property) &&
218                 tc == TopComponent.getRegistry().getActivated()) {
219             tc.setActivatedNodes(getExplorerManager().getSelectedNodes());
220         }
221     }
222
223     private void selectActivatedNodes() {
224         Node[] activated = TopComponent.getRegistry().getActivatedNodes();
225         List JavaDoc<Node> selNodes = new ArrayList JavaDoc<Node>();
226         for (Node n : activated) {
227             WSDLComponent wc = (WSDLComponent) n.getLookup().
228                     lookup(WSDLComponent.class);
229             if (wc != null) {
230                 List JavaDoc<Node> path = UIUtilities.findPathFromRoot(
231                         getExplorerManager().getRootContext(), wc);
232                 if (path != null && !path.isEmpty()) {
233                     selNodes.add(path.get(path.size() - 1));
234                 }
235             }
236         }
237         try {
238             getExplorerManager().setSelectedNodes(
239                     selNodes.toArray(new Node[0]));
240         } catch (PropertyVetoException JavaDoc pve) {
241         }
242     }
243
244     /**
245      * Display the "not available" message in place of the tree view.
246      */

247     private void showError() {
248         if (notAvailableLabel.isShowing()) {
249             return;
250         }
251         remove(treeView);
252         add(notAvailableLabel, BorderLayout.CENTER);
253         revalidate();
254         repaint();
255     }
256
257     /**
258      * Show the tree view for the given model.
259      *
260      * @param model component model to display.
261      */

262     private void show(WSDLModel model) {
263         remove(notAvailableLabel);
264         add(treeView, BorderLayout.CENTER);
265         NodesFactory factory = NodesFactory.getInstance();
266         rootNode = factory.create(model.getDefinitions());
267         getExplorerManager().setRootContext(rootNode);
268         EventQueue.invokeLater(this);
269         revalidate();
270         repaint();
271     }
272 }
273
Popular Tags