KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > navigator > NavigatorTC


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.netbeans.modules.navigator;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.CardLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.lang.ref.Reference JavaDoc;
28 import java.lang.ref.WeakReference JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import javax.swing.AbstractAction JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.FocusManager JavaDoc;
35 import javax.swing.JComboBox JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.KeyStroke JavaDoc;
39 import javax.swing.SwingConstants JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41 import org.netbeans.spi.navigator.NavigatorPanel;
42 import org.openide.ErrorManager;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.Lookup;
45 import org.openide.util.NbBundle;
46 import org.openide.util.Utilities;
47 import org.openide.util.lookup.ProxyLookup;
48 import org.openide.windows.TopComponent;
49 import org.openide.windows.WindowManager;
50
51 /** Navigator TopComponent. Simple visual envelope for navigator graphics
52  * content. Behaviour is delegated and separated into NavigatorController.
53  *
54  * @author Dafe Simonek
55  */

56 public final class NavigatorTC extends TopComponent {
57     
58     /** singleton instance */
59     private static NavigatorTC instance;
60     
61     /** Currently active panel in navigator (or null if empty) */
62     private NavigatorPanel selectedPanel;
63     /** A list of panels currently available (or null if empty) */
64     private List JavaDoc<NavigatorPanel> panels;
65     /** Controller, controls behaviour and reacts to user actions */
66     private NavigatorController controller;
67     /** label signalizing no available providers */
68     private final JLabel JavaDoc notAvailLbl = new JLabel JavaDoc(
69             NbBundle.getMessage(NavigatorTC.class, "MSG_NotAvailable")); //NOI18N
70
/** special lookup for naviagtor TC */
71     private Lookup navTCLookup;
72     
73     /** Creates new NavigatorTC, singleton */
74     private NavigatorTC() {
75         initComponents();
76         
77         setName(NbBundle.getMessage(NavigatorTC.class, "LBL_Navigator")); //NOI18N
78
setIcon(Utilities.loadImage("org/netbeans/modules/navigator/resources/navigator.png")); //NOI18N
79
// accept focus when empty to work correctly in nb winsys
80
setFocusable(true);
81         // special title for sliding mode
82
// XXX - please rewrite to regular API when available - see issue #55955
83
putClientProperty("SlidingName", getName());
84         
85         notAvailLbl.setHorizontalAlignment(SwingConstants.CENTER);
86         notAvailLbl.setEnabled(false);
87         Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
88
notAvailLbl.setBackground(usualWindowBkg != null ? usualWindowBkg : Color.white);
89         // to ensure our background color will have effect
90
notAvailLbl.setOpaque(true);
91         
92         getController().installActions();
93
94         // empty initially
95
setToEmpty();
96     }
97
98     /** Singleton accessor, finds instance in winsys structures */
99     public static final NavigatorTC getInstance () {
100         NavigatorTC navTC = (NavigatorTC)WindowManager.getDefault().
101                         findTopComponent("navigatorTC"); //NOI18N
102
if (navTC == null) {
103             // shouldn't happen under normal conditions
104
navTC = privateGetInstance();
105             Logger.getAnonymousLogger().warning(
106                 "Could not locate the navigator component via its winsys id"); //NOI18N
107
}
108         return navTC;
109     }
110     
111     /** Singleton intance accessor, to be used only from module's layer.xml
112      * file, winsys section and as fallback from getInstance().
113      *
114      * Please don't call directly otherwise.
115      */

116     public static final NavigatorTC privateGetInstance () {
117         if (instance == null) {
118             instance = new NavigatorTC();
119         }
120         return instance;
121     }
122
123     /** Shows given navigator panel's component
124      */

125     public void setSelectedPanel (NavigatorPanel panel) {
126         int panelIdx = panels.indexOf(panel);
127         assert panelIdx != -1 : "Panel to select is not available"; //NOI18N
128

129         if (panel.equals(selectedPanel)) {
130             return;
131         }
132         
133         this.selectedPanel = panel;
134         ((CardLayout JavaDoc)contentArea.getLayout()).show(contentArea, String.valueOf(panelIdx));
135         // #93123: follow-up, synchronizing combo selection with content area selection
136
panelSelector.setSelectedIndex(panelIdx);
137     }
138     
139     /** Returns panel currently selected.
140      * @return Panel currently selected or null if navigator is empty
141      */

142     public NavigatorPanel getSelectedPanel () {
143         return selectedPanel;
144     }
145     
146     /** List of panels currently contained in navigator component.
147      * @return List of NavigatorPanel instances or null if navigator is empty
148      */

149     public List JavaDoc<NavigatorPanel> getPanels () {
150         return panels;
151     }
152     
153     /** Sets content of navigator to given panels, selecting the first one
154      */

155     public void setPanels (List JavaDoc<NavigatorPanel> panels) {
156         this.panels = panels;
157         int panelsCount = panels == null ? -1 : panels.size();
158         // no panel, so make UI look empty
159
if (panelsCount <= 0) {
160             selectedPanel = null;
161             setToEmpty();
162         } else {
163             // clear regular content
164
contentArea.removeAll();
165             panelSelector.removeAllItems();
166             // #63777: hide panel selector when only one panel available
167
panelSelector.setVisible(panelsCount != 1);
168             // fill with new content
169
JComponent JavaDoc curComp = null;
170             int i = 0;
171             for (NavigatorPanel curPanel : panels) {
172                 panelSelector.addItem(curPanel.getDisplayName());
173                 curComp = curPanel.getComponent();
174                 // for better error report in cases like #68544
175
if (curComp == null) {
176                     Throwable JavaDoc npe = new NullPointerException JavaDoc(
177                             "Method " + curPanel.getClass().getName() + //NOI18N
178
".getComponent() must not return null under any condition!" //NOI18N
179
);
180                     ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, npe);
181                 } else {
182                     contentArea.add(curComp, String.valueOf(i));
183                 }
184                 if (i == 0) {
185                     selectedPanel = curPanel;
186                 }
187                 i++;
188             }
189             // show if was hidden
190
resetFromEmpty();
191         }
192     }
193     
194     /** Returns combo box, UI for selecting proper panels */
195     public JComboBox JavaDoc getPanelSelector () {
196         return panelSelector;
197     }
198     
199     public JComponent JavaDoc getContentArea () {
200         return contentArea;
201     }
202     
203     // Window System related methods >>
204

