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