1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.HashMap
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.servlet
.http
.HttpServletRequest
;
14 import org
.argeo
.api
.NodeConstants
;
15 import org
.argeo
.api
.NodeUtils
;
16 import org
.argeo
.cms
.ui
.CmsConstants
;
17 import org
.argeo
.cms
.ui
.CmsView
;
18 import org
.argeo
.eclipse
.ui
.Selected
;
19 import org
.argeo
.eclipse
.ui
.specific
.EclipseUiSpecificUtils
;
20 import org
.argeo
.jcr
.JcrUtils
;
21 import org
.eclipse
.rap
.rwt
.RWT
;
22 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.graphics
.Image
;
26 import org
.eclipse
.swt
.graphics
.ImageData
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.layout
.FormAttachment
;
29 import org
.eclipse
.swt
.layout
.FormData
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.layout
.RowData
;
33 import org
.eclipse
.swt
.widgets
.Button
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Control
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Table
;
39 import org
.eclipse
.swt
.widgets
.Text
;
40 import org
.eclipse
.swt
.widgets
.Widget
;
42 /** Static utilities for the CMS framework. */
43 public class CmsUiUtils
implements CmsConstants
{
44 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
50 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
51 public static void sendEventOnSelect(Control control
, String topic
, Map
<String
, Object
> properties
) {
52 SelectionListener listener
= (Selected
) (e
) -> {
53 CmsView
.getCmsView(control
.getParent()).sendEvent(topic
, properties
);
55 if (control
instanceof Button
) {
56 ((Button
) control
).addSelectionListener(listener
);
58 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
62 * Convenience method to sends an event via
63 * {@link CmsView#sendEvent(String, Map)}.
65 public static void sendEventOnSelect(Control control
, String topic
, String key
, Object value
) {
66 Map
<String
, Object
> properties
= new HashMap
<>();
67 properties
.put(key
, value
);
68 sendEventOnSelect(control
, topic
, properties
);
72 * The CMS view related to this display, or null if none is available from this
75 * @deprecated Use {@link CmsView#getCmsView(Composite)} instead.
78 public static CmsView
getCmsView() {
79 // return UiContext.getData(CmsView.class.getName());
80 return CmsView
.getCmsView(Display
.getCurrent().getActiveShell());
83 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
85 URL url
= new URL(request
.getRequestURL().toString());
86 StringBuilder buf
= new StringBuilder();
87 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
88 if (url
.getPort() != -1)
89 buf
.append(':').append(url
.getPort());
91 } catch (MalformedURLException e
) {
92 throw new IllegalArgumentException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
97 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
99 StringBuilder buf
= getServerBaseUrl(request
);
100 buf
.append(getDataPath(node
));
101 return new URL(buf
.toString()).toString();
102 } catch (MalformedURLException e
) {
103 throw new IllegalArgumentException("Cannot build data URL for " + node
, e
);
107 /** A path in the node repository */
108 public static String
getDataPath(Node node
) throws RepositoryException
{
109 return getDataPath(NodeConstants
.EGO_REPOSITORY
, node
);
112 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
113 return NodeUtils
.getDataPath(cn
, node
);
116 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
118 public static RowData ROW_DATA_16px
= new RowData(16, 16);
123 public static GridLayout
noSpaceGridLayout() {
124 return noSpaceGridLayout(new GridLayout());
127 public static GridLayout
noSpaceGridLayout(int columns
) {
128 return noSpaceGridLayout(new GridLayout(columns
, false));
131 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
132 layout
.horizontalSpacing
= 0;
133 layout
.verticalSpacing
= 0;
134 layout
.marginWidth
= 0;
135 layout
.marginHeight
= 0;
139 public static GridData
fillAll() {
140 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
143 public static GridData
fillWidth() {
144 return grabWidth(SWT
.FILL
, SWT
.FILL
);
147 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
148 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
151 public static GridData
fillHeight() {
152 return grabHeight(SWT
.FILL
, SWT
.FILL
);
155 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
156 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
159 public static RowData
rowData16px() {
160 return new RowData(16, 16);
167 public static FormData
coverAll() {
168 FormData fdLabel
= new FormData();
169 fdLabel
.top
= new FormAttachment(0, 0);
170 fdLabel
.left
= new FormAttachment(0, 0);
171 fdLabel
.right
= new FormAttachment(100, 0);
172 fdLabel
.bottom
= new FormAttachment(100, 0);
181 public static <T
extends Widget
> T
style(T widget
, String style
) {
183 return widget
;// does nothing
184 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
185 if (widget
instanceof Control
) {
186 CmsView
.getCmsView((Control
) widget
).applyStyles(widget
);
192 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
193 return style(widget
, style
.toStyleClass());
196 /** Enable markups on widget */
197 public static <T
extends Widget
> T
markup(T widget
) {
198 EclipseUiSpecificUtils
.setMarkupData(widget
);
203 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
205 * @param widget the widget to style and to use in order to display text
206 * @param txt the object to display via its <code>toString()</code> method.
207 * This argument should not be null, but if it is null and
208 * assertions are disabled "<null>" is displayed instead; if
209 * assertions are enabled the call will fail.
211 * @see #markup(Widget)
213 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
215 String str
= txt
!= null ? txt
.toString() : "<null>";
217 if (widget
instanceof Label
)
218 ((Label
) widget
).setText(str
);
219 else if (widget
instanceof Button
)
220 ((Button
) widget
).setText(str
);
221 else if (widget
instanceof Text
)
222 ((Text
) widget
).setText(str
);
224 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
228 /** A {@link Label} with markup activated. */
229 public static Label
lbl(Composite parent
, Object txt
) {
230 return text(new Label(parent
, SWT
.NONE
), txt
);
233 /** A read-only {@link Text} whose content can be copy/pasted. */
234 public static Text
txt(Composite parent
, Object txt
) {
235 return text(new Text(parent
, SWT
.NONE
), txt
);
239 public static void setItemHeight(Table table
, int height
) {
240 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
243 /** Dispose all children of a Composite */
244 public static void clear(Composite composite
) {
245 for (Control child
: composite
.getChildren())
252 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
253 if (has(parent
, child
))
254 return child(parent
, child
);
255 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
258 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
259 return parent
.getNode(en
.name());
262 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
263 return parent
.hasNode(en
.name());
266 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
267 return getOrAdd(parent
, en
, null);
270 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
272 return child(parent
, en
);
273 else if (primaryType
== null)
274 return parent
.addNode(en
.name());
276 return parent
.addNode(en
.name(), primaryType
);
280 public static String
img(Node fileNode
, String width
, String height
) {
281 String src
= NodeUtils
.getDataPath(fileNode
);
282 return imgBuilder(src
, width
, height
).append("/>").toString();
285 public static String
img(String src
, String width
, String height
) {
286 return imgBuilder(src
, width
, height
).append("/>").toString();
289 public static String
img(String src
, Point size
) {
290 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
293 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
294 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
295 .append("' src='").append(src
).append("'");
298 public static String
noImg(Point size
) {
299 ResourceManager rm
= RWT
.getResourceManager();
300 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
303 public static String
noImg() {
304 return noImg(NO_IMAGE_SIZE
);
307 public static Image
noImage(Point size
) {
308 ResourceManager rm
= RWT
.getResourceManager();
309 InputStream in
= null;
311 in
= rm
.getRegisteredContent(NO_IMAGE
);
312 ImageData id
= new ImageData(in
);
313 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
314 Image image
= new Image(Display
.getCurrent(), scaled
);
319 } catch (IOException e
) {
325 /** Lorem ipsum text to be used during development. */
326 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
327 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
328 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
329 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
330 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
331 + " Pellentesque commodo turpis ac diam ultricies dignissim."
332 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
333 + " Integer varius quis est et tristique."
334 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
335 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
336 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
339 private CmsUiUtils() {