001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.tmap.nodes;
021:
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyVetoException;
025: import javax.swing.tree.TreeSelectionModel;
026: import org.netbeans.modules.xml.xam.ComponentEvent;
027: import org.netbeans.modules.xml.xam.ComponentListener;
028: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
029: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
030: import org.netbeans.modules.xslt.tmap.navigator.TMapNavigatorController;
031: import org.openide.ErrorManager;
032: import org.openide.explorer.ExplorerManager;
033: import org.openide.explorer.view.BeanTreeView;
034: import org.openide.nodes.Children;
035: import org.openide.nodes.Node;
036: import org.openide.util.Lookup;
037: import org.openide.windows.TopComponent;
038:
039: /**
040: *
041: * @author Vitaly Bychkov
042: * @version 1.0
043: */
044: public class LogicalTreeHandler implements PropertyChangeListener,
045: ComponentListener {
046:
047: private static final long serialVersionUID = 1L;
048: private TMapModel myModel;
049: //context lookup
050: private Lookup myContextLookup;
051:
052: private BeanTreeView myBeanTreeView;
053: private ExplorerManager myExplorerManager;
054:
055: public LogicalTreeHandler(ExplorerManager explorerManager,
056: TMapModel tModel, Lookup contextLookup) {
057: myModel = tModel;
058: myExplorerManager = explorerManager;
059: myExplorerManager.addPropertyChangeListener(this );
060:
061: myContextLookup = contextLookup;
062:
063: myBeanTreeView = createBeanTreeView();
064:
065: //add TopComponent Active Node changes listener :
066: TopComponent.getRegistry().addPropertyChangeListener(this );
067: myModel.addComponentListener((ComponentListener) this );
068: }
069:
070: private BeanTreeView createBeanTreeView() {
071: BeanTreeView beanTreeView = new BeanTreeView();
072: beanTreeView.setRootVisible(true);
073: beanTreeView.setEnabled(true);
074: beanTreeView
075: .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
076: beanTreeView.setDefaultActionAllowed(true);
077: // doTreeNodeSelectionByActiveNode();
078:
079: return beanTreeView;
080: }
081:
082: public void removeListeners() {
083: if (myExplorerManager != null) {
084: myExplorerManager.removePropertyChangeListener(this );
085: }
086: TopComponent.getRegistry().removePropertyChangeListener(this );
087: if (myModel != null) {
088: myModel.removeComponentListener(this );
089: }
090: myModel = null;
091: myExplorerManager = null;
092:
093: }
094:
095: public BeanTreeView getBeanTreeView() {
096: return myBeanTreeView;
097: }
098:
099: public void propertyChange(PropertyChangeEvent evt) {
100: // System.out.println("propertyChange: "+evt);
101:
102: String propertyName = evt.getPropertyName();
103: TopComponent navigatorTopComponent = TMapNavigatorController
104: .getNavigatorTC();
105: // System.out.println("propertyChange name: "+propertyName);
106:
107: if (propertyName.equals(TopComponent.Registry.PROP_ACTIVATED)) {
108: if (TopComponent.getRegistry().getActivated() == navigatorTopComponent) {
109: // addUndoManager();
110: // triggerValidation();
111: }
112: } else if (propertyName
113: .equals(TopComponent.Registry.PROP_ACTIVATED_NODES)) {
114: if (TopComponent.getRegistry().getActivated() != navigatorTopComponent) {
115: doTreeNodeSelectionByActiveNode();
116: }
117: return;
118:
119: } else if (propertyName
120: .equals(ExplorerManager.PROP_SELECTED_NODES)) {
121: if (navigatorTopComponent == null) {
122: return;
123: }
124: // NAVIGATOR SELECTED NODES SETTED AS ACTIVE NODES
125: //navigatorTopComponent.setActivatedNodes(new Node[] {});
126: navigatorTopComponent.setActivatedNodes((Node[]) evt
127: .getNewValue());
128: } else if (propertyName
129: .equals(ExplorerManager.PROP_ROOT_CONTEXT)) {
130: //EVENT FOR PROPERTY PROP_ROOT_CONTEXT
131: doTreeNodeSelectionByActiveNode();
132: } else if (propertyName
133: .equals(TopComponent.Registry.PROP_OPENED)) {
134: // System.out.println("the set of the opened topComponent were changed");
135: TMapNavigatorController.activateLogicalPanel();
136: }/***/
137: else if (propertyName
138: .equals(TopComponent.Registry.PROP_CURRENT_NODES)) {
139: // System.out.println("the set of currently selected nodes were changed");
140: TMapNavigatorController.activateLogicalPanel();
141: }
142:
143: }
144:
145: public void valueChanged(ComponentEvent evt) {
146: // System.out.println("valueChanged: "+evt);
147: }
148:
149: public void childrenAdded(ComponentEvent evt) {
150: // System.out.println("childrenAdded: "+evt);
151: }
152:
153: public void childrenDeleted(ComponentEvent evt) {
154: // System.out.println("childrenDeleted: "+evt);
155: }
156:
157: protected void doTreeNodeSelectionByActiveNode() {
158: Node[] nodes = TopComponent.getRegistry().getActivatedNodes();
159: if (nodes == null || nodes.length == 0) {
160: return;
161: }
162:
163: for (Node node : nodes) {
164: if (node instanceof TMapComponentNode) {
165: doTreeNodeSelection((TMapComponentNode) node);
166: break;
167: }
168: }
169:
170: // for (Node elem : nodes) {
171: // if (elem instanceof BpelNode
172: // && ((BpelNode)elem).getReference() instanceof BpelEntity
173: // && !(org.netbeans.modules.bpel.editors.api.utils.Util.isNavigatorShowableNodeType(((BpelNode)elem).getNodeType())))
174: // {
175: // elem = org.netbeans.modules.bpel.editors.api.utils.Util.getClosestNavigatorNode(
176: // (BpelEntity)((BpelNode)elem).getReference(),
177: // elem.getLookup());
178: // }
179: //
180: // if (!(elem instanceof BpelNode)
181: // || ((BpelNode)elem).getNodeType().equals(NodeType.SCHEMA_ELEMENT)
182: // || !(((BpelNode)elem).getReference() instanceof BpelEntity))
183: // {
184: // continue;
185: // }
186: //
187: // BpelEntity refBpelEntityObj = BpelEntity.class.cast(
188: // ((BpelNode)elem).getReference());
189: // if (refBpelEntityObj != null) {
190: // doTreeNodeSelection((BpelNode)elem);
191: // }
192: // //just one node can be selected in navigator bpel logical View
193: // break;
194: // }
195: }
196:
197: private void doTreeNodeSelection(TMapComponentNode tMapNode) {
198:
199: try {
200: // myBeanTreeView.expandAll();
201: Node node2sel = findTMapNode(myExplorerManager
202: .getRootContext(), tMapNode.getComponentRef());
203: if (node2sel == null) {
204: return;
205: }
206:
207: myExplorerManager.setSelectedNodes(new Node[] { node2sel });
208: } catch (PropertyVetoException ex) {
209: ErrorManager.getDefault().notify(ex);
210: }
211: }
212:
213: private Node findTMapNode(Node parentNode, TMapComponent reference) {
214: if (parentNode == null || reference == null
215: || !(parentNode instanceof TMapComponentNode)) {
216: return null;
217: }
218:
219: if (reference.equals(((TMapComponentNode) parentNode)
220: .getComponentRef())) {
221: return (TMapComponentNode) parentNode;
222: }
223:
224: Children child = parentNode.getChildren();
225: if (child == null || child.equals(Children.LEAF)) {
226: return null;
227: }
228:
229: Node[] nodes = child.getNodes();
230: if (nodes == null || nodes.length < 1) {
231: return null;
232: }
233:
234: for (Node node : nodes) {
235: Node tmpNode = findTMapNode(node, reference);
236: if (tmpNode != null) {
237: return tmpNode;
238: }
239: }
240:
241: return null;
242: }
243: }
|