1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.net
.URLEncoder
;
8 import java
.nio
.charset
.StandardCharsets
;
9 import java
.util
.StringTokenizer
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.servlet
.http
.HttpServletRequest
;
15 import org
.argeo
.api
.cms
.Cms2DSize
;
16 import org
.argeo
.api
.cms
.CmsView
;
17 import org
.argeo
.api
.cms
.CmsConstants
;
18 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
19 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
20 import org
.argeo
.cms
.ui
.CmsUiConstants
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.eclipse
.rap
.rwt
.RWT
;
23 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
24 import org
.eclipse
.swt
.graphics
.Image
;
25 import org
.eclipse
.swt
.graphics
.ImageData
;
26 import org
.eclipse
.swt
.layout
.RowData
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Display
;
29 import org
.eclipse
.swt
.widgets
.Table
;
31 /** Static utilities for the CMS framework. */
32 public class CmsUiUtils
{
33 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
40 * The CMS view related to this display, or null if none is available from this
43 * @deprecated Use {@link CmsSwtUtils#getCmsView(Composite)} instead.
46 public static CmsView
getCmsView() {
47 // return UiContext.getData(CmsView.class.getName());
48 return CmsSwtUtils
.getCmsView(Display
.getCurrent().getActiveShell());
51 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
53 URL url
= new URL(request
.getRequestURL().toString());
54 StringBuilder buf
= new StringBuilder();
55 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
56 if (url
.getPort() != -1)
57 buf
.append(':').append(url
.getPort());
59 } catch (MalformedURLException e
) {
60 throw new IllegalArgumentException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
65 public static String
getDataUrl(Node node
, HttpServletRequest request
) {
67 StringBuilder buf
= getServerBaseUrl(request
);
68 buf
.append(getDataPath(node
));
69 return new URL(buf
.toString()).toString();
70 } catch (MalformedURLException e
) {
71 throw new IllegalArgumentException("Cannot build data URL for " + node
, e
);
75 /** A path in the node repository */
76 public static String
getDataPath(Node node
) {
77 return getDataPath(CmsConstants
.EGO_REPOSITORY
, node
);
80 public static String
getDataPath(String cn
, Node node
) {
81 return CmsJcrUtils
.getDataPath(cn
, node
);
84 /** Clean reserved URL characters for use in HTTP links. */
85 public static String
getDataPathForUrl(Node node
) {
86 return cleanPathForUrl(getDataPath(node
));
89 /** Clean reserved URL characters for use in HTTP links. */
90 public static String
cleanPathForUrl(String path
) {
91 StringTokenizer st
= new StringTokenizer(path
, "/");
92 StringBuilder sb
= new StringBuilder();
93 while (st
.hasMoreElements()) {
95 String encoded
= URLEncoder
.encode(st
.nextToken(), StandardCharsets
.UTF_8
);
96 encoded
= encoded
.replace("+", "%20");
100 return sb
.toString();
103 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
105 public static RowData ROW_DATA_16px
= new RowData(16, 16);
116 public static void setItemHeight(Table table
, int height
) {
117 table
.setData(CmsUiConstants
.ITEM_HEIGHT
, height
);
123 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
124 if (has(parent
, child
))
125 return child(parent
, child
);
126 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
129 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
130 return parent
.getNode(en
.name());
133 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
134 return parent
.hasNode(en
.name());
137 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
138 return getOrAdd(parent
, en
, null);
141 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
143 return child(parent
, en
);
144 else if (primaryType
== null)
145 return parent
.addNode(en
.name());
147 return parent
.addNode(en
.name(), primaryType
);
152 public static String
img(Node fileNode
, String width
, String height
) {
153 return img(null, fileNode
, width
, height
);
156 public static String
img(String serverBase
, Node fileNode
, String width
, String height
) {
157 // String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
159 src
= (serverBase
!= null ? serverBase
: "") + getDataPathForUrl(fileNode
);
160 return imgBuilder(src
, width
, height
).append("/>").toString();
163 public static String
img(String src
, String width
, String height
) {
164 return imgBuilder(src
, width
, height
).append("/>").toString();
167 public static String
img(String src
, Cms2DSize size
) {
168 return img(src
, Integer
.toString(size
.getWidth()), Integer
.toString(size
.getHeight()));
171 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
172 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
173 .append("' src='").append(src
).append("'");
176 public static String
noImg(Cms2DSize size
) {
177 ResourceManager rm
= RWT
.getResourceManager();
178 return CmsUiUtils
.img(rm
.getLocation(CmsUiConstants
.NO_IMAGE
), size
);
181 public static String
noImg() {
182 return noImg(CmsUiConstants
.NO_IMAGE_SIZE
);
185 public static Image
noImage(Cms2DSize size
) {
186 ResourceManager rm
= RWT
.getResourceManager();
187 InputStream in
= null;
189 in
= rm
.getRegisteredContent(CmsUiConstants
.NO_IMAGE
);
190 ImageData id
= new ImageData(in
);
191 ImageData scaled
= id
.scaledTo(size
.getWidth(), size
.getHeight());
192 Image image
= new Image(Display
.getCurrent(), scaled
);
197 } catch (IOException e
) {
203 /** Lorem ipsum text to be used during development. */
204 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
205 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
206 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
207 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
208 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
209 + " Pellentesque commodo turpis ac diam ultricies dignissim."
210 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
211 + " Integer varius quis est et tristique."
212 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
213 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
214 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
217 private CmsUiUtils() {