1 12 package org.eclipse.ui.forms.widgets; 13 14 import org.eclipse.jface.resource.JFaceResources; 15 import org.eclipse.jface.window.Window; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.custom.CCombo; 18 import org.eclipse.swt.custom.ScrolledComposite; 19 import org.eclipse.swt.events.FocusAdapter; 20 import org.eclipse.swt.events.FocusEvent; 21 import org.eclipse.swt.events.KeyAdapter; 22 import org.eclipse.swt.events.KeyEvent; 23 import org.eclipse.swt.events.MouseAdapter; 24 import org.eclipse.swt.events.MouseEvent; 25 import org.eclipse.swt.events.PaintEvent; 26 import org.eclipse.swt.events.PaintListener; 27 import org.eclipse.swt.graphics.Color; 28 import org.eclipse.swt.graphics.Font; 29 import org.eclipse.swt.graphics.GC; 30 import org.eclipse.swt.graphics.Point; 31 import org.eclipse.swt.graphics.RGB; 32 import org.eclipse.swt.graphics.Rectangle; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Display; 37 import org.eclipse.swt.widgets.Event; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Listener; 40 import org.eclipse.swt.widgets.Table; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.swt.widgets.Tree; 43 import org.eclipse.swt.widgets.Widget; 44 import org.eclipse.ui.forms.FormColors; 45 import org.eclipse.ui.forms.HyperlinkGroup; 46 import org.eclipse.ui.forms.IFormColors; 47 import org.eclipse.ui.internal.forms.widgets.FormUtil; 48 49 77 public class FormToolkit { 78 public static final String KEY_DRAW_BORDER = "FormWidgetFactory.drawBorder"; 80 public static final String TREE_BORDER = "treeBorder"; 82 public static final String TEXT_BORDER = "textBorder"; 84 private int borderStyle = SWT.NULL; 85 86 private FormColors colors; 87 88 private int orientation = Window.getDefaultOrientation(); 89 90 private BorderPainter borderPainter; 92 93 private BoldFontHolder boldFontHolder; 94 95 private HyperlinkGroup hyperlinkGroup; 96 97 98 VisibilityHandler visibilityHandler; 99 100 101 KeyboardHandler keyboardHandler; 102 103 private class BorderPainter implements PaintListener { 104 public void paintControl(PaintEvent event) { 105 Composite composite = (Composite) event.widget; 106 Control[] children = composite.getChildren(); 107 for (int i = 0; i < children.length; i++) { 108 Control c = children[i]; 109 boolean inactiveBorder = false; 110 boolean textBorder = false; 111 if (!c.isVisible()) 112 continue; 113 117 if (c instanceof Hyperlink) 118 continue; 119 Object flag = c.getData(KEY_DRAW_BORDER); 120 if (flag != null) { 121 if (flag.equals(Boolean.FALSE)) 122 continue; 123 if (flag.equals(TREE_BORDER)) 124 inactiveBorder = true; 125 else if (flag.equals(TEXT_BORDER)) 126 textBorder = true; 127 } 128 if (getBorderStyle() == SWT.BORDER) { 129 if (!inactiveBorder && !textBorder) { 130 continue; 131 } 132 if (c instanceof Text || c instanceof Table 133 || c instanceof Tree) 134 continue; 135 } 136 if (!inactiveBorder 137 && (c instanceof Text || c instanceof CCombo || textBorder)) { 138 Rectangle b = c.getBounds(); 139 GC gc = event.gc; 140 gc.setForeground(c.getBackground()); 141 gc.drawRectangle(b.x - 1, b.y - 1, b.width + 1, 142 b.height + 1); 143 gc.setForeground(colors.getBorderColor()); 146 if (c instanceof CCombo) 147 gc.drawRectangle(b.x - 1, b.y - 1, b.width + 1, 148 b.height + 1); 149 else 150 gc.drawRectangle(b.x - 1, b.y - 2, b.width + 1, 151 b.height + 3); 152 } else if (inactiveBorder || c instanceof Table 153 || c instanceof Tree) { 154 Rectangle b = c.getBounds(); 155 GC gc = event.gc; 156 gc.setForeground(colors.getBorderColor()); 157 gc.drawRectangle(b.x - 1, b.y - 1, b.width + 1, 158 b.height + 1); 159 } 160 } 161 } 162 } 163 164 private static class VisibilityHandler extends FocusAdapter { 165 public void focusGained(FocusEvent e) { 166 Widget w = e.widget; 167 if (w instanceof Control) { 168 FormUtil.ensureVisible((Control) w); 169 } 170 } 171 } 172 173 private static class KeyboardHandler extends KeyAdapter { 174 public void keyPressed(KeyEvent e) { 175 Widget w = e.widget; 176 if (w instanceof Control) { 177 if (e.doit) 178 FormUtil.processKey(e.keyCode, (Control) w); 179 } 180 } 181 } 182 183 private class BoldFontHolder { 184 private Font normalFont; 185 186 private Font boldFont; 187 188 public BoldFontHolder() { 189 } 190 191 public Font getBoldFont(Font font) { 192 createBoldFont(font); 193 return boldFont; 194 } 195 196 private void createBoldFont(Font font) { 197 if (normalFont == null || !normalFont.equals(font)) { 198 normalFont = font; 199 dispose(); 200 } 201 if (boldFont == null) { 202 boldFont = FormUtil.createBoldFont(colors.getDisplay(), 203 normalFont); 204 } 205 } 206 207 public void dispose() { 208 if (boldFont != null) { 209 boldFont.dispose(); 210 boldFont = null; 211 } 212 } 213 } 214 215 219 public FormToolkit(Display display) { 220 this(new FormColors(display)); 221 } 222 223 231 public FormToolkit(FormColors colors) { 232 this.colors = colors; 233 initialize(); 234 } 235 236 247 public Button createButton(Composite parent, String text, int style) { 248 Button button = new Button(parent, style | SWT.FLAT | orientation); 249 if (text != null) 250 button.setText(text); 251 adapt(button, true, true); 252 return button; 253 } 254 255 262 public Composite createComposite(Composite parent) { 263 return createComposite(parent, SWT.NULL); 264 } 265 266 275 public Composite createComposite(Composite parent, int style) { 276 Composite composite = new LayoutComposite(parent, style | orientation); 277 adapt(composite); 278 return composite; 279 } 280 281 290 public Composite createCompositeSeparator(Composite parent) { 291 final Composite composite = new Composite(parent, orientation); 292 composite.addListener(SWT.Paint, new Listener() { 293 public void handleEvent(Event e) { 294 if (composite.isDisposed()) 295 return; 296 Rectangle bounds = composite.getBounds(); 297 GC gc = e.gc; 298 gc.setForeground(colors.getColor(IFormColors.SEPARATOR)); 299 if (colors.getBackground() != null) 300 gc.setBackground(colors.getBackground()); 301 gc.fillGradientRectangle(0, 0, bounds.width, bounds.height, 302 false); 303 } 304 }); 305 if (parent instanceof Section) 306 ((Section) parent).setSeparatorControl(composite); 307 return composite; 308 } 309 310 319 public Label createLabel(Composite parent, String text) { 320 return createLabel(parent, text, SWT.NONE); 321 } 322 323 334 public Label createLabel(Composite parent, String text, int style) { 335 Label label = new Label(parent, style | orientation); 336 if (text != null) 337 label.setText(text); 338 adapt(label, false, false); 339 return label; 340 } 341 342 354 public Hyperlink createHyperlink(Composite parent, String text, int style) { 355 Hyperlink hyperlink = new Hyperlink(parent, style | orientation); 356 if (text != null) 357 hyperlink.setText(text); 358 hyperlink.addFocusListener(visibilityHandler); 359 hyperlink.addKeyListener(keyboardHandler); 360 hyperlinkGroup.add(hyperlink); 361 return hyperlink; 362 } 363 364 374 public ImageHyperlink createImageHyperlink(Composite parent, int style) { 375 ImageHyperlink hyperlink = new ImageHyperlink(parent, style 376 | orientation); 377 hyperlink.addFocusListener(visibilityHandler); 378 hyperlink.addKeyListener(keyboardHandler); 379 hyperlinkGroup.add(hyperlink); 380 return hyperlink; 381 } 382 383 394 public FormText createFormText(Composite parent, boolean trackFocus) { 395 FormText engine = new FormText(parent, SWT.WRAP | orientation); 396 engine.marginWidth = 1; 397 engine.marginHeight = 0; 398 engine.setHyperlinkSettings(getHyperlinkGroup()); 399 adapt(engine, trackFocus, true); 400 engine.setMenu(parent.getMenu()); 401 return engine; 402 } 403 404 425 public void adapt(Control control, boolean trackFocus, boolean trackKeyboard) { 426 control.setBackground(colors.getBackground()); 427 control.setForeground(colors.getForeground()); 428 if (control instanceof ExpandableComposite) { 429 ExpandableComposite ec = (ExpandableComposite) control; 430 if (ec.toggle != null) { 431 if (trackFocus) 432 ec.toggle.addFocusListener(visibilityHandler); 433 if (trackKeyboard) 434 ec.toggle.addKeyListener(keyboardHandler); 435 } 436 if (ec.textLabel != null) { 437 if (trackFocus) 438 ec.textLabel.addFocusListener(visibilityHandler); 439 if (trackKeyboard) 440 ec.textLabel.addKeyListener(keyboardHandler); 441 } 442 return; 443 } 444 if (trackFocus) 445 control.addFocusListener(visibilityHandler); 446 if (trackKeyboard) 447 control.addKeyListener(keyboardHandler); 448 } 449 450 456 public void adapt(Composite composite) { 457 composite.setBackground(colors.getBackground()); 458 composite.addMouseListener(new MouseAdapter() { 459 public void mouseDown(MouseEvent e) { 460 ((Control) e.widget).setFocus(); 461 } 462 }); 463 composite.setMenu(composite.getParent().getMenu()); 464 } 465 466 475 public static void ensureVisible(Control c) { 476 FormUtil.ensureVisible(c); 477 } 478 479 488 public Section createSection(Composite parent, int sectionStyle) { 489 Section section = new Section(parent, orientation, sectionStyle); 490 section.setMenu(parent.getMenu()); 491 adapt(section, true, true); 492 if (section.toggle != null) { 493 section.toggle.setHoverDecorationColor(colors 494 .getColor(IFormColors.TB_TOGGLE_HOVER)); 495 section.toggle.setDecorationColor(colors 496 .getColor(IFormColors.TB_TOGGLE)); 497 } 498 section.setFont(boldFontHolder.getBoldFont(parent.getFont())); 499 if ((sectionStyle & Section.TITLE_BAR) != 0 500 || (sectionStyle & Section.SHORT_TITLE_BAR) != 0) { 501 colors.initializeSectionToolBarColors(); 502 section.setTitleBarBackground(colors.getColor(IFormColors.TB_BG)); 503 section.setTitleBarBorderColor(colors 504 .getColor(IFormColors.TB_BORDER)); 505 section.setTitleBarForeground(colors 506 .getColor(IFormColors.TB_TOGGLE)); 507 } 508 return section; 509 } 510 511 520 public ExpandableComposite createExpandableComposite(Composite parent, 521 int expansionStyle) { 522 ExpandableComposite ec = new ExpandableComposite(parent, orientation, 523 expansionStyle); 524 ec.setMenu(parent.getMenu()); 525 adapt(ec, true, true); 526 ec.setFont(boldFontHolder.getBoldFont(ec.getFont())); 527 return ec; 528 } 529 530 539 public Label createSeparator(Composite parent, int style) { 540 Label label = new Label(parent, SWT.SEPARATOR | style | orientation); 541 label.setBackground(colors.getBackground()); 542 label.setForeground(colors.getBorderColor()); 543 return label; 544 } 545 546 555 public Table createTable(Composite parent, int style) { 556 Table table = new Table(parent, style | borderStyle | orientation); 557 adapt(table, false, false); 558 return table; 560 } 561 562 571 public Text createText(Composite parent, String value) { 572 return createText(parent, value, SWT.SINGLE); 573 } 574 575 586 public Text createText(Composite parent, String value, int style) { 587 Text text = new Text(parent, borderStyle | style | orientation); 588 if (value != null) 589 text.setText(value); 590 text.setForeground(colors.getForeground()); 591 text.setBackground(colors.getBackground()); 592 text.addFocusListener(visibilityHandler); 593 return text; 594 } 595 596 605 public Tree createTree(Composite parent, int style) { 606 Tree tree = new Tree(parent, borderStyle | style | orientation); 607 adapt(tree, false, false); 608 return tree; 610 } 611 612 622 public ScrolledForm createScrolledForm(Composite parent) { 623 ScrolledForm form = new ScrolledForm(parent, SWT.V_SCROLL 624 | SWT.H_SCROLL | orientation); 625 form.setExpandHorizontal(true); 626 form.setExpandVertical(true); 627 form.setBackground(colors.getBackground()); 628 form.setForeground(colors.getColor(IFormColors.TITLE)); 629 form.setFont(JFaceResources.getHeaderFont()); 630 return form; 631 } 632 633 643 public Form createForm(Composite parent) { 644 Form formContent = new Form(parent, orientation); 645 formContent.setBackground(colors.getBackground()); 646 formContent.setForeground(colors.getColor(IFormColors.TITLE)); 647 formContent.setFont(JFaceResources.getHeaderFont()); 648 return formContent; 649 } 650 651 660 661 public void decorateFormHeading(Form form) { 662 Color top = colors.getColor(IFormColors.H_GRADIENT_END); 663 Color bot = colors.getColor(IFormColors.H_GRADIENT_START); 664 form.setTextBackground(new Color[] { top, bot }, new int[] { 100 }, 665 true); 666 form.setHeadColor(IFormColors.H_BOTTOM_KEYLINE1, colors 667 .getColor(IFormColors.H_BOTTOM_KEYLINE1)); 668 form.setHeadColor(IFormColors.H_BOTTOM_KEYLINE2, colors 669 .getColor(IFormColors.H_BOTTOM_KEYLINE2)); 670 form.setHeadColor(IFormColors.H_HOVER_LIGHT, colors 671 .getColor(IFormColors.H_HOVER_LIGHT)); 672 form.setHeadColor(IFormColors.H_HOVER_FULL, colors 673 .getColor(IFormColors.H_HOVER_FULL)); 674 form.setHeadColor(IFormColors.TB_TOGGLE, colors 675 .getColor(IFormColors.TB_TOGGLE)); 676 form.setHeadColor(IFormColors.TB_TOGGLE_HOVER, colors 677 .getColor(IFormColors.TB_TOGGLE_HOVER)); 678 form.setSeparatorVisible(true); 679 } 680 681 690 public ScrolledPageBook createPageBook(Composite parent, int style) { 691 ScrolledPageBook book = new ScrolledPageBook(parent, style 692 | orientation); 693 adapt(book, true, true); 694 book.setMenu(parent.getMenu()); 695 return book; 696 } 697 698 701 public void dispose() { 702 if (colors.isShared() == false) { 703 colors.dispose(); 704 colors = null; 705 } 706 boldFontHolder.dispose(); 707 } 708 709 714 public HyperlinkGroup getHyperlinkGroup() { 715 return hyperlinkGroup; 716 } 717 718 726 public void setBackground(Color bg) { 727 hyperlinkGroup.setBackground(bg); 728 colors.setBackground(bg); 729 } 730 731 734 public void refreshHyperlinkColors() { 735 hyperlinkGroup.initializeDefaultForegrounds(colors.getDisplay()); 736 } 737 738 769 public void paintBordersFor(Composite parent) { 770 if (borderPainter == null) 773 borderPainter = new BorderPainter(); 774 parent.addPaintListener(borderPainter); 775 } 776 777 782 public FormColors getColors() { 783 return colors; 784 } 785 786 796 public int getBorderStyle() { 797 return borderStyle; 798 } 799 800 810 public int getBorderMargin() { 811 return getBorderStyle() == SWT.BORDER ? 0 : 2; 812 } 813 814 823 public void setBorderStyle(int style) { 824 this.borderStyle = style; 825 } 826 827 842 public static void setControlVisible(Control c, boolean verticalOnly) { 843 ScrolledComposite scomp = FormUtil.getScrolledComposite(c); 844 if (scomp == null) 845 return; 846 Point location = FormUtil.getControlLocation(scomp, c); 847 scomp.setOrigin(location); 848 } 849 850 private void initialize() { 851 initializeBorderStyle(); 852 hyperlinkGroup = new HyperlinkGroup(colors.getDisplay()); 853 hyperlinkGroup.setBackground(colors.getBackground()); 854 visibilityHandler = new VisibilityHandler(); 855 keyboardHandler = new KeyboardHandler(); 856 boldFontHolder = new BoldFontHolder(); 857 } 858 859 private void initializeBorderStyle() { 860 String osname = System.getProperty("os.name"); String osversion = System.getProperty("os.version"); if (osname.startsWith("Windows") && "5.1".compareTo(osversion) <= 0) { RGB rgb = colors.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 867 if (rgb.red != 212 && rgb.green != 208 && rgb.blue != 200) 868 borderStyle = SWT.BORDER; 869 } else if (osname.startsWith("Mac")) borderStyle = SWT.BORDER; 871 } 872 873 884 885 public int getOrientation() { 886 return orientation; 887 } 888 889 898 899 public void setOrientation(int orientation) { 900 this.orientation = orientation; 901 } 902 } 903 | Popular Tags |