1 package org
.argeo
.cms
.swt
;
3 import java
.util
.HashMap
;
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
;
29 public class CmsSwtUtils
{
32 private CmsSwtUtils() {
35 public static CmsTheme
getCmsTheme(Composite parent
) {
36 CmsTheme theme
= (CmsTheme
) parent
.getData(CmsTheme
.class.getName());
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
);
48 public static void registerCmsTheme(Shell shell
, CmsTheme theme
) {
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");
59 topShell
.setData(CmsTheme
.class.getName(), theme
);
62 public static CmsView
getCmsView(Control parent
) {
64 Shell topShell
= parent
.getShell();
65 while (topShell
.getParent() != null)
66 topShell
= (Shell
) topShell
.getParent();
67 return (CmsView
) topShell
.getData(CmsView
.class.getName());
70 public static void registerCmsView(Shell shell
, CmsView view
) {
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");
80 shell
.setData(CmsView
.class.getName(), view
);
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
);
88 if (control
instanceof Button
) {
89 ((Button
) control
).addSelectionListener(listener
);
91 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
95 * Convenience method to sends an event via
96 * {@link CmsView#sendEvent(String, Map)}.
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
);
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());
117 throw new IllegalArgumentException("Unsupported parent layout " + parentLayout
.getClass().getName());
124 /** A {@link GridLayout} without any spacing and one column. */
125 public static GridLayout
noSpaceGridLayout() {
126 return noSpaceGridLayout(new GridLayout());
130 * A {@link GridLayout} without any spacing and multiple columns of unequal
133 public static GridLayout
noSpaceGridLayout(int columns
) {
134 return noSpaceGridLayout(new GridLayout(columns
, false));
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;
146 public static GridData
fillAll() {
147 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
150 public static GridData
fillWidth() {
151 return grabWidth(SWT
.FILL
, SWT
.FILL
);
154 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
155 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
158 public static GridData
fillHeight() {
159 return grabHeight(SWT
.FILL
, SWT
.FILL
);
162 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
163 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
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;
178 public static RowLayout
noMarginsRowLayout(int type
) {
179 return noMarginsRowLayout(new RowLayout(type
));
182 public static RowData
rowData16px() {
183 return new RowData(16, 16);
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);
203 public static <T
extends Widget
> T
style(T widget
, String style
) {
205 return widget
;// does nothing
206 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
207 if (widget
instanceof Control
) {
208 CmsView cmsView
= getCmsView((Control
) widget
);
210 cmsView
.applyStyles(widget
);
216 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
217 return style(widget
, style
.style());
220 /** Enable markups on widget */
221 public static <T
extends Widget
> T
markup(T widget
) {
222 EclipseUiSpecificUtils
.setMarkupData(widget
);
226 /** Disable markup validation. */
227 public static <T
extends Widget
> T
disableMarkupValidation(T widget
) {
228 EclipseUiSpecificUtils
.setMarkupValidationDisabledData(widget
);
233 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
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.
243 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
245 String str
= txt
!= null ? txt
.toString() : "<null>";
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
);
254 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
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
);
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
);
268 /** Dispose all children of a Composite */
269 public static void clear(Composite composite
) {
270 if (composite
.isDisposed())
272 for (Control child
: composite
.getChildren())