]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsUiUtils.java
Change ScrolledPage package
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.ui / src / org / argeo / cms / ui / 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.cms.CmsConstants;
13 import org.argeo.api.cms.ux.Cms2DSize;
14 import org.argeo.api.cms.ux.CmsView;
15 import org.argeo.cms.jcr.CmsJcrUtils;
16 import org.argeo.cms.swt.CmsSwtUtils;
17 import org.argeo.cms.ui.CmsUiConstants;
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.graphics.Image;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.layout.RowData;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Table;
27
28 /** Static utilities for the CMS framework. */
29 public class CmsUiUtils {
30 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
31
32 /*
33 * CMS VIEW
34 */
35
36 /**
37 * The CMS view related to this display, or null if none is available from this
38 * call.
39 *
40 * @deprecated Use {@link CmsSwtUtils#getCmsView(Composite)} instead.
41 */
42 @Deprecated
43 public static CmsView getCmsView() {
44 // return UiContext.getData(CmsView.class.getName());
45 return CmsSwtUtils.getCmsView(Display.getCurrent().getActiveShell());
46 }
47
48 public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
49 try {
50 URL url = new URL(request.getRequestURL().toString());
51 StringBuilder buf = new StringBuilder();
52 buf.append(url.getProtocol()).append("://").append(url.getHost());
53 if (url.getPort() != -1)
54 buf.append(':').append(url.getPort());
55 return buf;
56 } catch (MalformedURLException e) {
57 throw new IllegalArgumentException("Cannot extract server base URL from " + request.getRequestURL(), e);
58 }
59 }
60
61 //
62 public static String getDataUrl(Node node, HttpServletRequest request) {
63 try {
64 StringBuilder buf = getServerBaseUrl(request);
65 buf.append(getDataPath(node));
66 return new URL(buf.toString()).toString();
67 } catch (MalformedURLException e) {
68 throw new IllegalArgumentException("Cannot build data URL for " + node, e);
69 }
70 }
71
72 /** A path in the node repository */
73 public static String getDataPath(Node node) {
74 return getDataPath(CmsConstants.EGO_REPOSITORY, node);
75 }
76
77 public static String getDataPath(String cn, Node node) {
78 return CmsJcrUtils.getDataPath(cn, node);
79 }
80
81 /** Clean reserved URL characters for use in HTTP links. */
82 public static String getDataPathForUrl(Node node) {
83 return CmsSwtUtils.cleanPathForUrl(getDataPath(node));
84 }
85
86 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
87 @Deprecated
88 public static RowData ROW_DATA_16px = new RowData(16, 16);
89
90
91
92 /*
93 * FORM LAYOUT
94 */
95
96
97
98 @Deprecated
99 public static void setItemHeight(Table table, int height) {
100 table.setData(CmsUiConstants.ITEM_HEIGHT, height);
101 }
102
103 //
104 // JCR
105 //
106 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
107 if (has(parent, child))
108 return child(parent, child);
109 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
110 }
111
112 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
113 return parent.getNode(en.name());
114 }
115
116 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
117 return parent.hasNode(en.name());
118 }
119
120 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
121 return getOrAdd(parent, en, null);
122 }
123
124 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
125 if (has(parent, en))
126 return child(parent, en);
127 else if (primaryType == null)
128 return parent.addNode(en.name());
129 else
130 return parent.addNode(en.name(), primaryType);
131 }
132
133 // IMAGES
134
135 public static String img(Node fileNode, String width, String height) {
136 return img(null, fileNode, width, height);
137 }
138
139 public static String img(String serverBase, Node fileNode, String width, String height) {
140 // String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
141 String src;
142 src = (serverBase != null ? serverBase : "") + getDataPathForUrl(fileNode);
143 return imgBuilder(src, width, height).append("/>").toString();
144 }
145
146 public static String img(String src, String width, String height) {
147 return imgBuilder(src, width, height).append("/>").toString();
148 }
149
150 public static String img(String src, Cms2DSize size) {
151 return img(src, Integer.toString(size.getWidth()), Integer.toString(size.getHeight()));
152 }
153
154 public static StringBuilder imgBuilder(String src, String width, String height) {
155 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
156 .append("' src='").append(src).append("'");
157 }
158
159 public static String noImg(Cms2DSize size) {
160 ResourceManager rm = RWT.getResourceManager();
161 return CmsUiUtils.img(rm.getLocation(CmsUiConstants.NO_IMAGE), size);
162 }
163
164 public static String noImg() {
165 return noImg(CmsUiConstants.NO_IMAGE_SIZE);
166 }
167
168 public static Image noImage(Cms2DSize size) {
169 ResourceManager rm = RWT.getResourceManager();
170 InputStream in = null;
171 try {
172 in = rm.getRegisteredContent(CmsUiConstants.NO_IMAGE);
173 ImageData id = new ImageData(in);
174 ImageData scaled = id.scaledTo(size.getWidth(), size.getHeight());
175 Image image = new Image(Display.getCurrent(), scaled);
176 return image;
177 } finally {
178 try {
179 in.close();
180 } catch (IOException e) {
181 // silent
182 }
183 }
184 }
185
186 /** Lorem ipsum text to be used during development. */
187 public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
188 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
189 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
190 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
191 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
192 + " Pellentesque commodo turpis ac diam ultricies dignissim."
193 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
194 + " Integer varius quis est et tristique."
195 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
196 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
197 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
198
199 /** Singleton. */
200 private CmsUiUtils() {
201 }
202
203 }