1 package org
.argeo
.cms
.util
;
3 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.WEBDAV_PUBLIC
;
4 import static org
.argeo
.jcr
.ArgeoJcrConstants
.ALIAS_NODE
;
6 import java
.io
.InputStream
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.servlet
.http
.HttpServletRequest
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.argeo
.cms
.CmsConstants
;
16 import org
.argeo
.cms
.CmsException
;
17 import org
.argeo
.cms
.CmsView
;
18 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.eclipse
.rap
.rwt
.RWT
;
21 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.graphics
.Image
;
24 import org
.eclipse
.swt
.graphics
.ImageData
;
25 import org
.eclipse
.swt
.graphics
.Point
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.layout
.RowData
;
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
.Table
;
33 import org
.eclipse
.swt
.widgets
.Widget
;
35 /** Static utilities for the CMS framework. */
36 public class CmsUtils
implements CmsConstants
{
38 * The CMS view related to this display, or null if none is available from
41 public static CmsView
getCmsView() {
42 return UiContext
.getData(CmsView
.KEY
);
45 public static String
getDataUrl(Node node
, HttpServletRequest request
)
46 throws RepositoryException
{
47 return request
.getRequestURL().append(getDataPath(node
).substring(1))
51 public static String
getDataPath(Node node
) throws RepositoryException
{
52 return new StringBuilder().append(WEBDAV_PUBLIC
).append('/')
53 .append(ALIAS_NODE
+ "/")
54 .append(node
.getSession().getWorkspace().getName())
55 .append(node
.getPath()).toString();
58 public static String
getCanonicalUrl(Node node
, HttpServletRequest request
)
59 throws RepositoryException
{
60 return request
.getRequestURL().append('!').append(node
.getPath())
64 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
66 public static RowData ROW_DATA_16px
= new RowData(16, 16);
68 public static GridLayout
noSpaceGridLayout() {
69 return noSpaceGridLayout(new GridLayout());
72 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
73 layout
.horizontalSpacing
= 0;
74 layout
.verticalSpacing
= 0;
75 layout
.marginWidth
= 0;
76 layout
.marginHeight
= 0;
83 public static GridData
fillWidth() {
84 return grabWidth(SWT
.FILL
, SWT
.FILL
);
87 public static GridData
fillAll() {
88 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
91 public static GridData
grabWidth(int horizontalAlignment
,
92 int verticalAlignment
) {
93 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
97 public static RowData
rowData16px() {
98 return new RowData(16, 16);
102 public static void style(Widget widget
, String style
) {
103 widget
.setData(CmsConstants
.STYLE
, style
);
106 /** Enable markups on widget */
107 public static void markup(Widget widget
) {
108 widget
.setData(CmsConstants
.MARKUP
, true);
111 public static void setItemHeight(Table table
, int height
) {
112 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
115 /** @return the path or null if not instrumented */
116 public static String
getDataPath(Widget widget
) {
118 Object data
= widget
.getData();
119 if (data
!= null && data
instanceof Item
) {
121 return ((Item
) data
).getPath();
122 } catch (RepositoryException e
) {
123 throw new CmsException("Cannot find data path of " + data
129 data
= widget
.getData(Property
.JCR_PATH
);
131 return data
.toString();
136 /** Dispose all children of a Composite */
137 public static void clear(Composite composite
) {
138 for (Control child
: composite
.getChildren())
145 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
146 throws RepositoryException
{
147 if (has(parent
, child
))
148 return child(parent
, child
);
149 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
152 public static Node
child(Node parent
, Enum
<?
> en
)
153 throws RepositoryException
{
154 return parent
.getNode(en
.name());
157 public static Boolean
has(Node parent
, Enum
<?
> en
)
158 throws RepositoryException
{
159 return parent
.hasNode(en
.name());
162 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
163 throws RepositoryException
{
164 return getOrAdd(parent
, en
, null);
167 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
168 throws RepositoryException
{
170 return child(parent
, en
);
171 else if (primaryType
== null)
172 return parent
.addNode(en
.name());
174 return parent
.addNode(en
.name(), primaryType
);
178 public static String
img(String src
, String width
, String height
) {
179 return imgBuilder(src
, width
, height
).append("/>").toString();
182 public static String
img(String src
, Point size
) {
183 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
186 public static StringBuilder
imgBuilder(String src
, String width
,
188 return new StringBuilder(64).append("<img width='").append(width
)
189 .append("' height='").append(height
).append("' src='")
190 .append(src
).append("'");
193 public static String
noImg(Point size
) {
194 ResourceManager rm
= RWT
.getResourceManager();
195 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
198 public static String
noImg() {
199 return noImg(NO_IMAGE_SIZE
);
202 public static Image
noImage(Point size
) {
203 ResourceManager rm
= RWT
.getResourceManager();
204 InputStream in
= null;
206 in
= rm
.getRegisteredContent(NO_IMAGE
);
207 ImageData id
= new ImageData(in
);
208 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
209 Image image
= new Image(Display
.getCurrent(), scaled
);
212 IOUtils
.closeQuietly(in
);