]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java
Add JGit to client.
[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.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.apache.commons.io.IOUtils;
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.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.Widget;
33
34 /** Static utilities for the CMS framework. */
35 public class CmsUtils implements CmsConstants {
36 // private final static Log log = LogFactory.getLog(CmsUtils.class);
37
38 /**
39 * The CMS view related to this display, or null if none is available from this
40 * call.
41 */
42 public static CmsView getCmsView() {
43 return UiContext.getData(CmsView.KEY);
44 }
45
46 public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
47 try {
48 URL url = new URL(request.getRequestURL().toString());
49 StringBuilder buf = new StringBuilder();
50 buf.append(url.getProtocol()).append("://").append(url.getHost());
51 if (url.getPort() != -1)
52 buf.append(':').append(url.getPort());
53 return buf;
54 } catch (MalformedURLException e) {
55 throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e);
56 }
57 }
58
59 //
60 public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
61 try {
62 StringBuilder buf = getServerBaseUrl(request);
63 buf.append(getDataPath(node));
64 return new URL(buf.toString()).toString();
65 } catch (MalformedURLException e) {
66 throw new CmsException("Cannot build data URL for " + node, e);
67 }
68 }
69
70 /** A path in the node repository */
71 public static String getDataPath(Node node) throws RepositoryException {
72 return getDataPath(NodeConstants.NODE, node);
73 }
74
75 public static String getDataPath(String cn, Node node) throws RepositoryException {
76 return NodeUtils.getDataPath(cn, node);
77 }
78
79 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
80 @Deprecated
81 public static RowData ROW_DATA_16px = new RowData(16, 16);
82
83 public static GridLayout noSpaceGridLayout() {
84 return noSpaceGridLayout(new GridLayout());
85 }
86
87 public static GridLayout noSpaceGridLayout(int columns) {
88 return noSpaceGridLayout(new GridLayout(columns, false));
89 }
90
91 public static GridLayout noSpaceGridLayout(GridLayout layout) {
92 layout.horizontalSpacing = 0;
93 layout.verticalSpacing = 0;
94 layout.marginWidth = 0;
95 layout.marginHeight = 0;
96 return layout;
97 }
98
99 //
100 // GRID DATA
101 //
102 public static GridData fillWidth() {
103 return grabWidth(SWT.FILL, SWT.FILL);
104 }
105
106 public static GridData fillAll() {
107 return new GridData(SWT.FILL, SWT.FILL, true, true);
108 }
109
110 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
111 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
112 }
113
114 public static RowData rowData16px() {
115 return new RowData(16, 16);
116 }
117
118 /** Style widget */
119 public static void style(Widget widget, String style) {
120 widget.setData(CmsConstants.STYLE, style);
121 }
122
123 /** Enable markups on widget */
124 public static void markup(Widget widget) {
125 widget.setData(CmsConstants.MARKUP, true);
126 }
127
128 public static void setItemHeight(Table table, int height) {
129 table.setData(CmsConstants.ITEM_HEIGHT, height);
130 }
131
132 /** Dispose all children of a Composite */
133 public static void clear(Composite composite) {
134 for (Control child : composite.getChildren())
135 child.dispose();
136 }
137
138 //
139 // JCR
140 //
141 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
142 if (has(parent, child))
143 return child(parent, child);
144 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
145 }
146
147 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
148 return parent.getNode(en.name());
149 }
150
151 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
152 return parent.hasNode(en.name());
153 }
154
155 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
156 return getOrAdd(parent, en, null);
157 }
158
159 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
160 if (has(parent, en))
161 return child(parent, en);
162 else if (primaryType == null)
163 return parent.addNode(en.name());
164 else
165 return parent.addNode(en.name(), primaryType);
166 }
167
168 // IMAGES
169 public static String img(String src, String width, String height) {
170 return imgBuilder(src, width, height).append("/>").toString();
171 }
172
173 public static String img(String src, Point size) {
174 return img(src, Integer.toString(size.x), Integer.toString(size.y));
175 }
176
177 public static StringBuilder imgBuilder(String src, String width, String height) {
178 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
179 .append("' src='").append(src).append("'");
180 }
181
182 public static String noImg(Point size) {
183 ResourceManager rm = RWT.getResourceManager();
184 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
185 }
186
187 public static String noImg() {
188 return noImg(NO_IMAGE_SIZE);
189 }
190
191 public static Image noImage(Point size) {
192 ResourceManager rm = RWT.getResourceManager();
193 InputStream in = null;
194 try {
195 in = rm.getRegisteredContent(NO_IMAGE);
196 ImageData id = new ImageData(in);
197 ImageData scaled = id.scaledTo(size.x, size.y);
198 Image image = new Image(Display.getCurrent(), scaled);
199 return image;
200 } finally {
201 IOUtils.closeQuietly(in);
202 }
203 }
204
205 public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
206 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
207 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
208 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
209 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
210 + " Pellentesque commodo turpis ac diam ultricies dignissim."
211 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
212 + " Integer varius quis est et tristique."
213 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
214 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
215 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
216
217 private CmsUtils() {
218 }
219 }