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