KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > SWTFactory


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  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Combo;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Group;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Text;
27 import org.eclipse.ui.dialogs.PreferencesUtil;
28 import org.eclipse.ui.forms.widgets.ExpandableComposite;
29
30 /**
31  * Factory class to create SWT resources.
32  * @since 3.3
33  */

34 public class SWTFactory {
35     
36     /**
37      * Returns a width hint for a button control.
38      */

39     public static int getButtonWidthHint(Button button) {
40         button.setFont(JFaceResources.getDialogFont());
41         PixelConverter converter= new PixelConverter(button);
42         int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
43         return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
44     }
45     
46     /**
47      * Sets width and height hint for the button control.
48      * <b>Note:</b> This is a NOP if the button's layout data is not
49      * an instance of <code>GridData</code>.
50      *
51      * @param the button for which to set the dimension hint
52      */

53     public static void setButtonDimensionHint(Button button) {
54         Object JavaDoc gd= button.getLayoutData();
55         if (gd instanceof GridData) {
56             ((GridData)gd).widthHint= getButtonWidthHint(button);
57             ((GridData)gd).horizontalAlignment = GridData.FILL;
58         }
59     }
60     
61     
62     /**
63      * Creates and returns a new push button with the given
64      * label and/or image.
65      *
66      * @param parent parent control
67      * @param label button label or <code>null</code>
68      * @param image image or <code>null</code>
69      *
70      * @return a new push button
71      */

72     public static Button createPushButton(Composite parent, String JavaDoc label, Image image) {
73         Button button = new Button(parent, SWT.PUSH);
74         button.setFont(parent.getFont());
75         if (image != null) {
76             button.setImage(image);
77         }
78         if (label != null) {
79             button.setText(label);
80         }
81         GridData gd = new GridData();
82         button.setLayoutData(gd);
83         SWTFactory.setButtonDimensionHint(button);
84         return button;
85     }
86
87     /**
88      * Creates and returns a new push button with the given
89      * label, tooltip and/or image.
90      *
91      * @param parent parent control
92      * @param label button label or <code>null</code>
93      * @param tooltip the tooltip text for the button or <code>null</code>
94      * @param image image of <code>null</code>
95      *
96      * @return a new push button
97      */

98     public static Button createPushButton(Composite parent, String JavaDoc label, String JavaDoc tooltip, Image image) {
99         Button button = createPushButton(parent, label, image);
100         button.setToolTipText(tooltip);
101         return button;
102     }
103     
104     /**
105      * Creates and returns a new radio button with the given
106      * label.
107      *
108      * @param parent parent control
109      * @param label button label or <code>null</code>
110      *
111      * @return a new radio button
112      */

113     public static Button createRadioButton(Composite parent, String JavaDoc label) {
114         Button button = new Button(parent, SWT.RADIO);
115         button.setFont(parent.getFont());
116         if (label != null) {
117             button.setText(label);
118         }
119         GridData gd = new GridData();
120         button.setLayoutData(gd);
121         SWTFactory.setButtonDimensionHint(button);
122         return button;
123     }
124     
125     /**
126      * Creates and returns a new radio button with the given
127      * label.
128      *
129      * @param parent parent control
130      * @param label button label or <code>null</code>
131      * @param hspan the number of columns to span in the parent composite
132      *
133      * @return a new radio button
134      */

135     public static Button createRadioButton(Composite parent, String JavaDoc label, int hspan) {
136         Button button = new Button(parent, SWT.RADIO);
137         button.setFont(parent.getFont());
138         if (label != null) {
139             button.setText(label);
140         }
141         GridData gd = new GridData(GridData.BEGINNING);
142         gd.horizontalSpan = hspan;
143         button.setLayoutData(gd);
144         SWTFactory.setButtonDimensionHint(button);
145         return button;
146     }
147     
148     /**
149      * Creates a check box button using the parents' font
150      * @param parent the parent to add the button to
151      * @param label the label for the button
152      * @param image the image for the button
153      * @param checked the initial checked state of the button
154      * @param hspan the horizontal span to take up in the parent composite
155      * @return a new checked button set to the initial checked state
156      */

157     public static Button createCheckButton(Composite parent, String JavaDoc label, Image image, boolean checked, int hspan) {
158         Button button = new Button(parent, SWT.CHECK);
159         button.setFont(parent.getFont());
160         button.setSelection(checked);
161         if(image != null) {
162             button.setImage(image);
163         }
164         if(label != null) {
165             button.setText(label);
166         }
167         GridData gd = new GridData();
168         gd.horizontalSpan = hspan;
169         button.setLayoutData(gd);
170         setButtonDimensionHint(button);
171         return button;
172     }
173     
174     /**
175      * Creates a new label widget
176      * @param parent the parent composite to add this label widget to
177      * @param text the text for the label
178      * @param hspan the horizontal span to take up in the parent composite
179      * @return the new label
180      */

181     public static Label createLabel(Composite parent, String JavaDoc text, Font font, int hspan) {
182         Label l = new Label(parent, SWT.NONE);
183         l.setFont(font);
184         l.setText(text);
185         GridData gd = new GridData();
186         gd.horizontalSpan = hspan;
187         l.setLayoutData(gd);
188         return l;
189     }
190     
191     /**
192      * Creates a new label widget
193      * @param parent the parent composite to add this label widget to
194      * @param text the text for the label
195      * @param hspan the horizontal span to take up in the parent composite
196      * @return the new label
197      */

198     public static Label createLabel(Composite parent, String JavaDoc text, int hspan) {
199         Label l = new Label(parent, SWT.NONE);
200         l.setFont(parent.getFont());
201         l.setText(text);
202         GridData gd = new GridData();
203         gd.horizontalSpan = hspan;
204         l.setLayoutData(gd);
205         return l;
206     }
207     
208     /**
209      * Creates a wrapping label
210      * @param parent the parent composite to add this label to
211      * @param text the text to be displayed in the label
212      * @param hspan the horizontal span that label should take up in the parent composite
213      * @param wrapwidth the width hint that the label should wrap at
214      * @return a new label that wraps at a specified width
215      */

216     public static Label createWrapLabel(Composite parent, String JavaDoc text, int hspan, int wrapwidth) {
217         Label l = new Label(parent, SWT.WRAP);
218         l.setFont(parent.getFont());
219         l.setText(text);
220         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
221         gd.horizontalSpan = hspan;
222         gd.widthHint = wrapwidth;
223         l.setLayoutData(gd);
224         return l;
225     }
226     
227     /**
228      * Creates a new text widget
229      * @param parent the parent composite to add this text widget to
230      * @param hspan the horizontal span to take up on the parent composite
231      * @return the new text widget
232      */

233     public static Text createSingleText(Composite parent, int hspan) {
234         Text t = new Text(parent, SWT.SINGLE | SWT.BORDER);
235         t.setFont(parent.getFont());
236         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
237         gd.horizontalSpan = hspan;
238         t.setLayoutData(gd);
239         return t;
240     }
241     
242     /**
243      * Creates a new text widget
244      * @param parent the parent composite to add this text widget to
245      * @param style the style bits for the text widget
246      * @param hspan the horizontal span to take up on the parent composite
247      * @param text the initial text, not <code>null</code>
248      * @return the new text widget
249      */

250     public static Text createText(Composite parent, int style, int hspan, String JavaDoc text) {
251         Text t = new Text(parent, style);
252         t.setFont(parent.getFont());
253         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
254         gd.horizontalSpan = hspan;
255         t.setLayoutData(gd);
256         t.setText(text);
257         return t;
258     }
259     
260     /**
261      * Creates a Group widget
262      * @param parent the parent composite to add this group to
263      * @param text the text for the heading of the group
264      * @param columns the number of columns within the group
265      * @param hspan the horizontal span the group should take up on the parent
266      * @param fill the style for how this composite should fill into its parent
267      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
268      * @return the new group
269      */

270     public static Group createGroup(Composite parent, String JavaDoc text, int columns, int hspan, int fill) {
271         Group g = new Group(parent, SWT.NONE);
272         g.setLayout(new GridLayout(columns, false));
273         g.setText(text);
274         g.setFont(parent.getFont());
275         GridData gd = new GridData(fill);
276         gd.horizontalSpan = hspan;
277         g.setLayoutData(gd);
278         return g;
279     }
280     
281     /**
282      * This method allows us to open the preference dialog on the specific page
283      * @param id the id of pref page to show
284      */

285     public static void showPreferencePage(String JavaDoc id) {
286         PreferencesUtil.createPreferenceDialogOn(JDIDebugUIPlugin.getShell(), id, new String JavaDoc[] { id }, null).open();
287     }
288     
289     /**
290      * This method allows users to open a specific preference page and supply a custom
291      * set of page filter items.
292      *
293      * This alternative to <code>showPreferencePage(String)</code> allows other related
294      * pref pages to be shown at the same time at the developers/context discretion.
295      * All pages can be shown if <code>null</code> is passed.
296      *
297      * @param page_id the id for the page to open
298      * @param page_filters the listing of pages to be shown in the dialog
299      * @since 3.3
300      */

301     public static void showPreferencePage(String JavaDoc page_id, String JavaDoc[] page_filters) {
302         PreferencesUtil.createPreferenceDialogOn(JDIDebugUIPlugin.getShell(), page_id, page_filters, null).open();
303     }
304     
305     /**
306      * Creates a Composite widget
307      * @param parent the parent composite to add this composite to
308      * @param columns the number of columns within the composite
309      * @param hspan the horizontal span the composite should take up on the parent
310      * @param fill the style for how this composite should fill into its parent
311      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
312      * @return the new group
313      */

314     public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill) {
315         Composite g = new Composite(parent, SWT.NONE);
316         g.setLayout(new GridLayout(columns, false));
317         g.setFont(font);
318         GridData gd = new GridData(fill);
319         gd.horizontalSpan = hspan;
320         g.setLayoutData(gd);
321         return g;
322     }
323     
324     /**
325      * Creates a Composite widget
326      * @param parent the parent composite to add this composite to
327      * @param columns the number of columns within the composite
328      * @param hspan the horizontal span the composite should take up on the parent
329      * @param fill the style for how this composite should fill into its parent
330      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
331      * @param marginwidth the width of the margin to place around the composite (default is 5, specified by GridLayout)
332      * @param marginheight the height of the margin to place around the composite (default is 5, specified by GridLayout)
333      * @return the new group
334      */

