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
.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
;
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())
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
;
49 public CmsLink(String label
, String target
) {
50 this(label
, target
, null);
53 public CmsLink(String label
, String target
, String custom
) {
63 ImageData image
= loadImage();
64 imageWidth
= image
.width
;
65 imageHeight
= image
.height
;
69 /** @return {@link Composite} with a single {@link Label} child. */
71 public Control
createUi(final Composite parent
, Node context
) {
72 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
73 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
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,
80 layoutData
.heightHint
= imageHeight
;
82 layoutData
.widthHint
= imageWidth
;
85 link
.setLayoutData(layoutData
);
87 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
88 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
90 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
91 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
95 StringBuilder labelText
= new StringBuilder();
98 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
100 // labelText.append('#');
101 labelText
.append(target
);
102 labelText
.append("\">");
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("\"/>");
111 // final Image img = loadImage(parent.getDisplay());
112 // link.setImage(img);
113 // link.addDisposeListener(new DListener(img));
117 // link.setText(label);
118 labelText
.append(' ').append(label
);
122 labelText
.append("</a>");
124 link
.setText(labelText
.toString());
126 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
127 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
129 if (mouseListener
!= null)
130 link
.addMouseListener(mouseListener
);
135 private void registerImageIfNeeded() {
136 ResourceManager resourceManager
= RWT
.getResourceManager();
137 if (!resourceManager
.isRegistered(image
)) {
138 URL res
= getImageUrl();
139 InputStream inputStream
= null;
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
);
149 IOUtils
.closeQuietly(inputStream
);
154 private ImageData
loadImage() {
155 URL url
= getImageUrl();
156 ImageData result
= null;
157 InputStream inputStream
= null;
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
);
166 IOUtils
.closeQuietly(inputStream
);
171 private URL
getImageUrl() {
175 url
= new URL(image
);
176 } catch (MalformedURLException e1
) {
177 url
= bc
.getBundle().getResource(image
);
181 throw new CmsException("No image " + image
+ " available.");
186 public void setLabel(String label
) {
190 public void setCustom(String custom
) {
191 this.custom
= custom
;
194 public void setTarget(String target
) {
195 this.target
= target
;
199 // } catch (MalformedURLException e1) {
204 public void setImage(String image
) {
208 public void setMouseListener(MouseListener mouseListener
) {
209 this.mouseListener
= mouseListener
;
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
;
220 throw new CmsException("Unsupported vertical allignment " + vAlign
221 + " (must be: top, bottom or center)");