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
.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
;
27 public class CmsSwtUtils
{
30 private CmsSwtUtils() {
33 public static CmsTheme
getCmsTheme(Composite parent
) {
34 CmsTheme theme
= (CmsTheme
) parent
.getData(CmsTheme
.class.getName());
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
);
46 public static void registerCmsTheme(Shell shell
, CmsTheme theme
) {
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");
57 topShell
.setData(CmsTheme
.class.getName(), theme
);
60 public static CmsView
getCmsView(Control parent
) {
62 Shell topShell
= parent
.getShell();
63 while (topShell
.getParent() != null)
64 topShell
= (Shell
) topShell
.getParent();
65 return (CmsView
) topShell
.getData(CmsView
.class.getName());
68 public static void registerCmsView(Shell shell
, CmsView view
) {
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");
78 shell
.setData(CmsView
.class.getName(), view
);
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
);
86 if (control
instanceof Button
) {
87 ((Button
) control
).addSelectionListener(listener
);
89 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
93 * Convenience method to sends an event via
94 * {@link CmsView#sendEvent(String, Map)}.
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
);
105 /** A {@link GridLayout} without any spacing and one column. */
106 public static GridLayout
noSpaceGridLayout() {
107 return noSpaceGridLayout(new GridLayout());
111 * A {@link GridLayout} without any spacing and multiple columns of unequal
114 public static GridLayout
noSpaceGridLayout(int columns
) {
115 return noSpaceGridLayout(new GridLayout(columns
, false));
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;
127 public static GridData
fillAll() {
128 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
131 public static GridData
fillWidth() {
132 return grabWidth(SWT
.FILL
, SWT
.FILL
);
135 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
136 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
139 public static GridData
fillHeight() {
140 return grabHeight(SWT
.FILL
, SWT
.FILL
);
143 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
144 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
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;
159 public static RowLayout
noMarginsRowLayout(int type
) {
160 return noMarginsRowLayout(new RowLayout(type
));
163 public static RowData
rowData16px() {
164 return new RowData(16, 16);
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);
184 public static <T
extends Widget
> T
style(T widget
, String style
) {
186 return widget
;// does nothing
187 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
188 if (widget
instanceof Control
) {
189 CmsView cmsView
= getCmsView((Control
) widget
);
191 cmsView
.applyStyles(widget
);
197 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
198 return style(widget
, style
.style());
201 /** Enable markups on widget */
202 public static <T
extends Widget
> T
markup(T widget
) {
203 EclipseUiSpecificUtils
.setMarkupData(widget
);
207 /** Disable markup validation. */
208 public static <T
extends Widget
> T
disableMarkupValidation(T widget
) {
209 EclipseUiSpecificUtils
.setMarkupValidationDisabledData(widget
);
214 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
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.
224 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
226 String str
= txt
!= null ? txt
.toString() : "<null>";
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
);
235 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
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
);
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
);
249 /** Dispose all children of a Composite */
250 public static void clear(Composite composite
) {
251 if (composite
.isDisposed())
253 for (Control child
: composite
.getChildren())