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
.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
;
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
;
32 private String custom
;
33 private String target
;
35 private MouseListener mouseListener
;
37 private int verticalAlignment
= SWT
.CENTER
;
40 // private Boolean isUrl = false;
41 private Integer imageWidth
, imageHeight
;
47 public CmsLink(String label
, String target
) {
48 this(label
, target
, null);
51 public CmsLink(String label
, String target
, String custom
) {
61 ImageData image
= loadImage();
62 imageWidth
= image
.width
;
63 imageHeight
= image
.height
;
67 /** @return {@link Composite} with a single {@link Label} child. */
69 public Control
createUi(final Composite parent
, Node context
) {
70 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
71 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
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,
78 layoutData
.heightHint
= imageHeight
;
80 layoutData
.widthHint
= imageWidth
;
83 link
.setLayoutData(layoutData
);
85 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
86 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
88 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
89 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
93 StringBuilder labelText
= new StringBuilder();
96 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
98 // labelText.append('#');
99 labelText
.append(target
);
100 labelText
.append("\">");
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("\"/>");
109 // final Image img = loadImage(parent.getDisplay());
110 // link.setImage(img);
111 // link.addDisposeListener(new DListener(img));
115 // link.setText(label);
116 labelText
.append(' ').append(label
);
120 labelText
.append("</a>");
122 link
.setText(labelText
.toString());
124 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
125 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
127 if (mouseListener
!= null)
128 link
.addMouseListener(mouseListener
);
133 private void registerImageIfNeeded() {
134 ResourceManager resourceManager
= RWT
.getResourceManager();
135 if (!resourceManager
.isRegistered(image
)) {
136 URL res
= getImageUrl();
137 InputStream inputStream
= null;
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
);
147 IOUtils
.closeQuietly(inputStream
);
152 private ImageData
loadImage() {
153 URL url
= getImageUrl();
154 ImageData result
= null;
155 InputStream inputStream
= null;
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
);
164 IOUtils
.closeQuietly(inputStream
);
169 private URL
getImageUrl() {
173 url
= new URL(image
);
174 } catch (MalformedURLException e1
) {
175 url
= bundleContext
.getBundle().getResource(image
);
179 throw new CmsException("No image " + image
+ " available.");
184 public void setBundleContext(BundleContext bundleContext
) {
185 this.bundleContext
= bundleContext
;
188 public void setLabel(String label
) {
192 public void setCustom(String custom
) {
193 this.custom
= custom
;
196 public void setTarget(String target
) {
197 this.target
= target
;
201 // } catch (MalformedURLException e1) {
206 public void setImage(String image
) {
210 public void setMouseListener(MouseListener mouseListener
) {
211 this.mouseListener
= mouseListener
;
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
;
222 throw new CmsException("Unsupported vertical allignment " + vAlign
223 + " (must be: top, bottom or center)");