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