]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java
[maven-release-plugin] prepare release argeo-commons-2.1.84
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / CmsUtils.java
1 package org.argeo.cms.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.cms.CmsException;
13 import org.argeo.cms.ui.CmsConstants;
14 import org.argeo.cms.ui.CmsView;
15 import org.argeo.eclipse.ui.specific.UiContext;
16 import org.argeo.jcr.JcrUtils;
17 import org.argeo.node.NodeConstants;
18 import org.argeo.node.NodeUtils;
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 CmsUtils implements CmsConstants {
39 // private final static Log log = LogFactory.getLog(CmsUtils.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.NODE, 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 * @see #markup(Widget)
137 */
138 public static <T extends Widget> T text(T widget, String txt) {
139 markup(widget);
140 if (widget instanceof Label)
141 ((Label) widget).setText(txt);
142 else if (widget instanceof Button)
143 ((Button) widget).setText(txt);
144 else if (widget instanceof Text)
145 ((Text) widget).setText(txt);
146 else
147 throw new IllegalArgumentException("Unsupported widget type " + widget.getClass());
148 return widget;
149 }
150
151 public static void setItemHeight(Table table, int height) {
152 table.setData(CmsConstants.ITEM_HEIGHT, height);
153 }
154
155 /** Dispose all children of a Composite */
156 public static void clear(Composite composite) {
157 for (Control child : composite.getChildren())
158 child.dispose();
159 }
160
161 //
162 // JCR
163 //
164 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
165 if (has(parent, child))
166 return child(parent, child);
167 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
168 }
169
170 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
171 return parent.getNode(en.name());
172 }
173
174 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
175 return parent.hasNode(en.name());
176 }
177
178 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
179 return getOrAdd(parent, en, null);
180 }
181
182 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
183 if (has(parent, en))
184 return child(parent, en);
185 else if (primaryType == null)
186 return parent.addNode(en.name());
187 else
188 return parent.addNode(en.name(), primaryType);
189 }
190
191 // IMAGES
192 public static String img(String src, String width, String height) {
193 return imgBuilder(src, width, height).append("/>").toString();
194 }
195
196 public static String img(String src, Point size) {
197 return img(src, Integer.toString(size.x), Integer.toString(size.y));
198 }
199
200 public static StringBuilder imgBuilder(String src, String width, String height) {
201 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
202 .append("' src='").append(src).append("'");
203 }
204
205 public static String noImg(Point size) {
206 ResourceManager rm = RWT.getResourceManager();
207 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
208 }
209
210 public static String noImg() {
211 return noImg(NO_IMAGE_SIZE);
212 }
213
214 public static Image noImage(Point size) {
215 ResourceManager rm = RWT.getResourceManager();
216 InputStream in = null;
217 try {
218 in = rm.getRegisteredContent(NO_IMAGE);
219 ImageData id = new ImageData(in);
220 ImageData scaled = id.scaledTo(size.x, size.y);
221 Image image = new Image(Display.getCurrent(), scaled);
222 return image;
223 } finally {
224 try {
225 in.close();
226 } catch (IOException e) {
227 // silent
228 }
229 }
230 }
231
232 /** Lorem ipsum text to be used during development. */
233 public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
234 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
235 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
236 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
237 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
238 + " Pellentesque commodo turpis ac diam ultricies dignissim."
239 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
240 + " Integer varius quis est et tristique."
241 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
242 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
243 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
244
245 /** Singleton. */
246 private CmsUtils() {
247 }
248 }