]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsSwtUtils.java
d97bc4c35ad2bb89b81663e7d17d15899fd8ded0
[lgpl/argeo-commons.git] / CmsSwtUtils.java
1 package org.argeo.cms.swt;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.argeo.api.cms.ux.CmsIcon;
7 import org.argeo.api.cms.ux.CmsStyle;
8 import org.argeo.api.cms.ux.CmsTheme;
9 import org.argeo.api.cms.ux.CmsView;
10 import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.graphics.Image;
14 import org.eclipse.swt.layout.FormAttachment;
15 import org.eclipse.swt.layout.FormData;
16 import org.eclipse.swt.layout.FormLayout;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.layout.RowData;
20 import org.eclipse.swt.layout.RowLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Layout;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Widget;
29
30 /** SWT utilities. */
31 public class CmsSwtUtils {
32 /*
33 * THEME AND VIEW
34 */
35
36 public static CmsSwtTheme getCmsTheme(Composite parent) {
37 CmsSwtTheme theme = (CmsSwtTheme) parent.getData(CmsTheme.class.getName());
38 if (theme == null) {
39 // find parent shell
40 Shell topShell = parent.getShell();
41 while (topShell.getParent() != null)
42 topShell = (Shell) topShell.getParent();
43 theme = (CmsSwtTheme) topShell.getData(CmsTheme.class.getName());
44 parent.setData(CmsTheme.class.getName(), theme);
45 }
46 return theme;
47 }
48
49 public static void registerCmsTheme(Shell shell, CmsTheme theme) {
50 // find parent shell
51 Shell topShell = shell;
52 while (topShell.getParent() != null)
53 topShell = (Shell) topShell.getParent();
54 // check if already set
55 if (topShell.getData(CmsTheme.class.getName()) != null) {
56 CmsTheme registeredTheme = (CmsTheme) topShell.getData(CmsTheme.class.getName());
57 throw new IllegalArgumentException(
58 "Theme " + registeredTheme.getThemeId() + " already registered in this shell");
59 }
60 topShell.setData(CmsTheme.class.getName(), theme);
61 }
62
63 public static CmsView getCmsView(Control parent) {
64 if (parent.isDisposed())
65 return null;
66 // find parent shell
67 Shell topShell = parent.getShell();
68 while (topShell.getParent() != null)
69 topShell = (Shell) topShell.getParent();
70 return (CmsView) topShell.getData(CmsView.class.getName());
71 }
72
73 public static void registerCmsView(Shell shell, CmsView view) {
74 // find parent shell
75 Shell topShell = shell;
76 while (topShell.getParent() != null)
77 topShell = (Shell) topShell.getParent();
78 // check if already set
79 if (topShell.getData(CmsView.class.getName()) != null) {
80 CmsView registeredView = (CmsView) topShell.getData(CmsView.class.getName());
81 throw new IllegalArgumentException("Cms view " + registeredView + " already registered in this shell");
82 }
83 shell.setData(CmsView.class.getName(), view);
84 }
85
86 /*
87 * EVENTS
88 */
89
90 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
91 public static void sendEventOnSelect(Control control, String topic, Map<String, Object> properties) {
92 SelectionListener listener = (Selected) (e) -> {
93 getCmsView(control.getParent()).sendEvent(topic, properties);
94 };
95 if (control instanceof Button) {
96 ((Button) control).addSelectionListener(listener);
97 } else
98 throw new UnsupportedOperationException("Control type " + control.getClass() + " is not supported.");
99 }
100
101 /**
102 * Convenience method to sends an event via
103 * {@link CmsView#sendEvent(String, Map)}.
104 */
105 public static void sendEventOnSelect(Control control, String topic, String key, Object value) {
106 Map<String, Object> properties = new HashMap<>();
107 properties.put(key, value);
108 sendEventOnSelect(control, topic, properties);
109 }
110
111 /*
112 * ICONS
113 */
114 /** Get a small icon from this theme. */
115 public static Image getSmallIcon(CmsTheme theme, CmsIcon icon) {
116 return ((CmsSwtTheme) theme).getSmallIcon(icon);
117 }
118
119 /** Get a big icon from this theme. */
120 public static Image getBigIcon(CmsTheme theme, CmsIcon icon) {
121 return ((CmsSwtTheme) theme).getBigIcon(icon);
122 }
123
124 /*
125 * LAYOUT INDEPENDENT
126 */
127 /** Takes the most space possible, depending on parent layout. */
128 public static void fill(Control control) {
129 Layout parentLayout = control.getParent().getLayout();
130 if (parentLayout == null)
131 throw new IllegalStateException("Parent layout is not set");
132 if (parentLayout instanceof GridLayout) {
133 control.setLayoutData(fillAll());
134 } else if (parentLayout instanceof FormLayout) {
135 control.setLayoutData(coverAll());
136 } else {
137 throw new IllegalArgumentException("Unsupported parent layout " + parentLayout.getClass().getName());
138 }
139 }
140
141 /*
142 * GRID LAYOUT
143 */
144 /** A {@link GridLayout} without any spacing and one column. */
145 public static GridLayout noSpaceGridLayout() {
146 return noSpaceGridLayout(new GridLayout());
147 }
148
149 /**
150 * A {@link GridLayout} without any spacing and multiple columns of unequal
151 * width.
152 */
153 public static GridLayout noSpaceGridLayout(int columns) {
154 return noSpaceGridLayout(new GridLayout(columns, false));
155 }
156
157 /** @return the same layout, with spaces removed. */
158 public static GridLayout noSpaceGridLayout(GridLayout layout) {
159 layout.horizontalSpacing = 0;
160 layout.verticalSpacing = 0;
161 layout.marginWidth = 0;
162 layout.marginHeight = 0;
163 return layout;
164 }
165
166 public static GridData fillAll() {
167 return new GridData(SWT.FILL, SWT.FILL, true, true);
168 }
169
170 public static GridData fillWidth() {
171 return grabWidth(SWT.FILL, SWT.FILL);
172 }
173
174 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
175 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
176 }
177
178 public static GridData fillHeight() {
179 return grabHeight(SWT.FILL, SWT.FILL);
180 }
181
182 public static GridData grabHeight(int horizontalAlignment, int verticalAlignment) {
183 return new GridData(horizontalAlignment, horizontalAlignment, false, true);
184 }
185
186 /*
187 * ROW LAYOUT
188 */
189 /** @return the same layout, with margins removed. */
190 public static RowLayout noMarginsRowLayout(RowLayout rowLayout) {
191 rowLayout.marginTop = 0;
192 rowLayout.marginBottom = 0;
193 rowLayout.marginLeft = 0;
194 rowLayout.marginRight = 0;
195 return rowLayout;
196 }
197
198 public static RowLayout noMarginsRowLayout(int type) {
199 return noMarginsRowLayout(new RowLayout(type));
200 }
201
202 public static RowData rowData16px() {
203 return new RowData(16, 16);
204 }
205
206 /*
207 * FORM LAYOUT
208 */
209 public static FormData coverAll() {
210 FormData fdLabel = new FormData();
211 fdLabel.top = new FormAttachment(0, 0);
212 fdLabel.left = new FormAttachment(0, 0);
213 fdLabel.right = new FormAttachment(100, 0);
214 fdLabel.bottom = new FormAttachment(100, 0);
215 return fdLabel;
216 }
217
218 /*
219 * STYLING
220 */
221
222 /** Style widget */
223 public static <T extends Widget> T style(T widget, String style) {
224 if (style == null || widget.isDisposed())
225 return widget;// does nothing
226 EclipseUiSpecificUtils.setStyleData(widget, style);
227 if (widget instanceof Control) {
228 CmsView cmsView = getCmsView((Control) widget);
229 if (cmsView != null)
230 cmsView.applyStyles(widget);
231 }
232 return widget;
233 }
234
235 /** Style widget */
236 public static <T extends Widget> T style(T widget, CmsStyle style) {
237 return style(widget, style.style());
238 }
239
240 /** Enable markups on widget */
241 public static <T extends Widget> T markup(T widget) {
242 EclipseUiSpecificUtils.setMarkupData(widget);
243 return widget;
244 }
245
246 /** Disable markup validation. */
247 public static <T extends Widget> T disableMarkupValidation(T widget) {
248 EclipseUiSpecificUtils.setMarkupValidationDisabledData(widget);
249 return widget;
250 }
251
252 /**
253 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
254 *
255 * @param widget the widget to style and to use in order to display text
256 * @param txt the object to display via its <code>toString()</code> method.
257 * This argument should not be null, but if it is null and
258 * assertions are disabled "<null>" is displayed instead; if
259 * assertions are enabled the call will fail.
260 *
261 * @see markup
262 */
263 public static <T extends Widget> T text(T widget, Object txt) {
264 assert txt != null;
265 String str = txt != null ? txt.toString() : "<null>";
266 markup(widget);
267 if (widget instanceof Label)
268 ((Label) widget).setText(str);
269 else if (widget instanceof Button)
270 ((Button) widget).setText(str);
271 else if (widget instanceof Text)
272 ((Text) widget).setText(str);
273 else
274 throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
275 return widget;
276 }
277
278 /** A {@link Label} with markup activated. */
279 public static Label lbl(Composite parent, Object txt) {
280 return text(new Label(parent, SWT.NONE), txt);
281 }
282
283 /** A read-only {@link Text} whose content can be copy/pasted. */
284 public static Text txt(Composite parent, Object txt) {
285 return text(new Text(parent, SWT.NONE), txt);
286 }
287
288 /** Dispose all children of a Composite */
289 public static void clear(Composite composite) {
290 if (composite.isDisposed())
291 return;
292 for (Control child : composite.getChildren())
293 child.dispose();
294 }
295
296 /** Singleton. */
297 private CmsSwtUtils() {
298 }
299
300 }