2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
;
18 import org
.eclipse
.jface
.resource
.JFaceResources
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.graphics
.Font
;
22 import org
.eclipse
.swt
.layout
.FormAttachment
;
23 import org
.eclipse
.swt
.layout
.FormData
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Control
;
28 import org
.eclipse
.swt
.widgets
.Label
;
29 import org
.eclipse
.swt
.widgets
.Text
;
31 /** Utilities to simplify UI development. */
32 public class EclipseUiUtils
{
34 /** Dispose all children of a Composite */
35 public static void clear(Composite composite
) {
36 for (Control child
: composite
.getChildren())
41 * Enables efficient call to the layout method of a composite, refreshing
42 * only some of the children controls.
44 public static void layout(Composite parent
, Control
... toUpdateControls
) {
45 parent
.layout(toUpdateControls
);
51 /** Shortcut to retrieve default italic font from display */
52 public static Font
getItalicFont(Composite parent
) {
53 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
54 .setStyle(SWT
.ITALIC
).createFont(parent
.getDisplay());
57 /** Shortcut to retrieve default bold font from display */
58 public static Font
getBoldFont(Composite parent
) {
59 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
60 .setStyle(SWT
.BOLD
).createFont(parent
.getDisplay());
63 /** Shortcut to retrieve default bold italic font from display */
64 public static Font
getBoldItalicFont(Composite parent
) {
65 return JFaceResources
.getFontRegistry().defaultFontDescriptor()
66 .setStyle(SWT
.BOLD
| SWT
.ITALIC
)
67 .createFont(parent
.getDisplay());
71 // Simplify grid layouts management
73 public static GridLayout
noSpaceGridLayout() {
74 return noSpaceGridLayout(new GridLayout());
77 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
78 layout
.horizontalSpacing
= 0;
79 layout
.verticalSpacing
= 0;
80 layout
.marginWidth
= 0;
81 layout
.marginHeight
= 0;
85 public static GridData
fillWidth() {
86 return grabWidth(SWT
.FILL
, SWT
.FILL
);
89 public static GridData
fillWidth(int colSpan
) {
90 GridData gd
= grabWidth(SWT
.FILL
, SWT
.FILL
);
91 gd
.horizontalSpan
= colSpan
;
95 public static GridData
fillAll() {
96 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
99 public static GridData
fillAll(int colSpan
, int rowSpan
) {
100 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true, colSpan
, rowSpan
);
103 public static GridData
grabWidth(int horizontalAlignment
,
104 int verticalAlignment
) {
105 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
110 // Simplify Form layout management
114 * Creates a basic form data that is attached to the 4 corners of the parent
117 public static FormData
fillFormData() {
118 FormData formData
= new FormData();
119 formData
.top
= new FormAttachment(0, 0);
120 formData
.left
= new FormAttachment(0, 0);
121 formData
.right
= new FormAttachment(100, 0);
122 formData
.bottom
= new FormAttachment(100, 0);
127 * Create a label and a text field for a grid layout, the text field
128 * grabbing excess horizontal
131 * the parent composite
133 * the label to display
134 * @param modifyListener
135 * a {@link ModifyListener} to listen on events on the text, can
137 * @return the created text
140 // FIXME why was this deprecated.
141 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
143 public static Text
createGridLT(Composite parent
, String label
,
144 ModifyListener modifyListener
) {
145 Label lbl
= new Label(parent
, SWT
.LEAD
);
147 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
148 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
);
149 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
150 if (modifyListener
!= null)
151 txt
.addModifyListener(modifyListener
);
156 * Create a label and a text field for a grid layout, the text field
157 * grabbing excess horizontal
159 public static Text
createGridLT(Composite parent
, String label
) {
160 return createGridLT(parent
, label
, null);
164 * Creates one label and a text field not editable with background colour of
165 * the parent (like a label but with selectable text)
167 public static Text
createGridLL(Composite parent
, String label
, String text
) {
168 Text txt
= createGridLT(parent
, label
);
170 txt
.setEditable(false);
171 txt
.setBackground(parent
.getBackground());
176 * Create a label and a text field with password display for a grid layout,
177 * the text field grabbing excess horizontal
179 public static Text
createGridLP(Composite parent
, String label
) {
180 return createGridLP(parent
, label
, null);
184 * Create a label and a text field with password display for a grid layout,
185 * the text field grabbing excess horizontal. The given modify listener will
186 * be added to the newly created text field if not null.
188 public static Text
createGridLP(Composite parent
, String label
,
189 ModifyListener modifyListener
) {
190 Label lbl
= new Label(parent
, SWT
.LEAD
);
192 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
193 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
194 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
195 if (modifyListener
!= null)
196 txt
.addModifyListener(modifyListener
);
202 /** Simply checks if a string is not null nor empty */
203 public static boolean notEmpty(String stringToTest
) {
204 return !(stringToTest
== null || "".equals(stringToTest
.trim()));
207 /** Simply checks if a string is null or empty */
208 public static boolean isEmpty(String stringToTest
) {
209 return stringToTest
== null || "".equals(stringToTest
.trim());