1 12 13 package org.eclipse.jface.window; 14 15 import java.lang.reflect.InvocationTargetException ; 16 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 import org.eclipse.jface.action.CoolBarManager; 19 import org.eclipse.jface.action.ICoolBarManager; 20 import org.eclipse.jface.action.IToolBarManager; 21 import org.eclipse.jface.action.MenuManager; 22 import org.eclipse.jface.action.StatusLineManager; 23 import org.eclipse.jface.action.ToolBarManager; 24 import org.eclipse.jface.internal.provisional.action.ICoolBarManager2; 25 import org.eclipse.jface.internal.provisional.action.IToolBarManager2; 26 import org.eclipse.jface.operation.IRunnableContext; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.operation.ModalContext; 29 import org.eclipse.jface.resource.JFaceResources; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.custom.BusyIndicator; 32 import org.eclipse.swt.graphics.Font; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.swt.graphics.Rectangle; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.CoolBar; 38 import org.eclipse.swt.widgets.Decorations; 39 import org.eclipse.swt.widgets.Display; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.Layout; 42 import org.eclipse.swt.widgets.Menu; 43 import org.eclipse.swt.widgets.Shell; 44 import org.eclipse.swt.widgets.ToolBar; 45 46 71 public class ApplicationWindow extends Window implements IRunnableContext { 72 73 78 private MenuManager menuBarManager = null; 79 80 85 private IToolBarManager toolBarManager = null; 86 87 92 private StatusLineManager statusLineManager = null; 93 94 100 private ICoolBarManager coolBarManager = null; 101 102 105 protected Label seperator1; 106 107 110 private boolean operationInProgress = false; 111 112 118 class ApplicationWindowLayout extends Layout { 119 120 static final int VGAP = 2; 121 122 static final int BAR_SIZE = 23; 123 124 protected Point computeSize(Composite composite, int wHint, int hHint, 125 boolean flushCache) { 126 if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) { 127 return new Point(wHint, hHint); 128 } 129 130 Point result = new Point(0, 0); 131 Control[] ws = composite.getChildren(); 132 for (int i = 0; i < ws.length; i++) { 133 Control w = ws[i]; 134 135 boolean hide = false; 136 if (getToolBarControl() == w) { 137 if (!toolBarChildrenExist()) { 138 hide = true; 139 result.y += BAR_SIZE; } 141 } else if (getCoolBarControl() == w) { 142 if (!coolBarChildrenExist()) { 143 hide = true; 144 result.y += BAR_SIZE; 145 } 146 } else if (statusLineManager != null 147 && statusLineManager.getControl() == w) { 148 } else if (i > 0) { 149 hide = false; 150 } 151 152 if (!hide) { 153 Point e = w.computeSize(wHint, hHint, flushCache); 154 result.x = Math.max(result.x, e.x); 155 result.y += e.y + VGAP; 156 } 157 } 158 159 if (wHint != SWT.DEFAULT) { 160 result.x = wHint; 161 } 162 if (hHint != SWT.DEFAULT) { 163 result.y = hHint; 164 } 165 return result; 166 } 167 168 protected void layout(Composite composite, boolean flushCache) { 169 Rectangle clientArea = composite.getClientArea(); 170 171 Control[] ws = composite.getChildren(); 172 173 176 for (int i = 0; i < ws.length; i++) { 177 Control w = ws[i]; 178 179 if (w == seperator1) { Point e = w.computeSize(SWT.DEFAULT, SWT.DEFAULT, 181 flushCache); 182 w.setBounds(clientArea.x, clientArea.y, clientArea.width, 183 e.y); 184 clientArea.y += e.y; 185 clientArea.height -= e.y; 186 } else if (getToolBarControl() == w) { 187 if (toolBarChildrenExist()) { 188 Point e = w.computeSize(SWT.DEFAULT, SWT.DEFAULT, 189 flushCache); 190 w.setBounds(clientArea.x, clientArea.y, 191 clientArea.width, e.y); 192 clientArea.y += e.y + VGAP; 193 clientArea.height -= e.y + VGAP; 194 } 195 } else if (getCoolBarControl() == w) { 196 if (coolBarChildrenExist()) { 197 Point e = w.computeSize(clientArea.width, SWT.DEFAULT, 198 flushCache); 199 w.setBounds(clientArea.x, clientArea.y, 200 clientArea.width, e.y); 201 clientArea.y += e.y + VGAP; 202 clientArea.height -= e.y + VGAP; 203 } 204 } else if (statusLineManager != null 205 && statusLineManager.getControl() == w) { 206 Point e = w.computeSize(SWT.DEFAULT, SWT.DEFAULT, 207 flushCache); 208 w.setBounds(clientArea.x, clientArea.y + clientArea.height 209 - e.y, clientArea.width, e.y); 210 clientArea.height -= e.y + VGAP; 211 } else { 212 w.setBounds(clientArea.x, clientArea.y + VGAP, 213 clientArea.width, clientArea.height - VGAP); 214 } 215 } 216 } 217 } 218 219 223 protected Label getSeperator1() { 224 return seperator1; 225 } 226 227 235 public ApplicationWindow(Shell parentShell) { 236 super(parentShell); 237 } 238 239 244 protected void addMenuBar() { 245 if ((getShell() == null) && (menuBarManager == null)) { 246 menuBarManager = createMenuManager(); 247 } 248 } 249 250 255 protected void addStatusLine() { 256 if ((getShell() == null) && (statusLineManager == null)) { 257 statusLineManager = createStatusLineManager(); 258 } 259 } 260 261 269 protected void addToolBar(int style) { 270 if ((getShell() == null) && (toolBarManager == null) 271 && (coolBarManager == null)) { 272 toolBarManager = createToolBarManager2(style); 273 } 274 } 275 276 284 protected void addCoolBar(int style) { 285 if ((getShell() == null) && (toolBarManager == null) 286 && (coolBarManager == null)) { 287 coolBarManager = createCoolBarManager2(style); 288 } 289 } 290 291 294 protected boolean canHandleShellCloseEvent() { 295 return super.canHandleShellCloseEvent() && !operationInProgress; 296 } 297 298 301 public boolean close() { 302 if (operationInProgress) { 303 return false; 304 } 305 306 if (super.close()) { 307 if (menuBarManager != null) { 308 menuBarManager.dispose(); 309 menuBarManager = null; 310 } 311 if (toolBarManager != null) { 312 if (toolBarManager instanceof IToolBarManager2) { 313 ((IToolBarManager2) toolBarManager).dispose(); 314 } else if (toolBarManager instanceof ToolBarManager) { 315 ((ToolBarManager) toolBarManager).dispose(); 316 } 317 toolBarManager = null; 318 } 319 if (statusLineManager != null) { 320 statusLineManager.dispose(); 321 statusLineManager = null; 322 } 323 if (coolBarManager != null) { 324 if (coolBarManager instanceof ICoolBarManager2) { 325 ((ICoolBarManager2) coolBarManager).dispose(); 326 } else if (coolBarManager instanceof CoolBarManager) { 327 ((CoolBarManager) coolBarManager).dispose(); 328 } 329 coolBarManager = null; 330 } 331 return true; 332 } 333 return false; 334 } 335 336 339 protected void configureShell(Shell shell) { 340 341 super.configureShell(shell); 342 343 createTrimWidgets(shell); 344 } 345 346 352 protected void createTrimWidgets(Shell shell) { 353 if (menuBarManager != null) { 354 menuBarManager.updateAll(true); 355 shell.setMenuBar(menuBarManager.createMenuBar((Decorations) shell)); 356 } 357 358 if (showTopSeperator()) { 359 seperator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 360 } 361 362 createToolBarControl(shell); 364 createCoolBarControl(shell); 365 createStatusLine(shell); 366 } 367 368 371 protected Layout getLayout() { 372 return new ApplicationWindowLayout(); 373 } 374 375 385 protected boolean showTopSeperator() { 386 return !"carbon".equals(SWT.getPlatform()); } 388 389 393 protected void createStatusLine(Shell shell) { 394 if (statusLineManager != null) { 395 statusLineManager.createControl(shell, SWT.NONE); 396 } 397 } 398 399 406 protected MenuManager createMenuManager() { 407 return new MenuManager(); 408 } 409 410 417 protected StatusLineManager createStatusLineManager() { 418 return new StatusLineManager(); 419 } 420 421 432 protected ToolBarManager createToolBarManager(int style) { 433 return new ToolBarManager(style); 434 } 435 436 450 protected IToolBarManager createToolBarManager2(int style) { 451 return createToolBarManager(style); 452 } 453 454 467 protected CoolBarManager createCoolBarManager(int style) { 468 return new CoolBarManager(style); 469 } 470 471 485 protected ICoolBarManager createCoolBarManager2(int style) { 486 return createCoolBarManager(style); 487 } 488 489 497 protected Control createToolBarControl(Composite parent) { 498 if (toolBarManager != null) { 499 if (toolBarManager instanceof IToolBarManager2) { 500 return ((IToolBarManager2) toolBarManager).createControl2(parent); 501 } 502 if (toolBarManager instanceof ToolBarManager) { 503 return ((ToolBarManager) toolBarManager).createControl(parent); 504 } 505 } 506 return null; 507 } 508 509 519 protected Control createCoolBarControl(Composite composite) { 520 if (coolBarManager != null) { 521 if (coolBarManager instanceof ICoolBarManager2) { 522 return ((ICoolBarManager2) coolBarManager).createControl2(composite); 523 } 524 if (coolBarManager instanceof CoolBarManager) { 525 return ((CoolBarManager) coolBarManager).createControl(composite); 526 } 527 } 528 return null; 529 } 530 531 545 protected Font getFont() { 546 return JFaceResources.getFont(getSymbolicFontName()); 547 } 548 549 556 public MenuManager getMenuBarManager() { 557 return menuBarManager; 558 } 559 560 567 protected StatusLineManager getStatusLineManager() { 568 return statusLineManager; 569 } 570 571 581 public String getSymbolicFontName() { 582 return JFaceResources.TEXT_FONT; 583 } 584 585 592 public ToolBarManager getToolBarManager() { 593 if (toolBarManager instanceof ToolBarManager) { 594 return (ToolBarManager)toolBarManager; 595 } 596 return null; 597 } 598 599 607 public IToolBarManager getToolBarManager2() { 608 return toolBarManager; 609 } 610 611 619 public CoolBarManager getCoolBarManager() { 620 if (coolBarManager instanceof CoolBarManager) { 621 return (CoolBarManager)coolBarManager; 622 } 623 return null; 624 } 625 626 634 public ICoolBarManager getCoolBarManager2() { 635 return coolBarManager; 636 } 637 638 645 protected Control getToolBarControl() { 646 if (toolBarManager != null) { 647 if (toolBarManager instanceof IToolBarManager2) { 648 return ((IToolBarManager2) toolBarManager).getControl2(); 649 } 650 if (toolBarManager instanceof ToolBarManager) { 651 return ((ToolBarManager) toolBarManager).getControl(); 652 } 653 } 654 return null; 655 } 656 657 666 protected Control getCoolBarControl() { 667 if (coolBarManager != null) { 668 if (coolBarManager instanceof ICoolBarManager2) { 669 return ((ICoolBarManager2) coolBarManager).getControl2(); 670 } 671 if (coolBarManager instanceof CoolBarManager) { 672 return ((CoolBarManager) coolBarManager).getControl(); 673 } 674 } 675 return null; 676 } 677 678 688 public void run(final boolean fork, boolean cancelable, 689 final IRunnableWithProgress runnable) 690 throws InvocationTargetException , InterruptedException { 691 try { 692 operationInProgress = true; 693 final StatusLineManager mgr = getStatusLineManager(); 694 if (mgr == null) { 695 runnable.run(new NullProgressMonitor()); 696 return; 697 } 698 boolean cancelWasEnabled = mgr.isCancelEnabled(); 699 700 final Control contents = getContents(); 701 final Display display = contents.getDisplay(); 702 Shell shell = getShell(); 703 boolean contentsWasEnabled = contents.getEnabled(); 704 MenuManager manager = getMenuBarManager(); 705 Menu menuBar = null; 706 if (manager != null) { 707 menuBar = manager.getMenu(); 708 manager = null; 709 } 710 boolean menuBarWasEnabled = false; 711 if (menuBar != null) { 712 menuBarWasEnabled = menuBar.getEnabled(); 713 } 714 715 Control toolbarControl = getToolBarControl(); 716 boolean toolbarWasEnabled = false; 717 if (toolbarControl != null) { 718 toolbarWasEnabled = toolbarControl.getEnabled(); 719 } 720 721 Control coolbarControl = getCoolBarControl(); 722 boolean coolbarWasEnabled = false; 723 if (coolbarControl != null) { 724 coolbarWasEnabled = coolbarControl.getEnabled(); 725 } 726 727 Shell[] shells = display.getShells(); 729 boolean[] enabled = new boolean[shells.length]; 730 for (int i = 0; i < shells.length; i++) { 731 Shell current = shells[i]; 732 if (current == shell) { 733 continue; 734 } 735 if (current != null && !current.isDisposed()) { 736 enabled[i] = current.getEnabled(); 737 current.setEnabled(false); 738 } 739 } 740 741 Control currentFocus = display.getFocusControl(); 742 try { 743 contents.setEnabled(false); 744 if (menuBar != null) { 745 menuBar.setEnabled(false); 746 } 747 if (toolbarControl != null) { 748 toolbarControl.setEnabled(false); 749 } 750 if (coolbarControl != null) { 751 coolbarControl.setEnabled(false); 752 } 753 mgr.setCancelEnabled(cancelable); 754 final Exception [] holder = new Exception [1]; 755 BusyIndicator.showWhile(display, new Runnable () { 756 public void run() { 757 try { 758 ModalContext.run(runnable, fork, mgr 759 .getProgressMonitor(), display); 760 } catch (InvocationTargetException ite) { 761 holder[0] = ite; 762 } catch (InterruptedException ie) { 763 holder[0] = ie; 764 } 765 } 766 }); 767 768 if (holder[0] != null) { 769 if (holder[0] instanceof InvocationTargetException ) { 770 throw (InvocationTargetException ) holder[0]; 771 } else if (holder[0] instanceof InterruptedException ) { 772 throw (InterruptedException ) holder[0]; 773 } 774 } 775 } finally { 776 operationInProgress = false; 777 for (int i = 0; i < shells.length; i++) { 779 Shell current = shells[i]; 780 if (current == shell) { 781 continue; 782 } 783 if (current != null && !current.isDisposed()) { 784 current.setEnabled(enabled[i]); 785 } 786 } 787 if (!contents.isDisposed()) { 788 contents.setEnabled(contentsWasEnabled); 789 } 790 if (menuBar != null && !menuBar.isDisposed()) { 791 menuBar.setEnabled(menuBarWasEnabled); 792 } 793 if (toolbarControl != null && !toolbarControl.isDisposed()) { 794 toolbarControl.setEnabled(toolbarWasEnabled); 795 } 796 if (coolbarControl != null && !coolbarControl.isDisposed()) { 797 coolbarControl.setEnabled(coolbarWasEnabled); 798 } 799 mgr.setCancelEnabled(cancelWasEnabled); 800 if (currentFocus != null && !currentFocus.isDisposed()) { 801 currentFocus.forceFocus(); 807 } 808 } 809 } finally { 810 operationInProgress = false; 811 } 812 } 813 814 821 public void setStatus(String message) { 822 if (statusLineManager != null) { 823 statusLineManager.setMessage(message); 824 } 825 } 826 827 833 protected boolean toolBarChildrenExist() { 834 Control toolControl = getToolBarControl(); 835 if (toolControl instanceof ToolBar) { 836 return ((ToolBar) toolControl).getItemCount() > 0; 837 } 838 return false; 839 } 840 841 848 protected boolean coolBarChildrenExist() { 849 Control coolControl = getCoolBarControl(); 850 if (coolControl instanceof CoolBar) { 851 return ((CoolBar) coolControl).getItemCount() > 0; 852 } 853 return false; 854 } 855 856 } 857 | Popular Tags |