]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUtils.java
6e895818a35c34fd49bf736c146dc7813cb82f80
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
.cms
.CmsView
;
14 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
15 import org
.argeo
.jcr
.JcrUtils
;
16 import org
.eclipse
.rap
.rwt
.RWT
;
17 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.graphics
.Image
;
20 import org
.eclipse
.swt
.graphics
.ImageData
;
21 import org
.eclipse
.swt
.graphics
.Point
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.layout
.RowData
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.swt
.widgets
.Widget
;
31 /** Static utilities for the CMS framework. */
32 public class CmsUtils
implements CmsConstants
{
34 * The CMS view related to this display, or null if none is available from
37 public static CmsView
getCmsView() {
38 return UiContext
.getData(CmsView
.KEY
);
41 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
43 public static RowData ROW_DATA_16px
= new RowData(16, 16);
45 public static GridLayout
noSpaceGridLayout() {
46 return noSpaceGridLayout(new GridLayout());
49 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
50 layout
.horizontalSpacing
= 0;
51 layout
.verticalSpacing
= 0;
52 layout
.marginWidth
= 0;
53 layout
.marginHeight
= 0;
60 public static GridData
fillWidth() {
61 return grabWidth(SWT
.FILL
, SWT
.FILL
);
64 public static GridData
fillAll() {
65 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
68 public static GridData
grabWidth(int horizontalAlignment
,
69 int verticalAlignment
) {
70 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
74 public static RowData
rowData16px() {
75 return new RowData(16, 16);
79 public static void style(Widget widget
, String style
) {
80 widget
.setData(CmsConstants
.STYLE
, style
);
83 /** Enable markups on widget */
84 public static void markup(Widget widget
) {
85 widget
.setData(CmsConstants
.MARKUP
, true);
88 public static void setItemHeight(Table table
, int height
) {
89 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
92 /** @return the path or null if not instrumented */
93 public static String
getDataPath(Widget widget
) {
95 Object data
= widget
.getData();
96 if (data
!= null && data
instanceof Item
) {
98 return ((Item
) data
).getPath();
99 } catch (RepositoryException e
) {
100 throw new CmsException("Cannot find data path of " + data
106 data
= widget
.getData(Property
.JCR_PATH
);
108 return data
.toString();
113 /** Dispose all children of a Composite */
114 public static void clear(Composite composite
) {
115 for (Control child
: composite
.getChildren())
122 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
123 throws RepositoryException
{
124 if (has(parent
, child
))
125 return child(parent
, child
);
126 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
129 public static Node
child(Node parent
, Enum
<?
> en
)
130 throws RepositoryException
{
131 return parent
.getNode(en
.name());
134 public static Boolean
has(Node parent
, Enum
<?
> en
)
135 throws RepositoryException
{
136 return parent
.hasNode(en
.name());
139 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
140 throws RepositoryException
{
141 return getOrAdd(parent
, en
, null);
144 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
145 throws RepositoryException
{
147 return child(parent
, en
);
148 else if (primaryType
== null)
149 return parent
.addNode(en
.name());
151 return parent
.addNode(en
.name(), primaryType
);
155 public static String
img(String src
, String width
, String height
) {
156 return imgBuilder(src
, width
, height
).append("/>").toString();
159 public static String
img(String src
, Point size
) {
160 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
163 public static StringBuilder
imgBuilder(String src
, String width
,
165 return new StringBuilder(64).append("<img width='").append(width
)
166 .append("' height='").append(height
).append("' src='")
167 .append(src
).append("'");
170 public static String
noImg(Point size
) {
171 ResourceManager rm
= RWT
.getResourceManager();
172 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
175 public static String
noImg() {
176 return noImg(NO_IMAGE_SIZE
);
179 public static Image
noImage(Point size
) {
180 ResourceManager rm
= RWT
.getResourceManager();
181 InputStream in
= null;
183 in
= rm
.getRegisteredContent(NO_IMAGE
);
184 ImageData id
= new ImageData(in
);
185 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
186 Image image
= new Image(Display
.getCurrent(), scaled
);
189 IOUtils
.closeQuietly(in
);