1 19 20 package org.netbeans.modules.navigator; 21 22 import java.awt.BorderLayout ; 23 import java.awt.CardLayout ; 24 import java.awt.Color ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.KeyEvent ; 27 import java.lang.ref.Reference ; 28 import java.lang.ref.WeakReference ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.logging.Logger ; 32 import javax.swing.AbstractAction ; 33 import javax.swing.Action ; 34 import javax.swing.FocusManager ; 35 import javax.swing.JComboBox ; 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.KeyStroke ; 39 import javax.swing.SwingConstants ; 40 import javax.swing.UIManager ; 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 56 public final class NavigatorTC extends TopComponent { 57 58 59 private static NavigatorTC instance; 60 61 62 private NavigatorPanel selectedPanel; 63 64 private List <NavigatorPanel> panels; 65 66 private NavigatorController controller; 67 68 private final JLabel notAvailLbl = new JLabel ( 69 NbBundle.getMessage(NavigatorTC.class, "MSG_NotAvailable")); 71 private Lookup navTCLookup; 72 73 74 private NavigatorTC() { 75 initComponents(); 76 77 setName(NbBundle.getMessage(NavigatorTC.class, "LBL_Navigator")); setIcon(Utilities.loadImage("org/netbeans/modules/navigator/resources/navigator.png")); setFocusable(true); 81 putClientProperty("SlidingName", getName()); 84 85 notAvailLbl.setHorizontalAlignment(SwingConstants.CENTER); 86 notAvailLbl.setEnabled(false); 87 Color usualWindowBkg = UIManager.getColor("window"); notAvailLbl.setBackground(usualWindowBkg != null ? usualWindowBkg : Color.white); 89 notAvailLbl.setOpaque(true); 91 92 getController().installActions(); 93 94 setToEmpty(); 96 } 97 98 99 public static final NavigatorTC getInstance () { 100 NavigatorTC navTC = (NavigatorTC)WindowManager.getDefault(). 101 findTopComponent("navigatorTC"); if (navTC == null) { 103 navTC = privateGetInstance(); 105 Logger.getAnonymousLogger().warning( 106 "Could not locate the navigator component via its winsys id"); } 108 return navTC; 109 } 110 111 116 public static final NavigatorTC privateGetInstance () { 117 if (instance == null) { 118 instance = new NavigatorTC(); 119 } 120 return instance; 121 } 122 123 125 public void setSelectedPanel (NavigatorPanel panel) { 126 int panelIdx = panels.indexOf(panel); 127 assert panelIdx != -1 : "Panel to select is not available"; 129 if (panel.equals(selectedPanel)) { 130 return; 131 } 132 133 this.selectedPanel = panel; 134 ((CardLayout )contentArea.getLayout()).show(contentArea, String.valueOf(panelIdx)); 135 panelSelector.setSelectedIndex(panelIdx); 137 } 138 139 142 public NavigatorPanel getSelectedPanel () { 143 return selectedPanel; 144 } 145 146 149 public List <NavigatorPanel> getPanels () { 150 return panels; 151 } 152 153 155 public void setPanels (List <NavigatorPanel> panels) { 156 this.panels = panels; 157 int panelsCount = panels == null ? -1 : panels.size(); 158 if (panelsCount <= 0) { 160 selectedPanel = null; 161 setToEmpty(); 162 } else { 163 contentArea.removeAll(); 165 panelSelector.removeAllItems(); 166 panelSelector.setVisible(panelsCount != 1); 168 JComponent curComp = null; 170 int i = 0; 171 for (NavigatorPanel curPanel : panels) { 172 panelSelector.addItem(curPanel.getDisplayName()); 173 curComp = curPanel.getComponent(); 174 if (curComp == null) { 176 Throwable npe = new NullPointerException ( 177 "Method " + curPanel.getClass().getName() + ".getComponent() must not return null under any condition!" ); 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 resetFromEmpty(); 191 } 192 } 193 194 195 public JComboBox getPanelSelector () { 196 return panelSelector; 197 } 198 199 public JComponent getContentArea () { 200 return contentArea; 201 } 202 203 205 public String preferredID () { 206 return "navigatorTC"; } 208 209 public int getPersistenceType () { 210 return PERSISTENCE_ALWAYS; 211 } 212 213 214 @SuppressWarnings ("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 225 public HelpCtx getHelpCtx () { 226 return new HelpCtx("navigator.java"); 227 } 228 229 230 public void componentOpened () { 231 getController().navigatorTCOpened(); 232 } 233 234 235 public void componentClosed () { 236 getController().navigatorTCClosed(); 237 } 238 239 241 242 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 257 public NavigatorController getController () { 258 if (controller == null) { 259 controller = new NavigatorController(this); 260 } 261 return controller; 262 } 263 264 265 266 267 268 private void setToEmpty () { 269 if (notAvailLbl.isShowing()) { 270 return; 272 } 273 remove(panelSelector); 274 remove(contentArea); 275 add(notAvailLbl, BorderLayout.CENTER); 276 revalidate(); 277 repaint(); 278 } 279 280 281 private void resetFromEmpty () { 282 if (contentArea.isShowing()) { 283 } 285 remove(notAvailLbl); 286 add(panelSelector, BorderLayout.NORTH); 287 add(contentArea, BorderLayout.CENTER); 288 revalidate(); 289 repaint(); 290 } 291 292 293 298 private void initComponents() { 300 panelSelector = new javax.swing.JComboBox (); 301 contentArea = new javax.swing.JPanel (); 302 303 setLayout(new java.awt.BorderLayout ()); 304 305 add(panelSelector, java.awt.BorderLayout.NORTH); 306 307 contentArea.setLayout(new java.awt.CardLayout ()); 308 309 add(contentArea, java.awt.BorderLayout.CENTER); 310 311 } 312 314 315 private javax.swing.JPanel contentArea; 317 private javax.swing.JComboBox panelSelector; 318 320 321 322 323 } 324 | Popular Tags |