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
.jcr
.JcrUtils
;
18 import org
.eclipse
.rap
.rwt
.RWT
;
19 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.graphics
.Image
;
22 import org
.eclipse
.swt
.graphics
.ImageData
;
23 import org
.eclipse
.swt
.graphics
.Point
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.layout
.RowData
;
27 import org
.eclipse
.swt
.widgets
.Button
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Table
;
33 import org
.eclipse
.swt
.widgets
.Text
;
34 import org
.eclipse
.swt
.widgets
.Widget
;
36 /** Static utilities for the CMS framework. */
37 public class CmsUiUtils
implements CmsConstants
{
38 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
41 * The CMS view related to this display, or null if none is available from this
44 * @deprecated Use {@link CmsView#getCmsView(Composite)} instead.
47 public static CmsView
getCmsView() {
48 // return UiContext.getData(CmsView.class.getName());
49 return CmsView
.getCmsView(Display
.getCurrent().getActiveShell());
52 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
54 URL url
= new URL(request
.getRequestURL().toString());
55 StringBuilder buf
= new StringBuilder();
56 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
57 if (url
.getPort() != -1)
58 buf
.append(':').append(url
.getPort());
60 } catch (MalformedURLException e
) {
61 throw new CmsException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
66 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
68 StringBuilder buf
= getServerBaseUrl(request
);
69 buf
.append(getDataPath(node
));
70 return new URL(buf
.toString()).toString();
71 } catch (MalformedURLException e
) {
72 throw new CmsException("Cannot build data URL for " + node
, e
);
76 /** A path in the node repository */
77 public static String
getDataPath(Node node
) throws RepositoryException
{
78 return getDataPath(NodeConstants
.EGO_REPOSITORY
, node
);
81 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
82 return NodeUtils
.getDataPath(cn
, node
);
85 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
87 public static RowData ROW_DATA_16px
= new RowData(16, 16);
89 public static GridLayout
noSpaceGridLayout() {
90 return noSpaceGridLayout(new GridLayout());
93 public static GridLayout
noSpaceGridLayout(int columns
) {
94 return noSpaceGridLayout(new GridLayout(columns
, false));
97 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
98 layout
.horizontalSpacing
= 0;
99 layout
.verticalSpacing
= 0;
100 layout
.marginWidth
= 0;
101 layout
.marginHeight
= 0;
108 public static GridData
fillAll() {
109 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
112 public static GridData
fillWidth() {
113 return grabWidth(SWT
.FILL
, SWT
.FILL
);
116 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
117 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
120 public static GridData
fillHeight() {
121 return grabHeight(SWT
.FILL
, SWT
.FILL
);
124 public static GridData
grabHeight(int horizontalAlignment
, int verticalAlignment
) {
125 return new GridData(horizontalAlignment
, horizontalAlignment
, false, true);
128 public static RowData
rowData16px() {
129 return new RowData(16, 16);
133 public static <T
extends Widget
> T
style(T widget
, String style
) {
134 widget
.setData(CmsConstants
.STYLE
, style
);
139 public static <T
extends Widget
> T
style(T widget
, CmsStyle style
) {
140 widget
.setData(CmsConstants
.STYLE
, style
.toStyleClass());
144 /** Enable markups on widget */
145 public static <T
extends Widget
> T
markup(T widget
) {
146 widget
.setData(CmsConstants
.MARKUP
, true);
151 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
153 * @param widget the widget to style and to use in order to display text
154 * @param txt the object to display via its <code>toString()</code> method.
155 * This argument should not be null, but if it is null and
156 * assertions are disabled "<null>" is displayed instead; if
157 * assertions are enabled the call will fail.
159 * @see #markup(Widget)
161 public static <T
extends Widget
> T
text(T widget
, Object txt
) {
163 String str
= txt
!= null ? txt
.toString() : "<null>";
165 if (widget
instanceof Label
)
166 ((Label
) widget
).setText(str
);
167 else if (widget
instanceof Button
)
168 ((Button
) widget
).setText(str
);
169 else if (widget
instanceof Text
)
170 ((Text
) widget
).setText(str
);
172 throw new IllegalArgumentException("Unsupported widget type " + widget
.getClass());
176 /** A {@link Label} with markup activated. */
177 public static Label
lbl(Composite parent
, Object txt
) {
178 return text(new Label(parent
, SWT
.NONE
), txt
);
181 /** A read-only {@link Text} whose content can be copy/pasted. */
182 public static Text
txt(Composite parent
, Object txt
) {
183 return text(new Text(parent
, SWT
.NONE
), txt
);
186 public static void setItemHeight(Table table
, int height
) {
187 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
190 /** Dispose all children of a Composite */
191 public static void clear(Composite composite
) {
192 for (Control child
: composite
.getChildren())
199 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
200 if (has(parent
, child
))
201 return child(parent
, child
);
202 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
205 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
206 return parent
.getNode(en
.name());
209 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
210 return parent
.hasNode(en
.name());
213 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
214 return getOrAdd(parent
, en
, null);
217 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
219 return child(parent
, en
);
220 else if (primaryType
== null)
221 return parent
.addNode(en
.name());
223 return parent
.addNode(en
.name(), primaryType
);
227 public static String
img(String src
, String width
, String height
) {
228 return imgBuilder(src
, width
, height
).append("/>").toString();
231 public static String
img(String src
, Point size
) {
232 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
235 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
236 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
237 .append("' src='").append(src
).append("'");
240 public static String
noImg(Point size
) {
241 ResourceManager rm
= RWT
.getResourceManager();
242 return CmsUiUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
245 public static String
noImg() {
246 return noImg(NO_IMAGE_SIZE
);
249 public static Image
noImage(Point size
) {
250 ResourceManager rm
= RWT
.getResourceManager();
251 InputStream in
= null;
253 in
= rm
.getRegisteredContent(NO_IMAGE
);
254 ImageData id
= new ImageData(in
);
255 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
256 Image image
= new Image(Display
.getCurrent(), scaled
);
261 } catch (IOException e
) {
267 /** Lorem ipsum text to be used during development. */
268 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
269 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
270 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
271 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
272 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
273 + " Pellentesque commodo turpis ac diam ultricies dignissim."
274 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
275 + " Integer varius quis est et tristique."
276 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
277 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
278 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
281 private CmsUiUtils() {