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