]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java
c1b319fbe99f348e68e3813a102053917ebc3720
1 package org
.argeo
.cms
.util
;
3 import java
.io
.InputStream
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.RepositoryException
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.cms
.CmsConstants
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.argeo
.jcr
.JcrUtils
;
14 import org
.eclipse
.rap
.rwt
.RWT
;
15 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.graphics
.Image
;
18 import org
.eclipse
.swt
.graphics
.ImageData
;
19 import org
.eclipse
.swt
.graphics
.Point
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.layout
.RowData
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Widget
;
28 /** Static utilities for the CMS framework. */
29 public class CmsUtils
implements CmsConstants
{
30 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
32 public static RowData ROW_DATA_16px
= new RowData(16, 16);
34 public static GridLayout
noSpaceGridLayout() {
35 return noSpaceGridLayout(new GridLayout());
38 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
39 layout
.horizontalSpacing
= 0;
40 layout
.verticalSpacing
= 0;
41 layout
.marginWidth
= 0;
42 layout
.marginHeight
= 0;
49 public static GridData
fillWidth() {
50 return grabWidth(SWT
.FILL
, SWT
.FILL
);
53 public static GridData
fillAll() {
54 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
57 public static GridData
grabWidth(int horizontalAlignment
,
58 int verticalAlignment
) {
59 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
63 public static RowData
rowData16px() {
64 return new RowData(16, 16);
67 public static void style(Widget widget
, String style
) {
68 widget
.setData(CmsConstants
.STYLE
, style
);
71 public static void markup(Widget widget
) {
72 widget
.setData(CmsConstants
.MARKUP
, true);
75 /** @return the path or null if not instrumented */
76 public static String
getDataPath(Widget widget
) {
78 Object data
= widget
.getData();
79 if (data
!= null && data
instanceof Item
) {
81 return ((Item
) data
).getPath();
82 } catch (RepositoryException e
) {
83 throw new CmsException("Cannot find data path of " + data
89 data
= widget
.getData(Property
.JCR_PATH
);
91 return data
.toString();
96 /** Dispose all children of a Composite */
97 public static void clear(Composite composite
) {
98 for (Control child
: composite
.getChildren())
105 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
106 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
)
113 throws RepositoryException
{
114 return parent
.getNode(en
.name());
117 public static Boolean
has(Node parent
, Enum
<?
> en
)
118 throws RepositoryException
{
119 return parent
.hasNode(en
.name());
122 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
123 throws RepositoryException
{
124 return getOrAdd(parent
, en
, null);
127 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
128 throws RepositoryException
{
130 return child(parent
, en
);
131 else if (primaryType
== null)
132 return parent
.addNode(en
.name());
134 return parent
.addNode(en
.name(), primaryType
);
138 public static String
img(String src
, String width
, String height
) {
139 return imgBuilder(src
, width
, height
).append("/>").toString();
142 public static String
img(String src
, Point size
) {
143 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
146 public static StringBuilder
imgBuilder(String src
, String width
,
148 return new StringBuilder(64).append("<img width='").append(width
)
149 .append("' height='").append(height
).append("' src='")
150 .append(src
).append("'");
153 public static String
noImg(Point size
) {
154 ResourceManager rm
= RWT
.getResourceManager();
155 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
158 public static String
noImg() {
159 return noImg(NO_IMAGE_SIZE
);
162 public static Image
noImage(Point size
) {
163 ResourceManager rm
= RWT
.getResourceManager();
164 InputStream in
= null;
166 in
= rm
.getRegisteredContent(NO_IMAGE
);
167 ImageData id
= new ImageData(in
);
168 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
169 Image image
= new Image(Display
.getCurrent(), scaled
);
172 IOUtils
.closeQuietly(in
);