1 package org
.argeo
.cms
.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
.cms
.CmsException
;
14 import org
.argeo
.cms
.auth
.CurrentUser
;
15 import org
.argeo
.cms
.ui
.CmsStyles
;
16 import org
.argeo
.cms
.ui
.CmsUiProvider
;
17 import org
.argeo
.node
.NodeUtils
;
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 verticalAlignment
= SWT
.CENTER
;
42 private String loggedInLabel
= null;
43 private String loggedInTarget
= null;
46 // private Boolean isUrl = false;
47 private Integer imageWidth
, imageHeight
;
53 public CmsLink(String label
, String target
) {
54 this(label
, target
, null);
57 public CmsLink(String label
, String target
, String custom
) {
67 ImageData image
= loadImage();
68 imageWidth
= image
.width
;
69 imageHeight
= image
.height
;
73 /** @return {@link Composite} with a single {@link Label} child. */
75 public Control
createUi(final Composite parent
, Node context
) {
76 if (image
!= null && (imageWidth
== null || imageHeight
== null)) {
77 throw new CmsException("Image is not properly configured."
78 + " Make sure bundleContext property is set and init() method has been called.");
81 Composite comp
= new Composite(parent
, SWT
.BOTTOM
);
82 comp
.setLayout(CmsUtils
.noSpaceGridLayout());
84 Label link
= new Label(comp
, SWT
.NONE
);
85 link
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
86 GridData layoutData
= new GridData(SWT
.CENTER
, verticalAlignment
, true, true);
88 layoutData
.heightHint
= imageHeight
;
90 layoutData
.widthHint
= imageWidth
;
93 link
.setLayoutData(layoutData
);
95 comp
.setData(RWT
.CUSTOM_VARIANT
, custom
);
96 link
.setData(RWT
.CUSTOM_VARIANT
, custom
);
98 comp
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
99 link
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_LINK
);
103 StringBuilder labelText
= new StringBuilder();
104 if (loggedInTarget
!= null && isLoggedIn()) {
105 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
106 if (loggedInTarget
.equals("")) {
108 Node homeNode
= NodeUtils
.getUserHome(context
.getSession());
109 String homePath
= homeNode
.getPath();
110 labelText
.append("/#" + homePath
);
111 } catch (RepositoryException e
) {
112 throw new CmsException("Cannot get home path", e
);
115 labelText
.append(loggedInTarget
);
117 labelText
.append("\">");
118 } else if (target
!= null) {
119 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
120 labelText
.append(target
);
121 labelText
.append("\">");
124 registerImageIfNeeded();
125 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
126 labelText
.append("<img width='").append(imageWidth
).append("' height='").append(imageHeight
)
127 .append("' src=\"").append(imageLocation
).append("\"/>");
131 if (loggedInLabel
!= null && isLoggedIn()) {
132 labelText
.append(' ').append(loggedInLabel
);
133 } else if (label
!= null) {
134 labelText
.append(' ').append(label
);
137 if ((loggedInTarget
!= null && isLoggedIn()) || target
!= null)
138 labelText
.append("</a>");
140 link
.setText(labelText
.toString());
142 if (mouseListener
!= null)
143 link
.addMouseListener(mouseListener
);
148 private void registerImageIfNeeded() {
149 ResourceManager resourceManager
= RWT
.getResourceManager();
150 if (!resourceManager
.isRegistered(image
)) {
151 URL res
= getImageUrl();
152 InputStream inputStream
= null;
154 IOUtils
.closeQuietly(inputStream
);
155 inputStream
= res
.openStream();
156 resourceManager
.register(image
, inputStream
);
157 if (log
.isTraceEnabled())
158 log
.trace("Registered image " + image
);
159 } catch (Exception e
) {
160 throw new CmsException("Cannot load image " + image
, e
);
162 IOUtils
.closeQuietly(inputStream
);
167 private ImageData
loadImage() {
168 URL url
= getImageUrl();
169 ImageData result
= null;
170 InputStream inputStream
= null;
172 inputStream
= url
.openStream();
173 result
= new ImageData(inputStream
);
174 if (log
.isTraceEnabled())
175 log
.trace("Loaded image " + image
);
176 } catch (Exception e
) {
177 throw new CmsException("Cannot load image " + image
, e
);
179 IOUtils
.closeQuietly(inputStream
);
184 private URL
getImageUrl() {
188 url
= new URL(image
);
189 } catch (MalformedURLException e1
) {
190 url
= bundleContext
.getBundle().getResource(image
);
194 throw new CmsException("No image " + image
+ " available.");
199 public void setBundleContext(BundleContext bundleContext
) {
200 this.bundleContext
= bundleContext
;
203 public void setLabel(String label
) {
207 public void setCustom(String custom
) {
208 this.custom
= custom
;
211 public void setTarget(String target
) {
212 this.target
= target
;
216 // } catch (MalformedURLException e1) {
221 public void setImage(String image
) {
225 public void setLoggedInLabel(String loggedInLabel
) {
226 this.loggedInLabel
= loggedInLabel
;
229 public void setLoggedInTarget(String loggedInTarget
) {
230 this.loggedInTarget
= loggedInTarget
;
233 public void setMouseListener(MouseListener mouseListener
) {
234 this.mouseListener
= mouseListener
;
237 public void setvAlign(String vAlign
) {
238 if ("bottom".equals(vAlign
)) {
239 verticalAlignment
= SWT
.BOTTOM
;
240 } else if ("top".equals(vAlign
)) {
241 verticalAlignment
= SWT
.TOP
;
242 } else if ("center".equals(vAlign
)) {
243 verticalAlignment
= SWT
.CENTER
;
245 throw new CmsException("Unsupported vertical allignment " + vAlign
+ " (must be: top, bottom or center)");
249 protected boolean isLoggedIn() {
250 return !CurrentUser
.isAnonymous();