205     public String JavaDoc preferredID () {
206         return "navigatorTC"; //NOI18N
207
}
208
209     public int getPersistenceType () {
210         return PERSISTENCE_ALWAYS;
211     }
212
213     /** Overriden to pass focus directly into content panel */
214     @SuppressWarnings JavaDoc("deprecation")
215     public boolean requestFocusInWindow () {
216         super.requestFocusInWindow();
217         boolean result = false;
218         if (selectedPanel != null) {
219             result = selectedPanel.getComponent().requestFocusInWindow();
220         }
221         return result;
222     }
223
224     /** Defines nagivator Help ID */
225     public HelpCtx getHelpCtx () {
226         return new HelpCtx("navigator.java");
227     }
228
229     /** Just delegates to controller */
230     public void componentOpened () {
231         getController().navigatorTCOpened();
232     }
233     
234     /** Just delegates to controller */
235     public void componentClosed () {
236         getController().navigatorTCClosed();
237     }
238     
239     // << Window system
240

241     
242     /** Combines default Lookup of TC with lookup from active navigator
243      * panel.
244      */

245     public Lookup getLookup() {
246         if (navTCLookup == null) {
247             Lookup defaultLookup = super.getLookup();
248             Lookup clientLookup = getController().getPanelLookup();
249             navTCLookup = new ProxyLookup(
250                     new Lookup [] { defaultLookup, clientLookup }
251             );
252         }
253         return navTCLookup;
254     }
255
256     /** Accessor for controller which controls UI behaviour */
257     public NavigatorController getController () {
258         if (controller == null) {
259             controller = new NavigatorController(this);
260         }
261         return controller;
262     }
263     
264     
265     /*************** private stuff ************/
266     
267     /** Removes regular UI content and sets UI to empty state */
268     private void setToEmpty () {
269         if (notAvailLbl.isShowing()) {
270             // already empty
271
return;
272         }
273         remove(panelSelector);
274         remove(contentArea);
275         add(notAvailLbl, BorderLayout.CENTER);
276         revalidate();
277         repaint();
278     }
279     
280     /** Puts regular UI content back */
281     private void resetFromEmpty () {
282         if (contentArea.isShowing()) {
283             // content already shown
284
}
285         remove(notAvailLbl);
286         add(panelSelector, BorderLayout.NORTH);
287         add(contentArea, BorderLayout.CENTER);
288         revalidate();
289         repaint();
290     }
291     
292     
293     /** This method is called from within the constructor to
294      * initialize the form.
295      * WARNING: Do NOT modify this code. The content of this method is
296      * always regenerated by the Form Editor.
297      */

298     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
299
private void initComponents() {
300         panelSelector = new javax.swing.JComboBox JavaDoc();
301         contentArea = new javax.swing.JPanel JavaDoc();
302
303         setLayout(new java.awt.BorderLayout JavaDoc());
304
305         add(panelSelector, java.awt.BorderLayout.NORTH);
306
307         contentArea.setLayout(new java.awt.CardLayout JavaDoc());
308
309         add(contentArea, java.awt.BorderLayout.CENTER);
310
311     }
312     // </editor-fold>//GEN-END:initComponents
313

314     
315     // Variables declaration - do not modify//GEN-BEGIN:variables
316
private javax.swing.JPanel JavaDoc contentArea;
317     private javax.swing.JComboBox JavaDoc panelSelector;
318     // End of variables declaration//GEN-END:variables
319

320     
321     
322     
323 }
324
Popular Tags