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
) {
65 Shell topShell
= parent
.getShell();
66 while (topShell
.getParent() != null)
67 topShell
= (Shell
) topShell
.getParent();
68 return (CmsView
) topShell
.getData(CmsView
.class.getName());
71 public static void registerCmsView(Shell shell
, CmsView view
) {
73 Shell topShell
= shell
;
74 while (topShell
.getParent() != null)
75 topShell
= (Shell
) topShell
.getParent();
76 // check if already set
77 if (topShell
.getData(CmsView
.class.getName()) != null) {
78 CmsView registeredView
= (CmsView
) topShell
.getData(CmsView
.class.getName());
79 throw new IllegalArgumentException("Cms view " + registeredView
+ " already registered in this shell");
81 shell
.setData(CmsView
.class.getName(), view
);
88 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
89 public static void sendEventOnSelect(Control control
, String topic
, Map
<String
, Object
> properties
) {
90 SelectionListener listener
= (Selected
) (e
) -> {
91 getCmsView(control
.getParent()).sendEvent(topic
, properties
);
93 if (control
instanceof Button
) {
94 ((Button
) control
).addSelectionListener(listener
);
96 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
100 * Convenience method to sends an event via
101 * {@link CmsView#sendEvent(String, Map)}.
103 public static void sendEventOnSelect(Control control
, String topic
, String key
, Object value
) {
104 Map
<String
, Object
> properties
= new HashMap
<>();
105 properties
.put(key
, value
);
106 sendEventOnSelect(control
, topic
, properties
);
112 /** Get a small icon from this theme. */
113 public static Image
getSmallIcon(CmsTheme theme
, CmsIcon icon
) {
114 return ((CmsSwtTheme
) theme
).getSmallIcon(icon
);
117 /** Get a big icon from this theme. */
118 public static Image
getBigIcon(CmsTheme theme
, CmsIcon icon
) {
119 return ((CmsSwtTheme
) theme
).getBigIcon(icon
);
125 /** Takes the most space possible, depending on parent layout. */
126 public static void fill(Control control
) {
127 Layout parentLayout
= control
.getParent().getLayout();
128 if (parentLayout
== null)
129 throw new IllegalStateException("Parent layout is not set");
130 if (parentLayout
instanceof GridLayout
) {
131 control
.setLayoutData(fillAll());
132 } else if (parentLayout
instanceof FormLayout
) {
133 control
.setLayoutData(coverAll());
135 throw new IllegalArgumentException("Unsupported parent layout " + parentLayout
.getClass().getName());
142 /** A {@link GridLayout} without any spacing and one column. */
143 public static GridLayout
noSpaceGridLayout() {
144 return noSpaceGridLayout(new GridLayout());
148 * A {@link GridLayout} without any spacing and multiple columns of unequal
151 public static GridLayout
noSpaceGridLayout(int columns
) {
152 return noSpaceGridLayout(new GridLayout(columns
, false));
155 /** @return the same layout, with spaces removed. */
156 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
157 layout
.horizontalSpacing
= 0;
158 layout
.verticalSpacing
= 0;
159 layout
.marginWidth
= 0;
160 layout
.marginHeight
= 0;
164 public static GridData
fillAll() {
165 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
168 public static GridData
fillWidth() {
169 return grabWidth(SWT
.FILL
, SWT
.FILL
);
172 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
173 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
176 public static GridData
fillHeight() {
177 return grabHeight(SWT
.FILL
, SWT
.FILL
);
180 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
181 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
187 /** @return the same layout, with margins removed. */
188 public static RowLayout
noMarginsRowLayout(RowLayout rowLayout
) {
189 rowLayout
.marginTop
= 0;
190 rowLayout
.marginBottom
= 0;
191 rowLayout
.marginLeft
= 0;
192 rowLayout
.marginRight
= 0;
196 public static RowLayout
noMarginsRowLayout(int type
) {
197 return noMarginsRowLayout(new RowLayout(type
));
200 public static RowData
rowData16px() {
201 return new RowData(16, 16);
207 public static FormData
coverAll() {
208 FormData fdLabel
= new FormData();
209 fdLabel
.top
= new FormAttachment(0, 0);
210 fdLabel
.left
= new FormAttachment(0, 0);
211 fdLabel
.right
= new FormAttachment(100, 0);
212 fdLabel
.bottom
= new FormAttachment(100, 0);
221 public static <T
extends Widget
> T
style(T widget
, String style
) {
223 return widget
;// does nothing
224 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
225 if (widget
instanceof Control
) {
226 CmsView cmsView
= getCmsView((Control
) widget
);
228 cmsView
.applyStyles(widget
);
234 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
235 return style(widget
, style
.style());
238 /** Enable markups on widget */
239 public static <T
extends Widget
> T
markup(T widget
) {
240 EclipseUiSpecificUtils
.setMarkupData(widget
);
244 /** Disable markup validation. */
245 public static <T
extends Widget
> T
disableMarkupValidation(T widget
) {
246 EclipseUiSpecificUtils
.setMarkupValidationDisabledData(widget
);
251 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
253 * @param widget the widget to style and to use in order to display text
254 * @param txt the object to display via its <code>toString()</code> method.
255 * This argument should not be null, but if it is null and
256 * assertions are disabled "<null>" is displayed instead; if
257 * assertions are enabled the call will fail.
261 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
263 String str
= txt
!= null ? txt
.toString() : "<null>";
265 if (widget
instanceof Label
)
266 ((Label
) widget
).setText(str
);
267 else if (widget
instanceof Button
)
268 ((Button
) widget
).setText(str
);
269 else if (widget
instanceof Text
)
270 ((Text
) widget
).setText(str
);
272 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
276 /** A {@link Label} with markup activated. */
277 public static Label
lbl(Composite parent
, Object txt
) {
278 return text(new Label(parent
, SWT
.NONE
), txt
);
281 /** A read-only {@link Text} whose content can be copy/pasted. */
282 public static Text
txt(Composite parent
, Object txt
) {
283 return text(new Text(parent
, SWT
.NONE
), txt
);
286 /** Dispose all children of a Composite */
287 public static void clear(Composite composite
) {
288 if (composite
.isDisposed())
290 for (Control child
: composite
.getChildren())
295 private CmsSwtUtils() {