]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUiUtils.java
33377b6ca365f1db0c3e57c258d733071d7fec67
[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.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.CmsException;
17 import org.argeo.cms.ui.CmsConstants;
18 import org.argeo.cms.ui.CmsView;
19 import org.argeo.eclipse.ui.Selected;
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 CmsException("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 CmsException("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 GridLayout standardSpaceGridLayout(GridLayout layout) {
140 layout.horizontalSpacing = 16;
141 layout.verticalSpacing = 16;
142 layout.marginWidth = 16;
143 layout.marginHeight = 16;
144 return layout;
145 }
146
147
148 public static GridData fillAll() {
149 return new GridData(SWT.FILL, SWT.FILL, true, true);
150 }
151
152 public static GridData fillWidth() {
153 return grabWidth(SWT.FILL, SWT.FILL);
154 }
155
156 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
157 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
158 }
159
160 public static GridData fillHeight() {
161 return grabHeight(SWT.FILL, SWT.FILL);
162 }
163
164 public static GridData grabHeight(int horizontalAlignment, int verticalAlignment) {
165 return new GridData(horizontalAlignment, horizontalAlignment, false, true);
166 }
167
168 public static RowData rowData16px() {
169 return new RowData(16, 16);
170 }
171
172 /*
173 * FORM LAYOUT
174 */
175
176 public static FormData coversAll() {
177 FormData fdLabel = new FormData();
178 fdLabel.top = new FormAttachment(0, 0);
179 fdLabel.left = new FormAttachment(0, 0);
180 fdLabel.right = new FormAttachment(100, 0);
181 fdLabel.bottom = new FormAttachment(100, 0);
182 return fdLabel;
183 }
184
185 /*
186 * STYLING
187 */
188
189 /** Style widget */
190 public static <T extends Widget> T style(T widget, String style) {
191 widget.setData(CmsConstants.STYLE, style);
192 return widget;
193 }
194
195 /** Style widget */
196 public static <T extends Widget> T style(T widget, CmsStyle style) {
197 widget.setData(CmsConstants.STYLE, style.toStyleClass());
198 return widget;
199 }
200
201 /** Enable markups on widget */
202 public static <T extends Widget> T markup(T widget) {
203 widget.setData(CmsConstants.MARKUP, true);
204 return widget;
205 }
206
207 /**
208 * Apply markup and set text on {@link Label}, {@link Button}, {@link Text}.
209 *
210 * @param widget the widget to style and to use in order to display text
211 * @param txt the object to display via its <code>toString()</code> method.
212 * This argument should not be null, but if it is null and
213 * assertions are disabled "<null>" is displayed instead; if
214 * assertions are enabled the call will fail.
215 *
216 * @see #markup(Widget)
217 */
218 public static <T extends Widget> T text(T widget, Object txt) {
219 assert txt != null;
220 String str = txt != null ? txt.toString() : "<null>";
221 markup(widget);
222 if (widget instanceof Label)
223 ((Label) widget).setText(str);
224 else if (widget instanceof Button)
225 ((Button) widget).setText(str);
226 else if (widget instanceof Text)
227 ((Text) widget).setText(str);
228 else
229 throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
230 return widget;
231 }
232
233 /** A {@link Label} with markup activated. */
234 public static Label lbl(Composite parent, Object txt) {
235 return text(new Label(parent, SWT.NONE), txt);
236 }
237
238 /** A read-only {@link Text} whose content can be copy/pasted. */
239 public static Text txt(Composite parent, Object txt) {
240 return text(new Text(parent, SWT.NONE), txt);
241 }
242
243 public static void setItemHeight(Table table, int height) {
244 table.setData(CmsConstants.ITEM_HEIGHT, height);
245 }
246
247 /** Dispose all children of a Composite */
248 public static void clear(Composite composite) {
249 for (Control child : composite.getChildren())
250 child.dispose();
251 }
252
253 //
254 // JCR
255 //
256 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
257 if (has(parent, child))
258 return child(parent, child);
259 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
260 }
261
262 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
263 return parent.getNode(en.name());
264 }
265
266 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
267 return parent.hasNode(en.name());
268 }
269
270 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
271 return getOrAdd(parent, en, null);
272 }
273
274 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
275 if (has(parent, en))
276 return child(parent, en);
277 else if (primaryType == null)
278 return parent.addNode(en.name());
279 else
280 return parent.addNode(en.name(), primaryType);
281 }
282
283 // IMAGES
284 public static String img(String src, String width, String height) {
285 return imgBuilder(src, width, height).append("/>").toString();
286 }
287
288 public static String img(String src, Point size) {
289 return img(src, Integer.toString(size.x), Integer.toString(size.y));
290 }
291
292 public static StringBuilder imgBuilder(String src, String width, String height) {
293 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
294 .append("' src='").append(src).append("'");
295 }
296
297 public static String noImg(Point size) {
298 ResourceManager rm = RWT.getResourceManager();
299 return CmsUiUtils.img(rm.getLocation(NO_IMAGE), size);
300 }
301
302 public static String noImg() {
303 return noImg(NO_IMAGE_SIZE);
304 }
305
306 public static Image noImage(Point size) {
307 ResourceManager rm = RWT.getResourceManager();
308 InputStream in = null;
309 try {
310 in = rm.getRegisteredContent(NO_IMAGE);
311 ImageData id = new ImageData(in);
312 ImageData scaled = id.scaledTo(size.x, size.y);
313 Image image = new Image(Display.getCurrent(), scaled);
314 return image;
315 } finally {
316 try {
317 in.close();
318 } catch (IOException e) {
319 // silent
320 }
321 }
322 }
323
324 /** Lorem ipsum text to be used during development. */
325 public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
326 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
327 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
328 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
329 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
330 + " Pellentesque commodo turpis ac diam ultricies dignissim."
331 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
332 + " Integer varius quis est et tristique."
333 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
334 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
335 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
336
337 /** Singleton. */
338 private CmsUiUtils() {
339 }
340 }