]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java
[maven-release-plugin] prepare release argeo-commons-2.1.46
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / CmsUtils.java
1 package org.argeo.cms.util;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.jcr.Item;
8 import javax.jcr.Node;
9 import javax.jcr.Property;
10 import javax.jcr.RepositoryException;
11 import javax.servlet.http.HttpServletRequest;
12
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.cms.CmsException;
17 import org.argeo.cms.auth.AuthConstants;
18 import org.argeo.cms.ui.CmsConstants;
19 import org.argeo.cms.ui.CmsView;
20 import org.argeo.eclipse.ui.specific.UiContext;
21 import org.argeo.jcr.JcrUtils;
22 import org.argeo.node.NodeAuthenticated;
23 import org.argeo.node.NodeConstants;
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(NodeAuthenticated.KEY);
49 }
50
51 public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
52 try {
53 URL url = new URL(request.getRequestURL().toString());
54 StringBuilder buf = new StringBuilder();
55 buf.append(url.getProtocol()).append("://").append(url.getHost());
56 if (url.getPort() != -1)
57 buf.append(':').append(url.getPort());
58 return buf;
59 } catch (MalformedURLException e) {
60 throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e);
61 }
62 }
63 //
64 public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
65 try {
66 StringBuilder buf = getServerBaseUrl(request);
67 buf.append(getDataPath(node));
68 return new URL(buf.toString()).toString();
69 } catch (MalformedURLException e) {
70 throw new CmsException("Cannot build data URL for " + node, e);
71 }
72 }
73 // FIXME
74 private final static String PATH_DATA = "/data";
75 private final static String WEBDAV_PUBLIC = PATH_DATA + "/public";
76 private final static String WEBDAV_PRIVATE = PATH_DATA + "/files";
77 public static String getDataPath(Node node) throws RepositoryException {
78 assert node != null;
79 String userId = node.getSession().getUserID();
80 if (log.isTraceEnabled())
81 log.trace(userId + " : " + node.getPath());
82 StringBuilder buf = new StringBuilder();
83 boolean isAnonymous = userId.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS);
84 if (isAnonymous)
85 buf.append(WEBDAV_PUBLIC);
86 else
87 buf.append(WEBDAV_PRIVATE);
88 // TODO convey repo alias vie repository properties
89 return buf.append('/').append(NodeConstants.ALIAS_NODE).append('/').append(node.getSession().getWorkspace().getName())
90 .append(node.getPath()).toString();
91 }
92 //
93 // public static String getCanonicalUrl(Node node, HttpServletRequest request) throws RepositoryException {
94 // try {
95 // StringBuilder buf = getServerBaseUrl(request);
96 // buf.append('/').append('!').append(node.getPath());
97 // return new URL(buf.toString()).toString();
98 // } catch (MalformedURLException e) {
99 // throw new CmsException("Cannot build data URL for " + node, e);
100 // }
101 // // return request.getRequestURL().append('!').append(node.getPath())
102 // // .toString();
103 // }
104
105 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
106 @Deprecated
107 public static RowData ROW_DATA_16px = new RowData(16, 16);
108
109 public static GridLayout noSpaceGridLayout() {
110 return noSpaceGridLayout(new GridLayout());
111 }
112
113 public static GridLayout noSpaceGridLayout(GridLayout layout) {
114 layout.horizontalSpacing = 0;
115 layout.verticalSpacing = 0;
116 layout.marginWidth = 0;
117 layout.marginHeight = 0;
118 return layout;
119 }
120
121 //
122 // GRID DATA
123 //
124 public static GridData fillWidth() {
125 return grabWidth(SWT.FILL, SWT.FILL);
126 }
127
128 public static GridData fillAll() {
129 return new GridData(SWT.FILL, SWT.FILL, true, true);
130 }
131
132 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
133 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
134 }
135
136 public static RowData rowData16px() {
137 return new RowData(16, 16);
138 }
139
140 /** Style widget */
141 public static void style(Widget widget, String style) {
142 widget.setData(CmsConstants.STYLE, style);
143 }
144
145 /** Enable markups on widget */
146 public static void markup(Widget widget) {
147 widget.setData(CmsConstants.MARKUP, true);
148 }
149
150 public static void setItemHeight(Table table, int height) {
151 table.setData(CmsConstants.ITEM_HEIGHT, height);
152 }
153
154 /** @return the path or null if not instrumented */
155 public static String getDataPath(Widget widget) {
156 // JCR item
157 Object data = widget.getData();
158 if (data != null && data instanceof Item) {
159 try {
160 return ((Item) data).getPath();
161 } catch (RepositoryException e) {
162 throw new CmsException("Cannot find data path of " + data + " for " + widget);
163 }
164 }
165
166 // JCR path
167 data = widget.getData(Property.JCR_PATH);
168 if (data != null)
169 return data.toString();
170
171 return null;
172 }
173
174 /** Dispose all children of a Composite */
175 public static void clear(Composite composite) {
176 for (Control child : composite.getChildren())
177 child.dispose();
178 }
179
180 //
181 // JCR
182 //
183 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
184 if (has(parent, child))
185 return child(parent, child);
186 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
187 }
188
189 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
190 return parent.getNode(en.name());
191 }
192
193 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
194 return parent.hasNode(en.name());
195 }
196
197 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
198 return getOrAdd(parent, en, null);
199 }
200
201 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
202 if (has(parent, en))
203 return child(parent, en);
204 else if (primaryType == null)
205 return parent.addNode(en.name());
206 else
207 return parent.addNode(en.name(), primaryType);
208 }
209
210 // IMAGES
211 public static String img(String src, String width, String height) {
212 return imgBuilder(src, width, height).append("/>").toString();
213 }
214
215 public static String img(String src, Point size) {
216 return img(src, Integer.toString(size.x), Integer.toString(size.y));
217 }
218
219 public static StringBuilder imgBuilder(String src, String width, String height) {
220 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
221 .append("' src='").append(src).append("'");
222 }
223
224 public static String noImg(Point size) {
225 ResourceManager rm = RWT.getResourceManager();
226 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
227 }
228
229 public static String noImg() {
230 return noImg(NO_IMAGE_SIZE);
231 }
232
233 public static Image noImage(Point size) {
234 ResourceManager rm = RWT.getResourceManager();
235 InputStream in = null;
236 try {
237 in = rm.getRegisteredContent(NO_IMAGE);
238 ImageData id = new ImageData(in);
239 ImageData scaled = id.scaledTo(size.x, size.y);
240 Image image = new Image(Display.getCurrent(), scaled);
241 return image;
242 } finally {
243 IOUtils.closeQuietly(in);
244 }
245 }
246
247 private CmsUtils() {
248 }
249 }