1 19 20 package org.netbeans.core.windows.view.ui; 21 22 23 import java.awt.AWTEvent ; 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.Dimension ; 27 import java.awt.EventQueue ; 28 import java.awt.FlowLayout ; 29 import java.awt.Graphics ; 30 import java.awt.Image ; 31 import java.awt.Rectangle ; 32 import java.awt.event.*; 33 import java.awt.image.BufferedImage ; 34 import java.text.*; 35 import java.util.*; 36 import javax.swing.*; 37 import javax.swing.border.*; 38 import javax.swing.event.*; 39 import org.netbeans.core.windows.*; 40 import org.openide.LifecycleManager; 41 import org.openide.awt.*; 42 import org.openide.cookies.InstanceCookie; 43 import org.openide.filesystems.*; 44 import org.openide.loaders.DataObject; 45 import org.openide.util.*; 46 import org.openide.windows.TopComponent; 47 import org.openide.windows.WindowManager; 48 49 56 public final class MainWindow extends JFrame { 57 58 static final long serialVersionUID = -1160791973145645501L; 59 60 61 private Component desktop; 62 63 64 private JPanel desktopPanel; 65 66 private static JPanel innerIconsPanel; 67 68 69 private boolean inited; 70 71 72 73 public MainWindow() { 74 } 75 76 77 protected void setRootPane(JRootPane root) { 78 super.setRootPane(root); 79 if(root != null) { 80 HelpCtx.setHelpIDString( 81 root, new HelpCtx(MainWindow.class).getHelpID()); 82 } 83 root.setOpaque(true); 88 if (Utilities.isWindows()) { 89 JComponent c = new JPanel() { 92 public void setVisible(boolean flag) { 93 if (flag != isVisible ()) { 94 super.setVisible(flag); 95 } 96 } 97 }; 98 c.setName(root.getName()+".nbGlassPane"); c.setVisible(false); 100 ((JPanel)c).setOpaque(false); 101 root.setGlassPane(c); 102 } 103 } 104 105 106 public void initializeComponents() { 107 if(inited) { 108 return; 109 } 110 inited = true; 111 112 setIconImage(createIDEImage()); 114 115 initListeners(); 116 117 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 118 119 getAccessibleContext().setAccessibleDescription( 120 NbBundle.getBundle(MainWindow.class).getString("ACSD_MainWindow")); 121 122 setJMenuBar(createMenuBar()); 123 124 if (!Constants.NO_TOOLBARS) { 125 JComponent tb = getToolbarComponent(); 126 getContentPane().add(tb, BorderLayout.NORTH); 127 } 128 129 if(!Constants.SWITCH_STATUSLINE_IN_MENUBAR) { 130 if (Constants.CUSTOM_STATUS_LINE_PATH == null) { 131 JLabel status = new StatusLine(); 132 status.setText(" "); status.setPreferredSize(new Dimension (0, status.getPreferredSize().height)); 135 status.setBorder (BorderFactory.createEmptyBorder (0, 4, 0, 0)); 137 138 JPanel statusLinePanel = new JPanel(new BorderLayout ()); 139 int magicConstant = 0; 140 if (Utilities.isMac()) { 141 magicConstant = 12; 144 } 145 146 statusLinePanel.setBorder(BorderFactory.createCompoundBorder( 148 BorderFactory.createEmptyBorder (0, 0, 0, magicConstant), 149 statusLinePanel.getBorder ())); 150 151 statusLinePanel.add(new JSeparator(), BorderLayout.NORTH); 152 statusLinePanel.add(status, BorderLayout.CENTER); 153 154 decoratePanel (statusLinePanel); 155 statusLinePanel.setName("statusLine"); getContentPane().add (statusLinePanel, BorderLayout.SOUTH); 157 } else { JComponent status = getCustomStatusLine(); 159 if (status != null) { 160 getContentPane().add(status, BorderLayout.SOUTH); 161 } 162 } 163 } 164 165 desktopPanel = new JPanel(); 167 168 desktopPanel.setBorder(getDesktopBorder()); 169 desktopPanel.setLayout(new BorderLayout ()); 170 171 176 getContentPane().add(desktopPanel, BorderLayout.CENTER); 177 MenuSelectionManager.defaultManager().addChangeListener(new ChangeListener(){ 180 public void stateChanged(ChangeEvent e) { 181 MenuElement[] elems = MenuSelectionManager.defaultManager().getSelectedPath(); 182 if (elems != null && elems.length > 0) { 183 if (elems[0] == getJMenuBar()) { 184 if (!isActive()) { 185 toFront(); 186 } 187 } 188 } 189 } 190 }); 191 setTitle(NbBundle.getMessage(MainWindow.class, "CTL_MainWindow_Title_No_Project", System.getProperty("netbeans.buildnumber"))); 193 } 194 195 private static void decoratePanel (JPanel panel) { 196 assert SwingUtilities.isEventDispatchThread () : "Must run in AWT queue."; 197 if (innerIconsPanel != null) { 198 panel.remove (innerIconsPanel); 199 } 200 innerIconsPanel = getStatusLineElements (panel); 201 if (innerIconsPanel != null) { 202 panel.add (innerIconsPanel, BorderLayout.EAST); 203 } 204 } 205 206 private static Lookup.Result<StatusLineElementProvider> result; 207 208 static JPanel getStatusLineElements (JPanel panel) { 210 if (result == null) { 212 result = Lookup.getDefault ().lookup ( 213 new Lookup.Template<StatusLineElementProvider> (StatusLineElementProvider.class)); 214 result.addLookupListener (new StatusLineElementsListener (panel)); 215 } 216 Collection<? extends StatusLineElementProvider> c = result.allInstances (); 217 if (c == null || c.isEmpty ()) { 218 return null; 219 } 220 Iterator<? extends StatusLineElementProvider> it = c.iterator (); 221 JPanel icons = new JPanel (new FlowLayout (FlowLayout.RIGHT, 0, 0)); 222 icons.setBorder (BorderFactory.createEmptyBorder (1, 0, 0, 2)); 223 boolean some = false; 224 while (it.hasNext ()) { 225 StatusLineElementProvider o = it.next (); 226 Component comp = o.getStatusLineElement (); 227 if (comp != null) { 228 some = true; 229 icons.add (comp); 230 } 231 } 232 return some ? icons : null; 233 } 234 235 static private class StatusLineElementsListener implements LookupListener { 236 private JPanel decoratingPanel; 237 StatusLineElementsListener (JPanel decoratingPanel) { 238 this.decoratingPanel = decoratingPanel; 239 } 240 public void resultChanged (LookupEvent ev) { 241 SwingUtilities.invokeLater (new Runnable () { 242 public void run () { 243 decoratePanel (decoratingPanel); 244 } 245 }); 246 } 247 } 248 249 251 private static Border getDesktopBorder () { 252 Border b = (Border) UIManager.get ("nb.desktop.splitpane.border"); 253 if (b != null) { 254 return b; 255 } else { 256 return new EmptyBorder(1, 1, 1, 1); 257 } 258 } 259 260 private static final String ICON_SMALL = "org/netbeans/core/startup/frame.gif"; private static final String ICON_BIG = "org/netbeans/core/startup/frame32.gif"; static Image createIDEImage() { 263 return Utilities.loadImage(Utilities.isLargeFrameIcons() ? ICON_BIG : ICON_SMALL, true); 264 } 265 266 private void initListeners() { 267 addWindowListener (new WindowAdapter() { 268 public void windowClosing(WindowEvent evt) { 269 LifecycleManager.getDefault().exit(); 270 } 271 272 public void windowActivated (WindowEvent evt) { 273 org.netbeans.core.windows.RegistryImpl.cancelMenu(MainWindow.this); 276 } 277 } 278 ); 279 } 280 281 282 private static JMenuBar createMenuBar() { 283 JMenuBar menu = getCustomMenuBar(); 284 if (menu == null) { 285 menu = new MenuBar (null); 286 } 287 menu.setBorderPainted(false); 288 if (menu instanceof MenuBar) { 289 ((MenuBar)menu).waitFinished(); 290 } 291 292 if(Constants.SWITCH_STATUSLINE_IN_MENUBAR) { 293 if (Constants.CUSTOM_STATUS_LINE_PATH == null) { 294 JLabel status = new StatusLine(); 295 JSeparator sep = new JSeparator(JSeparator.VERTICAL); 296 Dimension d = sep.getPreferredSize(); 297 d.width += 6; sep.setPreferredSize(d); 299 JPanel statusLinePanel = new JPanel(new BorderLayout ()); 300 statusLinePanel.add(sep, BorderLayout.WEST); 301 statusLinePanel.add(status, BorderLayout.CENTER); 302 303 decoratePanel (statusLinePanel); 304 statusLinePanel.setName("statusLine"); menu.add(statusLinePanel); 306 } else { 307 JComponent status = getCustomStatusLine(); 308 if (status != null) { 309 menu.add(status); 310 } 311 } 312 } 313 314 return menu; 315 } 316 317 322 private static JMenuBar getCustomMenuBar() { 323 try { 324 String fileName = Constants.CUSTOM_MENU_BAR_PATH; 325 if (fileName == null) { 326 return null; 327 } 328 FileObject fo = 329 Repository.getDefault().getDefaultFileSystem().findResource( 330 fileName); 331 if (fo != null) { 332 DataObject dobj = DataObject.find(fo); 333 InstanceCookie ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 334 if (ic != null) { 335 return (JMenuBar)ic.instanceCreate(); 336 } 337 } 338 } catch (Exception e) { 339 Exceptions.printStackTrace(e); 340 } 341 return null; 342 } 343 344 349 private static JComponent getCustomStatusLine() { 350 try { 351 String fileName = Constants.CUSTOM_STATUS_LINE_PATH; 352 if (fileName == null) { 353 return null; 354 } 355 FileObject fo = 356 Repository.getDefault().getDefaultFileSystem().findResource( 357 fileName); 358 if (fo != null) { 359 DataObject dobj = DataObject.find(fo); 360 InstanceCookie ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class); 361 if (ic != null) { 362 return (JComponent)ic.instanceCreate(); 363 } 364 } 365 } catch (Exception e) { 366 Exceptions.printStackTrace(e); 367 } 368 return null; 369 } 370 371 372 private static JComponent getToolbarComponent() { 373 ToolbarPool tp = ToolbarPool.getDefault(); 374 tp.waitFinished(); 375 378 return tp; 379 } 380 381 382 private void initializeBounds() { 383 Rectangle bounds; 384 if(WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_JOINED) { 385 bounds = WindowManagerImpl.getInstance().getMainWindowBoundsJoined(); 386 } else { 387 bounds = WindowManagerImpl.getInstance().getMainWindowBoundsSeparated(); 388 } 389 390 if(!bounds.isEmpty()) { 391 setBounds(bounds); 392 } 393 } 394 395 399 public void setBounds(Rectangle rect) { 400 Rectangle bounds = rect; 401 if (bounds != null) { 402 if (bounds.height < getPreferredSize().height) { 403 bounds = new Rectangle (bounds.x, bounds.y, bounds.width, getPreferredSize().height); 404 } 405 } 406 super.setBounds(bounds); 407 } 408 409 410 public void prepareWindow() { 411 initializeBounds(); 412 } 413 414 415 public void setDesktop(Component comp) { 416 if(desktop == comp) { 417 if(desktop != null 420 && !Arrays.asList(desktopPanel.getComponents()).contains(desktop)) { 421 desktopPanel.add(desktop, BorderLayout.CENTER); 422 } 423 return; 424 } 425 426 if(desktop != null) { 427 desktopPanel.remove(desktop); 428 } 429 430 desktop = comp; 431 432 if(desktop != null) { 433 desktopPanel.add(desktop, BorderLayout.CENTER); 434 } 435 invalidate(); 436 validate(); 437 if( isOlderJDK && !System.getProperty("os.name").startsWith("Windows") ) { 439 releaseWaitingForPaintDummyGraphic(); 440 } 441 442 repaint(); 443 } 444 445 public Component getDesktop() { 447 return desktop; 448 } 449 450 public boolean hasDesktop() { 451 return desktop != null; 452 } 453 454 456 public Rectangle getPureMainWindowBounds() { 457 Rectangle bounds = getBounds(); 458 459 if(desktop != null) { 462 Dimension desktopSize = desktop.getSize(); 463 bounds.height -= desktopSize.height; 464 } 465 466 return bounds; 467 } 468 469 473 private Image waitingForPaintDummyImage; 474 private Graphics waitingForPaintDummyGraphic; 475 boolean isOlderJDK = System.getProperty("java.version").startsWith("1.5"); 476 477 public void setVisible (boolean flag) { 478 if (flag && isOlderJDK) { 485 waitingForPaintDummyImage = new BufferedImage (1, 1, BufferedImage.TYPE_INT_RGB); 486 waitingForPaintDummyGraphic = waitingForPaintDummyImage.getGraphics(); 487 } 488 super.setVisible(flag); 489 } 490 491 public void paint(Graphics g) { 492 if (waitingForPaintDummyGraphic != null) { 494 releaseWaitingForPaintDummyGraphic(); 495 g = getGraphics(); 498 } 499 super.paint(g); 500 } 501 502 505 public Graphics getGraphics () { 506 if (waitingForPaintDummyGraphic != null) { 508 AWTEvent event = EventQueue.getCurrentEvent(); 511 if (event == null || (event.getID() != PaintEvent.PAINT && event.getSource() != this)) { 512 return waitingForPaintDummyGraphic; 513 } 514 releaseWaitingForPaintDummyGraphic(); 515 } 516 return super.getGraphics(); 517 } 518 519 private void releaseWaitingForPaintDummyGraphic () { 520 if (waitingForPaintDummyGraphic != null) { 521 waitingForPaintDummyGraphic.dispose(); 522 waitingForPaintDummyGraphic = null; 523 waitingForPaintDummyImage = null; 524 } 525 } 526 527 529 private boolean isFullScreenMode = false; 531 private Rectangle restoreBounds; 532 private int restoreExtendedState = JFrame.NORMAL; 533 534 public void setFullScreenMode( boolean fullScreenMode ) { 535 if( isFullScreenMode == fullScreenMode ) { 536 return; 537 } 538 final TopComponent activatedTc = WindowManager.getDefault().getRegistry().getActivated(); 539 if( !isFullScreenMode ) { 540 restoreExtendedState = getExtendedState(); 541 restoreBounds = getBounds(); 542 } 543 isFullScreenMode = fullScreenMode; 544 setVisible( false ); 545 dispose(); 546 setUndecorated( isFullScreenMode ); 547 setExtendedState( isFullScreenMode ? JFrame.MAXIMIZED_BOTH : restoreExtendedState ); 548 549 getJMenuBar().setVisible( !isFullScreenMode ); 550 getToolbarComponent().setVisible( !isFullScreenMode ); 551 if( !isFullScreenMode && restoreExtendedState != JFrame.MAXIMIZED_BOTH ) { 552 setBounds( restoreBounds ); 553 } 554 setVisible( true ); 555 SwingUtilities.invokeLater( new Runnable () { 556 public void run() { 557 invalidate(); 558 validate(); 559 repaint(); 560 if( null != activatedTc ) { 561 activatedTc.requestFocusInWindow(); 562 } 563 } 564 }); 565 } 566 567 public boolean isFullScreenMode() { 568 return isFullScreenMode; 569 } 570 } 571 572 | Popular Tags |