KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > FormToolkit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Michael Williamson (eclipse-bugs@magnaworks.com) - patch (see Bugzilla #92545)
11  *******************************************************************************/

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 /**
50  * The toolkit is responsible for creating SWT controls adapted to work in
51  * Eclipse forms. In addition to changing their presentation properties (fonts,
52  * colors etc.), various listeners are attached to make them behave correctly in
53  * the form context.
54  * <p>
55  * In addition to being the control factory, the toolkit is also responsible for
56  * painting flat borders for select controls, managing hyperlink groups and
57  * control colors.
58  * <p>
59  * The toolkit creates some of the most common controls used to populate Eclipse
60  * forms. Controls that must be created using their constructors,
61  * <code>adapt()</code> method is available to change its properties in the
62  * same way as with the supported toolkit controls.
63  * <p>
64  * Typically, one toolkit object is created per workbench part (for example, an
65  * editor or a form wizard). The toolkit is disposed when the part is disposed.
66  * To conserve resources, it is possible to create one color object for the
67  * entire plug-in and share it between several toolkits. The plug-in is
68  * responsible for disposing the colors (disposing the toolkit that uses shared
69  * color object will not dispose the colors).
70  * <p>
71  * FormToolkit is normally instantiated, but can also be subclassed if some of
72  * the methods needs to be modified. In those cases, <code>super</code> must
73  * be called to preserve normal behaviour.
74  *
75  * @since 3.0
76  */

77 public class FormToolkit {
78     public static final String JavaDoc KEY_DRAW_BORDER = "FormWidgetFactory.drawBorder"; //$NON-NLS-1$
79

80     public static final String JavaDoc TREE_BORDER = "treeBorder"; //$NON-NLS-1$
81

82     public static final String JavaDoc TEXT_BORDER = "textBorder"; //$NON-NLS-1$
83

84     private int borderStyle = SWT.NULL;
85
86     private FormColors colors;
87
88     private int orientation = Window.getDefaultOrientation();
89
90     // private KeyListener deleteListener;
91
private BorderPainter borderPainter;
92
93     private BoldFontHolder boldFontHolder;
94
95     private HyperlinkGroup hyperlinkGroup;
96
97     /* default */
98     VisibilityHandler visibilityHandler;
99
100     /* default */
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                 /*
114                  * if (c.getEnabled() == false && !(c instanceof CCombo))
115                  * continue;
116                  */

117                 if (c instanceof Hyperlink)
118                     continue;
119                 Object JavaDoc 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(getBorderStyle() == SWT.BORDER ? colors
144
// .getBorderColor() : colors.getForeground());
145
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     /**
216      * Creates a toolkit that is self-sufficient (will manage its own colors).
217      *
218      */

219     public FormToolkit(Display display) {
220         this(new FormColors(display));
221     }
222
223     /**
224      * Creates a toolkit that will use the provided (shared) colors. The toolkit
225      * will dispose the colors if and only if they are <b>not</b> marked as
226      * shared via the <code>markShared()</code> method.
227      *
228      * @param colors
229      * the shared colors
230      */

231     public FormToolkit(FormColors colors) {
232         this.colors = colors;
233         initialize();
234     }
235
236     /**
237      * Creates a button as a part of the form.
238      *
239      * @param parent
240      * the button parent
241      * @param text
242      * an optional text for the button (can be <code>null</code>)
243      * @param style
244      * the button style (for example, <code>SWT.PUSH</code>)
245      * @return the button widget
246      */

247     public Button createButton(Composite parent, String JavaDoc 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     /**
256      * Creates the composite as a part of the form.
257      *
258      * @param parent
259      * the composite parent
260      * @return the composite widget
261      */

262     public Composite createComposite(Composite parent) {
263         return createComposite(parent, SWT.NULL);
264     }
265
266     /**
267      * Creates the composite as part of the form using the provided style.
268      *
269      * @param parent
270      * the composite parent
271      * @param style
272      * the composite style
273      * @return the composite widget
274      */

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     /**
282      * Creats the composite that can server as a separator between various parts
283      * of a form. Separator height should be controlled by setting the height
284      * hint on the layout data for the composite.
285      *
286      * @param parent
287      * the separator parent
288      * @return the separator widget
289      */

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     /**
311      * Creates a label as a part of the form.
312      *
313      * @param parent
314      * the label parent
315      * @param text
316      * the label text
317      * @return the label widget
318      */

319     public Label createLabel(Composite parent, String JavaDoc text) {
320         return createLabel(parent, text, SWT.NONE);
321     }
322
323     /**
324      * Creates a label as a part of the form.
325      *
326      * @param parent
327      * the label parent
328      * @param text
329      * the label text
330      * @param style
331      * the label style
332      * @return the label widget
333      */

334     public Label createLabel(Composite parent, String JavaDoc 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     /**
343      * Creates a hyperlink as a part of the form. The hyperlink will be added to
344      * the hyperlink group that belongs to this toolkit.
345      *
346      * @param parent
347      * the hyperlink parent
348      * @param text
349      * the text of the hyperlink
350      * @param style
351      * the hyperlink style
352      * @return the hyperlink widget
353      */

354     public Hyperlink createHyperlink(Composite parent, String JavaDoc 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     /**
365      * Creates an image hyperlink as a part of the form. The hyperlink will be
366      * added to the hyperlink group that belongs to this toolkit.
367      *
368      * @param parent
369      * the hyperlink parent
370      * @param style
371      * the hyperlink style
372      * @return the image hyperlink widget
373      */

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     /**
384      * Creates a rich text as a part of the form.
385      *
386      * @param parent
387      * the rich text parent
388      * @param trackFocus
389      * if <code>true</code>, the toolkit will monitor focus
390      * transfers to ensure that the hyperlink in focus is visible in
391      * the form.
392      * @return the rich text widget
393      */

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     /**
405      * Adapts a control to be used in a form that is associated with this
406      * toolkit. This involves adjusting colors and optionally adding handlers to
407      * ensure focus tracking and keyboard management.
408      *
409      * @param control
410      * a control to adapt
411      * @param trackFocus
412      * if <code>true</code>, form will be scrolled horizontally
413      * and/or vertically if needed to ensure that the control is
414      * visible when it gains focus. Set it to <code>false</code> if
415      * the control is not capable of gaining focus.
416      * @param trackKeyboard
417      * if <code>true</code>, the control that is capable of
418      * gaining focus will be tracked for certain keys that are
419      * important to the underlying form (for example, PageUp,
420      * PageDown, ScrollUp, ScrollDown etc.). Set it to
421      * <code>false</code> if the control is not capable of gaining
422      * focus or these particular key event are already used by the
423      * control.
424      */

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     /**
451      * Adapts a composite to be used in a form associated with this toolkit.
452      *
453      * @param composite
454      * the composite to adapt
455      */

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     /**
467      * A helper method that ensures the provided control is visible when
468      * ScrolledComposite is somewhere in the parent chain. If scroll bars are
469      * visible and the control is clipped, the client of the scrolled composite
470      * will be scrolled to reveal the control.
471      *
472      * @param c
473      * the control to reveal
474      */

475     public static void ensureVisible(Control c) {
476         FormUtil.ensureVisible(c);
477     }
478
479     /**
480      * Creates a section as a part of the form.
481      *
482      * @param parent
483      * the section parent
484      * @param sectionStyle
485      * the section style
486      * @return the section widget
487      */

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     /**
512      * Creates an expandable composite as a part of the form.
513      *
514      * @param parent
515      * the expandable composite parent
516      * @param expansionStyle
517      * the expandable composite style
518      * @return the expandable composite widget
519      */

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     /**
531      * Creates a separator label as a part of the form.
532      *
533      * @param parent
534      * the separator parent
535      * @param style
536      * the separator style
537      * @return the separator label
538      */

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     /**
547      * Creates a table as a part of the form.
548      *
549      * @param parent
550      * the table parent
551      * @param style
552      * the table style
553      * @return the table widget
554      */

555     public Table createTable(Composite parent, int style) {
556         Table table = new Table(parent, style | borderStyle | orientation);
557         adapt(table, false, false);
558         // hookDeleteListener(table);
559
return table;
560     }
561
562     /**
563      * Creates a text as a part of the form.
564      *
565      * @param parent
566      * the text parent
567      * @param value
568      * the text initial value
569      * @return the text widget
570      */

571     public Text createText(Composite parent, String JavaDoc value) {
572         return createText(parent, value, SWT.SINGLE);
573     }
574
575     /**
576      * Creates a text as a part of the form.
577      *
578      * @param parent
579      * the text parent
580      * @param value
581      * the text initial value
582      * @param style
583      * the text style
584      * @return the text widget
585      */

586     public Text createText(Composite parent, String JavaDoc 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     /**
597      * Creates a tree widget as a part of the form.
598      *
599      * @param parent
600      * the tree parent
601      * @param style
602      * the tree style
603      * @return the tree widget
604      */

605     public Tree createTree(Composite parent, int style) {
606         Tree tree = new Tree(parent, borderStyle | style | orientation);
607         adapt(tree, false, false);
608         // hookDeleteListener(tree);
609
return tree;
610     }
611
612     /**
613      * Creates a scrolled form widget in the provided parent. If you do not
614      * require scrolling because there is already a scrolled composite up the
615      * parent chain, use 'createForm' instead.
616      *
617      * @param parent
618      * the scrolled form parent
619      * @return the form that can scroll itself
620      * @see #createForm
621      */

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     /**
634      * Creates a form widget in the provided parent. Note that this widget does
635      * not scroll its content, so make sure there is a scrolled composite up the
636      * parent chain. If you require scrolling, use 'createScrolledForm' instead.
637      *
638      * @param parent
639      * the form parent
640      * @return the form that does not scroll
641      * @see #createScrolledForm
642      */

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     /**
652      * Takes advantage of the gradients and other capabilities to decorate the
653      * form heading using colors computed based on the current skin and
654      * operating system.
655      *
656      * @since 3.3
657      * @param form
658      * the form to decorate
659      */

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     /**
682      * Creates a scrolled page book widget as a part of the form.
683      *
684      * @param parent
685      * the page book parent
686      * @param style
687      * the text style
688      * @return the scrolled page book widget
689      */

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     /**
699      * Disposes the toolkit.
700      */

701     public void dispose() {
702         if (colors.isShared() == false) {
703             colors.dispose();
704             colors = null;
705         }
706         boldFontHolder.dispose();
707     }
708
709     /**
710      * Returns the hyperlink group that manages hyperlinks for this toolkit.
711      *
712      * @return the hyperlink group
713      */

714     public HyperlinkGroup getHyperlinkGroup() {
715         return hyperlinkGroup;
716     }
717
718     /**
719      * Sets the background color for the entire toolkit. The method delegates
720      * the call to the FormColors object and also updates the hyperlink group so
721      * that hyperlinks and other objects are in sync.
722      *
723      * @param bg
724      * the new background color
725      */

726     public void setBackground(Color bg) {
727         hyperlinkGroup.setBackground(bg);
728         colors.setBackground(bg);
729     }
730
731     /**
732      * Refreshes the hyperlink colors by loading from JFace settings.
733      */

734     public void refreshHyperlinkColors() {
735         hyperlinkGroup.initializeDefaultForegrounds(colors.getDisplay());
736     }
737
738     /**
739      * Paints flat borders for widgets created by this toolkit within the
740      * provided parent. Borders will not be painted if the global border style
741      * is SWT.BORDER (i.e. if native borders are used). Call this method during
742      * creation of a form composite to get the borders of its children painted.
743      * Care should be taken when selection layout margins. At least one pixel
744      * pargin width and height must be chosen to allow the toolkit to paint the
745      * border on the parent around the widgets.
746      * <p>
747      * Borders are painted for some controls that are selected by the toolkit by
748      * default. If a control needs a border but is not on its list, it is
749      * possible to force border in the following way:
750      *
751      * <pre>
752      *
753      *
754      *
755      * widget.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
756      *
757      * or
758      *
759      * widget.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
760      *
761      *
762      *
763      * </pre>
764      *
765      * @param parent
766      * the parent that owns the children for which the border needs
767      * to be painted.
768      */

769     public void paintBordersFor(Composite parent) {
770         // if (borderStyle == SWT.BORDER)
771
// return;
772
if (borderPainter == null)
773             borderPainter = new BorderPainter();
774         parent.addPaintListener(borderPainter);
775     }
776
777     /**
778      * Returns the colors used by this toolkit.
779      *
780      * @return the color object
781      */

782     public FormColors getColors() {
783         return colors;
784     }
785
786     /**
787      * Returns the border style used for various widgets created by this
788      * toolkit. The intent of the toolkit is to create controls with styles that
789      * yield a 'flat' appearance. On systems where the native borders are
790      * already flat, we set the style to SWT.BORDER and don't paint the borders
791      * ourselves. Otherwise, the style is set to SWT.NULL, and borders are
792      * painted by the toolkit.
793      *
794      * @return the global border style
795      */

796     public int getBorderStyle() {
797         return borderStyle;
798     }
799
800     /**
801      * Returns the margin required around the children whose border is being
802      * painted by the toolkit using {@link #paintBordersFor(Composite)}. Since
803      * the border is painted around the controls on the parent, a number of
804      * pixels needs to be reserved for this border. For windowing systems where
805      * the native border is used, this margin is 0.
806      *
807      * @return the margin in the parent when children have their border painted
808      * @since 3.3
809      */

810     public int getBorderMargin() {
811         return getBorderStyle() == SWT.BORDER ? 0 : 2;
812     }
813
814     /**
815      * Sets the border style to be used when creating widgets. The toolkit
816      * chooses the correct style based on the platform but this value can be
817      * changed using this method.
818      *
819      * @param style
820      * <code>SWT.BORDER</code> or <code>SWT.NULL</code>
821      * @see #getBorderStyle
822      */

823     public void setBorderStyle(int style) {
824         this.borderStyle = style;
825     }
826
827     /**
828      * A utility method that ensures that the control is visible in the scrolled
829      * composite. The prerequisite for this method is that the control has a
830      * class that extends ScrolledComposite somewhere in the parent chain. If
831      * the control is partially or fully clipped, the composite is scrolled to
832      * set by setting the origin to the control origin.
833      *
834      * @param c
835      * the control to make visible
836      * @param verticalOnly
837      * if <code>true</code>, the scrolled composite will be
838      * scrolled only vertically if needed. Otherwise, the scrolled
839      * composite origin will be set to the control origin.
840      * @since 3.1
841      */

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 JavaDoc osname = System.getProperty("os.name"); //$NON-NLS-1$
861
String JavaDoc osversion = System.getProperty("os.version"); //$NON-NLS-1$
862
if (osname.startsWith("Windows") && "5.1".compareTo(osversion) <= 0) { //$NON-NLS-1$ //$NON-NLS-2$
863
// Skinned widgets used on newer Windows (e.g. XP (5.1), Vista
864
// (6.0))
865
// Check for Windows Classic. If not used, set the style to BORDER
866
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")) //$NON-NLS-1$
870
borderStyle = SWT.BORDER;
871     }
872
873     /**
874      * Returns the orientation that all the widgets created by this toolkit will
875      * inherit, if set. Can be <code>SWT.NULL</code>,
876      * <code>SWT.LEFT_TO_RIGHT</code> and <code>SWT.RIGHT_TO_LEFT</code>.
877      *
878      * @return orientation style for this toolkit, or <code>SWT.NULL</code> if
879      * not set. The default orientation is inherited from the Window
880      * default orientation.
881      * @see org.eclipse.jface.window.Window#getDefaultOrientation()
882      * @since 3.1
883      */

884
885     public int getOrientation() {
886         return orientation;
887     }
888
889     /**
890      * Sets the orientation that all the widgets created by this toolkit will
891      * inherit. Can be <code>SWT.NULL</code>, <code>SWT.LEFT_TO_RIGHT</code>
892      * and <code>SWT.RIGHT_TO_LEFT</code>.
893      *
894      * @param orientation
895      * style for this toolkit.
896      * @since 3.1
897      */

898
899     public void setOrientation(int orientation) {
900         this.orientation = orientation;
901     }
902 }
903
Popular Tags