KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.preference.IPreferenceNode;
16 import org.eclipse.jface.preference.IPreferencePage;
17 import org.eclipse.jface.preference.PreferenceDialog;
18 import org.eclipse.jface.preference.PreferenceManager;
19 import org.eclipse.jface.preference.PreferenceNode;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.util.Assert;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.BusyIndicator;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Text;
32
33 /**
34  * Utility class to simplify access to some SWT resources.
35  */

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

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

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

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

99     public static Button createRadioButton(Composite parent, String JavaDoc label) {
100         Button button = new Button(parent, SWT.RADIO);
101         button.setFont(parent.getFont());
102         if (label != null) {
103             button.setText(label);
104         }
105         GridData gd = new GridData();
106         button.setLayoutData(gd);
107         SWTUtil.setButtonDimensionHint(button);
108         return button;
109     }
110     
111     /**
112      * Creates a new label widget
113      * @param parent the parent composite to add this label widget to
114      * @param text the text for the label
115      * @param hspan the horizontal span to take up in the parent composite
116      * @return the new label
117      * @since 3.2
118      *
119      */

120     public static Label createLabel(Composite parent, String JavaDoc text, int hspan) {
121         Label l = new Label(parent, SWT.NONE);
122         l.setFont(parent.getFont());
123         l.setText(text);
124         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
125         gd.horizontalSpan = hspan;
126         l.setLayoutData(gd);
127         return l;
128     }
129     
130     /**
131      * Creates a new text widget
132      * @param parent the parent composite to add this text widget to
133      * @param hspan the horizontal span to take up on the parent composite
134      * @return the new text widget
135      * @since 3.2
136      *
137      */

138     public static Text createSingleText(Composite parent, int hspan) {
139         Text t = new Text(parent, SWT.SINGLE | SWT.BORDER);
140         t.setFont(parent.getFont());
141         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
142         gd.horizontalSpan = hspan;
143         t.setLayoutData(gd);
144         return t;
145     }
146     
147     /**
148      * Creates a Group widget
149      * @param parent the parent composite to add this group to
150      * @param text the text for the heading of the group
151      * @param columns the number of columns within the group
152      * @param hspan the horizontal span the group should take up on the parent
153      * @param fill the style for how this composite should fill into its parent
154      * @return the new group
155      * @since 3.2
156      *
157      */

158     public static Group createGroup(Composite parent, String JavaDoc text, int columns, int hspan, int fill) {
159         Group g = new Group(parent, SWT.NONE);
160         g.setLayout(new GridLayout(columns, false));
161         g.setText(text);
162         g.setFont(parent.getFont());
163         GridData gd = new GridData(fill);
164         gd.horizontalSpan = hspan;
165         g.setLayoutData(gd);
166         return g;
167     }
168     
169     /**
170      * This method allows us to open the preference dialog on the specific page, in this case the perspective page
171      * @param id the id of pref page to show
172      * @param page the actual page to show
173      * @since 3.2
174      */

175     public static void showPreferencePage(String JavaDoc id, IPreferencePage page) {
176         final IPreferenceNode targetNode = new PreferenceNode(id, page);
177         PreferenceManager manager = new PreferenceManager();
178         manager.addToRoot(targetNode);
179         final PreferenceDialog dialog = new PreferenceDialog(DebugUIPlugin.getShell(), manager);
180         BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), new Runnable JavaDoc() {
181             public void run() {
182                 dialog.create();
183                 dialog.setMessage(targetNode.getLabelText());
184                 dialog.open();
185             }
186         });
187     }
188 }
189
Popular Tags