1 package org
.argeo
.cms
.util
;
3 import java
.io
.InputStream
;
4 import java
.net
.MalformedURLException
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.servlet
.http
.HttpServletRequest
;
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
;
34 /** Static utilities for the CMS framework. */
35 public class CmsUtils
implements CmsConstants
{
36 // private final static Log log = LogFactory.getLog(CmsUtils.class);
39 * The CMS view related to this display, or null if none is available from
42 public static CmsView
getCmsView() {
43 return UiContext
.getData(CmsView
.KEY
);
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 CmsException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
60 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
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
);
70 /** A path in the node repository */
71 public static String
getDataPath(Node node
) throws RepositoryException
{
72 return getDataPath(NodeConstants
.NODE
, node
);
75 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
76 return NodeUtils
.getDataPath(cn
, node
);
79 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
81 public static RowData ROW_DATA_16px
= new RowData(16, 16);
83 public static GridLayout
noSpaceGridLayout() {
84 return noSpaceGridLayout(new GridLayout());
87 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
88 layout
.horizontalSpacing
= 0;
89 layout
.verticalSpacing
= 0;
90 layout
.marginWidth
= 0;
91 layout
.marginHeight
= 0;
98 public static GridData
fillWidth() {
99 return grabWidth(SWT
.FILL
, SWT
.FILL
);
102 public static GridData
fillAll() {
103 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
106 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
107 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
110 public static RowData
rowData16px() {
111 return new RowData(16, 16);
115 public static void style(Widget widget
, String style
) {
116 widget
.setData(CmsConstants
.STYLE
, style
);
119 /** Enable markups on widget */
120 public static void markup(Widget widget
) {
121 widget
.setData(CmsConstants
.MARKUP
, true);
124 public static void setItemHeight(Table table
, int height
) {
125 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
128 /** Dispose all children of a Composite */
129 public static void clear(Composite composite
) {
130 for (Control child
: composite
.getChildren())
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]);
143 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
144 return parent
.getNode(en
.name());
147 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
148 return parent
.hasNode(en
.name());
151 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
152 return getOrAdd(parent
, en
, null);
155 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
157 return child(parent
, en
);
158 else if (primaryType
== null)
159 return parent
.addNode(en
.name());
161 return parent
.addNode(en
.name(), primaryType
);
165 public static String
img(String src
, String width
, String height
) {
166 return imgBuilder(src
, width
, height
).append("/>").toString();
169 public static String
img(String src
, Point size
) {
170 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
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("'");
178 public static String
noImg(Point size
) {
179 ResourceManager rm
= RWT
.getResourceManager();
180 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
183 public static String
noImg() {
184 return noImg(NO_IMAGE_SIZE
);
187 public static Image
noImage(Point size
) {
188 ResourceManager rm
= RWT
.getResourceManager();
189 InputStream in
= null;
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
);
197 IOUtils
.closeQuietly(in
);