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
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
13 import org
.eclipse
.rap
.rwt
.RWT
;
14 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.MouseListener
;
17 import org
.eclipse
.swt
.graphics
.ImageData
;
18 import org
.eclipse
.swt
.layout
.GridData
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Control
;
21 import org
.eclipse
.swt
.widgets
.Label
;
22 import org
.osgi
.framework
.BundleContext
;
23 import org
.springframework
.beans
.factory
.InitializingBean
;
25 /** A link to an internal or external location. */
26 public class CmsLink
implements CmsUiProvider
, InitializingBean
,
28 private final static Log log
= LogFactory
.getLog(CmsLink
.class);
31 private String custom
;
32 private String target
;
34 private MouseListener mouseListener
;
36 private int verticalAlignment
= SWT
.CENTER
;
39 //private Boolean isUrl = false;
40 private Integer imageWidth
, imageHeight
;
42 private BundleContext bundleContext
;
48 public CmsLink(String label
, String target
) {
49 this(label
, target
, null);
52 public CmsLink(String label
, String target
, String custom
) {
61 public void afterPropertiesSet() {
62 // if (target != null) {
63 // if (target.startsWith("/")) {
69 // } catch (MalformedURLException e1) {
76 ImageData image
= loadImage();
77 imageWidth
= image
.width
;
78 imageHeight
= image
.height
;
83 public Control
createUi(final Composite parent
, Node context
) {
84 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
85 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
87 Label link
= new Label(comp
, SWT
.NONE
);
88 link
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
89 GridData layoutData
= new GridData(SWT
.CENTER
, verticalAlignment
, true,
92 layoutData
.heightHint
= imageHeight
;
94 layoutData
.widthHint
= imageWidth
;
97 link
.setLayoutData(layoutData
);
99 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
100 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
102 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
103 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
107 StringBuilder labelText
= new StringBuilder();
108 if (target
!= null) {
110 .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
112 // labelText.append('#');
113 labelText
.append(target
);
114 labelText
.append("\">");
117 registerImageIfNeeded();
118 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
119 labelText
.append("<img width='").append(imageWidth
)
120 .append("' height='").append(imageHeight
)
121 .append("' src=\"").append(imageLocation
).append("\"/>");
123 // final Image img = loadImage(parent.getDisplay());
124 // link.setImage(img);
125 // link.addDisposeListener(new DListener(img));
129 // link.setText(label);
130 labelText
.append(' ').append(label
);
134 labelText
.append("</a>");
136 link
.setText(labelText
.toString());
138 // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
139 // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
141 if (mouseListener
!= null)
142 link
.addMouseListener(mouseListener
);
147 private void registerImageIfNeeded() {
148 ResourceManager resourceManager
= RWT
.getResourceManager();
149 if (!resourceManager
.isRegistered(image
)) {
150 URL res
= getImageUrl();
151 InputStream inputStream
= null;
153 IOUtils
.closeQuietly(inputStream
);
154 inputStream
= res
.openStream();
155 resourceManager
.register(image
, inputStream
);
156 if (log
.isTraceEnabled())
157 log
.trace("Registered image " + image
);
158 } catch (Exception e
) {
159 throw new CmsException("Cannot load image " + image
, e
);
161 IOUtils
.closeQuietly(inputStream
);
166 private ImageData
loadImage() {
167 URL url
= getImageUrl();
168 ImageData result
= null;
169 InputStream inputStream
= null;
171 inputStream
= url
.openStream();
172 result
= new ImageData(inputStream
);
173 if (log
.isTraceEnabled())
174 log
.trace("Loaded image " + image
);
175 } catch (Exception e
) {
176 throw new CmsException("Cannot load image " + image
, e
);
178 IOUtils
.closeQuietly(inputStream
);
183 private URL
getImageUrl() {
187 url
= new URL(image
);
188 } catch (MalformedURLException e1
) {
190 if (bundleContext
== null)
191 throw new CmsException("No bundle context available");
192 url
= bundleContext
.getBundle().getResource(image
);
196 throw new CmsException("No image " + image
+ " available.");
201 public void setLabel(String label
) {
205 public void setCustom(String custom
) {
206 this.custom
= custom
;
209 public void setTarget(String target
) {
210 this.target
= target
;
214 // } catch (MalformedURLException e1) {
219 public void setImage(String image
) {
224 public void setBundleContext(BundleContext bundleContext
) {
225 this.bundleContext
= bundleContext
;
228 public void setMouseListener(MouseListener mouseListener
) {
229 this.mouseListener
= mouseListener
;
232 public void setvAlign(String vAlign
) {
233 if ("bottom".equals(vAlign
)) {
234 verticalAlignment
= SWT
.BOTTOM
;
235 } else if ("top".equals(vAlign
)) {
236 verticalAlignment
= SWT
.TOP
;
237 } else if ("center".equals(vAlign
)) {
238 verticalAlignment
= SWT
.CENTER
;
240 throw new CmsException("Unsupported vertical allignment " + vAlign
241 + " (must be: top, bottom or center)");
245 // private class MListener extends MouseAdapter {
246 // private static final long serialVersionUID = 3634864186295639792L;
249 // public void mouseDown(MouseEvent e) {
250 // if (e.button == 1) {
255 // private class DListener implements DisposeListener {
256 // private static final long serialVersionUID = -3808587499269394812L;
257 // private final Image img;
259 // public DListener(Image img) {
265 // public void widgetDisposed(DisposeEvent event) {