]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java
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);
69 public static void style(Widget widget
, String style
) {
70 widget
.setData(CmsConstants
.STYLE
, style
);
73 /** Enable markups on widget */
74 public static void markup(Widget widget
) {
75 widget
.setData(CmsConstants
.MARKUP
, true);
78 public static void setItemHeight(Table table
, int height
) {
79 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
82 /** @return the path or null if not instrumented */
83 public static String
getDataPath(Widget widget
) {
85 Object data
= widget
.getData();
86 if (data
!= null && data
instanceof Item
) {
88 return ((Item
) data
).getPath();
89 } catch (RepositoryException e
) {
90 throw new CmsException("Cannot find data path of " + data
96 data
= widget
.getData(Property
.JCR_PATH
);
98 return data
.toString();
103 /** Dispose all children of a Composite */
104 public static void clear(Composite composite
) {
105 for (Control child
: composite
.getChildren())
112 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
113 throws RepositoryException
{
114 if (has(parent
, child
))
115 return child(parent
, child
);
116 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
119 public static Node
child(Node parent
, Enum
<?
> en
)
120 throws RepositoryException
{
121 return parent
.getNode(en
.name());
124 public static Boolean
has(Node parent
, Enum
<?
> en
)
125 throws RepositoryException
{
126 return parent
.hasNode(en
.name());
129 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
130 throws RepositoryException
{
131 return getOrAdd(parent
, en
, null);
134 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
135 throws RepositoryException
{
137 return child(parent
, en
);
138 else if (primaryType
== null)
139 return parent
.addNode(en
.name());
141 return parent
.addNode(en
.name(), primaryType
);
145 public static String
img(String src
, String width
, String height
) {
146 return imgBuilder(src
, width
, height
).append("/>").toString();
149 public static String
img(String src
, Point size
) {
150 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
153 public static StringBuilder
imgBuilder(String src
, String width
,
155 return new StringBuilder(64).append("<img width='").append(width
)
156 .append("' height='").append(height
).append("' src='")
157 .append(src
).append("'");
160 public static String
noImg(Point size
) {
161 ResourceManager rm
= RWT
.getResourceManager();
162 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
165 public static String
noImg() {
166 return noImg(NO_IMAGE_SIZE
);
169 public static Image
noImage(Point size
) {
170 ResourceManager rm
= RWT
.getResourceManager();
171 InputStream in
= null;
173 in
= rm
.getRegisteredContent(NO_IMAGE
);
174 ImageData id
= new ImageData(in
);
175 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
176 Image image
= new Image(Display
.getCurrent(), scaled
);
179 IOUtils
.closeQuietly(in
);