]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/CmsUtils.java
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
.jcr
.JcrUtils
;
12 import org
.eclipse
.rap
.rwt
.RWT
;
13 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.graphics
.Image
;
16 import org
.eclipse
.swt
.graphics
.ImageData
;
17 import org
.eclipse
.swt
.graphics
.Point
;
18 import org
.eclipse
.swt
.layout
.GridData
;
19 import org
.eclipse
.swt
.layout
.GridLayout
;
20 import org
.eclipse
.swt
.layout
.RowData
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.eclipse
.swt
.widgets
.Widget
;
26 /** Static utilities for the CMS framework. */
27 public class CmsUtils
implements CmsConstants
{
28 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
30 public static RowData ROW_DATA_16px
= new RowData(16, 16);
32 public static GridLayout
noSpaceGridLayout() {
33 return noSpaceGridLayout(new GridLayout());
36 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
37 layout
.horizontalSpacing
= 0;
38 layout
.verticalSpacing
= 0;
39 layout
.marginWidth
= 0;
40 layout
.marginHeight
= 0;
47 public static GridData
fillWidth() {
48 return grabWidth(SWT
.FILL
, SWT
.FILL
);
51 public static GridData
fillAll() {
52 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
55 public static GridData
grabWidth(int horizontalAlignment
,
56 int verticalAlignment
) {
57 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
61 public static RowData
rowData16px() {
62 return new RowData(16, 16);
65 public static void style(Widget widget
, String style
) {
66 widget
.setData(CmsConstants
.STYLE
, style
);
69 public static void markup(Widget widget
) {
70 widget
.setData(CmsConstants
.MARKUP
, true);
73 /** @return the path or null if not instrumented */
74 public static String
getDataPath(Widget widget
) {
76 Object data
= widget
.getData();
77 if (data
!= null && data
instanceof Item
) {
79 return ((Item
) data
).getPath();
80 } catch (RepositoryException e
) {
81 throw new CmsException("Cannot find data path of " + data
87 data
= widget
.getData(Property
.JCR_PATH
);
89 return data
.toString();
94 /** Dispose all children of a Composite */
95 public static void clear(Composite composite
) {
96 for (Control child
: composite
.getChildren())
103 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
104 throws RepositoryException
{
105 if (has(parent
, child
))
106 return child(parent
, child
);
107 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
110 public static Node
child(Node parent
, Enum
<?
> en
)
111 throws RepositoryException
{
112 return parent
.getNode(en
.name());
115 public static Boolean
has(Node parent
, Enum
<?
> en
)
116 throws RepositoryException
{
117 return parent
.hasNode(en
.name());
120 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
121 throws RepositoryException
{
122 return getOrAdd(parent
, en
, null);
125 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
126 throws RepositoryException
{
128 return child(parent
, en
);
129 else if (primaryType
== null)
130 return parent
.addNode(en
.name());
132 return parent
.addNode(en
.name(), primaryType
);
136 public static String
img(String src
, String width
, String height
) {
137 return imgBuilder(src
, width
, height
).append("/>").toString();
140 public static String
img(String src
, Point size
) {
141 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
144 public static StringBuilder
imgBuilder(String src
, String width
,
146 return new StringBuilder(64).append("<img width='").append(width
)
147 .append("' height='").append(height
).append("' src='")
148 .append(src
).append("'");
151 public static String
noImg(Point size
) {
152 ResourceManager rm
= RWT
.getResourceManager();
153 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
156 public static String
noImg() {
157 return noImg(NO_IMAGE_SIZE
);
160 public static Image
noImage(Point size
) {
161 ResourceManager rm
= RWT
.getResourceManager();
162 InputStream in
= null;
164 in
= rm
.getRegisteredContent(NO_IMAGE
);
165 ImageData id
= new ImageData(in
);
166 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
167 Image image
= new Image(Display
.getCurrent(), scaled
);
170 IOUtils
.closeQuietly(in
);