]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java
Improve CMS web app lifecycle.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / CmsUiUtils.java
1 package org.argeo.cms.ui.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.net.URLEncoder;
8 import java.nio.charset.StandardCharsets;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.StringTokenizer;
12
13 import javax.jcr.Node;
14 import javax.jcr.RepositoryException;
15 import javax.servlet.http.HttpServletRequest;
16
17 import org.argeo.api.NodeConstants;
18 import org.argeo.api.NodeUtils;
19 import org.argeo.cms.ui.CmsConstants;
20 import org.argeo.cms.ui.CmsView;
21 import org.argeo.eclipse.ui.Selected;
22 import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
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.events.SelectionListener;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.graphics.ImageData;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.FormAttachment;
32 import org.eclipse.swt.layout.FormData;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.layout.RowData;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.swt.widgets.Widget;
44
45 /** Static utilities for the CMS framework. */
46 public class CmsUiUtils implements CmsConstants {
47 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
48
49 /*
50 * CMS VIEW
51 */
52
53 /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */
54 public static void sendEventOnSelect(Control control, String topic, Map<String, Object> properties) {
55 SelectionListener listener = (Selected) (e) -> {
56 CmsView.getCmsView(control.getParent()).sendEvent(topic, properties);
57 };
58 if (control instanceof Button) {
59 ((Button) control).addSelectionListener(listener);
60 } else
61 throw new UnsupportedOperationException("Control type " + control.getClass() + " is not supported.");
62 }
63
64 /**
65 * Convenience method to sends an event via
66 * {@link CmsView#sendEvent(String, Map)}.
67 */
68 public static void sendEventOnSelect(Control control, String topic, String key, Object value) {
69 Map<String, Object> properties = new HashMap<>();
70 properties.put(key, value);
71 sendEventOnSelect(control, topic, properties);
72 }
73
74 /**
75 * The CMS view related to this display, or null if none is available from this
76 * call.
77 *
78 * @deprecated Use {@link CmsView#getCmsView(Composite)} instead.
79 */
80 @Deprecated
81 public static CmsView getCmsView() {
82 // return UiContext.getData(CmsView.class.getName());
83 return CmsView.getCmsView(Display.getCurrent().getActiveShell());
84 }
85
86 public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
87 try {
88 URL url = new URL(request.getRequestURL().toString());
89 StringBuilder buf = new StringBuilder();
90 buf.append(url.getProtocol()).append("://").append(url.getHost());
91 if (url.getPort() != -1)
92 buf.append(':').append(url.getPort());
93 return buf;
94 } catch (MalformedURLException e) {
95 throw new IllegalArgumentException("Cannot extract server base URL from " + request.getRequestURL(), e);
96 }
97 }
98
99 //
100 public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
101 try {
102 StringBuilder buf = getServerBaseUrl(request);
103 buf.append(getDataPath(node));
104 return new URL(buf.toString()).toString();
105 } catch (MalformedURLException e) {
106 throw new IllegalArgumentException("Cannot build data URL for " + node, e);
107 }
108 }
109
110 /** A path in the node repository */
111 public static String getDataPath(Node node) throws RepositoryException {
112 return getDataPath(NodeConstants.EGO_REPOSITORY, node);
113 }
114
115 public static String getDataPath(String cn, Node node) throws RepositoryException {
116 return NodeUtils.getDataPath(cn, node);
117 }
118
119 /** Clean reserved URL characters for use in HTTP links. */
120 public static String getDataPathForUrl(Node node) throws RepositoryException {
121 return cleanPathForUrl(getDataPath(node));
122 }
123
124 /** Clean reserved URL characters for use in HTTP links. */
125 public static String cleanPathForUrl(String path) throws RepositoryException {
126 StringTokenizer st = new StringTokenizer(path, "/");
127 StringBuilder sb = new StringBuilder();
128 while (st.hasMoreElements()) {
129 sb.append('/');
130 String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8);
131 encoded = encoded.replace("+", "%20");
132 sb.append(encoded);
133
134 }
135 return sb.toString();
136 }
137
138 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
139 @Deprecated
140 public static RowData ROW_DATA_16px = new RowData(16, 16);
141
142 /*
143 * GRID LAYOUT
144 */
145 public static GridLayout noSpaceGridLayout() {
146 return noSpaceGridLayout(new GridLayout());
147 }
148
149 public static GridLayout noSpaceGridLayout(int columns) {
150 return noSpaceGridLayout(new GridLayout(columns, false));
151 }
152
153 public static GridLayout noSpaceGridLayout(GridLayout layout) {
154 layout.horizontalSpacing = 0;
155 layout.verticalSpacing = 0;
156 layout.marginWidth = 0;
157 layout.marginHeight = 0;
158 return layout;
159 }
160
161 public static GridData fillAll() {
162 return new GridData(SWT.FILL, SWT.FILL, true, true);
163 }
164
165 public static GridData fillWidth() {
166 return grabWidth(SWT.FILL, SWT.FILL);
167 }
168
169 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
170 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
171 }
172
173 public static GridData fillHeight() {
174 return grabHeight(SWT.FILL, SWT.FILL);
175 }
176
177 public static GridData grabHeight(int horizontalAlignment, int verticalAlignment) {
178 return new GridData(horizontalAlignment, horizontalAlignment, false, true);
179 }
180
181 /*
182 * ROW LAYOUT
183 */
184 public static RowData rowData16px() {
185 return new RowData(16, 16);
186 }
187
188 /*
189 * FORM LAYOUT
190 */
191
192 public static FormData coverAll() {
193 FormData fdLabel = new FormData();
194 fdLabel.top = new FormAttachment(0, 0);
195 fdLabel.left = new FormAttachment(0, 0);
196 fdLabel.right = new FormAttachment(100, 0);
197 fdLabel.bottom = new FormAttachment(100, 0);
198 return fdLabel;
199 }
200
201 /*
202 * STYLING
203 */
204
205 /** Style widget */
206 public static <T extends Widget> T style(T widget, String style) {
207 if (style == null)
208 return widget;// does nothing
209 EclipseUiSpecificUtils.setStyleData(widget, style);
210 if (widget instanceof Control) {
211 CmsView.getCmsView((Control) widget).applyStyles(widget);
212 }
213 return widget;
214 }
215
216 /** Style widget */
217 public static <T extends Widget> T style(T widget, CmsStyle style) {
218 return style(widget, style.toStyleClass());
219 }
220
221 /** Enable markups on widget */
222 public static <T extends Widget> T markup(T widget) {
223 EclipseUiSpecificUtils.setMarkupData(widget);
224 return widget;
225 }
226
227 /**
228 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
229 *
230 * @param widget the widget to style and to use in order to display text
231 * @param txt the object to display via its <code>toString()</code> method.
232 * This argument should not be null, but if it is null and
233 * assertions are disabled "<null>" is displayed instead; if
234 * assertions are enabled the call will fail.
235 *
236 * @see #markup(Widget)
237 */
238 public static <T extends Widget> T text(T widget, Object txt) {
239 assert txt != null;
240 String str = txt != null ? txt.toString() : "<null>";
241 markup(widget);
242 if (widget instanceof Label)
243 ((Label) widget).setText(str);
244 else if (widget instanceof Button)
245 ((Button) widget).setText(str);
246 else if (widget instanceof Text)
247 ((Text) widget).setText(str);
248 else
249 throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
250 return widget;
251 }
252
253 /** A {@link Label} with markup activated. */
254 public static Label lbl(Composite parent, Object txt) {
255 return text(new Label(parent, SWT.NONE), txt);
256 }
257
258 /** A read-only {@link Text} whose content can be copy/pasted. */
259 public static Text txt(Composite parent, Object txt) {
260 return text(new Text(parent, SWT.NONE), txt);
261 }
262
263 @Deprecated
264 public static void setItemHeight(Table table, int height) {
265 table.setData(CmsConstants.ITEM_HEIGHT, height);
266 }
267
268 /** Dispose all children of a Composite */
269 public static void clear(Composite composite) {
270 if (composite.isDisposed())
271 return;
272 for (Control child : composite.getChildren())
273 child.dispose();
274 }
275
276 //
277 // JCR
278 //
279 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
280 if (has(parent, child))
281 return child(parent, child);
282 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
283 }
284
285 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
286 return parent.getNode(en.name());
287 }
288
289 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
290 return parent.hasNode(en.name());
291 }
292
293 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
294 return getOrAdd(parent, en, null);
295 }
296
297 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
298 if (has(parent, en))
299 return child(parent, en);
300 else if (primaryType == null)
301 return parent.addNode(en.name());
302 else
303 return parent.addNode(en.name(), primaryType);
304 }
305
306 // IMAGES
307 public static String img(Node fileNode, String width, String height) {
308 return img(null, fileNode, width, height);
309 }
310
311 public static String img(String serverBase, Node fileNode, String width, String height) {
312 String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
313 return imgBuilder(src, width, height).append("/>").toString();
314 }
315
316 public static String img(String src, String width, String height) {
317 return imgBuilder(src, width, height).append("/>").toString();
318 }
319
320 public static String img(String src, Point size) {
321 return img(src, Integer.toString(size.x), Integer.toString(size.y));
322 }
323
324 public static StringBuilder imgBuilder(String src, String width, String height) {
325 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
326 .append("' src='").append(src).append("'");
327 }
328
329 public static String noImg(Point size) {
330 ResourceManager rm = RWT.getResourceManager();
331 return CmsUiUtils.img(rm.getLocation(NO_IMAGE), size);
332 }
333
334 public static String noImg() {
335 return noImg(NO_IMAGE_SIZE);
336 }
337
338 public static Image noImage(Point size) {
339 ResourceManager rm = RWT.getResourceManager();
340 InputStream in = null;
341 try {
342 in = rm.getRegisteredContent(NO_IMAGE);
343 ImageData id = new ImageData(in);
344 ImageData scaled = id.scaledTo(size.x, size.y);
345 Image image = new Image(Display.getCurrent(), scaled);
346 return image;
347 } finally {
348 try {
349 in.close();
350 } catch (IOException e) {
351 // silent
352 }
353 }
354 }
355
356 /** Lorem ipsum text to be used during development. */
357 public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
358 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
359 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
360 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
361 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
362 + " Pellentesque commodo turpis ac diam ultricies dignissim."
363 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
364 + " Integer varius quis est et tristique."
365 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
366 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
367 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
368
369 /** Singleton. */
370 private CmsUiUtils() {
371 }
372
373 }