KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > 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.debug.internal.ui;
12
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Group;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Layout;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.dialogs.PreferencesUtil;
31
32 /**
33  * Factory class to create some SWT resources.
34  */

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

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

54     public static void setButtonDimensionHint(Button button) {
55         Assert.isNotNull(button);
56         Object JavaDoc gd= button.getLayoutData();
57         if (gd instanceof GridData) {
58             ((GridData)gd).widthHint= getButtonWidthHint(button);
59             ((GridData)gd).horizontalAlignment = GridData.FILL;
60         }
61     }
62     
63     /**
64      * Creates a check box button using the parents' font
65      * @param parent the parent to add the button to
66      * @param label the label for the button
67      * @param image the image for the button
68      * @param checked the initial checked state of the button
69      * @param hspan the horizontal span to take up in the parent composite
70      * @return a new checked button set to the initial checked state
71      * @since 3.3
72      */

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

100     public static Button createPushButton(Composite parent, String JavaDoc label, Image image) {
101         Button button = new Button(parent, SWT.PUSH);
102         button.setFont(parent.getFont());
103         if (image != null) {
104             button.setImage(image);
105         }
106         if (label != null) {
107             button.setText(label);
108         }
109         GridData gd = new GridData();
110         button.setLayoutData(gd);
111         SWTFactory.setButtonDimensionHint(button);
112         return button;
113     }
114
115     /**
116      * Creates and returns a new radio button with the given
117      * label.
118      *
119      * @param parent parent control
120      * @param label button label or <code>null</code>
121      *
122      * @return a new radio button
123      */

124     public static Button createRadioButton(Composite parent, String JavaDoc label) {
125         Button button = new Button(parent, SWT.RADIO);
126         button.setFont(parent.getFont());
127         if (label != null) {
128             button.setText(label);
129         }
130         GridData gd = new GridData();
131         button.setLayoutData(gd);
132         SWTFactory.setButtonDimensionHint(button);
133         return button;
134     }
135     
136     /**
137      * Creates a new label widget
138      * @param parent the parent composite to add this label widget to
139      * @param text the text for the label
140      * @param hspan the horizontal span to take up in the parent composite
141      * @return the new label
142      * @since 3.2
143      *
144      */

145     public static Label createLabel(Composite parent, String JavaDoc text, int hspan) {
146         Label l = new Label(parent, SWT.NONE);
147         l.setFont(parent.getFont());
148         l.setText(text);
149         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
150         gd.horizontalSpan = hspan;
151         gd.grabExcessHorizontalSpace = false;
152         l.setLayoutData(gd);
153         return l;
154     }
155     
156     /**
157      * Creates a new label widget
158      * @param parent the parent composite to add this label widget to
159      * @param text the text for the label
160      * @param font the font for the label
161      * @param hspan the horizontal span to take up in the parent composite
162      * @return the new label
163      * @since 3.3
164      */

165     public static Label createLabel(Composite parent, String JavaDoc text, Font font, int hspan) {
166         Label l = new Label(parent, SWT.NONE);
167         l.setFont(font);
168         l.setText(text);
169         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
170         gd.horizontalSpan = hspan;
171         l.setLayoutData(gd);
172         return l;
173     }
174     
175     /**
176      * Creates a wrapping label
177      * @param parent the parent composite to add this label to
178      * @param text the text to be displayed in the label
179      * @param hspan the horizontal span that label should take up in the parent composite
180      * @param wrapwidth the width hint that the label should wrap at
181      * @return a new label that wraps at a specified width
182      * @since 3.3
183      */

184     public static Label createWrapLabel(Composite parent, String JavaDoc text, int hspan, int wrapwidth) {
185         Label l = new Label(parent, SWT.NONE | SWT.WRAP);
186         l.setFont(parent.getFont());
187         l.setText(text);
188         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
189         gd.horizontalSpan = hspan;
190         gd.widthHint = wrapwidth;
191         l.setLayoutData(gd);
192         return l;
193     }
194     
195     /**
196      * Creates a new <code>CLabel</code> that will wrap at the specified width and has the specified image
197      * @param parent the parent to add this label to
198      * @param text the text for the label
199      * @param image the image for the label
200      * @param hspan the h span to take up in the parent
201      * @param wrapwidth the with to wrap at
202      * @return a new <code>CLabel</code>
203      * @since 3.3
204      */

