]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java
Keep working on DocBoook support
[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
40 * this 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(GridLayout layout) {
88 layout.horizontalSpacing = 0;
89 layout.verticalSpacing = 0;
90 layout.marginWidth = 0;
91 layout.marginHeight = 0;
92 return layout;
93 }
94
95 //
96 // GRID DATA
97 //
98 public static GridData fillWidth() {
99 return grabWidth(SWT.FILL, SWT.FILL);
100 }
101
102 public static GridData fillAll() {
103 return new GridData(SWT.FILL, SWT.FILL, true, true);
104 }
105
106 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
107 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
108 }
109
110 public static RowData rowData16px() {
111 return new RowData(16, 16);
112 }
113
114 /** Style widget */
115 public static void style(Widget widget, String style) {
116 widget.setData(CmsConstants.STYLE, style);
117 }
118
119 /** Enable markups on widget */
120 public static void markup(Widget widget) {
121 widget.setData(CmsConstants.MARKUP, true);
122 }
123
124 public static void setItemHeight(Table table, int height) {
125 table.setData(CmsConstants.ITEM_HEIGHT, height);
126 }
127
128 /** Dispose all children of a Composite */
129 public static void clear(Composite composite) {
130 for (Control child : composite.getChildren())
131 child.dispose();
132 }
133
134 //
135 // JCR
136 //
137 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
138 if (has(parent, child))
139 return child(parent, child);
140 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
141 }
142
143 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
144 return parent.getNode(en.name());
145 }
146
147 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
148 return parent.hasNode(en.name());
149 }
150
151 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
152 return getOrAdd(parent, en, null);
153 }
154
155 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
156 if (has(parent, en))
157 return child(parent, en);
158 else if (primaryType == null)
159 return parent.addNode(en.name());
160 else
161 return parent.addNode(en.name(), primaryType);
162 }
163
164 // IMAGES
165 public static String img(String src, String width, String height) {
166 return imgBuilder(src, width, height).append("/>").toString();
167 }
168
169 public static String img(String src, Point size) {
170 return img(src, Integer.toString(size.x), Integer.toString(size.y));
171 }
172
173 public static StringBuilder imgBuilder(String src, String width, String height) {
174 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
175 .append("' src='").append(src).append("'");
176 }
177
178 public static String noImg(Point size) {
179 ResourceManager rm = RWT.getResourceManager();
180 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
181 }
182
183 public static String noImg() {
184 return noImg(NO_IMAGE_SIZE);
185 }
186
187 public static Image noImage(Point size) {
188 ResourceManager rm = RWT.getResourceManager();
189 InputStream in = null;
190 try {
191 in = rm.getRegisteredContent(NO_IMAGE);
192 ImageData id = new ImageData(in);
193 ImageData scaled = id.scaledTo(size.x, size.y);
194 Image image = new Image(Display.getCurrent(), scaled);
195 return image;
196 } finally {
197 IOUtils.closeQuietly(in);
198 }
199 }
200
201 private CmsUtils() {
202 }
203 }