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