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
;
86 public Control
createUi(final Composite parent
, Node context
) {
87 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
88 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
90 Label link
= new Label(comp
, SWT
.NONE
);
91 link
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
92 GridData layoutData
= new GridData(SWT
.CENTER
, verticalAlignment
, true,
95 layoutData
.heightHint
= imageHeight
;
97 layoutData
.widthHint
= imageWidth
;
100 link
.setLayoutData(layoutData
);
101 if (custom
!= null) {
102 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
103 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
105 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
106 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
110 StringBuilder labelText
= new StringBuilder();
111 if (target
!= null) {
113 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
115 // labelText.append('#');
116 labelText
.append(target
);
117 labelText
.append("\">");
120 registerImageIfNeeded();
121 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
122 labelText
.append("<img width='").append(imageWidth
)
123 .append("' height='").append(imageHeight
)
124 .append("' src=\"").append(imageLocation
).append("\"/>");
126 // final Image img = loadImage(parent.getDisplay());
127 // link.setImage(img);
128 // link.addDisposeListener(new DListener(img));
132 // link.setText(label);
133 labelText
.append(' ').append(label
);
137 labelText
.append("</a>");
139 link
.setText(labelText
.toString());
141 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
142 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
144 if (mouseListener
!= null)
145 link
.addMouseListener(mouseListener
);
150 private void registerImageIfNeeded() {
151 ResourceManager resourceManager
= RWT
.getResourceManager();
152 if (!resourceManager
.isRegistered(image
)) {
153 URL res
= getImageUrl();
154 InputStream inputStream
= null;
156 IOUtils
.closeQuietly(inputStream
);
157 inputStream
= res
.openStream();
158 resourceManager
.register(image
, inputStream
);
159 if (log
.isTraceEnabled())
160 log
.trace("Registered image " + image
);
161 } catch (Exception e
) {
162 throw new CmsException("Cannot load image " + image
, e
);
164 IOUtils
.closeQuietly(inputStream
);
169 private ImageData
loadImage() {
170 URL url
= getImageUrl();
171 ImageData result
= null;
172 InputStream inputStream
= null;
174 inputStream
= url
.openStream();
175 result
= new ImageData(inputStream
);
176 if (log
.isTraceEnabled())
177 log
.trace("Loaded image " + image
);
178 } catch (Exception e
) {
179 throw new CmsException("Cannot load image " + image
, e
);
181 IOUtils
.closeQuietly(inputStream
);
186 private URL
getImageUrl() {
190 url
= new URL(image
);
191 } catch (MalformedURLException e1
) {
193 if (bundleContext
== null)
194 throw new CmsException("No bundle context available");
195 url
= bundleContext
.getBundle().getResource(image
);
199 throw new CmsException("No image " + image
+ " available.");
204 public void setLabel(String label
) {
208 public void setCustom(String custom
) {
209 this.custom
= custom
;
212 public void setTarget(String target
) {
213 this.target
= target
;
217 // } catch (MalformedURLException e1) {
222 public void setImage(String image
) {
227 public void setBundleContext(BundleContext bundleContext
) {
228 this.bundleContext
= bundleContext
;
231 public void setMouseListener(MouseListener mouseListener
) {
232 this.mouseListener
= mouseListener
;
235 public void setvAlign(String vAlign
) {
236 if ("bottom".equals(vAlign
)) {
237 verticalAlignment
= SWT
.BOTTOM
;
238 } else if ("top".equals(vAlign
)) {
239 verticalAlignment
= SWT
.TOP
;
240 } else if ("center".equals(vAlign
)) {
241 verticalAlignment
= SWT
.CENTER
;
243 throw new CmsException("Unsupported vertical allignment " + vAlign
244 + " (must be: top, bottom or center)");
248 // private class MListener extends MouseAdapter {
249 // private static final long serialVersionUID = 3634864186295639792L;
252 // public void mouseDown(MouseEvent e) {
253 // if (e.button == 1) {
258 // private class DListener implements DisposeListener {
259 // private static final long serialVersionUID = -3808587499269394812L;
260 // private final Image img;
262 // public DListener(Image img) {
268 // public void widgetDisposed(DisposeEvent event) {