]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUtils.java
2788ed9d34205c3336811471a4262cfe4e28c1d7
[lgpl/argeo-commons.git] / CmsUtils.java
1 package org.argeo.cms.util;
2
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;
6
7 import java.io.InputStream;
8
9 import javax.jcr.Item;
10 import javax.jcr.Node;
11 import javax.jcr.Property;
12 import javax.jcr.RepositoryException;
13 import javax.servlet.http.HttpServletRequest;
14
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;
38
39 /** Static utilities for the CMS framework. */
40 public class CmsUtils implements CmsConstants {
41 private final static Log log = LogFactory.getLog(CmsUtils.class);
42
43 /**
44 * The CMS view related to this display, or null if none is available from
45 * this call.
46 */
47 public static CmsView getCmsView() {
48 return UiContext.getData(CmsView.KEY);
49 }
50
51 public static String getDataUrl(Node node, HttpServletRequest request)
52 throws RepositoryException {
53 return request.getRequestURL().append(getDataPath(node).substring(1))
54 .toString();
55 }
56
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);
63 else
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();
69 }
70
71 public static String getCanonicalUrl(Node node, HttpServletRequest request)
72 throws RepositoryException {
73 return request.getRequestURL().append('!').append(node.getPath())
74 .toString();
75 }
76
77 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
78 @Deprecated
79 public static RowData ROW_DATA_16px = new RowData(16, 16);
80
81 public static GridLayout noSpaceGridLayout() {
82 return noSpaceGridLayout(new GridLayout());
83 }
84
85 public static GridLayout noSpaceGridLayout(GridLayout layout) {
86 layout.horizontalSpacing = 0;
87 layout.verticalSpacing = 0;
88 layout.marginWidth = 0;
89 layout.marginHeight = 0;
90 return layout;
91 }
92
93 //
94 // GRID DATA
95 //
96 public static GridData fillWidth() {
97 return grabWidth(SWT.FILL, SWT.FILL);
98 }
99
100 public static GridData fillAll() {
101 return new GridData(SWT.FILL, SWT.FILL, true, true);
102 }
103
104 public static GridData grabWidth(int horizontalAlignment,
105 int verticalAlignment) {
106 return new GridData(horizontalAlignment, horizontalAlignment, true,
107 false);
108 }
109
110 public static RowData rowData16px() {
111 return new RowData(16, 16);
112 }
113
114 /** Style widget */
115 public static void style(Widget widget, String style) {
116 widget.setData(CmsConstants.STYLE, style);
117 }
118
119 /** Enable markups on widget */
120 public static void markup(Widget widget) {
121 widget.setData(CmsConstants.MARKUP, true);
122 }
123
124 public static void setItemHeight(Table table, int height) {
125 table.setData(CmsConstants.ITEM_HEIGHT, height);
126 }
127
128 /** @return the path or null if not instrumented */
129 public static String getDataPath(Widget widget) {
130 // JCR item
131 Object data = widget.getData();
132 if (data != null && data instanceof Item) {
133 try {
134 return ((Item) data).getPath();
135 } catch (RepositoryException e) {
136 throw new CmsException("Cannot find data path of " + data
137 + " for " + widget);
138 }
139 }
140
141 // JCR path
142 data = widget.getData(Property.JCR_PATH);
143 if (data != null)
144 return data.toString();
145
146 return null;
147 }
148
149 /** Dispose all children of a Composite */
150 public static void clear(Composite composite) {
151 for (Control child : composite.getChildren())
152 child.dispose();
153 }
154
155 //
156 // JCR
157 //
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]);
163 }
164
165 public static Node child(Node parent, Enum<?> en)
166 throws RepositoryException {
167 return parent.getNode(en.name());
168 }
169
170 public static Boolean has(Node parent, Enum<?> en)
171 throws RepositoryException {
172 return parent.hasNode(en.name());
173 }
174
175 public static Node getOrAdd(Node parent, Enum<?> en)
176 throws RepositoryException {
177 return getOrAdd(parent, en, null);
178 }
179
180 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType)
181 throws RepositoryException {
182 if (has(parent, en))
183 return child(parent, en);
184 else if (primaryType == null)
185 return parent.addNode(en.name());
186 else
187 return parent.addNode(en.name(), primaryType);
188 }
189
190 // IMAGES
191 public static String img(String src, String width, String height) {
192 return imgBuilder(src, width, height).append("/>").toString();
193 }
194
195 public static String img(String src, Point size) {
196 return img(src, Integer.toString(size.x), Integer.toString(size.y));
197 }
198
199 public static StringBuilder imgBuilder(String src, String width,
200 String height) {
201 return new StringBuilder(64).append("<img width='").append(width)
202 .append("' height='").append(height).append("' src='")
203 .append(src).append("'");
204 }
205
206 public static String noImg(Point size) {
207 ResourceManager rm = RWT.getResourceManager();
208 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
209 }
210
211 public static String noImg() {
212 return noImg(NO_IMAGE_SIZE);
213 }
214
215 public static Image noImage(Point size) {
216 ResourceManager rm = RWT.getResourceManager();
217 InputStream in = null;
218 try {
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);
223 return image;
224 } finally {
225 IOUtils.closeQuietly(in);
226 }
227 }
228
229 private CmsUtils() {
230 }
231 }