1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.InputStream
;
4 import java
.net
.MalformedURLException
;
8 import javax
.jcr
.RepositoryException
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.api
.NodeUtils
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.cms
.auth
.CurrentUser
;
16 import org
.argeo
.cms
.ui
.CmsStyles
;
17 import org
.argeo
.cms
.ui
.CmsUiProvider
;
18 import org
.eclipse
.rap
.rwt
.RWT
;
19 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.MouseListener
;
22 import org
.eclipse
.swt
.graphics
.ImageData
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.osgi
.framework
.BundleContext
;
29 /** A link to an internal or external location. */
30 public class CmsLink
implements CmsUiProvider
{
31 private final static Log log
= LogFactory
.getLog(CmsLink
.class);
32 private BundleContext bundleContext
;
35 private String custom
;
36 private String target
;
38 private MouseListener mouseListener
;
40 private int horizontalAlignment
= SWT
.CENTER
;
41 private int verticalAlignment
= SWT
.CENTER
;
43 private String loggedInLabel
= null;
44 private String loggedInTarget
= null;
47 // private Boolean isUrl = false;
48 private Integer imageWidth
, imageHeight
;
54 public CmsLink(String label
, String target
) {
55 this(label
, target
, null);
58 public CmsLink(String label
, String target
, String custom
) {
68 ImageData image
= loadImage();
69 if (imageHeight
== null && imageWidth
== null) {
70 imageWidth
= image
.width
;
71 imageHeight
= image
.height
;
72 } else if (imageHeight
== null) {
73 imageHeight
= (imageWidth
* image
.height
) / image
.width
;
74 } else if (imageWidth
== null) {
75 imageWidth
= (imageHeight
* image
.width
) / image
.height
;
80 /** @return {@link Composite} with a single {@link Label} child. */
82 public Control
createUi(final Composite parent
, Node context
) {
83 // if (image != null && (imageWidth == null || imageHeight == null)) {
84 // throw new CmsException("Image is not properly configured."
85 // + " Make sure bundleContext property is set and init() method has been called.");
88 Composite comp
= new Composite(parent
, SWT
.NONE
);
89 comp
.setLayout(CmsUiUtils
.noSpaceGridLayout());
91 Label link
= new Label(comp
, SWT
.NONE
);
92 link
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
93 GridData layoutData
= new GridData(horizontalAlignment
, verticalAlignment
, false, false);
95 if (imageHeight
!= null)
96 layoutData
.heightHint
= imageHeight
;
98 if (imageWidth
!= null)
99 layoutData
.widthHint
= imageWidth
;
102 link
.setLayoutData(layoutData
);
103 if (custom
!= null) {
104 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
105 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
107 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
108 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
112 StringBuilder labelText
= new StringBuilder();
113 if (loggedInTarget
!= null && isLoggedIn()) {
114 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
115 if (loggedInTarget
.equals("")) {
117 Node homeNode
= NodeUtils
.getUserHome(context
.getSession());
118 String homePath
= homeNode
.getPath();
119 labelText
.append("/#" + homePath
);
120 } catch (RepositoryException e
) {
121 throw new CmsException("Cannot get home path", e
);
124 labelText
.append(loggedInTarget
);
126 labelText
.append("\">");
127 } else if (target
!= null) {
128 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
129 labelText
.append(target
);
130 labelText
.append("\">");
133 registerImageIfNeeded();
134 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
135 labelText
.append("<img");
136 if (imageWidth
!= null)
137 labelText
.append(" width='").append(imageWidth
).append('\'');
138 if (imageHeight
!= null)
139 labelText
.append(" height='").append(imageHeight
).append('\'');
140 labelText
.append(" src=\"").append(imageLocation
).append("\"/>");
144 if (loggedInLabel
!= null && isLoggedIn()) {
145 labelText
.append(' ').append(loggedInLabel
);
146 } else if (label
!= null) {
147 labelText
.append(' ').append(label
);
150 if ((loggedInTarget
!= null && isLoggedIn()) || target
!= null)
151 labelText
.append("</a>");
153 link
.setText(labelText
.toString());
155 if (mouseListener
!= null)
156 link
.addMouseListener(mouseListener
);
161 private void registerImageIfNeeded() {
162 ResourceManager resourceManager
= RWT
.getResourceManager();
163 if (!resourceManager
.isRegistered(image
)) {
164 URL res
= getImageUrl();
165 InputStream inputStream
= null;
167 IOUtils
.closeQuietly(inputStream
);
168 inputStream
= res
.openStream();
169 resourceManager
.register(image
, inputStream
);
170 if (log
.isTraceEnabled())
171 log
.trace("Registered image " + image
);
172 } catch (Exception e
) {
173 throw new CmsException("Cannot load image " + image
, e
);
175 IOUtils
.closeQuietly(inputStream
);
180 private ImageData
loadImage() {
181 URL url
= getImageUrl();
182 ImageData result
= null;
183 InputStream inputStream
= null;
185 inputStream
= url
.openStream();
186 result
= new ImageData(inputStream
);
187 if (log
.isTraceEnabled())
188 log
.trace("Loaded image " + image
);
189 } catch (Exception e
) {
190 throw new CmsException("Cannot load image " + image
, e
);
192 IOUtils
.closeQuietly(inputStream
);
197 private URL
getImageUrl() {
201 url
= new URL(image
);
202 } catch (MalformedURLException e1
) {
203 url
= bundleContext
.getBundle().getResource(image
);
207 throw new CmsException("No image " + image
+ " available.");
212 public void setBundleContext(BundleContext bundleContext
) {
213 this.bundleContext
= bundleContext
;
216 public void setLabel(String label
) {
220 public void setCustom(String custom
) {
221 this.custom
= custom
;
224 public void setTarget(String target
) {
225 this.target
= target
;
229 // } catch (MalformedURLException e1) {
234 public void setImage(String image
) {
238 public void setLoggedInLabel(String loggedInLabel
) {
239 this.loggedInLabel
= loggedInLabel
;
242 public void setLoggedInTarget(String loggedInTarget
) {
243 this.loggedInTarget
= loggedInTarget
;
246 public void setMouseListener(MouseListener mouseListener
) {
247 this.mouseListener
= mouseListener
;
250 public void setvAlign(String vAlign
) {
251 if ("bottom".equals(vAlign
)) {
252 verticalAlignment
= SWT
.BOTTOM
;
253 } else if ("top".equals(vAlign
)) {
254 verticalAlignment
= SWT
.TOP
;
255 } else if ("center".equals(vAlign
)) {
256 verticalAlignment
= SWT
.CENTER
;
258 throw new CmsException("Unsupported vertical allignment " + vAlign
+ " (must be: top, bottom or center)");
262 protected boolean isLoggedIn() {
263 return !CurrentUser
.isAnonymous();
266 public void setImageWidth(Integer imageWidth
) {
267 this.imageWidth
= imageWidth
;
270 public void setImageHeight(Integer imageHeight
) {
271 this.imageHeight
= imageHeight
;