]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUtils.java
5d31e2c51f0d68639fe64234a69027249e82f5e2
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
.Table
;
27 import org
.eclipse
.swt
.widgets
.Widget
;
29 /** Static utilities for the CMS framework. */
30 public class CmsUtils
implements CmsConstants
{
31 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
33 public static RowData ROW_DATA_16px
= new RowData(16, 16);
35 public static GridLayout
noSpaceGridLayout() {
36 return noSpaceGridLayout(new GridLayout());
39 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
40 layout
.horizontalSpacing
= 0;
41 layout
.verticalSpacing
= 0;
42 layout
.marginWidth
= 0;
43 layout
.marginHeight
= 0;
50 public static GridData
fillWidth() {
51 return grabWidth(SWT
.FILL
, SWT
.FILL
);
54 public static GridData
fillAll() {
55 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
58 public static GridData
grabWidth(int horizontalAlignment
,
59 int verticalAlignment
) {
60 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
64 public static RowData
rowData16px() {
65 return new RowData(16, 16);
68 public static void style(Widget widget
, String style
) {
69 widget
.setData(CmsConstants
.STYLE
, style
);
72 public static void markup(Widget widget
) {
73 widget
.setData(CmsConstants
.MARKUP
, true);
76 public static void setItemHeight(Table table
, int height
) {
77 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
80 /** @return the path or null if not instrumented */
81 public static String
getDataPath(Widget widget
) {
83 Object data
= widget
.getData();
84 if (data
!= null && data
instanceof Item
) {
86 return ((Item
) data
).getPath();
87 } catch (RepositoryException e
) {
88 throw new CmsException("Cannot find data path of " + data
94 data
= widget
.getData(Property
.JCR_PATH
);
96 return data
.toString();
101 /** Dispose all children of a Composite */
102 public static void clear(Composite composite
) {
103 for (Control child
: composite
.getChildren())
110 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
111 throws RepositoryException
{
112 if (has(parent
, child
))
113 return child(parent
, child
);
114 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
117 public static Node
child(Node parent
, Enum
<?
> en
)
118 throws RepositoryException
{
119 return parent
.getNode(en
.name());
122 public static Boolean
has(Node parent
, Enum
<?
> en
)
123 throws RepositoryException
{
124 return parent
.hasNode(en
.name());
127 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
128 throws RepositoryException
{
129 return getOrAdd(parent
, en
, null);
132 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
133 throws RepositoryException
{
135 return child(parent
, en
);
136 else if (primaryType
== null)
137 return parent
.addNode(en
.name());
139 return parent
.addNode(en
.name(), primaryType
);
143 public static String
img(String src
, String width
, String height
) {
144 return imgBuilder(src
, width
, height
).append("/>").toString();
147 public static String
img(String src
, Point size
) {
148 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
151 public static StringBuilder
imgBuilder(String src
, String width
,
153 return new StringBuilder(64).append("<img width='").append(width
)
154 .append("' height='").append(height
).append("' src='")
155 .append(src
).append("'");
158 public static String
noImg(Point size
) {
159 ResourceManager rm
= RWT
.getResourceManager();
160 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
163 public static String
noImg() {
164 return noImg(NO_IMAGE_SIZE
);
167 public static Image
noImage(Point size
) {
168 ResourceManager rm
= RWT
.getResourceManager();
169 InputStream in
= null;
171 in
= rm
.getRegisteredContent(NO_IMAGE
);
172 ImageData id
= new ImageData(in
);
173 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
174 Image image
= new Image(Display
.getCurrent(), scaled
);
177 IOUtils
.closeQuietly(in
);