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