]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/util/CmsLink.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / util / 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.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 import org.osgi.framework.FrameworkUtil;
26
27 /** A link to an internal or external location. */
28 public class CmsLink implements CmsUiProvider {
29 private final static Log log = LogFactory.getLog(CmsLink.class);
30 private BundleContext bc = FrameworkUtil.getBundle(getClass())
31 .getBundleContext();
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 public CmsLink() {
46 super();
47 }
48
49 public CmsLink(String label, String target) {
50 this(label, target, null);
51 }
52
53 public CmsLink(String label, String target, String custom) {
54 super();
55 this.label = label;
56 this.target = target;
57 this.custom = custom;
58 init();
59 }
60
61 public void init() {
62 if (image != null) {
63 ImageData image = loadImage();
64 imageWidth = image.width;
65 imageHeight = image.height;
66 }
67 }
68
69 /** @return {@link Composite} with a single {@link Label} child. */
70 @Override
71 public Control createUi(final Composite parent, Node context) {
72 Composite comp = new Composite(parent, SWT.BOTTOM);
73 comp.setLayout(CmsUtils.noSpaceGridLayout());
74
75 Label link = new Label(comp, SWT.NONE);
76 link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
77 GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
78 true);
79 if (image != null) {
80 layoutData.heightHint = imageHeight;
81 if (label == null)
82 layoutData.widthHint = imageWidth;
83 }
84
85 link.setLayoutData(layoutData);
86 if (custom != null) {
87 comp.setData(RWT.CUSTOM_VARIANT, custom);
88 link.setData(RWT.CUSTOM_VARIANT, custom);
89 } else {
90 comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
91 link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
92 }
93
94 // label
95 StringBuilder labelText = new StringBuilder();
96 if (target != null) {
97 labelText
98 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
99 // if (!isUrl)
100 // labelText.append('#');
101 labelText.append(target);
102 labelText.append("\">");
103 }
104 if (image != null) {
105 registerImageIfNeeded();
106 String imageLocation = RWT.getResourceManager().getLocation(image);
107 labelText.append("<img width='").append(imageWidth)
108 .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 = bc.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 setLabel(String label) {
187 this.label = label;
188 }
189
190 public void setCustom(String custom) {
191 this.custom = custom;
192 }
193
194 public void setTarget(String target) {
195 this.target = target;
196 // try {
197 // new URL(target);
198 // isUrl = true;
199 // } catch (MalformedURLException e1) {
200 // isUrl = false;
201 // }
202 }
203
204 public void setImage(String image) {
205 this.image = image;
206 }
207
208 public void setMouseListener(MouseListener mouseListener) {
209 this.mouseListener = mouseListener;
210 }
211
212 public void setvAlign(String vAlign) {
213 if ("bottom".equals(vAlign)) {
214 verticalAlignment = SWT.BOTTOM;
215 } else if ("top".equals(vAlign)) {
216 verticalAlignment = SWT.TOP;
217 } else if ("center".equals(vAlign)) {
218 verticalAlignment = SWT.CENTER;
219 } else {
220 throw new CmsException("Unsupported vertical allignment " + vAlign
221 + " (must be: top, bottom or center)");
222 }
223 }
224 }