]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUtils.java
0eda74f53496be9ade00a176f3e05276c991f46c
[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 /** Style widget */
69 public static void style(Widget widget, String style) {
70 widget.setData(CmsConstants.STYLE, style);
71 }
72
73 /** Enable markups on widget */
74 public static void markup(Widget widget) {
75 widget.setData(CmsConstants.MARKUP, true);
76 }
77
78 public static void setItemHeight(Table table, int height) {
79 table.setData(CmsConstants.ITEM_HEIGHT, height);
80 }
81
82 /** @return the path or null if not instrumented */
83 public static String getDataPath(Widget widget) {
84 // JCR item
85 Object data = widget.getData();
86 if (data != null && data instanceof Item) {
87 try {
88 return ((Item) data).getPath();
89 } catch (RepositoryException e) {
90 throw new CmsException("Cannot find data path of " + data
91 + " for " + widget);
92 }
93 }
94
95 // JCR path
96 data = widget.getData(Property.JCR_PATH);
97 if (data != null)
98 return data.toString();
99
100 return null;
101 }
102
103 /** Dispose all children of a Composite */
104 public static void clear(Composite composite) {
105 for (Control child : composite.getChildren())
106 child.dispose();
107 }
108
109 //
110 // JCR
111 //
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]);
117 }
118
119 public static Node child(Node parent, Enum<?> en)
120 throws RepositoryException {
121 return parent.getNode(en.name());
122 }
123
124 public static Boolean has(Node parent, Enum<?> en)
125 throws RepositoryException {
126 return parent.hasNode(en.name());
127 }
128
129 public static Node getOrAdd(Node parent, Enum<?> en)
130 throws RepositoryException {
131 return getOrAdd(parent, en, null);
132 }
133
134 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType)
135 throws RepositoryException {
136 if (has(parent, en))
137 return child(parent, en);
138 else if (primaryType == null)
139 return parent.addNode(en.name());
140 else
141 return parent.addNode(en.name(), primaryType);
142 }
143
144 // IMAGES
145 public static String img(String src, String width, String height) {
146 return imgBuilder(src, width, height).append("/>").toString();
147 }
148
149 public static String img(String src, Point size) {
150 return img(src, Integer.toString(size.x), Integer.toString(size.y));
151 }
152
153 public static StringBuilder imgBuilder(String src, String width,
154 String height) {
155 return new StringBuilder(64).append("<img width='").append(width)
156 .append("' height='").append(height).append("' src='")
157 .append(src).append("'");
158 }
159
160 public static String noImg(Point size) {
161 ResourceManager rm = RWT.getResourceManager();
162 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
163 }
164
165 public static String noImg() {
166 return noImg(NO_IMAGE_SIZE);
167 }
168
169 public static Image noImage(Point size) {
170 ResourceManager rm = RWT.getResourceManager();
171 InputStream in = null;
172 try {
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);
177 return image;
178 } finally {
179 IOUtils.closeQuietly(in);
180 }
181 }
182
183 private CmsUtils() {
184 }
185 }