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
.net
.URLEncoder
;
8 import java
.nio
.charset
.StandardCharsets
;
9 import java
.util
.HashMap
;
11 import java
.util
.StringTokenizer
;
13 import javax
.jcr
.Node
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.servlet
.http
.HttpServletRequest
;
17 import org
.argeo
.api
.NodeConstants
;
18 import org
.argeo
.api
.NodeUtils
;
19 import org
.argeo
.cms
.ui
.CmsConstants
;
20 import org
.argeo
.cms
.ui
.CmsView
;
21 import org
.argeo
.eclipse
.ui
.Selected
;
22 import org
.argeo
.eclipse
.ui
.specific
.EclipseUiSpecificUtils
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.eclipse
.rap
.rwt
.RWT
;
25 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.SelectionListener
;
28 import org
.eclipse
.swt
.graphics
.Image
;
29 import org
.eclipse
.swt
.graphics
.ImageData
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.layout
.FormAttachment
;
32 import org
.eclipse
.swt
.layout
.FormData
;
33 import org
.eclipse
.swt
.layout
.GridData
;
34 import org
.eclipse
.swt
.layout
.GridLayout
;
35 import org
.eclipse
.swt
.layout
.RowData
;
36 import org
.eclipse
.swt
.widgets
.Button
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Control
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Table
;
42 import org
.eclipse
.swt
.widgets
.Text
;
43 import org
.eclipse
.swt
.widgets
.Widget
;
45 /** Static utilities for the CMS framework. */
46 public class CmsUiUtils
implements CmsConstants
{
47 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
53 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
54 public static void sendEventOnSelect(Control control
, String topic
, Map
<String
, Object
> properties
) {
55 SelectionListener listener
= (Selected
) (e
) -> {
56 CmsView
.getCmsView(control
.getParent()).sendEvent(topic
, properties
);
58 if (control
instanceof Button
) {
59 ((Button
) control
).addSelectionListener(listener
);
61 throw new UnsupportedOperationException("Control type " + control
.getClass() + " is not supported.");
65 * Convenience method to sends an event via
66 * {@link CmsView#sendEvent(String, Map)}.
68 public static void sendEventOnSelect(Control control
, String topic
, String key
, Object value
) {
69 Map
<String
, Object
> properties
= new HashMap
<>();
70 properties
.put(key
, value
);
71 sendEventOnSelect(control
, topic
, properties
);
75 * The CMS view related to this display, or null if none is available from this
78 * @deprecated Use {@link CmsView#getCmsView(Composite)} instead.
81 public static CmsView
getCmsView() {
82 // return UiContext.getData(CmsView.class.getName());
83 return CmsView
.getCmsView(Display
.getCurrent().getActiveShell());
86 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
88 URL url
= new URL(request
.getRequestURL().toString());
89 StringBuilder buf
= new StringBuilder();
90 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
91 if (url
.getPort() != -1)
92 buf
.append(':').append(url
.getPort());
94 } catch (MalformedURLException e
) {
95 throw new IllegalArgumentException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
100 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
102 StringBuilder buf
= getServerBaseUrl(request
);
103 buf
.append(getDataPath(node
));
104 return new URL(buf
.toString()).toString();
105 } catch (MalformedURLException e
) {
106 throw new IllegalArgumentException("Cannot build data URL for " + node
, e
);
110 /** A path in the node repository */
111 public static String
getDataPath(Node node
) throws RepositoryException
{
112 return getDataPath(NodeConstants
.EGO_REPOSITORY
, node
);
115 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
116 return NodeUtils
.getDataPath(cn
, node
);
119 /** Clean reserved URL characters for use in HTTP links. */
120 public static String
getDataPathForUrl(Node node
) throws RepositoryException
{
121 return cleanPathForUrl(getDataPath(node
));
124 /** Clean reserved URL characters for use in HTTP links. */
125 public static String
cleanPathForUrl(String path
) throws RepositoryException
{
126 StringTokenizer st
= new StringTokenizer(path
, "/");
127 StringBuilder sb
= new StringBuilder();
128 while (st
.hasMoreElements()) {
130 String encoded
= URLEncoder
.encode(st
.nextToken(), StandardCharsets
.UTF_8
);
131 encoded
= encoded
.replace("+", "%20");
135 return sb
.toString();
138 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
140 public static RowData ROW_DATA_16px
= new RowData(16, 16);
145 public static GridLayout
noSpaceGridLayout() {
146 return noSpaceGridLayout(new GridLayout());
149 public static GridLayout
noSpaceGridLayout(int columns
) {
150 return noSpaceGridLayout(new GridLayout(columns
, false));
153 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
154 layout
.horizontalSpacing
= 0;
155 layout
.verticalSpacing
= 0;
156 layout
.marginWidth
= 0;
157 layout
.marginHeight
= 0;
161 public static GridData
fillAll() {
162 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
165 public static GridData
fillWidth() {
166 return grabWidth(SWT
.FILL
, SWT
.FILL
);
169 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
170 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
173 public static GridData
fillHeight() {
174 return grabHeight(SWT
.FILL
, SWT
.FILL
);
177 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
178 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
184 public static RowData
rowData16px() {
185 return new RowData(16, 16);
192 public static FormData
coverAll() {
193 FormData fdLabel
= new FormData();
194 fdLabel
.top
= new FormAttachment(0, 0);
195 fdLabel
.left
= new FormAttachment(0, 0);
196 fdLabel
.right
= new FormAttachment(100, 0);
197 fdLabel
.bottom
= new FormAttachment(100, 0);
206 public static <T
extends Widget
> T
style(T widget
, String style
) {
208 return widget
;// does nothing
209 EclipseUiSpecificUtils
.setStyleData(widget
, style
);
210 if (widget
instanceof Control
) {
211 CmsView
.getCmsView((Control
) widget
).applyStyles(widget
);
217 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
218 return style(widget
, style
.toStyleClass());
221 /** Enable markups on widget */
222 public static <T
extends Widget
> T
markup(T widget
) {
223 EclipseUiSpecificUtils
.setMarkupData(widget
);
228 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
230 * @param widget the widget to style and to use in order to display text
231 * @param txt the object to display via its <code>toString()</code> method.
232 * This argument should not be null, but if it is null and
233 * assertions are disabled "<null>" is displayed instead; if
234 * assertions are enabled the call will fail.
236 * @see #markup(Widget)
238 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
240 String str
= txt
!= null ? txt
.toString() : "<null>";
242 if (widget
instanceof Label
)
243 ((Label
) widget
).setText(str
);
244 else if (widget
instanceof Button
)
245 ((Button
) widget
).setText(str
);
246 else if (widget
instanceof Text
)
247 ((Text
) widget
).setText(str
);
249 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
253 /** A {@link Label} with markup activated. */
254 public static Label
lbl(Composite parent
, Object txt
) {
255 return text(new Label(parent
, SWT
.NONE
), txt
);
258 /** A read-only {@link Text} whose content can be copy/pasted. */
259 public static Text
txt(Composite parent
, Object txt
) {
260 return text(new Text(parent
, SWT
.NONE
), txt
);
264 public static void setItemHeight(Table table
, int height
) {
265 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
268 /** Dispose all children of a Composite */
269 public static void clear(Composite composite
) {
270 if (composite
.isDisposed())
272 for (Control child
: composite
.getChildren())
279 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
280 if (has(parent
, child
))
281 return child(parent
, child
);
282 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
285 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
286 return parent
.getNode(en
.name());
289 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
290 return parent
.hasNode(en
.name());
293 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
294 return getOrAdd(parent
, en
, null);
297 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
299 return child(parent
, en
);
300 else if (primaryType
== null)
301 return parent
.addNode(en
.name());
303 return parent
.addNode(en
.name(), primaryType
);
307 public static String
img(Node fileNode
, String width
, String height
) {
308 return img(null, fileNode
, width
, height
);
311 public static String
img(String serverBase
, Node fileNode
, String width
, String height
) {
312 String src
= (serverBase
!= null ? serverBase
: "") + NodeUtils
.getDataPath(fileNode
);
313 return imgBuilder(src
, width
, height
).append("/>").toString();
316 public static String
img(String src
, String width
, String height
) {
317 return imgBuilder(src
, width
, height
).append("/>").toString();
320 public static String
img(String src
, Point size
) {
321 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
324 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
325 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
326 .append("' src='").append(src
).append("'");
329 public static String
noImg(Point size
) {
330 ResourceManager rm
= RWT
.getResourceManager();
331 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
334 public static String
noImg() {
335 return noImg(NO_IMAGE_SIZE
);
338 public static Image
noImage(Point size
) {
339 ResourceManager rm
= RWT
.getResourceManager();
340 InputStream in
= null;
342 in
= rm
.getRegisteredContent(NO_IMAGE
);
343 ImageData id
= new ImageData(in
);
344 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
345 Image image
= new Image(Display
.getCurrent(), scaled
);
350 } catch (IOException e
) {
356 /** Lorem ipsum text to be used during development. */
357 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
358 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
359 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
360 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
361 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
362 + " Pellentesque commodo turpis ac diam ultricies dignissim."
363 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
364 + " Integer varius quis est et tristique."
365 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
366 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
367 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
370 private CmsUiUtils() {