]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsLink.java
674dc3084795118b1686ae17b420772c8bbf8fb8
[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 @Override
86 public Control createUi(final Composite parent, Node context) {
87 Composite comp = new Composite(parent, SWT.BOTTOM);
88 comp.setLayout(CmsUtils.noSpaceGridLayout());
89
90 Label link = new Label(comp, SWT.NONE);
91 link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
92 GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
93 true);
94 if (image != null) {
95 layoutData.heightHint = imageHeight;
96 if (label == null)
97 layoutData.widthHint = imageWidth;
98 }
99
100 link.setLayoutData(layoutData);
101 if (custom != null) {
102 comp.setData(RWT.CUSTOM_VARIANT, custom);
103 link.setData(RWT.CUSTOM_VARIANT, custom);
104 } else {
105 comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
106 link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
107 }
108
109 // label
110 StringBuilder labelText = new StringBuilder();
111 if (target != null) {
112 labelText
113 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
114 // if (!isUrl)
115 // labelText.append('#');
116 labelText.append(target);
117 labelText.append("\">");
118 }
119 if (image != null) {
120 registerImageIfNeeded();
121 String imageLocation = RWT.getResourceManager().getLocation(image);
122 labelText.append("<img width='").append(imageWidth)
123 .append("' height='").append(imageHeight)
124 .append("' src=\"").append(imageLocation).append("\"/>");
125
126 // final Image img = loadImage(parent.getDisplay());
127 // link.setImage(img);
128 // link.addDisposeListener(new DListener(img));
129 }
130
131 if (label != null) {
132 // link.setText(label);
133 labelText.append(' ').append(label);
134 }
135
136 if (target != null)
137 labelText.append("</a>");
138
139 link.setText(labelText.toString());
140
141 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
142 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
143 // CmsSession.KEY);
144 if (mouseListener != null)
145 link.addMouseListener(mouseListener);
146
147 return comp;
148 }
149
150 private void registerImageIfNeeded() {
151 ResourceManager resourceManager = RWT.getResourceManager();
152 if (!resourceManager.isRegistered(image)) {
153 URL res = getImageUrl();
154 InputStream inputStream = null;
155 try {
156 IOUtils.closeQuietly(inputStream);
157 inputStream = res.openStream();
158 resourceManager.register(image, inputStream);
159 if (log.isTraceEnabled())
160 log.trace("Registered image " + image);
161 } catch (Exception e) {
162 throw new CmsException("Cannot load image " + image, e);
163 } finally {
164 IOUtils.closeQuietly(inputStream);
165 }
166 }
167 }
168
169 private ImageData loadImage() {
170 URL url = getImageUrl();
171 ImageData result = null;
172 InputStream inputStream = null;
173 try {
174 inputStream = url.openStream();
175 result = new ImageData(inputStream);
176 if (log.isTraceEnabled())
177 log.trace("Loaded image " + image);
178 } catch (Exception e) {
179 throw new CmsException("Cannot load image " + image, e);
180 } finally {
181 IOUtils.closeQuietly(inputStream);
182 }
183 return result;
184 }
185
186 private URL getImageUrl() {
187 URL url;
188 try {
189 // pure URL
190 url = new URL(image);
191 } catch (MalformedURLException e1) {
192 // in OSGi bundle
193 if (bundleContext == null)
194 throw new CmsException("No bundle context available");
195 url = bundleContext.getBundle().getResource(image);
196 }
197
198 if (url == null)
199 throw new CmsException("No image " + image + " available.");
200
201 return url;
202 }
203
204 public void setLabel(String label) {
205 this.label = label;
206 }
207
208 public void setCustom(String custom) {
209 this.custom = custom;
210 }
211
212 public void setTarget(String target) {
213 this.target = target;
214 // try {
215 // new URL(target);
216 // isUrl = true;
217 // } catch (MalformedURLException e1) {
218 // isUrl = false;
219 // }
220 }
221
222 public void setImage(String image) {
223 this.image = image;
224 }
225
226 @Override
227 public void setBundleContext(BundleContext bundleContext) {
228 this.bundleContext = bundleContext;
229 }
230
231 public void setMouseListener(MouseListener mouseListener) {
232 this.mouseListener = mouseListener;
233 }
234
235 public void setvAlign(String vAlign) {
236 if ("bottom".equals(vAlign)) {
237 verticalAlignment = SWT.BOTTOM;
238 } else if ("top".equals(vAlign)) {
239 verticalAlignment = SWT.TOP;
240 } else if ("center".equals(vAlign)) {
241 verticalAlignment = SWT.CENTER;
242 } else {
243 throw new CmsException("Unsupported vertical allignment " + vAlign
244 + " (must be: top, bottom or center)");
245 }
246 }
247
248 // private class MListener extends MouseAdapter {
249 // private static final long serialVersionUID = 3634864186295639792L;
250 //
251 // @Override
252 // public void mouseDown(MouseEvent e) {
253 // if (e.button == 1) {
254 // }
255 // }
256 // }
257 //
258 // private class DListener implements DisposeListener {
259 // private static final long serialVersionUID = -3808587499269394812L;
260 // private final Image img;
261 //
262 // public DListener(Image img) {
263 // super();
264 // this.img = img;
265 // }
266 //
267 // @Override
268 // public void widgetDisposed(DisposeEvent event) {
269 // img.dispose();
270 // }
271 //
272 // }
273 }