335     public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill, int marginwidth, int marginheight) {
336         Composite g = new Composite(parent, SWT.NONE);
337         GridLayout layout = new GridLayout(columns, false);
338         layout.marginWidth = marginwidth;
339         layout.marginHeight = marginheight;
340         g.setLayout(layout);
341         g.setFont(font);
342         GridData gd = new GridData(fill);
343         gd.horizontalSpan = hspan;
344         g.setLayoutData(gd);
345         return g;
346     }
347     
348     /**
349      * creates a vertical spacer for separating components
350      * @param comp
351      * @param numlines
352      */

353     public static void createVerticalSpacer(Composite comp, int numlines) {
354         Label lbl = new Label(comp, SWT.NONE);
355         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
356         gd.heightHint = numlines;
357         lbl.setLayoutData(gd);
358     }
359     
360     /**
361      * creates a horizontal spacer for separating components
362      * @param comp
363      * @param numlines
364      */

365     public static void createHorizontalSpacer(Composite comp, int numlines) {
366         Label lbl = new Label(comp, SWT.NONE);
367         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
368         gd.horizontalSpan = numlines;
369         lbl.setLayoutData(gd);
370     }
371     
372     /**
373      * This method is used to make a combo box
374      * @param parent the parent composite to add the new combo to
375      * @param style the style for the Combo
376      * @param hspan the horizontal span to take up on the parent composite
377      * @param fill how the combo will fill into the composite
378      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
379      * @param items the item to put into the combo
380      * @return a new Combo instance
381      */

