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