1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.servlet
.http
.HttpServletRequest
;
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
;
28 /** Static utilities for the CMS framework. */
29 public class CmsUiUtils
{
30 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
37 * The CMS view related to this display, or null if none is available from this
40 * @deprecated Use {@link CmsSwtUtils#getCmsView(Composite)} instead.
43 public static CmsView
getCmsView() {
44 // return UiContext.getData(CmsView.class.getName());
45 return CmsSwtUtils
.getCmsView(Display
.getCurrent().getActiveShell());
48 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
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());
56 } catch (MalformedURLException e
) {
57 throw new IllegalArgumentException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
62 public static String
getDataUrl(Node node
, HttpServletRequest request
) {
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
);
72 /** A path in the node repository */
73 public static String
getDataPath(Node node
) {
74 return getDataPath(CmsConstants
.EGO_REPOSITORY
, node
);
77 public static String
getDataPath(String cn
, Node node
) {
78 return CmsJcrUtils
.getDataPath(cn
, node
);
81 /** Clean reserved URL characters for use in HTTP links. */
82 public static String
getDataPathForUrl(Node node
) {
83 return CmsSwtUtils
.cleanPathForUrl(getDataPath(node
));
86 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
88 public static RowData ROW_DATA_16px
= new RowData(16, 16);
99 public static void setItemHeight(Table table
, int height
) {
100 table
.setData(CmsUiConstants
.ITEM_HEIGHT
, height
);
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]);
112 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
113 return parent
.getNode(en
.name());
116 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
117 return parent
.hasNode(en
.name());
120 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
121 return getOrAdd(parent
, en
, null);
124 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
126 return child(parent
, en
);
127 else if (primaryType
== null)
128 return parent
.addNode(en
.name());
130 return parent
.addNode(en
.name(), primaryType
);
135 public static String
img(Node fileNode
, String width
, String height
) {
136 return img(null, fileNode
, width
, height
);
139 public static String
img(String serverBase
, Node fileNode
, String width
, String height
) {
140 // String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
142 src
= (serverBase
!= null ? serverBase
: "") + getDataPathForUrl(fileNode
);
143 return imgBuilder(src
, width
, height
).append("/>").toString();
146 public static String
img(String src
, String width
, String height
) {
147 return imgBuilder(src
, width
, height
).append("/>").toString();
150 public static String
img(String src
, Cms2DSize size
) {
151 return img(src
, Integer
.toString(size
.getWidth()), Integer
.toString(size
.getHeight()));
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("'");
159 public static String
noImg(Cms2DSize size
) {
160 ResourceManager rm
= RWT
.getResourceManager();
161 return CmsUiUtils
.img(rm
.getLocation(CmsUiConstants
.NO_IMAGE
), size
);
164 public static String
noImg() {
165 return noImg(CmsUiConstants
.NO_IMAGE_SIZE
);
168 public static Image
noImage(Cms2DSize size
) {
169 ResourceManager rm
= RWT
.getResourceManager();
170 InputStream in
= null;
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
);
180 } catch (IOException e
) {
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.";
200 private CmsUiUtils() {