205     public static CLabel createWrapCLabel(Composite parent, String JavaDoc text, Image image, int hspan, int wrapwidth) {
206         CLabel label = new CLabel(parent, SWT.NONE | SWT.WRAP);
207         label.setFont(parent.getFont());
208         if(text != null) {
209             label.setText(text);
210         }
211         if(image != null) {
212             label.setImage(image);
213         }
214         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
215         gd.horizontalSpan = hspan;
216         gd.widthHint = wrapwidth;
217         label.setLayoutData(gd);
218         return label;
219     }
220     
221     /**
222      * Creates a wrapping label
223      * @param parent the parent composite to add this label to
224      * @param text the text to be displayed in the label
225      * @param hspan the horizontal span that label should take up in the parent composite
226      * @return a new label that wraps at a specified width
227      * @since 3.3
228      */

229     public static Label createWrapLabel(Composite parent, String JavaDoc text, int hspan) {
230         Label l = new Label(parent, SWT.NONE | SWT.WRAP);
231         l.setFont(parent.getFont());
232         l.setText(text);
233         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
234         gd.horizontalSpan = hspan;
235         l.setLayoutData(gd);
236         return l;
237     }
238     
239     /**
240      * Creates a new text widget
241      * @param parent the parent composite to add this text widget to
242      * @param hspan the horizontal span to take up on the parent composite
243      * @return the new text widget
244      * @since 3.2
245      *
246      */

247     public static Text createSingleText(Composite parent, int hspan) {
248         Text t = new Text(parent, SWT.SINGLE | SWT.BORDER);
249         t.setFont(parent.getFont());
250         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
251         gd.horizontalSpan = hspan;
252         t.setLayoutData(gd);
253         return t;
254     }
255     
256     /**
257      * Creates a new text widget
258      * @param parent the parent composite to add this text widget to
259      * @param style the style bits for the text widget
260      * @param hspan the horizontal span to take up on the parent composite
261      * @param fill the fill for the grid layout
262      * @return the new text widget
263      * @since 3.3
264      */

265     public static Text createText(Composite parent, int style, int hspan, int fill) {
266         Text t = new Text(parent, style);
267         t.setFont(parent.getFont());
268         GridData gd = new GridData(fill);
269         gd.horizontalSpan = hspan;
270         t.setLayoutData(gd);
271         return t;
272     }
273     
274     /**
275      * Creates a new text widget
276      * @param parent the parent composite to add this text widget to
277      * @param style the style bits for the text widget
278      * @param hspan the horizontal span to take up on the parent composite
279      * @return the new text widget
280      * @since 3.3
281      */

282     public static Text createText(Composite parent, int style, int hspan) {
283         Text t = new Text(parent, style);
284         t.setFont(parent.getFont());
285         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
286         gd.horizontalSpan = hspan;
287         t.setLayoutData(gd);
288         return t;
289     }
290     
291     /**
292      * Creates a new text widget
293      * @param parent the parent composite to add this text widget to
294      * @param style the style bits for the text widget
295      * @param hspan the horizontal span to take up on the parent composite
296      * @param width the desired width of the text widget
297      * @param height the desired height of the text widget
298      * @param fill the fill style for the widget
299      * @return the new text widget
300      * @since 3.3
301      */

302     public static Text createText(Composite parent, int style, int hspan, int width, int height, int fill) {
303         Text t = new Text(parent, style);
304         t.setFont(parent.getFont());
305         GridData gd = new GridData(fill);
306         gd.horizontalSpan = hspan;
307         gd.widthHint = width;
308         gd.heightHint = height;
309         t.setLayoutData(gd);
310         return t;
311     }
312     
313     /**
314      * Creates a Group widget
315      * @param parent the parent composite to add this group to
316      * @param text the text for the heading of the group
317      * @param columns the number of columns within the group
318      * @param hspan the horizontal span the group should take up on the parent
319      * @param fill the style for how this composite should fill into its parent
320      * @return the new group
321      * @since 3.2
322      *
323      */

324     public static Group createGroup(Composite parent, String JavaDoc text, int columns, int hspan, int fill) {
325         Group g = new Group(parent, SWT.NONE);
326         g.setLayout(new GridLayout(columns, false));
327         g.setText(text);
328         g.setFont(parent.getFont());
329         GridData gd = new GridData(fill);
330         gd.horizontalSpan = hspan;
331         g.setLayoutData(gd);
332         return g;
333     }
334     
335     /**
336      * Creates a Composite widget
337      * @param parent the parent composite to add this composite to
338      * @param columns the number of columns within the composite
339      * @param hspan the horizontal span the composite should take up on the parent
340      * @param fill the style for how this composite should fill into its parent
341      * @return the new group
342      * @since 3.3
343      */

