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