]> git.argeo.org Git - lgpl/argeo-commons.git/blob - EclipseUiUtils.java
fad0e0df9910208a0ad1599a85e6459c6334a3bd
[lgpl/argeo-commons.git] / EclipseUiUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui;
17
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;
30
31 /** Utilities to simplify UI development. */
32 public class EclipseUiUtils {
33
34 /** Dispose all children of a Composite */
35 public static void clear(Composite composite) {
36 for (Control child : composite.getChildren())
37 child.dispose();
38 }
39
40 /**
41 * Enables efficient call to the layout method of a composite, refreshing
42 * only some of the children controls.
43 */
44 public static void layout(Composite parent, Control... toUpdateControls) {
45 parent.layout(toUpdateControls);
46 }
47
48 //
49 // FONTS
50 //
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());
55 }
56
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());
61 }
62
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());
68 }
69
70 //
71 // Simplify grid layouts management
72 //
73 public static GridLayout noSpaceGridLayout() {
74 return noSpaceGridLayout(new GridLayout());
75 }
76
77 public static GridLayout noSpaceGridLayout(GridLayout layout) {
78 layout.horizontalSpacing = 0;
79 layout.verticalSpacing = 0;
80 layout.marginWidth = 0;
81 layout.marginHeight = 0;
82 return layout;
83 }
84
85 public static GridData fillWidth() {
86 return grabWidth(SWT.FILL, SWT.FILL);
87 }
88
89 public static GridData fillWidth(int colSpan) {
90 GridData gd = grabWidth(SWT.FILL, SWT.FILL);
91 gd.horizontalSpan = colSpan;
92 return gd;
93 }
94
95 public static GridData fillAll() {
96 return new GridData(SWT.FILL, SWT.FILL, true, true);
97 }
98
99 public static GridData fillAll(int colSpan, int rowSpan) {
100 return new GridData(SWT.FILL, SWT.FILL, true, true, colSpan, rowSpan);
101 }
102
103 public static GridData grabWidth(int horizontalAlignment,
104 int verticalAlignment) {
105 return new GridData(horizontalAlignment, horizontalAlignment, true,
106 false);
107 }
108
109 //
110 // Simplify Form layout management
111 //
112
113 /**
114 * Creates a basic form data that is attached to the 4 corners of the parent
115 * composite
116 */
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);
123 return formData;
124 }
125
126 /**
127 * Create a label and a text field for a grid layout, the text field
128 * grabbing excess horizontal
129 *
130 * @param parent
131 * the parent composite
132 * @param label
133 * the label to display
134 * @param modifyListener
135 * a {@link ModifyListener} to listen on events on the text, can
136 * be null
137 * @return the created text
138 *
139 */
140 // FIXME why was this deprecated.
141 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
142 // @ Deprecated
143 public static Text createGridLT(Composite parent, String label,
144 ModifyListener modifyListener) {
145 Label lbl = new Label(parent, SWT.LEAD);
146 lbl.setText(label);
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);
152 return txt;
153 }
154
155 /**
156 * Create a label and a text field for a grid layout, the text field
157 * grabbing excess horizontal
158 */
159 public static Text createGridLT(Composite parent, String label) {
160 return createGridLT(parent, label, null);
161 }
162
163 /**
164 * Creates one label and a text field not editable with background colour of
165 * the parent (like a label but with selectable text)
166 */
167 public static Text createGridLL(Composite parent, String label, String text) {
168 Text txt = createGridLT(parent, label);
169 txt.setText(text);
170 txt.setEditable(false);
171 txt.setBackground(parent.getBackground());
172 return txt;
173 }
174
175 /**
176 * Create a label and a text field with password display for a grid layout,
177 * the text field grabbing excess horizontal
178 */
179 public static Text createGridLP(Composite parent, String label) {
180 return createGridLP(parent, label, null);
181 }
182
183 /**
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.
187 */
188 public static Text createGridLP(Composite parent, String label,
189 ModifyListener modifyListener) {
190 Label lbl = new Label(parent, SWT.LEAD);
191 lbl.setText(label);
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);
197 return txt;
198 }
199
200 // MISCELLANEOUS
201
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()));
205 }
206
207 /** Simply checks if a string is null or empty */
208 public static boolean isEmpty(String stringToTest) {
209 return stringToTest == null || "".equals(stringToTest.trim());
210 }
211 }