KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > BaseWidgetUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets;
22
23
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.FontMetrics;
29 import org.eclipse.swt.graphics.GC;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Combo;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Group;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Link;
38 import org.eclipse.swt.widgets.Text;
39
40
41 /**
42  * This class provides utility methods to create SWT widgets.
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class BaseWidgetUtils
48 {
49
50     /**
51      * Creates a SWT {@link Group} under the given parent.
52      *
53      * @param parent the parent
54      * @param label the label of the group
55      * @param span the horizontal span
56      * @return the created group
57      */

58     public static Group createGroup( Composite parent, String JavaDoc label, int span )
59     {
60         Group group = new Group( parent, SWT.NONE );
61         GridData gd = new GridData( GridData.FILL_BOTH );
62         gd.horizontalSpan = span;
63         group.setLayoutData( gd );
64         group.setText( label );
65         group.setLayout( new GridLayout() );
66         return group;
67     }
68
69
70     /**
71      * Creates a SWT {@link Composite} under the given parent.
72      * A GridLayout with the given number of columns is used.
73      *
74      * @param parent the parent
75      * @param columnCount the number of columns
76      * @param span the horizontal span
77      * @return the created composite
78      */

79     public static Composite createColumnContainer( Composite parent, int columnCount, int span )
80     {
81         Composite container = new Composite( parent, SWT.NONE );
82         GridLayout gl = new GridLayout( columnCount, false );
83         gl.marginHeight = gl.marginWidth = 0;
84         container.setLayout( gl );
85         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
86         gd.horizontalSpan = span;
87         container.setLayoutData( gd );
88         return container;
89     }
90
91
92     /**
93      * Creates a SWT {@link Label} under the given parent.
94      *
95      * @param parent the parent
96      * @param text the label's text
97      * @param span the horizontal span
98      * @return the created label
99      */

100     public static Label createLabel( Composite parent, String JavaDoc text, int span )
101     {
102         Label l = new Label( parent, SWT.NONE );
103         GridData gd = new GridData();
104         gd.horizontalSpan = span;
105         // gd.verticalAlignment = SWT.BEGINNING;
106
l.setLayoutData( gd );
107         l.setText( text );
108         return l;
109     }
110
111
112     /**
113      * Creates a SWT {@link Label} under the given parent.
114      * The label is created with the SWT.WRAP style to enable line wrapping.
115      *
116      * @param parent the parent
117      * @param text the label's text
118      * @param span the horizontal span
119      * @return the created label
120      */

121     public static Label createWrappedLabel( Composite parent, String JavaDoc text, int span )
122     {
123         Label l = new Label( parent, SWT.WRAP );
124         GridData gd = new GridData();
125         gd.horizontalSpan = span;
126         // gd.verticalAlignment = SWT.BEGINNING;
127
l.setLayoutData( gd );
128         l.setText( text );
129         return l;
130     }
131
132
133     /**
134      * Creates a SWT {@link Text} under the given parent.
135      * The created text control is modifyable.
136      *
137      * @param parent the parent
138      * @param text the initial text
139      * @param span the horizontal span
140      * @return the created text
141      */

142     public static Text createText( Composite parent, String JavaDoc text, int span )
143     {
144         Text t = new Text( parent, SWT.NONE | SWT.BORDER );
145         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
146         gd.horizontalSpan = span;
147         t.setLayoutData( gd );
148         t.setText( text );
149         return t;
150     }
151
152
153     /**
154      * Creates a SWT {@link Text} under the given parent.
155      * The created text control is modifyable.
156      *
157      * @param parent the parent
158      * @param text the initial text
159      * @param textWidth the width of the text control
160      * @param span the horizontal span
161      * @return the created text
162      */

163     public static Text createText( Composite parent, String JavaDoc text, int textWidth, int span )
164     {
165         Text t = new Text( parent, SWT.NONE | SWT.BORDER );
166         GridData gd = new GridData();
167         gd.horizontalSpan = span;
168         gd.widthHint = 9 * textWidth;
169         t.setLayoutData( gd );
170         t.setText( text );
171         t.setTextLimit( textWidth );
172         return t;
173     }
174
175
176     /**
177      * Creates a SWT {@link Text} under the given parent.
178      * The created text control is created with the SWT.PASSWORD style.
179      *
180      * @param parent the parent
181      * @param text the initial text
182      * @param span the horizontal span
183      * @return the created text
184      */

