1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.servlet
.http
.HttpServletRequest
;
12 import org
.argeo
.api
.NodeConstants
;
13 import org
.argeo
.api
.NodeUtils
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.cms
.ui
.CmsConstants
;
16 import org
.argeo
.cms
.ui
.CmsView
;
17 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.eclipse
.rap
.rwt
.RWT
;
20 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.graphics
.Image
;
23 import org
.eclipse
.swt
.graphics
.ImageData
;
24 import org
.eclipse
.swt
.graphics
.Point
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.layout
.RowData
;
28 import org
.eclipse
.swt
.widgets
.Button
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.Table
;
34 import org
.eclipse
.swt
.widgets
.Text
;
35 import org
.eclipse
.swt
.widgets
.Widget
;
37 /** Static utilities for the CMS framework. */
38 public class CmsUiUtils
implements CmsConstants
{
39 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
42 * The CMS view related to this display, or null if none is available from this
45 public static CmsView
getCmsView() {
46 return UiContext
.getData(CmsView
.KEY
);
49 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
51 URL url
= new URL(request
.getRequestURL().toString());
52 StringBuilder buf
= new StringBuilder();
53 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
54 if (url
.getPort() != -1)
55 buf
.append(':').append(url
.getPort());
57 } catch (MalformedURLException e
) {
58 throw new CmsException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
63 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
65 StringBuilder buf
= getServerBaseUrl(request
);
66 buf
.append(getDataPath(node
));
67 return new URL(buf
.toString()).toString();
68 } catch (MalformedURLException e
) {
69 throw new CmsException("Cannot build data URL for " + node
, e
);
73 /** A path in the node repository */
74 public static String
getDataPath(Node node
) throws RepositoryException
{
75 return getDataPath(NodeConstants
.NODE
, node
);
78 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
79 return NodeUtils
.getDataPath(cn
, node
);
82 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
84 public static RowData ROW_DATA_16px
= new RowData(16, 16);
86 public static GridLayout
noSpaceGridLayout() {
87 return noSpaceGridLayout(new GridLayout());
90 public static GridLayout
noSpaceGridLayout(int columns
) {
91 return noSpaceGridLayout(new GridLayout(columns
, false));
94 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
95 layout
.horizontalSpacing
= 0;
96 layout
.verticalSpacing
= 0;
97 layout
.marginWidth
= 0;
98 layout
.marginHeight
= 0;
105 public static GridData
fillWidth() {
106 return grabWidth(SWT
.FILL
, SWT
.FILL
);
109 public static GridData
fillAll() {
110 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
113 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
114 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
117 public static RowData
rowData16px() {
118 return new RowData(16, 16);
122 public static <T
extends Widget
> T
style(T widget
, String style
) {
123 widget
.setData(CmsConstants
.STYLE
, style
);
127 /** Enable markups on widget */
128 public static <T
extends Widget
> T
markup(T widget
) {
129 widget
.setData(CmsConstants
.MARKUP
, true);
134 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
136 * @see #markup(Widget)
138 public static <T
extends Widget
> T
text(T widget
, String txt
) {
140 if (widget
instanceof Label
)
141 ((Label
) widget
).setText(txt
);
142 else if (widget
instanceof Button
)
143 ((Button
) widget
).setText(txt
);
144 else if (widget
instanceof Text
)
145 ((Text
) widget
).setText(txt
);
147 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
151 public static void setItemHeight(Table table
, int height
) {
152 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
155 /** Dispose all children of a Composite */
156 public static void clear(Composite composite
) {
157 for (Control child
: composite
.getChildren())
164 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
165 if (has(parent
, child
))
166 return child(parent
, child
);
167 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
170 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
171 return parent
.getNode(en
.name());
174 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
175 return parent
.hasNode(en
.name());
178 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
179 return getOrAdd(parent
, en
, null);
182 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
184 return child(parent
, en
);
185 else if (primaryType
== null)
186 return parent
.addNode(en
.name());
188 return parent
.addNode(en
.name(), primaryType
);
192 public static String
img(String src
, String width
, String height
) {
193 return imgBuilder(src
, width
, height
).append("/>").toString();
196 public static String
img(String src
, Point size
) {
197 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
200 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
201 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
202 .append("' src='").append(src
).append("'");
205 public static String
noImg(Point size
) {
206 ResourceManager rm
= RWT
.getResourceManager();
207 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
210 public static String
noImg() {
211 return noImg(NO_IMAGE_SIZE
);
214 public static Image
noImage(Point size
) {
215 ResourceManager rm
= RWT
.getResourceManager();
216 InputStream in
= null;
218 in
= rm
.getRegisteredContent(NO_IMAGE
);
219 ImageData id
= new ImageData(in
);
220 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
221 Image image
= new Image(Display
.getCurrent(), scaled
);
226 } catch (IOException e
) {
232 /** Lorem ipsum text to be used during development. */
233 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
234 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
235 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
236 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
237 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
238 + " Pellentesque commodo turpis ac diam ultricies dignissim."
239 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
240 + " Integer varius quis est et tristique."
241 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
242 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
243 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
246 private CmsUiUtils() {