382     public static Combo createCombo(Composite parent, int style, int hspan, int fill, String JavaDoc[] items) {
383         Combo c = new Combo(parent, style);
384         c.setFont(parent.getFont());
385         GridData gd = new GridData(fill);
386         gd.horizontalSpan = hspan;
387         c.setLayoutData(gd);
388         c.setItems(items);
389         c.select(0);
390         return c;
391     }
392     
393     /**
394      * This method is used to make a combo box with a default fill style of GridData.FILL_HORIZONTAL
395      * @param parent the parent composite to add the new combo to
396      * @param style the style for the Combo
397      * @param hspan the horizontal span to take up on the parent composite
398      * @param items the item to put into the combo
399      * @return a new Combo instance
400      */

401     public static Combo createCombo(Composite parent, int style, int hspan, String JavaDoc[] items) {
402         Combo c = new Combo(parent, style);
403         c.setFont(parent.getFont());
404         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
405         gd.horizontalSpan = hspan;
406         c.setLayoutData(gd);
407         c.setItems(items);
408         c.select(0);
409         return c;
410     }
411     
412     /**
413      * Creates an ExpandibleComposite widget
414      * @param parent the parent to add this widget to
415      * @param style the style for ExpandibleComposite expanding handle, and layout
416      * @param label the label for the widget
417      * @param hspan how many columns to span in the parent
418      * @param fill the fill style for the widget
419      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
420      * @return a new ExpandibleComposite widget
421      */

422     public static ExpandableComposite createExpandibleComposite(Composite parent, int style, String JavaDoc label, int hspan, int fill) {
423         ExpandableComposite ex = new ExpandableComposite(parent, SWT.NONE, style);
424         ex.setText(label);
425         ex.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT));
426         GridData gd = new GridData(fill);
427         gd.horizontalSpan = hspan;
428         ex.setLayoutData(gd);
429         return ex;
430     }
431     
432 }
433
Popular Tags