1 package org
.argeo
.cms
.swt
;
3 import java
.util
.HashMap
;
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
;
31 public class CmsSwtUtils
{
36 public static CmsSwtTheme
getCmsTheme(Composite parent
) {
37 CmsSwtTheme theme
= (CmsSwtTheme
) parent
.getData(CmsTheme
.class.getName());
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
);
49 public static void registerCmsTheme(Shell shell
, CmsTheme theme
) {
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");
60 topShell
.setData(CmsTheme
.class.getName(), theme
);
63 public static CmsView
getCmsView(Control parent
) {
64 if (parent
.isDisposed())
67 Shell topShell
= parent
.getShell();
68 while (topShell
.getParent() != null)
69 topShell
= (Shell
) topShell
.getParent();
70 return (CmsView
) topShell
.getData(CmsView
.class.getName());
73 public static void registerCmsView(Shell shell
, CmsView view
) {
75 Shell topShell
= shell
;
76 while (topShell
.getParent() != null)
77 topShell
= (Shell
) topShell
.getParent();
78 // check if already set
79 if (topShell
.getData(CmsView
.class.getName()) != null) {
80 CmsView registeredView
= (CmsView
) topShell
.getData(CmsView
.class.getName());
81 throw new IllegalArgumentException("Cms view " + registeredView
+ " already registered in this shell");
83 shell
.setData(CmsView
.class.getName(), view
);
90 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
91 public static void sendEventOnSelect(Control control
, String topic
, Map
<String
, Object
> properties
) {
92 SelectionListener listener
= (Selected
) (e
) -> {
93 getCmsView(control
.getParent()).sendEvent(topic
, properties
);
95 if (control
instanceof Button
) {
96 ((Button
) control
).addSelectionListener(listener
);
98 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
102 * Convenience method to sends an event via
103 * {@link CmsView#sendEvent(String, Map)}.
105 public static void sendEventOnSelect(Control control
, String topic
, String key
, Object value
) {
106 Map
<String
, Object
> properties
= new HashMap
<>();
107 properties
.put(key
, value
);
108 sendEventOnSelect(control
, topic
, properties
);
114 /** Get a small icon from this theme. */
115 public static Image
getSmallIcon(CmsTheme theme
, CmsIcon icon
) {
116 return ((CmsSwtTheme
) theme
).getSmallIcon(icon
);
119 /** Get a big icon from this theme. */
120 public static Image
getBigIcon(CmsTheme theme
, CmsIcon icon
) {
121 return ((CmsSwtTheme
) theme
).getBigIcon(icon
);
127 /** Takes the most space possible, depending on parent layout. */
128 public static void fill(Control control
) {
129 Layout parentLayout
= control
.getParent().getLayout();
130 if (parentLayout
== null)
131 throw new IllegalStateException("Parent layout is not set");
132 if (parentLayout
instanceof GridLayout
) {
133 control
.setLayoutData(fillAll());
134 } else if (parentLayout
instanceof FormLayout
) {
135 control
.setLayoutData(coverAll());
137 throw new IllegalArgumentException("Unsupported parent layout " + parentLayout
.getClass().getName());
144 /** A {@link GridLayout} without any spacing and one column. */
145 public static GridLayout
noSpaceGridLayout() {
146 return noSpaceGridLayout(new GridLayout());
150 * A {@link GridLayout} without any spacing and multiple columns of unequal
153 public static GridLayout
noSpaceGridLayout(int columns
) {
154 return noSpaceGridLayout(new GridLayout(columns
, false));
157 /** @return the same layout, with spaces removed. */
158 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
159 layout
.horizontalSpacing
= 0;
160 layout
.verticalSpacing
= 0;
161 layout
.marginWidth
= 0;
162 layout
.marginHeight
= 0;
166 public static GridData
fillAll() {
167 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
170 public static GridData
fillWidth() {
171 return grabWidth(SWT
.FILL
, SWT
.FILL
);
174 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
175 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
178 public static GridData
fillHeight() {
179 return grabHeight(SWT
.FILL
, SWT
.FILL
);
182 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
183 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
189 /** @return the same layout, with margins removed. */
190 public static RowLayout
noMarginsRowLayout(RowLayout rowLayout
) {
191 rowLayout
.marginTop
= 0;
192 rowLayout
.marginBottom
= 0;
193 rowLayout
.marginLeft
= 0;
194 rowLayout
.marginRight
= 0;
198 public static RowLayout
noMarginsRowLayout(int type
) {
199 return noMarginsRowLayout(new RowLayout(type
));
202 public static RowData
rowData16px() {
203 return new RowData(16, 16);
209 public static FormData
coverAll() {
210 FormData fdLabel
= new FormData();
211 fdLabel
.top
= new FormAttachment(0, 0);
212 fdLabel
.left
= new FormAttachment(0, 0);
213 fdLabel
.right
= new FormAttachment(100, 0);
214 fdLabel
.bottom
= new FormAttachment(100, 0);
223 public static <T
extends Widget
> T
style(T widget
, String style
) {
224 if (style
== null || widget
.isDisposed())
225 return widget
;// does nothing
226 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
227 if (widget
instanceof Control
) {
228 CmsView cmsView
= getCmsView((Control
) widget
);
230 cmsView
.applyStyles(widget
);
236 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
237 return style(widget
, style
.style());
240 /** Enable markups on widget */
241 public static <T
extends Widget
> T
markup(T widget
) {
242 EclipseUiSpecificUtils
.setMarkupData(widget
);
246 /** Disable markup validation. */
247 public static <T
extends Widget
> T
disableMarkupValidation(T widget
) {
248 EclipseUiSpecificUtils
.setMarkupValidationDisabledData(widget
);
253 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
255 * @param widget the widget to style and to use in order to display text
256 * @param txt the object to display via its <code>toString()</code> method.
257 * This argument should not be null, but if it is null and
258 * assertions are disabled "<null>" is displayed instead; if
259 * assertions are enabled the call will fail.
263 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
265 String str
= txt
!= null ? txt
.toString() : "<null>";
267 if (widget
instanceof Label
)
268 ((Label
) widget
).setText(str
);
269 else if (widget
instanceof Button
)
270 ((Button
) widget
).setText(str
);
271 else if (widget
instanceof Text
)
272 ((Text
) widget
).setText(str
);
274 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
278 /** A {@link Label} with markup activated. */
279 public static Label
lbl(Composite parent
, Object txt
) {
280 return text(new Label(parent
, SWT
.NONE
), txt
);
283 /** A read-only {@link Text} whose content can be copy/pasted. */
284 public static Text
txt(Composite parent
, Object txt
) {
285 return text(new Text(parent
, SWT
.NONE
), txt
);
288 /** Dispose all children of a Composite */
289 public static void clear(Composite composite
) {
290 if (composite
.isDisposed())
292 for (Control child
: composite
.getChildren())
297 private CmsSwtUtils() {