185     public static Text createPasswordText( Composite parent, String JavaDoc text, int span )
186     {
187         Text t = new Text( parent, SWT.NONE | SWT.BORDER | SWT.PASSWORD );
188         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
189         gd.horizontalSpan = span;
190         t.setLayoutData( gd );
191         t.setText( text );
192         return t;
193     }
194
195
196     /**
197      * Creates a SWT {@link Text} under the given parent.
198      * The created text control is created with the SWT.PASSWORD and
199      * SWT.READ_ONLY style. So the created controls is not modifyable.
200      *
201      * @param parent the parent
202      * @param text the initial text
203      * @param span the horizontal span
204      * @return the created text
205      */

206     public static Text createReadonlyPasswordText( Composite parent, String JavaDoc text, int span )
207     {
208         Text t = new Text( parent, SWT.NONE | SWT.BORDER | SWT.PASSWORD | SWT.READ_ONLY );
209         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
210         gd.horizontalSpan = span;
211         t.setLayoutData( gd );
212         t.setEditable( false );
213         t.setBackground( parent.getBackground() );
214         t.setText( text );
215         return t;
216     }
217
218
219     /**
220      * Creates a SWT {@link Text} under the given parent.
221      * The created text control behaves like a label: it has no border,
222      * a grayed background and is not modifyable.
223      * But the text is selectable and could be copied.
224      *
225      * @param parent the parent
226      * @param text the initial text
227      * @param span the horizontal span
228      * @return the created text
229      */

230     public static Text createLabeledText( Composite parent, String JavaDoc text, int span )
231     {
232         Text t = new Text( parent, SWT.NONE );
233         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
234         gd.horizontalSpan = span;
235         t.setLayoutData( gd );
236         t.setEditable( false );
237         t.setBackground( parent.getBackground() );
238         t.setText( text );
239         return t;
240     }
241
242
243     /**
244      * Creates a SWT {@link Text} under the given parent.
245      * The created text control behaves like a label: it has no border,
246      * a grayed background and is not modifyable.
247      * But the text is selectable and could be copied.
248      * The label is created with the SWT.WRAP style to enable line wrapping.
249      *
250      * @param parent the parent
251      * @param text the initial text
252      * @param span the horizontal span
253      * @return the created text
254      */

255     public static Text createWrappedLabeledText( Composite parent, String JavaDoc text, int span )
256     {
257         Text t = new Text( parent, SWT.WRAP );
258         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
259         gd.horizontalSpan = span;
260         gd.widthHint = 10;
261         gd.grabExcessHorizontalSpace = true;
262         gd.horizontalAlignment = GridData.FILL;
263         t.setLayoutData( gd );
264         t.setEditable( false );
265         t.setBackground( parent.getBackground() );
266         t.setText( text );
267         return t;
268     }
269
270
271     /**
272      * Creates a SWT {@link Text} under the given parent.
273      * The text is not modifyable, but the text is selectable
274      * and could be copied.
275      *
276      * @param parent the parent
277      * @param text the initial text
278      * @param span the horizontal span
279      * @return the created text
280      */

281     public static Text createReadonlyText( Composite parent, String JavaDoc text, int span )
282     {
283         Text t = new Text( parent, SWT.NONE | SWT.BORDER | SWT.READ_ONLY );
284         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
285         gd.horizontalSpan = span;
286         t.setLayoutData( gd );
287         t.setEditable( false );
288         t.setBackground( parent.getBackground() );
289         t.setText( text );
290         return t;
291     }
292
293
294     /**
295      * Creates a SWT {@link Combo} under the given parent.
296      * Beside the selection of an item it is also possible to type
297      * free text into the combo.
298      *
299      * @param parent the parent
300      * @param items the initial visible items
301      * @param selectedIndex the initial selected item, zero-based
302      * @param span the horizontal span
303      * @return the created combo
304      */

305     public static Combo createCombo( Composite parent, String JavaDoc[] items, int selectedIndex, int span )
306     {
307         Combo c = new Combo( parent, SWT.DROP_DOWN | SWT.BORDER );
308         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
309         gd.horizontalSpan = span;
310         c.setLayoutData( gd );
311         c.setItems( items );
312         c.select( selectedIndex );
313         c.setVisibleItemCount( 20 );
314         return c;
315     }
316
317
318     /**
319      * Creates a SWT {@link Combo} under the given parent.
320      * It is not possible to type free text into the combo, only
321      * selection of predefined items is possible.
322      *
323      * @param parent the parent
324      * @param items the initial visible items
325      * @param selectedIndex the initial selected item, zero-based
326      * @param span the horizontal span
327      * @return the created combo
328      */

