1 package org
.argeo
.cms
.ui
.util
;
3 import java
.net
.MalformedURLException
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.servlet
.http
.HttpServletRequest
;
10 import org
.argeo
.api
.cms
.CmsConstants
;
11 import org
.argeo
.api
.cms
.ux
.Cms2DSize
;
12 import org
.argeo
.api
.cms
.ux
.CmsView
;
13 import org
.argeo
.cms
.acr
.ContentUtils
;
14 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
15 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
16 import org
.argeo
.cms
.ux
.AbstractImageManager
;
17 import org
.argeo
.cms
.ux
.CmsUxUtils
;
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
.layout
.RowData
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.eclipse
.swt
.widgets
.Table
;
26 /** Static utilities for the CMS framework. */
27 public class CmsUiUtils
{
28 // private final static Log log = LogFactory.getLog(CmsUiUtils.class);
35 * The CMS view related to this display, or null if none is available from this
38 * @deprecated Use {@link CmsSwtUtils#getCmsView(Composite)} instead.
41 public static CmsView
getCmsView() {
42 // return UiContext.getData(CmsView.class.getName());
43 return CmsSwtUtils
.getCmsView(Display
.getCurrent().getActiveShell());
46 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
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());
54 } catch (MalformedURLException e
) {
55 throw new IllegalArgumentException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
60 public static String
getDataUrl(Node node
, HttpServletRequest request
) {
62 StringBuilder buf
= getServerBaseUrl(request
);
63 buf
.append(getDataPath(node
));
64 return new URL(buf
.toString()).toString();
65 } catch (MalformedURLException e
) {
66 throw new IllegalArgumentException("Cannot build data URL for " + node
, e
);
70 /** A path in the node repository */
71 public static String
getDataPath(Node node
) {
72 return getDataPath(CmsConstants
.EGO_REPOSITORY
, node
);
75 public static String
getDataPath(String cn
, Node node
) {
76 return CmsJcrUtils
.getDataPath(cn
, node
);
79 /** Clean reserved URL characters for use in HTTP links. */
80 public static String
getDataPathForUrl(Node node
) {
81 return ContentUtils
.cleanPathForUrl(getDataPath(node
));
84 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
86 public static RowData ROW_DATA_16px
= new RowData(16, 16);
92 public final static String ITEM_HEIGHT
= "org.eclipse.rap.rwt.customItemHeight";
95 public static void setItemHeight(Table table
, int height
) {
96 table
.setData(ITEM_HEIGHT
, height
);
102 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
103 if (has(parent
, child
))
104 return child(parent
, child
);
105 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
108 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
109 return parent
.getNode(en
.name());
112 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
113 return parent
.hasNode(en
.name());
116 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
117 return getOrAdd(parent
, en
, null);
120 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
122 return child(parent
, en
);
123 else if (primaryType
== null)
124 return parent
.addNode(en
.name());
126 return parent
.addNode(en
.name(), primaryType
);
131 public static String
img(Node fileNode
, String width
, String height
) {
132 return img(null, fileNode
, width
, height
);
135 public static String
img(String serverBase
, Node fileNode
, String width
, String height
) {
136 // String src = (serverBase != null ? serverBase : "") + NodeUtils.getDataPath(fileNode);
138 src
= (serverBase
!= null ? serverBase
: "") + getDataPathForUrl(fileNode
);
139 return CmsUxUtils
.imgBuilder(src
, width
, height
).append("/>").toString();
142 public static String
noImg(Cms2DSize size
) {
143 ResourceManager rm
= RWT
.getResourceManager();
144 return CmsUxUtils
.img(rm
.getLocation(AbstractImageManager
.NO_IMAGE
), size
);
147 public static String
noImg() {
148 return noImg(AbstractImageManager
.NO_IMAGE_SIZE
);
151 // public static Image noImage(Cms2DSize size) {
152 // ResourceManager rm = RWT.getResourceManager();
153 // InputStream in = null;
155 // in = rm.getRegisteredContent(AbstractImageManager.NO_IMAGE);
156 // ImageData id = new ImageData(in);
157 // ImageData scaled = id.scaledTo(size.getWidth(), size.getHeight());
158 // Image image = new Image(Display.getCurrent(), scaled);
163 // } catch (IOException e) {
169 /** Lorem ipsum text to be used during development. */
170 public final static String LOREM_IPSUM
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
171 + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac."
172 + " Cras aliquam sodales risus, vitae varius lacus molestie quis."
173 + " Vivamus consequat, leo id lacinia volutpat, eros diam efficitur urna, finibus interdum risus turpis at nisi."
174 + " Curabitur vulputate nulla quis scelerisque fringilla. Integer consectetur turpis id lobortis accumsan."
175 + " Pellentesque commodo turpis ac diam ultricies dignissim."
176 + " Curabitur sit amet dolor volutpat lacus aliquam ornare quis sed velit."
177 + " Integer varius quis est et tristique."
178 + " Suspendisse pharetra porttitor purus, eget condimentum magna."
179 + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum."
180 + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue.";
183 private CmsUiUtils() {