]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.swt/src/org/argeo/cms/swt/CmsSwtUtils.java
SLF4J implementation.
[lgpl/argeo-commons.git] / 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 public static GridLayout noSpaceGridLayout() {
106 return noSpaceGridLayout(new GridLayout());
107 }
108
109 public static GridLayout noSpaceGridLayout(int columns) {
110 return noSpaceGridLayout(new GridLayout(columns, false));
111 }
112
113 /** @return the same layout, with spaces removed. */
114 public static GridLayout noSpaceGridLayout(GridLayout layout) {
115 layout.horizontalSpacing = 0;
116 layout.verticalSpacing = 0;
117 layout.marginWidth = 0;
118 layout.marginHeight = 0;
119 return layout;
120 }
121
122 public static GridData fillAll() {
123 return new GridData(SWT.FILL, SWT.FILL, true, true);
124 }
125
126 public static GridData fillWidth() {
127 return grabWidth(SWT.FILL, SWT.FILL);
128 }
129
130 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
131 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
132 }
133
134 public static GridData fillHeight() {
135 return grabHeight(SWT.FILL, SWT.FILL);
136 }
137
138 public static GridData grabHeight(int horizontalAlignment, int verticalAlignment) {
139 return new GridData(horizontalAlignment, horizontalAlignment, false, true);
140 }
141
142 /*
143 * ROW LAYOUT
144 */
145 /** @return the same layout, with margins removed. */
146 public static RowLayout noMarginsRowLayout(RowLayout rowLayout) {
147 rowLayout.marginTop = 0;
148 rowLayout.marginBottom = 0;
149 rowLayout.marginLeft = 0;
150 rowLayout.marginRight = 0;
151 return rowLayout;
152 }
153
154 public static RowLayout noMarginsRowLayout(int type) {
155 return noMarginsRowLayout(new RowLayout(type));
156 }
157
158 public static RowData rowData16px() {
159 return new RowData(16, 16);
160 }
161
162 /*
163 * FORM LAYOUT
164 */
165 public static FormData coverAll() {
166 FormData fdLabel = new FormData();
167 fdLabel.top = new FormAttachment(0, 0);
168 fdLabel.left = new FormAttachment(0, 0);
169 fdLabel.right = new FormAttachment(100, 0);
170 fdLabel.bottom = new FormAttachment(100, 0);
171 return fdLabel;
172 }
173
174 /*
175 * STYLING
176 */
177
178 /** Style widget */
179 public static <T extends Widget> T style(T widget, String style) {
180 if (style == null)
181 return widget;// does nothing
182 EclipseUiSpecificUtils.setStyleData(widget, style);
183 if (widget instanceof Control) {
184 CmsView cmsView = getCmsView((Control) widget);
185 if (cmsView != null)
186 cmsView.applyStyles(widget);
187 }
188 return widget;
189 }
190
191 /** Style widget */
192 public static <T extends Widget> T style(T widget, CmsStyle style) {
193 return style(widget, style.style());
194 }
195
196 /** Enable markups on widget */
197 public static <T extends Widget> T markup(T widget) {
198 EclipseUiSpecificUtils.setMarkupData(widget);
199 return widget;
200 }
201
202 /** Disable markup validation. */
203 public static <T extends Widget> T disableMarkupValidation(T widget) {
204 EclipseUiSpecificUtils.setMarkupValidationDisabledData(widget);
205 return widget;
206 }
207
208 /**
209 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
210 *
211 * @param widget the widget to style and to use in order to display text
212 * @param txt the object to display via its <code>toString()</code> method.
213 * This argument should not be null, but if it is null and
214 * assertions are disabled "<null>" is displayed instead; if
215 * assertions are enabled the call will fail.
216 *
217 * @see markup
218 */
219 public static <T extends Widget> T text(T widget, Object txt) {
220 assert txt != null;
221 String str = txt != null ? txt.toString() : "<null>";
222 markup(widget);
223 if (widget instanceof Label)
224 ((Label) widget).setText(str);
225 else if (widget instanceof Button)
226 ((Button) widget).setText(str);
227 else if (widget instanceof Text)
228 ((Text) widget).setText(str);
229 else
230 throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
231 return widget;
232 }
233
234 /** A {@link Label} with markup activated. */
235 public static Label lbl(Composite parent, Object txt) {
236 return text(new Label(parent, SWT.NONE), txt);
237 }
238
239 /** A read-only {@link Text} whose content can be copy/pasted. */
240 public static Text txt(Composite parent, Object txt) {
241 return text(new Text(parent, SWT.NONE), txt);
242 }
243
244 /** Dispose all children of a Composite */
245 public static void clear(Composite composite) {
246 if (composite.isDisposed())
247 return;
248 for (Control child : composite.getChildren())
249 child.dispose();
250 }
251 }