1 17 package org.eclipse.emf.common.ui; 18 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 27 import org.eclipse.jface.action.MenuManager; 28 import org.eclipse.jface.action.ToolBarManager; 29 import org.eclipse.jface.resource.ImageDescriptor; 30 import org.eclipse.jface.viewers.ContentViewer; 31 import org.eclipse.jface.viewers.IBaseLabelProvider; 32 import org.eclipse.jface.viewers.ILabelProvider; 33 import org.eclipse.jface.viewers.Viewer; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.custom.CLabel; 36 import org.eclipse.swt.custom.CTabFolder; 37 import org.eclipse.swt.custom.SashForm; 38 import org.eclipse.swt.custom.ViewForm; 39 import org.eclipse.swt.events.DisposeEvent; 40 import org.eclipse.swt.events.DisposeListener; 41 import org.eclipse.swt.events.MouseAdapter; 42 import org.eclipse.swt.events.MouseEvent; 43 import org.eclipse.swt.events.MouseListener; 44 import org.eclipse.swt.events.PaintEvent; 45 import org.eclipse.swt.events.PaintListener; 46 import org.eclipse.swt.events.SelectionAdapter; 47 import org.eclipse.swt.events.SelectionEvent; 48 import org.eclipse.swt.graphics.Color; 49 import org.eclipse.swt.graphics.GC; 50 import org.eclipse.swt.graphics.Image; 51 import org.eclipse.swt.graphics.Point; 52 import org.eclipse.swt.graphics.RGB; 53 import org.eclipse.swt.graphics.Rectangle; 54 import org.eclipse.swt.widgets.Composite; 55 import org.eclipse.swt.widgets.Control; 56 import org.eclipse.swt.widgets.Display; 57 import org.eclipse.swt.widgets.Event; 58 import org.eclipse.swt.widgets.Listener; 59 import org.eclipse.swt.widgets.Menu; 60 import org.eclipse.swt.widgets.MenuItem; 61 import org.eclipse.swt.widgets.ToolBar; 62 import org.eclipse.swt.widgets.ToolItem; 63 import org.eclipse.ui.IPartListener; 64 import org.eclipse.ui.IPropertyListener; 65 import org.eclipse.ui.IWorkbenchPage; 66 import org.eclipse.ui.IWorkbenchPart; 67 68 69 72 public abstract class ViewerPane implements IPropertyListener, Listener 73 { 74 protected IWorkbenchPage page; 75 protected IWorkbenchPart part; 76 protected Collection buddies = new ArrayList (); 77 protected Viewer viewer; 78 protected Composite container; 79 boolean isActive; 80 protected CLabel titleLabel; 81 protected ToolBar actionBar; 82 protected ToolBarManager toolBarManager; 83 protected MenuManager menuManager; 84 protected Image pullDownImage; 85 protected ToolBar systemBar; 86 protected ViewForm control; 87 88 protected MouseListener mouseListener = 89 new MouseAdapter() 90 { 91 public void mouseDown(MouseEvent e) 92 { 93 requestActivation(); 94 } 95 public void mouseDoubleClick(MouseEvent e) 96 { 97 if (e.getSource() == titleLabel) 98 { 99 doMaximize(); 100 } 101 } 102 }; 103 104 protected IPartListener partListener = 105 new IPartListener() 106 { 107 public void partActivated(IWorkbenchPart p) 108 { 109 } 110 public void partBroughtToTop(IWorkbenchPart p) 111 { 112 } 113 public void partClosed(IWorkbenchPart p) 114 { 115 } 116 public void partDeactivated(IWorkbenchPart p) 117 { 118 if (p == ViewerPane.this.part) 119 { 120 showFocus(false); 121 } 122 } 123 public void partOpened(IWorkbenchPart p) 124 { 125 } 126 }; 127 128 129 132 public ViewerPane(IWorkbenchPage page, IWorkbenchPart part) 133 { 134 WorkbenchColors.startup(); 135 this.page = page; 136 this.part = part; 137 138 page.addPartListener(partListener); 139 } 140 141 abstract public Viewer createViewer(Composite parent); 142 143 public Collection getBudies() 144 { 145 return buddies; 146 } 147 148 public void createControl(Composite parent) 149 { 150 if (getControl() == null) 151 { 152 container = parent; 153 154 control = new ViewForm(parent, SWT.NONE); 157 control.addDisposeListener 158 (new DisposeListener() 159 { 160 public void widgetDisposed(DisposeEvent event) 161 { 162 dispose(); 163 } 164 }); 165 control.marginWidth = 0; 166 control.marginHeight = 0; 167 168 createTitleBar(); 170 171 viewer = createViewer(control); 172 control.setContent(viewer.getControl()); 173 174 control.setTabList(new Control [] { viewer.getControl() }); 175 176 control.addListener(SWT.Activate, this); 178 hookFocus(control); 179 hookFocus(viewer.getControl()); 180 } 181 } 182 183 public Viewer getViewer() 184 { 185 return viewer; 186 } 187 188 191 public Control getControl() 192 { 193 return control; 194 } 195 196 199 protected ViewForm getViewForm() 200 { 201 return control; 202 } 203 204 207 public void handleEvent(Event event) 208 { 209 if (event.type == SWT.Activate) 210 { 211 requestActivation(); 212 } 213 } 214 215 218 public void hookFocus(Control ctrl) 219 { 220 ctrl.addMouseListener(mouseListener); 221 } 222 223 227 protected void requestActivation() 228 { 229 control.getContent().setFocus(); 230 showFocus(true); 231 } 232 233 236 public void setFocus() 237 { 238 requestActivation(); 239 control.getContent().setFocus(); 240 } 241 242 245 class PaneToolBarManager extends ToolBarManager 246 { 247 public PaneToolBarManager(ToolBar paneToolBar) 248 { 249 super(paneToolBar); 250 } 251 252 255 protected void relayout(ToolBar toolBar, int oldCount, int newCount) 256 { 257 if (newCount < 1) 261 { 262 if (control.getTopCenter() != null) 263 { 264 control.setTopCenter(null); 265 } 266 } 267 else 268 { 269 toolBar.layout(); 270 if (control.getTopCenter() == null) 271 { 272 control.setTopCenter(toolBar); 273 } 274 } 275 Composite parent= toolBar.getParent(); 276 parent.layout(); 277 if (parent.getParent() != null) 278 { 279 parent.getParent().layout(); 280 } 281 } 282 } 283 284 287 private void createMenuManager() 288 { 289 menuManager = new MenuManager("Pane Menu"); 290 if (systemBar != null) 291 { 292 createPulldownMenu(); 293 } 294 } 295 296 299 private void createPulldownMenu() 300 { 301 if (systemBar != null) 302 { 303 ToolItem ti = new ToolItem(systemBar, SWT.PUSH, 0); 304 try 305 { 306 pullDownImage = 307 ImageDescriptor.createFromURL 308 (new URL (CommonUIPlugin.INSTANCE.getImage("full/ctool16/ViewPullDown").toString())).createImage(); 309 ti.setImage(pullDownImage); 310 ti.addSelectionListener 311 (new SelectionAdapter() 312 { 313 public void widgetSelected(SelectionEvent e) 314 { 315 showViewMenu(); 316 } 317 }); 318 } 319 catch (MalformedURLException exception) 320 { 321 } 322 } 323 } 324 325 332 protected void createTitleBar() 333 { 334 if (titleLabel == null) 336 { 337 titleLabel = new CLabel(control, SWT.SHADOW_NONE); 339 hookFocus(titleLabel); 340 titleLabel.setAlignment(SWT.LEFT); 341 titleLabel.setBackground(null, null); 342 titleLabel.addMouseListener 343 (new MouseAdapter() 344 { 345 public void mouseDown(MouseEvent e) 346 { 347 if (e.button == 3) 348 { 349 showTitleLabelMenu(e); 350 } 351 } 352 }); 353 titleLabel.addPaintListener 354 (new PaintListener() 355 { 356 public void paintControl(PaintEvent event) 357 { 358 if (isActive) 359 { 360 Rectangle clientRectangle = titleLabel.getClientArea(); 361 event.gc.drawImage 362 (WorkbenchColors.getGradient(), 363 10, 0, 10, 10, 364 0, 0, 24, clientRectangle.height); 365 366 Image image = titleLabel.getImage(); 367 if (image != null) 368 { 369 Rectangle imageRectangle = image.getBounds(); 370 event.gc.drawImage 371 (image, 372 0, 0, imageRectangle.width, imageRectangle.height, 373 3, (clientRectangle.height-imageRectangle.height) / 2, imageRectangle.width, imageRectangle.height); 374 } 375 } 376 } 377 }); 378 379 updateTitles(); 380 control.setTopLeft(titleLabel); 381 382 385 actionBar = new ToolBar(control, SWT.FLAT | SWT.WRAP); 387 hookFocus(actionBar); 388 control.setTopCenter(actionBar); 389 390 systemBar = new ToolBar(control, SWT.FLAT | SWT.WRAP); 392 hookFocus(systemBar); 393 if (menuManager != null && !menuManager.isEmpty()) 394 { 395 createPulldownMenu(); 396 } 397 control.setTopRight(systemBar); 398 } 399 } 400 401 protected void doMaximize() 402 { 403 Control child = control; 404 for (Control parent = control.getParent(); parent instanceof SashForm || parent instanceof CTabFolder; parent = parent.getParent()) 405 { 406 if (parent instanceof CTabFolder) 407 { 408 CTabFolder cTabFolder = (CTabFolder)parent; 409 cTabFolder.setMaximized(!cTabFolder.getMaximized()); 410 } 411 else if (parent instanceof SashForm) 412 { 413 SashForm sashForm = (SashForm)parent; 414 if (sashForm.getMaximizedControl() == null) 415 { 416 sashForm.setMaximizedControl(child); 417 } 418 else 419 { 420 sashForm.setMaximizedControl(null); 421 } 422 } 423 child = parent; 424 } 425 } 426 427 public void dispose() 428 { 429 if ((control != null) && (!control.isDisposed())) 430 { 431 control.removeListener(SWT.Activate, this); 432 control = null; 433 page.removePartListener(partListener); 434 } 435 436 if (pullDownImage != null) 437 { 438 pullDownImage.dispose(); 439 pullDownImage = null; 440 } 441 } 442 443 public MenuManager getMenuManager() 444 { 445 if (menuManager == null) 446 { 447 createMenuManager(); 448 } 449 return menuManager; 450 } 451 452 public ToolBarManager getToolBarManager() 453 { 454 if (toolBarManager == null) 455 { 456 toolBarManager = new PaneToolBarManager(actionBar); 457 } 458 return toolBarManager; 459 } 460 461 468 public void propertyChanged(Object source, int propId) 469 { 470 if (propId == IWorkbenchPart.PROP_TITLE) 471 { 472 updateTitles(); 473 } 474 } 475 476 479 public void showFocus(boolean inFocus) 480 { 481 if (inFocus != isActive) 482 { 483 isActive = inFocus; 484 485 if (titleLabel != null) 486 { 487 if (inFocus) 488 { 489 titleLabel.update(); 492 titleLabel.redraw(); 493 } 496 else 497 { 498 titleLabel.update(); 501 titleLabel.redraw(); 502 } 505 } 506 } 507 } 508 509 512 private void showViewMenu() 513 { 514 Menu aMenu = menuManager.createContextMenu(getControl()); 515 Point topLeft = new Point(0, 0); 516 topLeft.y += systemBar.getBounds().height; 517 topLeft = systemBar.toDisplay(topLeft); 518 aMenu.setLocation(topLeft.x, topLeft.y); 519 aMenu.setVisible(true); 520 } 521 522 public String toString() 523 { 524 String label = "disposed"; 525 if((titleLabel != null) && (!titleLabel.isDisposed())) 526 label = titleLabel.getText(); 527 528 return getClass().getName() + "@" + Integer.toHexString(hashCode()) + 529 "(" + label + ")"; 530 } 531 532 public void updateActionBars() 533 { 534 if (menuManager != null) 535 { 536 menuManager.updateAll(false); 537 } 538 539 if (toolBarManager != null) 540 { 541 getToolBarManager().update(false); 542 } 543 } 544 545 548 public void updateTitles() 549 { 550 titleLabel.update(); 554 } 555 556 public void setTitle(Object object) 557 { 558 if (viewer != null) 559 { 560 if (viewer instanceof ContentViewer) 561 { 562 IBaseLabelProvider labelProvider = ((ContentViewer)viewer).getLabelProvider(); 563 if (labelProvider instanceof ILabelProvider) 564 { 565 if (object == null) 566 { 567 titleLabel.setImage(null); 568 titleLabel.setText(""); 569 } 570 else 571 { 572 titleLabel.setImage(((ILabelProvider)labelProvider).getImage(object)); 573 titleLabel.setText(((ILabelProvider)labelProvider).getText(object)); 574 } 575 } 576 } 577 } 578 } 579 580 public void setTitle(String title, Image image) 581 { 582 if (titleLabel != null) 583 { 584 titleLabel.setImage(image); 585 titleLabel.setText(title); 586 } 587 } 588 589 private void showTitleLabelMenu(MouseEvent e) 590 { 591 Menu menu = new Menu(titleLabel); 592 593 boolean isMaximized = 594 control.getParent() instanceof SashForm ? 595 ((SashForm)control.getParent()).getMaximizedControl() != null : 596 control.getParent() instanceof CTabFolder && ((CTabFolder)control.getParent()).getMaximized(); 597 598 MenuItem restoreItem = new MenuItem(menu, SWT.NONE); 599 restoreItem.setText(CommonUIPlugin.INSTANCE.getString("_UI_Restore_menu_item")); 600 restoreItem.addSelectionListener 601 (new SelectionAdapter() 602 { 603 public void widgetSelected(SelectionEvent selectionEvent) 604 { 605 doMaximize(); 606 } 607 }); 608 restoreItem.setEnabled(isMaximized); 609 610 MenuItem maximizeItem = new MenuItem(menu, SWT.NONE); 611 maximizeItem.setText(CommonUIPlugin.INSTANCE.getString("_UI_Maximize_menu_item")); 612 maximizeItem.addSelectionListener 613 (new SelectionAdapter() 614 { 615 public void widgetSelected(SelectionEvent selectionEvent) 616 { 617 doMaximize(); 618 } 619 }); 620 maximizeItem.setEnabled(!isMaximized); 621 622 Point point = new Point(e.x, e.y); 623 point = titleLabel.toDisplay(point); 624 menu.setLocation(point.x, point.y); 625 menu.setVisible(true); 626 } 627 } 628 629 630 635 class WorkbenchColors 636 { 637 static private boolean init = false; 638 static private HashMap colorMap; 639 static private Color [] activeGradient; 640 static private int [] activePercentages; 641 final static private String CLR_GRAD_START = "clrGradStart"; 642 final static private String CLR_GRAD_MID = "clrGradMid"; 643 final static private String CLR_GRAD_END = "clrGradEnd"; 644 645 648 static public Color [] getActiveGradient() { 649 return activeGradient; 650 } 651 654 static public Color getActiveGradientStart() { 655 Color clr = (Color)colorMap.get(CLR_GRAD_START); 656 return clr; 657 } 658 661 static public Color getActiveGradientEnd() { 662 Color clr = (Color)colorMap.get(CLR_GRAD_END); 663 return clr; 664 } 665 668 static public int [] getActiveGradientPercents() { 669 return activePercentages; 670 } 671 674 static public Color getColor(RGB rgbValue) { 675 Color clr = (Color)colorMap.get(rgbValue); 676 if (clr == null) { 677 Display disp = Display.getDefault(); 678 clr = new Color(disp, rgbValue); 679 colorMap.put(rgbValue, clr); 680 } 681 return clr; 682 } 683 686 static public Color getSystemColor(int swtId) { 687 Integer bigInt = new Integer (swtId); 688 Color clr = (Color)colorMap.get(bigInt); 689 if (clr == null) { 690 Display disp = Display.getDefault(); 691 clr = disp.getSystemColor(swtId); 692 colorMap.put(bigInt, clr); 693 } 694 return clr; 695 } 696 699 static public void shutdown() { 700 if (!init) 701 return; 702 703 Iterator iter = colorMap.values().iterator(); 704 while (iter.hasNext()) { 705 Color clr = (Color)iter.next(); 706 clr.dispose(); 707 } 708 colorMap.clear(); 709 gradient.dispose(); 710 } 711 712 715 static public void startup() { 716 if (init) 717 return; 718 719 init = true; 720 Display disp = Display.getDefault(); 721 colorMap = new HashMap (10); 722 723 734 735 743 744 Color clr1 = disp.getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 746 Color clr2 = disp.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT); 747 Color clr3 = disp.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 748 749 int r = clr1.getRGB().red + 2 * (clr3.getRGB().red - clr1.getRGB().red) / 3; 750 r = (clr3.getRGB().red > clr1.getRGB().red) ? Math.min(r, clr3.getRGB().red) : Math.max(r, clr3.getRGB().red); 751 int g = clr1.getRGB().green + 2 * (clr3.getRGB().green - clr1.getRGB().green) / 3; 752 g = (clr3.getRGB().green > clr1.getRGB().green) ? Math.min(g, clr3.getRGB().green) : Math.max(g, clr3.getRGB().green); 753 int b = clr1.getRGB().blue + 2 * (clr3.getRGB().blue - clr1.getRGB().blue) / 3; 754 b = (clr3.getRGB().blue > clr1.getRGB().blue) ? Math.min(b, clr3.getRGB().blue) : Math.max(b, clr3.getRGB().blue); 755 Color clr4 = new Color(disp, r, g, b); 756 757 colorMap.put(CLR_GRAD_START, clr4); 759 colorMap.put(CLR_GRAD_MID, clr2); 760 colorMap.put(CLR_GRAD_END, clr3); 761 762 activeGradient = new Color [] { clr4, clr3, clr3 }; 763 activePercentages = new int[] {25, 100}; 764 765 getSystemColor(SWT.COLOR_WIDGET_FOREGROUND); 767 getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 768 } 769 770 protected static Image gradient; 771 public static Image getGradient() 772 { 773 if (gradient == null) 774 { 775 int width = 20; 776 int height = 10; 777 778 Display display = Display.getDefault(); 779 780 gradient = new Image(display, width, height); 781 GC gc = new GC(gradient); 782 783 Color startColor = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 784 RGB rgb1 = startColor.getRGB(); 785 786 Color endColor = display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND); 787 RGB rgb2 = endColor.getRGB(); 788 789 for (int k = 0; k < width; k++) 790 { 791 int r = rgb1.red + k * (rgb2.red - rgb1.red) / width; 792 r = (rgb2.red > rgb1.red) ? Math.min(r, rgb2.red) : Math.max(r, rgb2.red); 793 int g = rgb1.green + k * (rgb2.green - rgb1.green) / width; 794 g = (rgb2.green > rgb1.green) ? Math.min(g, rgb2.green) : Math.max(g, rgb2.green); 795 int b = rgb1.blue + k * (rgb2.blue - rgb1.blue) / width; 796 b = (rgb2.blue > rgb1.blue) ? Math.min(b, rgb2.blue) : Math.max(b, rgb2.blue); 797 798 Color color = new Color(display, r, g, b); 799 800 gc.setBackground(color); 801 gc.fillRectangle(width - k - 1, 0, 1, height); 802 803 color.dispose(); 804 } 805 806 gc.dispose(); 807 } 808 809 return gradient; 810 } 811 } 812 | Popular Tags |