]> git.argeo.org Git - gpl/argeo-jcr.git/blob - CmsLink.java
e91f9ba482354231d381c92e8266fee6c3786ce1
[gpl/argeo-jcr.git] / CmsLink.java
1 package org.argeo.cms.ui.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7
8 import javax.jcr.Node;
9 import javax.jcr.RepositoryException;
10
11 import org.argeo.api.cms.CmsLog;
12 import org.argeo.api.cms.ux.CmsStyle;
13 import org.argeo.cms.auth.CurrentUser;
14 import org.argeo.cms.jcr.CmsJcrUtils;
15 import org.argeo.cms.swt.CmsSwtUtils;
16 import org.argeo.cms.ui.CmsUiProvider;
17 import org.argeo.jcr.JcrException;
18 import org.eclipse.rap.rwt.RWT;
19 import org.eclipse.rap.rwt.service.ResourceManager;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.MouseListener;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.osgi.framework.BundleContext;
28
29 /** A link to an internal or external location. */
30 public class CmsLink implements CmsUiProvider {
31 private final static CmsLog log = CmsLog.getLog(CmsLink.class);
32 private BundleContext bundleContext;
33
34 private String label;
35 private String style;
36 private String target;
37 private String image;
38 private boolean openNew = false;
39 private MouseListener mouseListener;
40
41 private int horizontalAlignment = SWT.CENTER;
42 private int verticalAlignment = SWT.CENTER;
43
44 private String loggedInLabel = null;
45 private String loggedInTarget = null;
46
47 // internal
48 // private Boolean isUrl = false;
49 private Integer imageWidth, imageHeight;
50
51 public CmsLink() {
52 super();
53 }
54
55 public CmsLink(String label, String target) {
56 this(label, target, (String) null);
57 }
58
59 public CmsLink(String label, String target, CmsStyle style) {
60 this(label, target, style != null ? style.style() : null);
61 }
62
63 public CmsLink(String label, String target, String style) {
64 super();
65 this.label = label;
66 this.target = target;
67 this.style = style;
68 init();
69 }
70
71 public void init() {
72 if (image != null) {
73 ImageData image = loadImage();
74 if (imageHeight == null && imageWidth == null) {
75 imageWidth = image.width;
76 imageHeight = image.height;
77 } else if (imageHeight == null) {
78 imageHeight = (imageWidth * image.height) / image.width;
79 } else if (imageWidth == null) {
80 imageWidth = (imageHeight * image.width) / image.height;
81 }
82 }
83 }
84
85 /** @return {@link Composite} with a single {@link Label} child. */
86 @Override
87 public Control createUi(final Composite parent, Node context) {
88 // if (image != null && (imageWidth == null || imageHeight == null)) {
89 // throw new CmsException("Image is not properly configured."
90 // + " Make sure bundleContext property is set and init() method has been called.");
91 // }
92
93 Composite comp = new Composite(parent, SWT.NONE);
94 comp.setLayout(CmsSwtUtils.noSpaceGridLayout());
95
96 Label link = new Label(comp, SWT.NONE);
97 CmsSwtUtils.markup(link);
98 GridData layoutData = new GridData(horizontalAlignment, verticalAlignment, false, false);
99 if (image != null) {
100 if (imageHeight != null)
101 layoutData.heightHint = imageHeight;
102 if (label == null)
103 if (imageWidth != null)
104 layoutData.widthHint = imageWidth;
105 }
106
107 link.setLayoutData(layoutData);
108 CmsSwtUtils.style(comp, style != null ? style : getDefaultStyle());
109 CmsSwtUtils.style(link, style != null ? style : getDefaultStyle());
110
111 // label
112 StringBuilder labelText = new StringBuilder();
113 if (loggedInTarget != null && isLoggedIn()) {
114 labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
115 if (loggedInTarget.equals("")) {
116 try {
117 Node homeNode = CmsJcrUtils.getUserHome(context.getSession());
118 String homePath = homeNode.getPath();
119 labelText.append("/#" + homePath);
120 } catch (RepositoryException e) {
121 throw new JcrException("Cannot get home path", e);
122 }
123 } else {
124 labelText.append(loggedInTarget);
125 }
126 labelText.append("\">");
127 } else if (target != null) {
128 labelText.append("<a style='color:inherit;text-decoration:inherit;' href='");
129 labelText.append(target).append("'");
130 if (openNew) {
131 labelText.append(" target='_blank'");
132 }
133 labelText.append(">");
134 }
135 if (image != null) {
136 registerImageIfNeeded();
137 String imageLocation = RWT.getResourceManager().getLocation(image);
138 labelText.append("<img");
139 if (imageWidth != null)
140 labelText.append(" width='").append(imageWidth).append('\'');
141 if (imageHeight != null)
142 labelText.append(" height='").append(imageHeight).append('\'');
143 labelText.append(" src=\"").append(imageLocation).append("\"/>");
144
145 }
146
147 if (loggedInLabel != null && isLoggedIn()) {
148 labelText.append(' ').append(loggedInLabel);
149 } else if (label != null) {
150 labelText.append(' ').append(label);
151 }
152
153 if ((loggedInTarget != null && isLoggedIn()) || target != null)
154 labelText.append("</a>");
155
156 link.setText(labelText.toString());
157
158 if (mouseListener != null)
159 link.addMouseListener(mouseListener);
160
161 return comp;
162 }
163
164 private void registerImageIfNeeded() {
165 ResourceManager resourceManager = RWT.getResourceManager();
166 if (!resourceManager.isRegistered(image)) {
167 URL res = getImageUrl();
168 try (InputStream inputStream = res.openStream()) {
169 resourceManager.register(image, inputStream);
170 if (log.isTraceEnabled())
171 log.trace("Registered image " + image);
172 } catch (IOException e) {
173 throw new RuntimeException("Cannot load image " + image, e);
174 }
175 }
176 }
177
178 private ImageData loadImage() {
179 URL url = getImageUrl();
180 ImageData result = null;
181 try (InputStream inputStream = url.openStream()) {
182 result = new ImageData(inputStream);
183 if (log.isTraceEnabled())
184 log.trace("Loaded image " + image);
185 } catch (IOException e) {
186 throw new RuntimeException("Cannot load image " + image, e);
187 }
188 return result;
189 }
190
191 private URL getImageUrl() {
192 URL url;
193 try {
194 // pure URL
195 url = new URL(image);
196 } catch (MalformedURLException e1) {
197 url = bundleContext.getBundle().getResource(image);
198 }
199
200 if (url == null)
201 throw new IllegalStateException("No image " + image + " available.");
202
203 return url;
204 }
205
206 public void setBundleContext(BundleContext bundleContext) {
207 this.bundleContext = bundleContext;
208 }
209
210 public void setLabel(String label) {
211 this.label = label;
212 }
213
214 public void setStyle(String style) {
215 this.style = style;
216 }
217
218 /** @deprecated Use {@link #setStyle(String)} instead. */
219 @Deprecated
220 public void setCustom(String custom) {
221 this.style = custom;
222 }
223
224 public void setTarget(String target) {
225 this.target = target;
226 // try {
227 // new URL(target);
228 // isUrl = true;
229 // } catch (MalformedURLException e1) {
230 // isUrl = false;
231 // }
232 }
233
234 public void setImage(String image) {
235 this.image = image;
236 }
237
238 public void setLoggedInLabel(String loggedInLabel) {
239 this.loggedInLabel = loggedInLabel;
240 }
241
242 public void setLoggedInTarget(String loggedInTarget) {
243 this.loggedInTarget = loggedInTarget;
244 }
245
246 public void setMouseListener(MouseListener mouseListener) {
247 this.mouseListener = mouseListener;
248 }
249
250 public void setvAlign(String vAlign) {
251 if ("bottom".equals(vAlign)) {
252 verticalAlignment = SWT.BOTTOM;
253 } else if ("top".equals(vAlign)) {
254 verticalAlignment = SWT.TOP;
255 } else if ("center".equals(vAlign)) {
256 verticalAlignment = SWT.CENTER;
257 } else {
258 throw new IllegalArgumentException(
259 "Unsupported vertical alignment " + vAlign + " (must be: top, bottom or center)");
260 }
261 }
262
263 protected boolean isLoggedIn() {
264 return !CurrentUser.isAnonymous();
265 }
266
267 public void setImageWidth(Integer imageWidth) {
268 this.imageWidth = imageWidth;
269 }
270
271 public void setImageHeight(Integer imageHeight) {
272 this.imageHeight = imageHeight;
273 }
274
275 public void setOpenNew(boolean openNew) {
276 this.openNew = openNew;
277 }
278
279 protected String getDefaultStyle() {
280 return SimpleStyle.link.name();
281 }
282 }