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);
162 public static RowData
rowData16px() {
163 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
.getCmsView((Control
) widget
).applyStyles(widget
);
195 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
196 return style(widget
, style
.toStyleClass());
199 /** Enable markups on widget */
200 public static <T
extends Widget
> T
markup(T widget
) {
201 EclipseUiSpecificUtils
.setMarkupData(widget
);
206 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
208 * @param widget the widget to style and to use in order to display text
209 * @param txt the object to display via its <code>toString()</code> method.
210 * This argument should not be null, but if it is null and
211 * assertions are disabled "<null>" is displayed instead; if
212 * assertions are enabled the call will fail.
214 * @see #markup(Widget)
216 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
218 String str
= txt
!= null ? txt
.toString() : "<null>";
220 if (widget
instanceof Label
)
221 ((Label
) widget
).setText(str
);
222 else if (widget
instanceof Button
)
223 ((Button
) widget
).setText(str
);
224 else if (widget
instanceof Text
)
225 ((Text
) widget
).setText(str
);
227 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
231 /** A {@link Label} with markup activated. */
232 public static Label
lbl(Composite parent
, Object txt
) {
233 return text(new Label(parent
, SWT
.NONE
), txt
);
236 /** A read-only {@link Text} whose content can be copy/pasted. */
237 public static Text
txt(Composite parent
, Object txt
) {
238 return text(new Text(parent
, SWT
.NONE
), txt
);
242 public static void setItemHeight(Table table
, int height
) {
243 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
246 /** Dispose all children of a Composite */
247 public static void clear(Composite composite
) {
248 for (Control child
: composite
.getChildren())
255 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
256 if (has(parent
, child
))
257 return child(parent
, child
);
258 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
261 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
262 return parent
.getNode(en
.name());
265 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
266 return parent
.hasNode(en
.name());
269 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
270 return getOrAdd(parent
, en
, null);
273 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
275 return child(parent
, en
);
276 else if (primaryType
== null)
277 return parent
.addNode(en
.name());
279 return parent
.addNode(en
.name(), primaryType
);
283 public static String
img(Node fileNode
, String width
, String height
) {
284 return img(null, fileNode
, width
, height
);
287 public static String
img(String serverBase
, Node fileNode
, String width
, String height
) {
288 String src
= (serverBase
!= null ? serverBase
: "") + NodeUtils
.getDataPath(fileNode
);
289 return imgBuilder(src
, width
, height
).append("/>").toString();
292 public static String
img(String src
, String width
, String height
) {
293 return imgBuilder(src
, width
, height
).append("/>").toString();
296 public static String
img(String src
, Point size
) {
297 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
300 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
301 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
302 .append("' src='").append(src
).append("'");
305 public static String
noImg(Point size
) {
306 ResourceManager rm
= RWT
.getResourceManager();
307 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
310 public static String
noImg() {
311 return noImg(NO_IMAGE_SIZE
);
314 public static Image
noImage(Point size
) {
315 ResourceManager rm
= RWT
.getResourceManager();
316 InputStream in
= null;
318 in
= rm
.getRegisteredContent(NO_IMAGE
);
319 ImageData id
= new ImageData(in
);
320 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
321 Image image
= new Image(Display
.getCurrent(), scaled
);
326 } catch (IOException e
) {
332 /** Lorem ipsum text to be used during development. */
333 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
334 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
335 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
336 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
337 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
338 + " Pellentesque commodo turpis ac diam ultricies dignissim."
339 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
340 + " Integer varius quis est et tristique."
341 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
342 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
343 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
346 private CmsUiUtils() {