1 package org
.argeo
.eclipse
.ui
;
3 import org
.eclipse
.jface
.resource
.JFaceResources
;
4 import org
.eclipse
.swt
.SWT
;
5 import org
.eclipse
.swt
.events
.ModifyListener
;
6 import org
.eclipse
.swt
.graphics
.Font
;
7 import org
.eclipse
.swt
.layout
.FormAttachment
;
8 import org
.eclipse
.swt
.layout
.FormData
;
9 import org
.eclipse
.swt
.layout
.GridData
;
10 import org
.eclipse
.swt
.layout
.GridLayout
;
11 import org
.eclipse
.swt
.widgets
.Composite
;
12 import org
.eclipse
.swt
.widgets
.Control
;
13 import org
.eclipse
.swt
.widgets
.Label
;
14 import org
.eclipse
.swt
.widgets
.Text
;
16 /** Utilities to simplify UI development. */
17 public class EclipseUiUtils
{
19 /** Dispose all children of a Composite */
20 public static void clear(Composite composite
) {
21 for (Control child
: composite
.getChildren())
26 * Enables efficient call to the layout method of a composite, refreshing only
27 * some of the children controls.
29 public static void layout(Composite parent
, Control
... toUpdateControls
) {
30 parent
.layout(toUpdateControls
);
36 /** Shortcut to retrieve default italic font from display */
37 public static Font
getItalicFont(Composite parent
) {
38 return JFaceResources
.getFontRegistry().defaultFontDescriptor().setStyle(SWT
.ITALIC
)
39 .createFont(parent
.getDisplay());
42 /** Shortcut to retrieve default bold font from display */
43 public static Font
getBoldFont(Composite parent
) {
44 return JFaceResources
.getFontRegistry().defaultFontDescriptor().setStyle(SWT
.BOLD
)
45 .createFont(parent
.getDisplay());
48 /** Shortcut to retrieve default bold italic font from display */
49 public static Font
getBoldItalicFont(Composite parent
) {
50 return JFaceResources
.getFontRegistry().defaultFontDescriptor().setStyle(SWT
.BOLD
| SWT
.ITALIC
)
51 .createFont(parent
.getDisplay());
55 // Simplify grid layouts management
57 public static GridLayout
noSpaceGridLayout() {
58 return noSpaceGridLayout(new GridLayout());
61 public static GridLayout
noSpaceGridLayout(int columns
) {
62 return noSpaceGridLayout(new GridLayout(columns
, false));
65 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
66 layout
.horizontalSpacing
= 0;
67 layout
.verticalSpacing
= 0;
68 layout
.marginWidth
= 0;
69 layout
.marginHeight
= 0;
73 public static GridData
fillWidth() {
74 return grabWidth(SWT
.FILL
, SWT
.FILL
);
77 public static GridData
fillWidth(int colSpan
) {
78 GridData gd
= grabWidth(SWT
.FILL
, SWT
.FILL
);
79 gd
.horizontalSpan
= colSpan
;
83 public static GridData
fillAll() {
84 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
87 public static GridData
fillAll(int colSpan
, int rowSpan
) {
88 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true, colSpan
, rowSpan
);
91 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
92 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
96 // Simplify Form layout management
100 * Creates a basic form data that is attached to the 4 corners of the parent
103 public static FormData
fillFormData() {
104 FormData formData
= new FormData();
105 formData
.top
= new FormAttachment(0, 0);
106 formData
.left
= new FormAttachment(0, 0);
107 formData
.right
= new FormAttachment(100, 0);
108 formData
.bottom
= new FormAttachment(100, 0);
113 * Create a label and a text field for a grid layout, the text field grabbing
117 * the parent composite
119 * the label to display
120 * @param modifyListener
121 * a {@link ModifyListener} to listen on events on the text, can be
123 * @return the created text
126 // FIXME why was this deprecated.
127 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
129 public static Text
createGridLT(Composite parent
, String label
, ModifyListener modifyListener
) {
130 Label lbl
= new Label(parent
, SWT
.LEAD
);
132 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
133 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
);
134 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
135 if (modifyListener
!= null)
136 txt
.addModifyListener(modifyListener
);
141 * Create a label and a text field for a grid layout, the text field grabbing
144 public static Text
createGridLT(Composite parent
, String label
) {
145 return createGridLT(parent
, label
, null);
149 * Creates one label and a text field not editable with background colour of the
150 * parent (like a label but with selectable text)
152 public static Text
createGridLL(Composite parent
, String label
, String text
) {
153 Text txt
= createGridLT(parent
, label
);
155 txt
.setEditable(false);
156 txt
.setBackground(parent
.getBackground());
161 * Create a label and a text field with password display for a grid layout, the
162 * text field grabbing excess horizontal
164 public static Text
createGridLP(Composite parent
, String label
) {
165 return createGridLP(parent
, label
, null);
169 * Create a label and a text field with password display for a grid layout, the
170 * text field grabbing excess horizontal. The given modify listener will be
171 * added to the newly created text field if not null.
173 public static Text
createGridLP(Composite parent
, String label
, ModifyListener modifyListener
) {
174 Label lbl
= new Label(parent
, SWT
.LEAD
);
176 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
177 Text txt
= new Text(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.PASSWORD
);
178 txt
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
179 if (modifyListener
!= null)
180 txt
.addModifyListener(modifyListener
);
186 /** Simply checks if a string is not null nor empty */
187 public static boolean notEmpty(String stringToTest
) {
188 return !(stringToTest
== null || "".equals(stringToTest
.trim()));
191 /** Simply checks if a string is null or empty */
192 public static boolean isEmpty(String stringToTest
) {
193 return stringToTest
== null || "".equals(stringToTest
.trim());