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