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