KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > nodes > AntNavigatorPanel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.nodes;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.beans.PropertyVetoException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import javax.swing.ActionMap JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.ListSelectionModel JavaDoc;
29 import org.netbeans.spi.navigator.NavigatorPanel;
30 import org.openide.explorer.ExplorerManager;
31 import org.openide.explorer.ExplorerUtils;
32 import org.openide.explorer.view.ListView;
33 import org.openide.loaders.DataObject;
34 import org.openide.nodes.Node;
35 import org.openide.util.Lookup;
36 import org.openide.util.LookupEvent;
37 import org.openide.util.LookupListener;
38 import org.openide.util.Mutex;
39 import org.openide.util.NbBundle;
40
41 /**
42  * Displays Ant targets in the Navigator.
43  * @author Jesse Glick
44  */

45 public final class AntNavigatorPanel implements NavigatorPanel {
46     
47     private Lookup.Result<DataObject> selection;
48     private final LookupListener selectionListener = new LookupListener() {
49         public void resultChanged(LookupEvent ev) {
50             Mutex.EVENT.readAccess(new Runnable JavaDoc() { // #69355: safest to run in EQ
51
public void run() {
52                     display(selection.allInstances());
53                 }
54             });
55         }
56     };
57     private JComponent JavaDoc panel;
58     private final ExplorerManager manager = new ExplorerManager();
59     
60     /**
61      * Default constructor for layer instance.
62      */

63     public AntNavigatorPanel() {}
64     
65     public String JavaDoc getDisplayName() {
66         return NbBundle.getMessage(AntNavigatorPanel.class, "ANP_label");
67     }
68     
69     public String JavaDoc getDisplayHint() {
70         return NbBundle.getMessage(AntNavigatorPanel.class, "ANP_hint");
71     }
72     
73     public JComponent JavaDoc getComponent() {
74         if (panel == null) {
75             final ListView view = new ListView();
76             view.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77             class Panel extends JPanel JavaDoc implements ExplorerManager.Provider, Lookup.Provider {
78                 // Make sure action context works correctly:
79
private final Lookup lookup = ExplorerUtils.createLookup(manager, new ActionMap JavaDoc());
80                 {
81                     setLayout(new BorderLayout JavaDoc());
82                     add(view, BorderLayout.CENTER);
83                 }
84                 public ExplorerManager getExplorerManager() {
85                     return manager;
86                 }
87                 // Make sure list gets focus, with first node initially selected:
88
@Override JavaDoc
89                 public boolean requestFocusInWindow() {
90                     boolean b = view.requestFocusInWindow();
91                     if (manager.getSelectedNodes().length == 0) {
92                         Node[] children = manager.getRootContext().getChildren().getNodes(true);
93                         if (children.length > 0) {
94                             try {
95                                 manager.setSelectedNodes(new Node[] {children[0]});
96                             } catch (PropertyVetoException JavaDoc e) {
97                                 assert false : e;
98                             }
99                         }
100                     }
101                     return b;
102                 }
103                 public Lookup getLookup() {
104                     return lookup;
105                 }
106             }
107             panel = new Panel JavaDoc();
108         }
109         return panel;
110     }
111     
112     public void panelActivated(Lookup context) {
113         selection = context.lookupResult(DataObject.class);
114         selection.addLookupListener(selectionListener);
115         selectionListener.resultChanged(null);
116     }
117     
118     public void panelDeactivated() {
119         selection.removeLookupListener(selectionListener);
120         selection = null;
121     }
122     
123     public Lookup getLookup() {
124         return null;
125     }
126     
127     private void display(Collection JavaDoc<? extends DataObject> selectedFiles) {
128         // Show list of targets for selected file:
129
if (selectedFiles.size() == 1) {
130             DataObject d = selectedFiles.iterator().next();
131             manager.setRootContext(d.getNodeDelegate());
132             return;
133         }
134         // Fallback:
135
manager.setRootContext(Node.EMPTY);
136     }
137     
138 }
139
Popular Tags