1 package org
.argeo
.cms
.util
;
3 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.WEBDAV_PRIVATE
;
4 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.WEBDAV_PUBLIC
;
5 import static org
.argeo
.jcr
.ArgeoJcrConstants
.ALIAS_NODE
;
7 import java
.io
.InputStream
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.servlet
.http
.HttpServletRequest
;
15 import org
.apache
.commons
.io
.IOUtils
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.cms
.CmsConstants
;
19 import org
.argeo
.cms
.CmsException
;
20 import org
.argeo
.cms
.CmsView
;
21 import org
.argeo
.cms
.auth
.CurrentUser
;
22 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.eclipse
.rap
.rwt
.RWT
;
25 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.graphics
.Image
;
28 import org
.eclipse
.swt
.graphics
.ImageData
;
29 import org
.eclipse
.swt
.graphics
.Point
;
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
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Table
;
37 import org
.eclipse
.swt
.widgets
.Widget
;
39 /** Static utilities for the CMS framework. */
40 public class CmsUtils
implements CmsConstants
{
41 private final static Log log
= LogFactory
.getLog(CmsUtils
.class);
44 * The CMS view related to this display, or null if none is available from
47 public static CmsView
getCmsView() {
48 return UiContext
.getData(CmsView
.KEY
);
51 public static String
getDataUrl(Node node
, HttpServletRequest request
)
52 throws RepositoryException
{
53 return request
.getRequestURL().append(getDataPath(node
).substring(1))
57 public static String
getDataPath(Node node
) throws RepositoryException
{
58 if (log
.isTraceEnabled())
59 log
.trace(CurrentUser
.getUsername() + " : " + node
.getPath());
60 StringBuilder buf
= new StringBuilder();
61 if (CurrentUser
.isAnonymous())
62 buf
.append(WEBDAV_PUBLIC
);
64 buf
.append(WEBDAV_PRIVATE
);
65 // TODO convey repo alias vie repository properties
66 return buf
.append('/').append(ALIAS_NODE
).append('/')
67 .append(node
.getSession().getWorkspace().getName())
68 .append(node
.getPath()).toString();
71 public static String
getCanonicalUrl(Node node
, HttpServletRequest request
)
72 throws RepositoryException
{
73 return request
.getRequestURL().append('!').append(node
.getPath())
77 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
79 public static RowData ROW_DATA_16px
= new RowData(16, 16);
81 public static GridLayout
noSpaceGridLayout() {
82 return noSpaceGridLayout(new GridLayout());
85 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
86 layout
.horizontalSpacing
= 0;
87 layout
.verticalSpacing
= 0;
88 layout
.marginWidth
= 0;
89 layout
.marginHeight
= 0;
96 public static GridData
fillWidth() {
97 return grabWidth(SWT
.FILL
, SWT
.FILL
);
100 public static GridData
fillAll() {
101 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
104 public static GridData
grabWidth(int horizontalAlignment
,
105 int verticalAlignment
) {
106 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
110 public static RowData
rowData16px() {
111 return new RowData(16, 16);
115 public static void style(Widget widget
, String style
) {
116 widget
.setData(CmsConstants
.STYLE
, style
);
119 /** Enable markups on widget */
120 public static void markup(Widget widget
) {
121 widget
.setData(CmsConstants
.MARKUP
, true);
124 public static void setItemHeight(Table table
, int height
) {
125 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
128 /** @return the path or null if not instrumented */
129 public static String
getDataPath(Widget widget
) {
131 Object data
= widget
.getData();
132 if (data
!= null && data
instanceof Item
) {
134 return ((Item
) data
).getPath();
135 } catch (RepositoryException e
) {
136 throw new CmsException("Cannot find data path of " + data
142 data
= widget
.getData(Property
.JCR_PATH
);
144 return data
.toString();
149 /** Dispose all children of a Composite */
150 public static void clear(Composite composite
) {
151 for (Control child
: composite
.getChildren())
158 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
159 throws RepositoryException
{
160 if (has(parent
, child
))
161 return child(parent
, child
);
162 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
165 public static Node
child(Node parent
, Enum
<?
> en
)
166 throws RepositoryException
{
167 return parent
.getNode(en
.name());
170 public static Boolean
has(Node parent
, Enum
<?
> en
)
171 throws RepositoryException
{
172 return parent
.hasNode(en
.name());
175 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
176 throws RepositoryException
{
177 return getOrAdd(parent
, en
, null);
180 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
181 throws RepositoryException
{
183 return child(parent
, en
);
184 else if (primaryType
== null)
185 return parent
.addNode(en
.name());
187 return parent
.addNode(en
.name(), primaryType
);
191 public static String
img(String src
, String width
, String height
) {
192 return imgBuilder(src
, width
, height
).append("/>").toString();
195 public static String
img(String src
, Point size
) {
196 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
199 public static StringBuilder
imgBuilder(String src
, String width
,
201 return new StringBuilder(64).append("<img width='").append(width
)
202 .append("' height='").append(height
).append("' src='")
203 .append(src
).append("'");
206 public static String
noImg(Point size
) {
207 ResourceManager rm
= RWT
.getResourceManager();
208 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
211 public static String
noImg() {
212 return noImg(NO_IMAGE_SIZE
);
215 public static Image
noImage(Point size
) {
216 ResourceManager rm
= RWT
.getResourceManager();
217 InputStream in
= null;
219 in
= rm
.getRegisteredContent(NO_IMAGE
);
220 ImageData id
= new ImageData(in
);
221 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
222 Image image
= new Image(Display
.getCurrent(), scaled
);
225 IOUtils
.closeQuietly(in
);