]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/EclipseUiUtils.java
Merge branch 'master' of https://mbaudier@code.argeo.org/git/lgpl/argeo-commons.git
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / EclipseUiUtils.java
1 package org.argeo.eclipse.ui;
2
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;
15
16 /** Utilities to simplify UI development. */
17 public class EclipseUiUtils {
18
19 /** Dispose all children of a Composite */
20 public static void clear(Composite composite) {
21 for (Control child : composite.getChildren())
22 child.dispose();
23 }
24
25 /**
26 * Enables efficient call to the layout method of a composite, refreshing only
27 * some of the children controls.
28 */
29 public static void layout(Composite parent, Control... toUpdateControls) {
30 parent.layout(toUpdateControls);
31 }
32
33 //
34 // FONTS
35 //
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());
40 }
41
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());
46 }
47
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());
52 }
53
54 //
55 // Simplify grid layouts management
56 //
57 public static GridLayout noSpaceGridLayout() {
58 return noSpaceGridLayout(new GridLayout());
59 }
60
61 public static GridLayout noSpaceGridLayout(int columns) {
62 return noSpaceGridLayout(new GridLayout(columns, false));
63 }
64
65 public static GridLayout noSpaceGridLayout(GridLayout layout) {
66 layout.horizontalSpacing = 0;
67 layout.verticalSpacing = 0;
68 layout.marginWidth = 0;
69 layout.marginHeight = 0;
70 return layout;
71 }
72
73 public static GridData fillWidth() {
74 return grabWidth(SWT.FILL, SWT.FILL);
75 }
76
77 public static GridData fillWidth(int colSpan) {
78 GridData gd = grabWidth(SWT.FILL, SWT.FILL);
79 gd.horizontalSpan = colSpan;
80 return gd;
81 }
82
83 public static GridData fillAll() {
84 return new GridData(SWT.FILL, SWT.FILL, true, true);
85 }
86
87 public static GridData fillAll(int colSpan, int rowSpan) {
88 return new GridData(SWT.FILL, SWT.FILL, true, true, colSpan, rowSpan);
89 }
90
91 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
92 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
93 }
94
95 //
96 // Simplify Form layout management
97 //
98
99 /**
100 * Creates a basic form data that is attached to the 4 corners of the parent
101 * composite
102 */
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);
109 return formData;
110 }
111
112 /**
113 * Create a label and a text field for a grid layout, the text field grabbing
114 * excess horizontal
115 *
116 * @param parent
117 * the parent composite
118 * @param label
119 * the label to display
120 * @param modifyListener
121 * a {@link ModifyListener} to listen on events on the text, can be
122 * null
123 * @return the created text
124 *
125 */
126 // FIXME why was this deprecated.
127 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
128 // @ Deprecated
129 public static Text createGridLT(Composite parent, String label, ModifyListener modifyListener) {
130 Label lbl = new Label(parent, SWT.LEAD);
131 lbl.setText(label);
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);
137 return txt;
138 }
139
140 /**
141 * Create a label and a text field for a grid layout, the text field grabbing
142 * excess horizontal
143 */
144 public static Text createGridLT(Composite parent, String label) {
145 return createGridLT(parent, label, null);
146 }
147
148 /**
149 * Creates one label and a text field not editable with background colour of the
150 * parent (like a label but with selectable text)
151 */
152 public static Text createGridLL(Composite parent, String label, String text) {
153 Text txt = createGridLT(parent, label);
154 txt.setText(text);
155 txt.setEditable(false);
156 txt.setBackground(parent.getBackground());
157 return txt;
158 }
159
160 /**
161 * Create a label and a text field with password display for a grid layout, the
162 * text field grabbing excess horizontal
163 */
164 public static Text createGridLP(Composite parent, String label) {
165 return createGridLP(parent, label, null);
166 }
167
168 /**
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.
172 */
173 public static Text createGridLP(Composite parent, String label, ModifyListener modifyListener) {
174 Label lbl = new Label(parent, SWT.LEAD);
175 lbl.setText(label);
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);
181 return txt;
182 }
183
184 // MISCELLANEOUS
185
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()));
189 }
190
191 /** Simply checks if a string is null or empty */
192 public static boolean isEmpty(String stringToTest) {
193 return stringToTest == null || "".equals(stringToTest.trim());
194 }
195 }