344     public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill) {
345         Composite g = new Composite(parent, SWT.NONE);
346         g.setLayout(new GridLayout(columns, false));
347         g.setFont(font);
348         GridData gd = new GridData(fill);
349         gd.horizontalSpan = hspan;
350         g.setLayoutData(gd);
351         return g;
352     }
353     
354     /**
355      * Creates a composite that uses the parents' font and has a grid layout
356      * @param parent the parent to add the composite to
357      * @param columns the number of columns the composite should have
358      * @param hspan the horizontal span the new composite should take up in the parent
359      * @param fill the fill style of the composite {@link GridData}
360      * @return a new composite with a grid layout
361      *
362      * @since 3.3
363      */

364     public static Composite createComposite(Composite parent, int columns, int hspan, int fill) {
365         Composite g = new Composite(parent, SWT.NONE);
366         g.setLayout(new GridLayout(columns, false));
367         g.setFont(parent.getFont());
368         GridData gd = new GridData(fill);
369         gd.horizontalSpan = hspan;
370         g.setLayoutData(gd);
371         return g;
372     }
373     
374     /**
375      * Creates a vertical spacer for separating components. If applied to a
376      * <code>GridLayout</code>, this method will automatically span all of the columns of the parent
377      * to make vertical space
378      *
379      * @param parent the parent composite to add this spacer to
380      * @param numlines the number of vertical lines to make as space
381      * @since 3.3
382      */

383     public static void createVerticalSpacer(Composite parent, int numlines) {
384         Label lbl = new Label(parent, SWT.NONE);
385         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
386         Layout layout = parent.getLayout();
387         if(layout instanceof GridLayout) {
388             gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
389         }
390         gd.heightHint = numlines;
391         lbl.setLayoutData(gd);
392     }
393     
394     /**
395      * creates a horizontal spacer for separating components
396      * @param comp
397      * @param numlines
398      * @since 3.3
399      */

400     public static void createHorizontalSpacer(Composite comp, int numlines) {
401         Label lbl = new Label(comp, SWT.NONE);
402         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
403         gd.horizontalSpan = numlines;
404         lbl.setLayoutData(gd);
405     }
406     
407     /**
408      * Creates a Composite widget
409      * @param parent the parent composite to add this composite to
410      * @param columns the number of columns within the composite
411      * @param hspan the horizontal span the composite should take up on the parent
412      * @param fill the style for how this composite should fill into its parent
413      * @param marginwidth the width of the margin to place around the composite (default is 5, specified by GridLayout)
414      * @return the new group
415      * @since 3.3
416      */

417     public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill, int marginwidth, int marginheight) {
418         Composite g = new Composite(parent, SWT.NONE);
419         GridLayout layout = new GridLayout(columns, false);
420         layout.marginWidth = marginwidth;
421         layout.marginHeight = marginheight;
422         g.setLayout(layout);
423         g.setFont(font);
424         GridData gd = new GridData(fill);
425         gd.horizontalSpan = hspan;
426         g.setLayoutData(gd);
427         return g;
428     }
429     
430     /**
431      * This method is used to make a combo box
432      * @param parent the parent composite to add the new combo to
433      * @param style the style for the Combo
434      * @param hspan the horizontal span to take up on the parent composite
435      * @param fill how the combo will fill into the composite
436      * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code>
437      * @param items the item to put into the combo
438      * @return a new Combo instance
439      * @since 3.3
440      */

441     public static Combo createCombo(Composite parent, int style, int hspan, int fill, String JavaDoc[] items) {
442         Combo c = new Combo(parent, style);
443         c.setFont(parent.getFont());
444         GridData gd = new GridData(fill);
445         gd.horizontalSpan = hspan;
446         c.setLayoutData(gd);
447         c.setItems(items);
448         c.select(0);
449         return c;
450     }
451     
452     /**
453      * This method is used to make a combo box with a default fill style of GridData.FILL_HORIZONTAL
454      * @param parent the parent composite to add the new combo to
455      * @param style the style for the Combo
456      * @param hspan the horizontal span to take up on the parent composite
457      * @param items the item to put into the combo
458      * @return a new Combo instance
459      * @since 3.3
460      */

461     public static Combo createCombo(Composite parent, int style, int hspan, String JavaDoc[] items) {
462         Combo c = new Combo(parent, style);
463         c.setFont(parent.getFont());
464         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
465         gd.horizontalSpan = hspan;
466         c.setLayoutData(gd);
467         c.setItems(items);
468         c.select(0);
469         return c;
470     }
471     
472     /**
473      * This method allows us to open the preference dialog on the specific page, in this case the perspective page
474      * @param id the id of preference page to show
475      * @param page the actual page to show
476      * @since 3.2
477      */

478     public static void showPreferencePage(String JavaDoc id) {
479         PreferencesUtil.createPreferenceDialogOn(DebugUIPlugin.getShell(), id, new String JavaDoc[] {id}, null).open();
480     }
481 }
482
Popular Tags