]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsLink.java
14f3755ff9c4bbe4b1ca526860fb466bf5fbd5ba
[lgpl/argeo-commons.git] / CmsLink.java
1 package org.argeo.cms.util;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.jcr.Node;
8
9 import org.apache.commons.io.IOUtils;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.cms.CmsException;
13 import org.argeo.cms.CmsStyles;
14 import org.argeo.cms.CmsUiProvider;
15 import org.eclipse.gemini.blueprint.context.BundleContextAware;
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.events.MouseListener;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.osgi.framework.BundleContext;
26 import org.springframework.beans.factory.InitializingBean;
27
28 /** A link to an internal or external location. */
29 public class CmsLink implements CmsUiProvider, InitializingBean,
30 BundleContextAware {
31 private final static Log log = LogFactory.getLog(CmsLink.class);
32
33 private String label;
34 private String custom;
35 private String target;
36 private String image;
37 private MouseListener mouseListener;
38
39 private int verticalAlignment = SWT.CENTER;
40
41 // internal
42 // private Boolean isUrl = false;
43 private Integer imageWidth, imageHeight;
44
45 private BundleContext bundleContext;
46
47 public CmsLink() {
48 super();
49 }
50
51 public CmsLink(String label, String target) {
52 this(label, target, null);
53 }
54
55 public CmsLink(String label, String target, String custom) {
56 super();
57 this.label = label;
58 this.target = target;
59 this.custom = custom;
60 afterPropertiesSet();
61 }
62
63 @Override
64 public void afterPropertiesSet() {
65 // if (target != null) {
66 // if (target.startsWith("/")) {
67 // isUrl = true;
68 // } else {
69 // try {
70 // new URL(target);
71 // isUrl = true;
72 // } catch (MalformedURLException e1) {
73 // isUrl = false;
74 // }
75 // }
76 // }
77
78 if (image != null) {
79 ImageData image = loadImage();
80 imageWidth = image.width;
81 imageHeight = image.height;
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 Composite comp = new Composite(parent, SWT.BOTTOM);
89 comp.setLayout(CmsUtils.noSpaceGridLayout());
90
91 Label link = new Label(comp, SWT.NONE);
92 link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
93 GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
94 true);
95 if (image != null) {
96 layoutData.heightHint = imageHeight;
97 if (label == null)
98 layoutData.widthHint = imageWidth;
99 }
100
101 link.setLayoutData(layoutData);
102 if (custom != null) {
103 comp.setData(RWT.CUSTOM_VARIANT, custom);
104 link.setData(RWT.CUSTOM_VARIANT, custom);
105 } else {
106 comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
107 link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
108 }
109
110 // label
111 StringBuilder labelText = new StringBuilder();
112 if (target != null) {
113 labelText
114 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
115 // if (!isUrl)
116 // labelText.append('#');
117 labelText.append(target);
118 labelText.append("\">");
119 }
120 if (image != null) {
121 registerImageIfNeeded();
122 String imageLocation = RWT.getResourceManager().getLocation(image);
123 labelText.append("<img width='").append(imageWidth)
124 .append("' height='").append(imageHeight)
125 .append("' src=\"").append(imageLocation).append("\"/>");
126
127 // final Image img = loadImage(parent.getDisplay());
128 // link.setImage(img);
129 // link.addDisposeListener(new DListener(img));
130 }
131
132 if (label != null) {
133 // link.setText(label);
134 labelText.append(' ').append(label);
135 }
136
137 if (target != null)
138 labelText.append("</a>");
139
140 link.setText(labelText.toString());
141
142 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
143 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
144 // CmsSession.KEY);
145 if (mouseListener != null)
146 link.addMouseListener(mouseListener);
147
148 return comp;
149 }
150
151 private void registerImageIfNeeded() {
152 ResourceManager resourceManager = RWT.getResourceManager();
153 if (!resourceManager.isRegistered(image)) {
154 URL res = getImageUrl();
155 InputStream inputStream = null;
156 try {
157 IOUtils.closeQuietly(inputStream);
158 inputStream = res.openStream();
159 resourceManager.register(image, inputStream);
160 if (log.isTraceEnabled())
161 log.trace("Registered image " + image);
162 } catch (Exception e) {
163 throw new CmsException("Cannot load image " + image, e);
164 } finally {
165 IOUtils.closeQuietly(inputStream);
166 }
167 }
168 }
169
170 private ImageData loadImage() {
171 URL url = getImageUrl();
172 ImageData result = null;
173 InputStream inputStream = null;
174 try {
175 inputStream = url.openStream();
176 result = new ImageData(inputStream);
177 if (log.isTraceEnabled())
178 log.trace("Loaded image " + image);
179 } catch (Exception e) {
180 throw new CmsException("Cannot load image " + image, e);
181 } finally {
182 IOUtils.closeQuietly(inputStream);
183 }
184 return result;
185 }
186
187 private URL getImageUrl() {
188 URL url;
189 try {
190 // pure URL
191 url = new URL(image);
192 } catch (MalformedURLException e1) {
193 // in OSGi bundle
194 if (bundleContext == null)
195 throw new CmsException("No bundle context available");
196 url = bundleContext.getBundle().getResource(image);
197 }
198
199 if (url == null)
200 throw new CmsException("No image " + image + " available.");
201
202 return url;
203 }
204
205 public void setLabel(String label) {
206 this.label = label;
207 }
208
209 public void setCustom(String custom) {
210 this.custom = custom;
211 }
212
213 public void setTarget(String target) {
214 this.target = target;
215 // try {
216 // new URL(target);
217 // isUrl = true;
218 // } catch (MalformedURLException e1) {
219 // isUrl = false;
220 // }
221 }
222
223 public void setImage(String image) {
224 this.image = image;
225 }
226
227 @Override
228 public void setBundleContext(BundleContext bundleContext) {
229 this.bundleContext = bundleContext;
230 }
231
232 public void setMouseListener(MouseListener mouseListener) {
233 this.mouseListener = mouseListener;
234 }
235
236 public void setvAlign(String vAlign) {
237 if ("bottom".equals(vAlign)) {
238 verticalAlignment = SWT.BOTTOM;
239 } else if ("top".equals(vAlign)) {
240 verticalAlignment = SWT.TOP;
241 } else if ("center".equals(vAlign)) {
242 verticalAlignment = SWT.CENTER;
243 } else {
244 throw new CmsException("Unsupported vertical allignment " + vAlign
245 + " (must be: top, bottom or center)");
246 }
247 }
248
249 // private class MListener extends MouseAdapter {
250 // private static final long serialVersionUID = 3634864186295639792L;
251 //
252 // @Override
253 // public void mouseDown(MouseEvent e) {
254 // if (e.button == 1) {
255 // }
256 // }
257 // }
258 //
259 // private class DListener implements DisposeListener {
260 // private static final long serialVersionUID = -3808587499269394812L;
261 // private final Image img;
262 //
263 // public DListener(Image img) {
264 // super();
265 // this.img = img;
266 // }
267 //
268 // @Override
269 // public void widgetDisposed(DisposeEvent event) {
270 // img.dispose();
271 // }
272 //
273 // }
274 }