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-2006 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.apache.tools.ant.module.nodes;
043:
044: import java.awt.BorderLayout;
045: import java.beans.PropertyVetoException;
046: import java.util.Collection;
047: import javax.swing.ActionMap;
048: import javax.swing.JComponent;
049: import javax.swing.JPanel;
050: import javax.swing.ListSelectionModel;
051: import org.netbeans.spi.navigator.NavigatorPanel;
052: import org.openide.explorer.ExplorerManager;
053: import org.openide.explorer.ExplorerUtils;
054: import org.openide.explorer.view.ListView;
055: import org.openide.loaders.DataObject;
056: import org.openide.nodes.Node;
057: import org.openide.util.Lookup;
058: import org.openide.util.LookupEvent;
059: import org.openide.util.LookupListener;
060: import org.openide.util.Mutex;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * Displays Ant targets in the Navigator.
065: * @author Jesse Glick
066: */
067: public final class AntNavigatorPanel implements NavigatorPanel {
068:
069: private Lookup.Result<DataObject> selection;
070: private final LookupListener selectionListener = new LookupListener() {
071: public void resultChanged(LookupEvent ev) {
072: Mutex.EVENT.readAccess(new Runnable() { // #69355: safest to run in EQ
073: public void run() {
074: if (selection != null) { // #111772
075: display(selection.allInstances());
076: }
077: }
078: });
079: }
080: };
081: private JComponent panel;
082: private final ExplorerManager manager = new ExplorerManager();
083:
084: /**
085: * Default constructor for layer instance.
086: */
087: public AntNavigatorPanel() {
088: }
089:
090: public String getDisplayName() {
091: return NbBundle
092: .getMessage(AntNavigatorPanel.class, "ANP_label");
093: }
094:
095: public String getDisplayHint() {
096: return NbBundle.getMessage(AntNavigatorPanel.class, "ANP_hint");
097: }
098:
099: public JComponent getComponent() {
100: if (panel == null) {
101: final ListView view = new ListView();
102: view.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
103: class Panel extends JPanel implements
104: ExplorerManager.Provider, Lookup.Provider {
105: // Make sure action context works correctly:
106: private final Lookup lookup = ExplorerUtils
107: .createLookup(manager, new ActionMap());
108: {
109: setLayout(new BorderLayout());
110: add(view, BorderLayout.CENTER);
111: }
112:
113: public ExplorerManager getExplorerManager() {
114: return manager;
115: }
116:
117: // Make sure list gets focus, with first node initially selected:
118: @Override
119: public boolean requestFocusInWindow() {
120: boolean b = view.requestFocusInWindow();
121: if (manager.getSelectedNodes().length == 0) {
122: Node[] children = manager.getRootContext()
123: .getChildren().getNodes(true);
124: if (children.length > 0) {
125: try {
126: manager
127: .setSelectedNodes(new Node[] { children[0] });
128: } catch (PropertyVetoException e) {
129: assert false : e;
130: }
131: }
132: }
133: return b;
134: }
135:
136: public Lookup getLookup() {
137: return lookup;
138: }
139: }
140: panel = new Panel();
141: }
142: return panel;
143: }
144:
145: public void panelActivated(Lookup context) {
146: selection = context.lookupResult(DataObject.class);
147: selection.addLookupListener(selectionListener);
148: selectionListener.resultChanged(null);
149: }
150:
151: public void panelDeactivated() {
152: selection.removeLookupListener(selectionListener);
153: selection = null;
154: }
155:
156: public Lookup getLookup() {
157: return null;
158: }
159:
160: private void display(Collection<? extends DataObject> selectedFiles) {
161: // Show list of targets for selected file:
162: if (selectedFiles.size() == 1) {
163: DataObject d = selectedFiles.iterator().next();
164: manager.setRootContext(d.getNodeDelegate());
165: return;
166: }
167: // Fallback:
168: manager.setRootContext(Node.EMPTY);
169: }
170:
171: }
|