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