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-2007 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.modules.compapp.casaeditor.navigator;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Color;
046: import java.awt.EventQueue;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.beans.PropertyVetoException;
050: import java.util.ArrayList;
051: import java.util.List;
052: import javax.swing.JPanel;
053: import javax.swing.JLabel;
054: import javax.swing.SwingConstants;
055: import javax.swing.SwingUtilities;
056: import javax.swing.UIManager;
057: import javax.swing.tree.TreeSelectionModel;
058: import org.netbeans.modules.compapp.casaeditor.CasaDataObject;
059: import org.openide.util.HelpCtx;
060:
061: import org.openide.util.NbBundle;
062: import org.netbeans.modules.compapp.casaeditor.model.casa.CasaWrapperModel;
063: import org.netbeans.modules.compapp.casaeditor.nodes.CasaNode;
064: import org.netbeans.modules.compapp.casaeditor.nodes.CasaNodeFactory;
065:
066: import org.openide.explorer.ExplorerManager;
067: import org.openide.explorer.view.BeanTreeView;
068: import org.openide.explorer.view.TreeView;
069: import org.openide.nodes.Children;
070: import org.openide.nodes.Node;
071: import org.openide.windows.TopComponent;
072:
073: /**
074: *
075: * @author Nathan Fiedler
076: * @version 1.0
077: */
078: public class CasaNavigatorContentPanel extends JPanel implements
079: ExplorerManager.Provider, HelpCtx.Provider, Runnable,
080: PropertyChangeListener {
081:
082: static final long serialVersionUID = 9068382751477968023L;
083:
084: private CasaWrapperModel mCasaModel;
085: private CasaDataObject mDataObject;
086: private CasaNodeFactory mNodeFactory;
087: private boolean isRequireRepaint;
088: private ExplorerManager myExplorerManager;
089: private TreeView treeView;
090:
091: private final JLabel notAvailableLabel = new JLabel(NbBundle
092: .getMessage(CasaNavigatorContentPanel.class,
093: "MSG_NotAvailable")); // NOI18N
094:
095: public CasaNavigatorContentPanel() {
096: setLayout(new BorderLayout());
097:
098: myExplorerManager = new ExplorerManager();
099: treeView = new BeanTreeView();
100: treeView.setRootVisible(true);
101: treeView.setEnabled(true);
102: treeView
103: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
104: treeView.setDefaultActionAllowed(true);
105:
106: myExplorerManager.addPropertyChangeListener(this );
107:
108: notAvailableLabel.setHorizontalAlignment(SwingConstants.CENTER);
109: notAvailableLabel.setEnabled(false);
110: Color usualWindowBkg = UIManager.getColor("window"); // NOI18N
111: notAvailableLabel
112: .setBackground(usualWindowBkg != null ? usualWindowBkg
113: : Color.white);
114: // to ensure our background color will have effect
115: notAvailableLabel.setOpaque(true);
116: }
117:
118: public void addNotify() {
119: super .addNotify();
120: }
121:
122: public void removeNotify() {
123: super .removeNotify();
124: }
125:
126: public ExplorerManager getExplorerManager() {
127: return myExplorerManager;
128: }
129:
130: public HelpCtx getHelpCtx() {
131: if (myExplorerManager != null) {
132: Node[] selNodes = myExplorerManager.getSelectedNodes();
133: if (selNodes != null && selNodes.length > 0) {
134: HelpCtx helpCtx = selNodes[0].getHelpCtx();
135: if (helpCtx != null) {
136: return helpCtx;
137: }
138: }
139: }
140: return new HelpCtx(CasaNavigatorContentPanel.class);
141: }
142:
143: public void propertyChange(
144: final PropertyChangeEvent propertyChangeEvent) {
145: Runnable runner = new Runnable() {
146: public void run() {
147: propertyUpdate(propertyChangeEvent);
148: }
149: };
150: if (SwingUtilities.isEventDispatchThread()) {
151: runner.run();
152: } else {
153: SwingUtilities.invokeLater(runner);
154: }
155: }
156:
157: private void propertyUpdate(PropertyChangeEvent propertyChangeEvent) {
158: String propertyName = propertyChangeEvent.getPropertyName();
159: TopComponent navigatorTopComponent = (TopComponent) SwingUtilities
160: .getAncestorOfClass(TopComponent.class, this );
161:
162: if (propertyName
163: .equals(TopComponent.Registry.PROP_ACTIVATED_NODES)
164: && navigatorTopComponent != null
165: && navigatorTopComponent != TopComponent.getRegistry()
166: .getActivated()) {
167: EventQueue.invokeLater(new Runnable() {
168: public void run() {
169: selectActivatedNodes();
170: }
171: });
172:
173: } else if (propertyName
174: .equals(ExplorerManager.PROP_SELECTED_NODES)
175: && navigatorTopComponent != null
176: && navigatorTopComponent == TopComponent.getRegistry()
177: .getActivated()) {
178: Node[] filteredNodes = (Node[]) propertyChangeEvent
179: .getNewValue();
180: if (filteredNodes != null && filteredNodes.length >= 1) {
181: // Set the active nodes for the parent TopComponent.
182: navigatorTopComponent.setActivatedNodes(filteredNodes);
183: }
184:
185: } else if (propertyName
186: .startsWith(CasaWrapperModel.PROPERTY_PREFIX)) {
187: // TODO unless this is a full reload, we should just add/remove
188: // the affected node instead of resetting the tree.
189: showNavTree(true);
190: }
191: }
192:
193: public void navigate(CasaDataObject dataObject) {
194: // get the model and create the new bpel logical tree in background
195: mDataObject = dataObject;
196: if (dataObject != null) {
197: mCasaModel = dataObject.getEditorSupport().getModel();
198: } else {
199: mCasaModel = null;
200: }
201: if (mCasaModel == null) {
202: showError();
203: } else {
204: mNodeFactory = new CasaNodeFactory(mDataObject, mCasaModel);
205: mCasaModel.removePropertyChangeListener(this );
206: mCasaModel.addPropertyChangeListener(this );
207: showNavTree(false);
208: }
209: }
210:
211: private void showNavTree(boolean showImmediately) {
212: removeAll();
213: Node rootNode = mNodeFactory.createModelNode(mCasaModel);
214: myExplorerManager.setRootContext(rootNode);
215: add(treeView, BorderLayout.CENTER);
216: if (showImmediately) {
217: run();
218: } else {
219: EventQueue.invokeLater(this );
220: }
221: revalidate();
222: repaint();
223: }
224:
225: public void run() {
226: // Initially expand root node and the folder nodes below it.
227: Node rootNode = myExplorerManager.getRootContext();
228: treeView.expandNode(rootNode);
229: expandNodes(treeView, 1, rootNode);
230: selectActivatedNodes();
231: }
232:
233: public static void expandNodes(TreeView tv, int level, Node rootNode) {
234: if (level == 0)
235: return;
236: Children children = rootNode.getChildren();
237: if (children != null) {
238: Node[] nodes = children.getNodes();
239: if (nodes != null) {
240: for (int i = 0; i < nodes.length; i++) {
241: tv.expandNode(nodes[i]); //Expand node
242: expandNodes(tv, level - 1, nodes[i]); //expand children
243: }
244: }
245: }
246: }
247:
248: private void selectActivatedNodes() {
249: Node[] activated = TopComponent.getRegistry()
250: .getActivatedNodes();
251: Node activatedNode = null;
252: for (Node node : activated) {
253: if (node instanceof CasaNode) {
254: // Select the first CasaNode in the list.
255: activatedNode = node;
256: break;
257: }
258: }
259: if (activatedNode != null) {
260: List<Node> selNodes = new ArrayList<Node>();
261: findNavigatorNode(myExplorerManager.getRootContext(),
262: ((CasaNode) activatedNode).getData(), selNodes);
263: try {
264: myExplorerManager.setSelectedNodes(selNodes
265: .toArray(new Node[0]));
266: } catch (PropertyVetoException pve) {
267: }
268: }
269: }
270:
271: private void findNavigatorNode(Node parent, Object datumToFind,
272: List<Node> toSelect) {
273: Children children = parent.getChildren();
274: for (Node n : children.getNodes()) {
275: if (n instanceof CasaNode) {
276: CasaNode child = (CasaNode) n;
277: if (child.getData() == datumToFind) {
278: toSelect.add(child);
279: // Only interested in finding one result.
280: return;
281: }
282: if (child.getChildren().getNodesCount() > 0) {
283: findNavigatorNode(child, datumToFind, toSelect);
284: }
285: }
286: }
287: }
288:
289: private void showError() {
290: if (notAvailableLabel.isShowing()) {
291: return;
292: }
293: removeAll();
294: add(notAvailableLabel, BorderLayout.CENTER);
295: revalidate();
296: repaint();
297: }
298: }
|