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