KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > multiview > ActivatedNodesMediator


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.xam.ui.multiview;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.openide.explorer.ExplorerManager;
28 import org.openide.nodes.Node;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.AbstractLookup;
31 import org.openide.util.lookup.InstanceContent;
32
33 /**
34  * A little hack to get the activated nodes to work properly from within the
35  * multiview element. Basically the MultiViewTopComponent lookup does not
36  * deal with the activatedNodes property, so the activated nodes of the
37  * multiview elements must be pushed to the MVTC via a custom Lookup.
38  *
39  * <p>See IssueZilla for more information on this topic:</p>
40  *
41  * <pre>
42  * http://www.netbeans.org/issues/show_bug.cgi?id=67257
43  * </pre>
44  *
45  * @author Nathan Fiedler
46  */

47 public class ActivatedNodesMediator
48         implements Lookup.Provider, PropertyChangeListener JavaDoc {
49     /** Contents of our Lookup (the activated nodes). */
50     private InstanceContent nodesHack;
51     /** The lookup for the activated nodes. */
52     private Lookup lookup;
53     /** The Node that must be excluded from the instance content,
54      * may be null. */

55     private Node delegate;
56     /** Signal that we are processing a property change event. */
57     private boolean propertyChanging;
58     /** explorer manager which should also receive the events */
59     private ExplorerManager.Provider explorerManagerProvider;
60     
61     /**
62      * Creates a new instance of ActivatedNodesMediator.
63      *
64      * @param delegate the Node delegate that must be excluded from Lookup
65      * (may be null).
66      */

67     public ActivatedNodesMediator(Node delegate) {
68         nodesHack = new InstanceContent();
69         lookup = new AbstractLookup(nodesHack);
70         this.delegate = delegate;
71     }
72
73     public Lookup getLookup() {
74         return lookup;
75     }
76
77     public void setExplorerManager(ExplorerManager.Provider provider) {
78     explorerManagerProvider = provider;
79     }
80     
81     public synchronized void propertyChange(PropertyChangeEvent JavaDoc event) {
82         if (propertyChanging) {
83             // Avoid an infinite loop whereby changing the lookup contents
84
// causes the activated nodes to change, which calls us again.
85
return;
86         }
87         propertyChanging = true;
88         try {
89             Node[] nodes = (Node[]) event.getNewValue();
90             List JavaDoc<Node> list = new ArrayList JavaDoc<Node>();
91             for (Node node : nodes) {
92                 // Can't have same object in two lookups, apparently.
93
if (delegate == null || !node.equals(delegate)) {
94                     list.add(node);
95                 }
96             }
97             nodesHack.set(list, null);
98         updateExplorerManager(list.toArray(new Node[list.size()]));
99         } finally {
100             propertyChanging = false;
101         }
102     }
103     
104     private void updateExplorerManager(Node[] selected) {
105         if (explorerManagerProvider != null) {
106             if (selected.length > 0) {
107                 explorerManagerProvider.getExplorerManager().setRootContext(
108                         getRoot(selected[0]));
109             }
110             try {
111                 explorerManagerProvider.getExplorerManager().setSelectedNodes(
112                         selected);
113             } catch (PropertyVetoException JavaDoc pve) {
114                 // nothing we can do here
115
} catch (IllegalArgumentException JavaDoc iae) {
116                 // Thread timing can result in our selecting nodes that
117
// are not under the root node, so catch and ignore.
118
}
119         }
120     }
121
122     private Node getRoot(final Node n) {
123         assert n != null;
124         Node root = n;
125         while (root.getParentNode() != null) {
126             root = root.getParentNode();
127         }
128         return root;
129     }
130 }
131
Popular Tags