]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/EclipseUiUtils.java
Adapt to changes with third parties.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / 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 only
42 * 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().setStyle(SWT.ITALIC)
54 .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().setStyle(SWT.BOLD)
60 .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().setStyle(SWT.BOLD | SWT.ITALIC)
66 .createFont(parent.getDisplay());
67 }
68
69 //
70 // Simplify grid layouts management
71 //
72 public static GridLayout noSpaceGridLayout() {
73 return noSpaceGridLayout(new GridLayout());
74 }
75
76 public static GridLayout noSpaceGridLayout(int columns) {
77 return noSpaceGridLayout(new GridLayout(columns, false));
78 }
79
80 public static GridLayout noSpaceGridLayout(GridLayout layout) {
81 layout.horizontalSpacing = 0;
82 layout.verticalSpacing = 0;
83 layout.marginWidth = 0;
84 layout.marginHeight = 0;
85 return layout;
86 }
87
88 public static GridData fillWidth() {
89 return grabWidth(SWT.FILL, SWT.FILL);
90 }
91
92 public static GridData fillWidth(int colSpan) {
93 GridData gd = grabWidth(SWT.FILL, SWT.FILL);
94 gd.horizontalSpan = colSpan;
95 return gd;
96 }
97
98 public static GridData fillAll() {
99 return new GridData(SWT.FILL, SWT.FILL, true, true);
100 }
101
102 public static GridData fillAll(int colSpan, int rowSpan) {
103 return new GridData(SWT.FILL, SWT.FILL, true, true, colSpan, rowSpan);
104 }
105
106 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
107 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
108 }
109
110 //
111 // Simplify Form layout management
112 //
113
114 /**
115 * Creates a basic form data that is attached to the 4 corners of the parent
116 * composite
117 */
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);
124 return formData;
125 }
126
127 /**
128 * Create a label and a text field for a grid layout, the text field grabbing
129 * excess horizontal
130 *
131 * @param parent
132 * the parent composite
133 * @param label
134 * the label to display
135 * @param modifyListener
136 * a {@link ModifyListener} to listen on events on the text, can be
137 * null
138 * @return the created text
139 *
140 */
141 // FIXME why was this deprecated.
142 // * @ deprecated use { @ link #createGridLT(Composite, String)} instead
143 // @ Deprecated
144 public static Text createGridLT(Composite parent, String label, 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 grabbing
157 * 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 the
165 * 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, the
177 * 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, the
185 * text field grabbing excess horizontal. The given modify listener will be
186 * added to the newly created text field if not null.
187 */
188 public static Text createGridLP(Composite parent, String label, ModifyListener modifyListener) {
189 Label lbl = new Label(parent, SWT.LEAD);
190 lbl.setText(label);
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);
196 return txt;
197 }
198
199 // MISCELLANEOUS
200
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()));
204 }
205
206 /** Simply checks if a string is null or empty */
207 public static boolean isEmpty(String stringToTest) {
208 return stringToTest == null || "".equals(stringToTest.trim());
209 }
210 }