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