329     public static Combo createReadonlyCombo( Composite parent, String JavaDoc[] items, int selectedIndex, int span )
330     {
331         Combo c = new Combo( parent, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.BORDER );
332         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
333         gd.horizontalSpan = span;
334         c.setLayoutData( gd );
335         // c.setBackground(parent.getBackground());
336
c.setItems( items );
337         c.select( selectedIndex );
338         c.setVisibleItemCount( 20 );
339         return c;
340     }
341
342
343     /**
344      * Creates a checkbox under the given parent.
345      *
346      * @param parent the parent
347      * @param text the label of the checkbox
348      * @param span the horizontal span
349      * @return the created checkbox
350      */

351     public static Button createCheckbox( Composite parent, String JavaDoc text, int span )
352     {
353         Button checkbox = new Button( parent, SWT.CHECK );
354         checkbox.setText( text );
355         GridData gd = new GridData();
356         gd.horizontalSpan = span;
357         checkbox.setLayoutData( gd );
358         return checkbox;
359     }
360
361
362     /**
363      * Creates a radio button under the given parent.
364      *
365      * @param parent the parent
366      * @param text the label of the radio button
367      * @param span the horizontal span
368      * @return the created radio button
369      */

370     public static Button createRadiobutton( Composite parent, String JavaDoc text, int span )
371     {
372         Button radio = new Button( parent, SWT.RADIO );
373         radio.setText( text );
374         GridData gd = new GridData();
375         gd.horizontalSpan = span;
376         radio.setLayoutData( gd );
377         return radio;
378     }
379
380
381     /**
382      * Creates a button under the given parent.
383      * The button width is set to the default width.
384      *
385      * @param parent the parent
386      * @param text the label of the button
387      * @param span the horizontal span
388      * @return the created button
389      */

390     public static Button createButton( Composite parent, String JavaDoc text, int span )
391     {
392         GC gc = new GC( parent );
393         gc.setFont( JFaceResources.getDialogFont() );
394         FontMetrics fontMetrics = gc.getFontMetrics();
395         gc.dispose();
396
397         Button button = new Button( parent, SWT.PUSH );
398         GridData gd = new GridData();
399         gd.widthHint = Dialog.convertHorizontalDLUsToPixels( fontMetrics, IDialogConstants.BUTTON_WIDTH );
400         button.setLayoutData( gd );
401         button.setText( text );
402         return button;
403     }
404
405
406     /**
407      * Adds some space to indent radio buttons.
408      *
409      * @param parent the parent
410      * @param span the horizontal span
411      */

412     public static void createRadioIndent( Composite parent, int span )
413     {
414         Label l = new Label( parent, SWT.NONE );
415         GridData gd = new GridData();
416         gd.horizontalSpan = span;
417         gd.horizontalIndent = 22;
418         l.setLayoutData( gd );
419     }
420
421
422     /**
423      * Creates a spacer.
424      *
425      * @param parent the parent
426      * @param span the horizontal span
427      */

428     public static void createSpacer( Composite parent, int span )
429     {
430         Label l = new Label( parent, SWT.NONE );
431         // GridData gd = new GridData(GridData.FILL_HORIZONTAL);
432
GridData gd = new GridData();
433         gd.horizontalSpan = span;
434         gd.heightHint = 1;
435         l.setLayoutData( gd );
436     }
437
438
439     /**
440      * Creates a separator line.
441      *
442      * @param parent the parent
443      * @param span the horizontal span
444      */

445     public static void createSeparator( Composite parent, int span )
446     {
447         Label l = new Label( parent, SWT.SEPARATOR | SWT.HORIZONTAL );
448         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
449         gd.horizontalSpan = span;
450         // gd.heightHint = 1;
451
l.setLayoutData( gd );
452     }
453
454
455     /**
456      * Creates a SWT {@link Link} under the given parent.
457      *
458      * @param parent the parent
459      * @param text the initial text
460      * @param span the horizontal span
461      * @return the created text
462      */

463     public static Link createLink( Composite parent, String JavaDoc text, int span )
464     {
465         Link link = new Link( parent, SWT.NONE );
466         link.setText( text );
467         GridData gd = new GridData( SWT.FILL, SWT.BEGINNING, true, false );
468         gd.horizontalSpan = span;
469         gd.widthHint = 150;
470         link.setLayoutData( gd );
471         return link;
472     }
473
474 }
475
Popular Tags