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