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 only
42 * 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().setStyle(SWT
.ITALIC
)
54 .createFont(parent
.getDisplay());
57 /** Shortcut to retrieve default bold font from display */
58 public static Font
getBoldFont(Composite parent
) {
59 return JFaceResources
.getFontRegistry().defaultFontDescriptor().setStyle(SWT
.BOLD
)
60 .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().setStyle(SWT
.BOLD
| SWT
.ITALIC
)
66 .createFont(parent
.getDisplay());
70 // Simplify grid layouts management
72 public static GridLayout
noSpaceGridLayout() {
73 return noSpaceGridLayout(new GridLayout());
76 public static GridLayout
noSpaceGridLayout(int columns
) {
77 return noSpaceGridLayout(new GridLayout(columns
, false));
80 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
81 layout
.horizontalSpacing
= 0;
82 layout
.verticalSpacing
= 0;
83 layout
.marginWidth
= 0;
84 layout
.marginHeight
= 0;
88 public static GridData
fillWidth() {
89 return grabWidth(SWT
.FILL
, SWT
.FILL
);
92 public static GridData
fillWidth(int colSpan
) {
93 GridData gd
= grabWidth(SWT
.FILL
, SWT
.FILL
);
94 gd
.horizontalSpan
= colSpan
;
98 public static GridData
fillAll() {
99 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
102 public static GridData
fillAll(int colSpan
, int rowSpan
) {
103 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true, colSpan
, rowSpan
);
106 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
107 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
111 // Simplify Form layout management
115 * Creates a basic form data that is attached to the 4 corners of the parent
118 public static FormData
fillFormData() {
119 FormData formData
= new FormData();
120 formData
.top
= new FormAttachment(0, 0);
121 formData
.left
= new FormAttachment(0, 0);
122 formData
.right
= new FormAttachment(100, 0);
123 formData
.bottom
= new FormAttachment(100, 0);
128 * Create a label and a text field for a grid layout, the text field grabbing
132 * the parent composite
134 * the label to display
135 * @param modifyListener
136 * a {@link ModifyListener} to listen on events on the text, can be
138 * @return the created text
141 // FIXME why was this deprecated.
142 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
144 public static Text
createGridLT(Composite parent
, String label
, 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 grabbing
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 the
165 * 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, the
177 * 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, the
185 * text field grabbing excess horizontal. The given modify listener will be
186 * added to the newly created text field if not null.
188 public static Text
createGridLP(Composite parent
, String label
, ModifyListener modifyListener
) {
189 Label lbl
= new Label(parent
, SWT
.LEAD
);
191 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
192 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
193 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
194 if (modifyListener
!= null)
195 txt
.addModifyListener(modifyListener
);
201 /** Simply checks if a string is not null nor empty */
202 public static boolean notEmpty(String stringToTest
) {
203 return !(stringToTest
== null || "".equals(stringToTest
.trim()));
206 /** Simply checks if a string is null or empty */
207 public static boolean isEmpty(String stringToTest
) {
208 return stringToTest
== null || "".equals(stringToTest
.trim());