]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/CmsUtils.java
10e52387302a45f1e7aba319ed2b749615c89ae3
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / CmsUtils.java
1 package org.argeo.cms;
2
3 import java.io.InputStream;
4
5 import javax.jcr.Item;
6 import javax.jcr.Node;
7 import javax.jcr.Property;
8 import javax.jcr.RepositoryException;
9
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;
25
26 /** Static utilities for the CMS framework. */
27 public class CmsUtils implements CmsConstants {
28 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
29 @Deprecated
30 public static RowData ROW_DATA_16px = new RowData(16, 16);
31
32 public static GridLayout noSpaceGridLayout() {
33 return noSpaceGridLayout(new GridLayout());
34 }
35
36 public static GridLayout noSpaceGridLayout(GridLayout layout) {
37 layout.horizontalSpacing = 0;
38 layout.verticalSpacing = 0;
39 layout.marginWidth = 0;
40 layout.marginHeight = 0;
41 return layout;
42 }
43
44 //
45 // GRID DATA
46 //
47 public static GridData fillWidth() {
48 return grabWidth(SWT.FILL, SWT.FILL);
49 }
50
51 public static GridData fillAll() {
52 return new GridData(SWT.FILL, SWT.FILL, true, true);
53 }
54
55 public static GridData grabWidth(int horizontalAlignment,
56 int verticalAlignment) {
57 return new GridData(horizontalAlignment, horizontalAlignment, true,
58 false);
59 }
60
61 public static RowData rowData16px() {
62 return new RowData(16, 16);
63 }
64
65 public static void style(Widget widget, String style) {
66 widget.setData(CmsConstants.STYLE, style);
67 }
68
69 public static void markup(Widget widget) {
70 widget.setData(CmsConstants.MARKUP, true);
71 }
72
73 /** @return the path or null if not instrumented */
74 public static String getDataPath(Widget widget) {
75 // JCR item
76 Object data = widget.getData();
77 if (data != null && data instanceof Item) {
78 try {
79 return ((Item) data).getPath();
80 } catch (RepositoryException e) {
81 throw new CmsException("Cannot find data path of " + data
82 + " for " + widget);
83 }
84 }
85
86 // JCR path
87 data = widget.getData(Property.JCR_PATH);
88 if (data != null)
89 return data.toString();
90
91 return null;
92 }
93
94 /** Dispose all children of a Composite */
95 public static void clear(Composite composite) {
96 for (Control child : composite.getChildren())
97 child.dispose();
98 }
99
100 //
101 // JCR
102 //
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]);
108 }
109
110 public static Node child(Node parent, Enum<?> en)
111 throws RepositoryException {
112 return parent.getNode(en.name());
113 }
114
115 public static Boolean has(Node parent, Enum<?> en)
116 throws RepositoryException {
117 return parent.hasNode(en.name());
118 }
119
120 public static Node getOrAdd(Node parent, Enum<?> en)
121 throws RepositoryException {
122 return getOrAdd(parent, en, null);
123 }
124
125 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType)
126 throws RepositoryException {
127 if (has(parent, en))
128 return child(parent, en);
129 else if (primaryType == null)
130 return parent.addNode(en.name());
131 else
132 return parent.addNode(en.name(), primaryType);
133 }
134
135 // IMAGES
136 public static String img(String src, String width, String height) {
137 return imgBuilder(src, width, height).append("/>").toString();
138 }
139
140 public static String img(String src, Point size) {
141 return img(src, Integer.toString(size.x), Integer.toString(size.y));
142 }
143
144 public static StringBuilder imgBuilder(String src, String width,
145 String height) {
146 return new StringBuilder(64).append("<img width='").append(width)
147 .append("' height='").append(height).append("' src='")
148 .append(src).append("'");
149 }
150
151 public static String noImg(Point size) {
152 ResourceManager rm = RWT.getResourceManager();
153 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
154 }
155
156 public static String noImg() {
157 return noImg(NO_IMAGE_SIZE);
158 }
159
160 public static Image noImage(Point size) {
161 ResourceManager rm = RWT.getResourceManager();
162 InputStream in = null;
163 try {
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);
168 return image;
169 } finally {
170 IOUtils.closeQuietly(in);
171 }
172 }
173
174 private CmsUtils() {
175 }
176 }