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
.EGO_REPOSITORY
, 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 * @param widget the widget to style and to use in order to display text
137 * @param txt the object to display via its <code>toString()</code> method.
138 * This argument should not be null, but if it is null and
139 * assertions are disabled "<null>" is displayed instead; if
140 * assertions are enabled the call will fail.
142 * @see #markup(Widget)
144 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
146 String str
= txt
!= null ? txt
.toString() : "<null>";
148 if (widget
instanceof Label
)
149 ((Label
) widget
).setText(str
);
150 else if (widget
instanceof Button
)
151 ((Button
) widget
).setText(str
);
152 else if (widget
instanceof Text
)
153 ((Text
) widget
).setText(str
);
155 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
159 /** A {@link Label} with markup activated. */
160 public static Label
lbl(Composite parent
, Object txt
) {
161 return text(new Label(parent
, SWT
.NONE
), txt
);
164 /** A read-only {@link Text} whose content can be copy/pasted. */
165 public static Text
txt(Composite parent
, Object txt
) {
166 return text(new Text(parent
, SWT
.NONE
), txt
);
169 public static void setItemHeight(Table table
, int height
) {
170 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
173 /** Dispose all children of a Composite */
174 public static void clear(Composite composite
) {
175 for (Control child
: composite
.getChildren())
182 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
183 if (has(parent
, child
))
184 return child(parent
, child
);
185 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
188 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
189 return parent
.getNode(en
.name());
192 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
193 return parent
.hasNode(en
.name());
196 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
197 return getOrAdd(parent
, en
, null);
200 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
202 return child(parent
, en
);
203 else if (primaryType
== null)
204 return parent
.addNode(en
.name());
206 return parent
.addNode(en
.name(), primaryType
);
210 public static String
img(String src
, String width
, String height
) {
211 return imgBuilder(src
, width
, height
).append("/>").toString();
214 public static String
img(String src
, Point size
) {
215 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
218 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
219 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
220 .append("' src='").append(src
).append("'");
223 public static String
noImg(Point size
) {
224 ResourceManager rm
= RWT
.getResourceManager();
225 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
228 public static String
noImg() {
229 return noImg(NO_IMAGE_SIZE
);
232 public static Image
noImage(Point size
) {
233 ResourceManager rm
= RWT
.getResourceManager();
234 InputStream in
= null;
236 in
= rm
.getRegisteredContent(NO_IMAGE
);
237 ImageData id
= new ImageData(in
);
238 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
239 Image image
= new Image(Display
.getCurrent(), scaled
);
244 } catch (IOException e
) {
250 /** Lorem ipsum text to be used during development. */
251 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
252 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
253 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
254 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
255 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
256 + " Pellentesque commodo turpis ac diam ultricies dignissim."
257 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
258 + " Integer varius quis est et tristique."
259 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
260 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
261 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
264 private CmsUiUtils() {