1 package org
.argeo
.cms
.util
;
3 import java
.io
.InputStream
;
4 import java
.net
.MalformedURLException
;
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
;
28 /** A link to an internal or external location. */
29 public class CmsLink
implements CmsUiProvider
, InitializingBean
,
31 private final static Log log
= LogFactory
.getLog(CmsLink
.class);
34 private String custom
;
35 private String target
;
37 private MouseListener mouseListener
;
39 private int verticalAlignment
= SWT
.CENTER
;
42 // private Boolean isUrl = false;
43 private Integer imageWidth
, imageHeight
;
45 private BundleContext bundleContext
;
51 public CmsLink(String label
, String target
) {
52 this(label
, target
, null);
55 public CmsLink(String label
, String target
, String custom
) {
64 public void afterPropertiesSet() {
65 // if (target != null) {
66 // if (target.startsWith("/")) {
72 // } catch (MalformedURLException e1) {
79 ImageData image
= loadImage();
80 imageWidth
= image
.width
;
81 imageHeight
= image
.height
;
85 /** @return {@link Composite} with a single {@link Label} child. */
87 public Control
createUi(final Composite parent
, Node context
) {
88 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
89 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
91 Label link
= new Label(comp
, SWT
.NONE
);
92 link
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
93 GridData layoutData
= new GridData(SWT
.CENTER
, verticalAlignment
, true,
96 layoutData
.heightHint
= imageHeight
;
98 layoutData
.widthHint
= imageWidth
;
101 link
.setLayoutData(layoutData
);
102 if (custom
!= null) {
103 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
104 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
106 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
107 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
111 StringBuilder labelText
= new StringBuilder();
112 if (target
!= null) {
114 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
116 // labelText.append('#');
117 labelText
.append(target
);
118 labelText
.append("\">");
121 registerImageIfNeeded();
122 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
123 labelText
.append("<img width='").append(imageWidth
)
124 .append("' height='").append(imageHeight
)
125 .append("' src=\"").append(imageLocation
).append("\"/>");
127 // final Image img = loadImage(parent.getDisplay());
128 // link.setImage(img);
129 // link.addDisposeListener(new DListener(img));
133 // link.setText(label);
134 labelText
.append(' ').append(label
);
138 labelText
.append("</a>");
140 link
.setText(labelText
.toString());
142 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
143 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
145 if (mouseListener
!= null)
146 link
.addMouseListener(mouseListener
);
151 private void registerImageIfNeeded() {
152 ResourceManager resourceManager
= RWT
.getResourceManager();
153 if (!resourceManager
.isRegistered(image
)) {
154 URL res
= getImageUrl();
155 InputStream inputStream
= null;
157 IOUtils
.closeQuietly(inputStream
);
158 inputStream
= res
.openStream();
159 resourceManager
.register(image
, inputStream
);
160 if (log
.isTraceEnabled())
161 log
.trace("Registered image " + image
);
162 } catch (Exception e
) {
163 throw new CmsException("Cannot load image " + image
, e
);
165 IOUtils
.closeQuietly(inputStream
);
170 private ImageData
loadImage() {
171 URL url
= getImageUrl();
172 ImageData result
= null;
173 InputStream inputStream
= null;
175 inputStream
= url
.openStream();
176 result
= new ImageData(inputStream
);
177 if (log
.isTraceEnabled())
178 log
.trace("Loaded image " + image
);
179 } catch (Exception e
) {
180 throw new CmsException("Cannot load image " + image
, e
);
182 IOUtils
.closeQuietly(inputStream
);
187 private URL
getImageUrl() {
191 url
= new URL(image
);
192 } catch (MalformedURLException e1
) {
194 if (bundleContext
== null)
195 throw new CmsException("No bundle context available");
196 url
= bundleContext
.getBundle().getResource(image
);
200 throw new CmsException("No image " + image
+ " available.");
205 public void setLabel(String label
) {
209 public void setCustom(String custom
) {
210 this.custom
= custom
;
213 public void setTarget(String target
) {
214 this.target
= target
;
218 // } catch (MalformedURLException e1) {
223 public void setImage(String image
) {
228 public void setBundleContext(BundleContext bundleContext
) {
229 this.bundleContext
= bundleContext
;
232 public void setMouseListener(MouseListener mouseListener
) {
233 this.mouseListener
= mouseListener
;
236 public void setvAlign(String vAlign
) {
237 if ("bottom".equals(vAlign
)) {
238 verticalAlignment
= SWT
.BOTTOM
;
239 } else if ("top".equals(vAlign
)) {
240 verticalAlignment
= SWT
.TOP
;
241 } else if ("center".equals(vAlign
)) {
242 verticalAlignment
= SWT
.CENTER
;
244 throw new CmsException("Unsupported vertical allignment " + vAlign
245 + " (must be: top, bottom or center)");
249 // private class MListener extends MouseAdapter {
250 // private static final long serialVersionUID = 3634864186295639792L;
253 // public void mouseDown(MouseEvent e) {
254 // if (e.button == 1) {
259 // private class DListener implements DisposeListener {
260 // private static final long serialVersionUID = -3808587499269394812L;
261 // private final Image img;
263 // public DListener(Image img) {
269 // public void widgetDisposed(DisposeEvent event) {