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.xml.xam.ui.multiview;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyVetoException;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.openide.explorer.ExplorerManager;
050: import org.openide.nodes.Node;
051: import org.openide.util.Lookup;
052: import org.openide.util.lookup.AbstractLookup;
053: import org.openide.util.lookup.InstanceContent;
054:
055: /**
056: * A little hack to get the activated nodes to work properly from within the
057: * multiview element. Basically the MultiViewTopComponent lookup does not
058: * deal with the activatedNodes property, so the activated nodes of the
059: * multiview elements must be pushed to the MVTC via a custom Lookup.
060: *
061: * <p>See IssueZilla for more information on this topic:</p>
062: *
063: * <pre>
064: * http://www.netbeans.org/issues/show_bug.cgi?id=67257
065: * </pre>
066: *
067: * @author Nathan Fiedler
068: */
069: public class ActivatedNodesMediator implements Lookup.Provider,
070: PropertyChangeListener {
071: /** Contents of our Lookup (the activated nodes). */
072: private InstanceContent nodesHack;
073: /** The lookup for the activated nodes. */
074: private Lookup lookup;
075: /** The Node that must be excluded from the instance content,
076: * may be null. */
077: private Node delegate;
078: /** Signal that we are processing a property change event. */
079: private boolean propertyChanging;
080: /** explorer manager which should also receive the events */
081: private ExplorerManager.Provider explorerManagerProvider;
082:
083: /**
084: * Creates a new instance of ActivatedNodesMediator.
085: *
086: * @param delegate the Node delegate that must be excluded from Lookup
087: * (may be null).
088: */
089: public ActivatedNodesMediator(Node delegate) {
090: nodesHack = new InstanceContent();
091: lookup = new AbstractLookup(nodesHack);
092: this .delegate = delegate;
093: }
094:
095: public Lookup getLookup() {
096: return lookup;
097: }
098:
099: public void setExplorerManager(ExplorerManager.Provider provider) {
100: explorerManagerProvider = provider;
101: }
102:
103: public synchronized void propertyChange(PropertyChangeEvent event) {
104: if (propertyChanging) {
105: // Avoid an infinite loop whereby changing the lookup contents
106: // causes the activated nodes to change, which calls us again.
107: return;
108: }
109: propertyChanging = true;
110: try {
111: Node[] nodes = (Node[]) event.getNewValue();
112: List<Node> list = new ArrayList<Node>();
113: for (Node node : nodes) {
114: // Can't have same object in two lookups, apparently.
115: if (delegate == null || !node.equals(delegate)) {
116: list.add(node);
117: }
118: }
119: nodesHack.set(list, null);
120: updateExplorerManager(list.toArray(new Node[list.size()]));
121: } finally {
122: propertyChanging = false;
123: }
124: }
125:
126: private void updateExplorerManager(Node[] selected) {
127: if (explorerManagerProvider != null) {
128: if (selected.length > 0) {
129: explorerManagerProvider.getExplorerManager()
130: .setRootContext(getRoot(selected[0]));
131: }
132: try {
133: explorerManagerProvider.getExplorerManager()
134: .setSelectedNodes(selected);
135: } catch (PropertyVetoException pve) {
136: // nothing we can do here
137: } catch (IllegalArgumentException iae) {
138: // Thread timing can result in our selecting nodes that
139: // are not under the root node, so catch and ignore.
140: }
141: }
142: }
143:
144: private Node getRoot(final Node n) {
145: assert n != null;
146: Node root = n;
147: while (root.getParentNode() != null) {
148: root = root.getParentNode();
149: }
150: return root;
151: }
152: }
|