]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsLink.java
865824c1a90b09890db356db0feaa0d9a78fd48f
[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 Composite comp = new Composite(parent, SWT.BOTTOM);
71 comp.setLayout(CmsUtils.noSpaceGridLayout());
72
73 Label link = new Label(comp, SWT.NONE);
74 link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
75 GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
76 true);
77 if (image != null) {
78 layoutData.heightHint = imageHeight;
79 if (label == null)
80 layoutData.widthHint = imageWidth;
81 }
82
83 link.setLayoutData(layoutData);
84 if (custom != null) {
85 comp.setData(RWT.CUSTOM_VARIANT, custom);
86 link.setData(RWT.CUSTOM_VARIANT, custom);
87 } else {
88 comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
89 link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
90 }
91
92 // label
93 StringBuilder labelText = new StringBuilder();
94 if (target != null) {
95 labelText
96 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
97 // if (!isUrl)
98 // labelText.append('#');
99 labelText.append(target);
100 labelText.append("\">");
101 }
102 if (image != null) {
103 registerImageIfNeeded();
104 String imageLocation = RWT.getResourceManager().getLocation(image);
105 labelText.append("<img width='").append(imageWidth)
106 .append("' height='").append(imageHeight)
107 .append("' src=\"").append(imageLocation).append("\"/>");
108
109 // final Image img = loadImage(parent.getDisplay());
110 // link.setImage(img);
111 // link.addDisposeListener(new DListener(img));
112 }
113
114 if (label != null) {
115 // link.setText(label);
116 labelText.append(' ').append(label);
117 }
118
119 if (target != null)
120 labelText.append("</a>");
121
122 link.setText(labelText.toString());
123
124 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
125 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
126 // CmsSession.KEY);
127 if (mouseListener != null)
128 link.addMouseListener(mouseListener);
129
130 return comp;
131 }
132
133 private void registerImageIfNeeded() {
134 ResourceManager resourceManager = RWT.getResourceManager();
135 if (!resourceManager.isRegistered(image)) {
136 URL res = getImageUrl();
137 InputStream inputStream = null;
138 try {
139 IOUtils.closeQuietly(inputStream);
140 inputStream = res.openStream();
141 resourceManager.register(image, inputStream);
142 if (log.isTraceEnabled())
143 log.trace("Registered image " + image);
144 } catch (Exception e) {
145 throw new CmsException("Cannot load image " + image, e);
146 } finally {
147 IOUtils.closeQuietly(inputStream);
148 }
149 }
150 }
151
152 private ImageData loadImage() {
153 URL url = getImageUrl();
154 ImageData result = null;
155 InputStream inputStream = null;
156 try {
157 inputStream = url.openStream();
158 result = new ImageData(inputStream);
159 if (log.isTraceEnabled())
160 log.trace("Loaded image " + image);
161 } catch (Exception e) {
162 throw new CmsException("Cannot load image " + image, e);
163 } finally {
164 IOUtils.closeQuietly(inputStream);
165 }
166 return result;
167 }
168
169 private URL getImageUrl() {
170 URL url;
171 try {
172 // pure URL
173 url = new URL(image);
174 } catch (MalformedURLException e1) {
175 url = bundleContext.getBundle().getResource(image);
176 }
177
178 if (url == null)
179 throw new CmsException("No image " + image + " available.");
180
181 return url;
182 }
183
184 public void setBundleContext(BundleContext bundleContext) {
185 this.bundleContext = bundleContext;
186 }
187
188 public void setLabel(String label) {
189 this.label = label;
190 }
191
192 public void setCustom(String custom) {
193 this.custom = custom;
194 }
195
196 public void setTarget(String target) {
197 this.target = target;
198 // try {
199 // new URL(target);
200 // isUrl = true;
201 // } catch (MalformedURLException e1) {
202 // isUrl = false;
203 // }
204 }
205
206 public void setImage(String image) {
207 this.image = image;
208 }
209
210 public void setMouseListener(MouseListener mouseListener) {
211 this.mouseListener = mouseListener;
212 }
213
214 public void setvAlign(String vAlign) {
215 if ("bottom".equals(vAlign)) {
216 verticalAlignment = SWT.BOTTOM;
217 } else if ("top".equals(vAlign)) {
218 verticalAlignment = SWT.TOP;
219 } else if ("center".equals(vAlign)) {
220 verticalAlignment = SWT.CENTER;
221 } else {
222 throw new CmsException("Unsupported vertical allignment " + vAlign
223 + " (must be: top, bottom or center)");
224 }
225 }
226 }