1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
9 import javax
.jcr
.RepositoryException
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.api
.cms
.CmsStyle
;
14 import org
.argeo
.cms
.auth
.CurrentUser
;
15 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
16 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
17 import org
.argeo
.cms
.ui
.CmsUiProvider
;
18 import org
.argeo
.jcr
.JcrException
;
19 import org
.eclipse
.rap
.rwt
.RWT
;
20 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.MouseListener
;
23 import org
.eclipse
.swt
.graphics
.ImageData
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.osgi
.framework
.BundleContext
;
30 /** A link to an internal or external location. */
31 public class CmsLink
implements CmsUiProvider
{
32 private final static Log log
= LogFactory
.getLog(CmsLink
.class);
33 private BundleContext bundleContext
;
37 private String target
;
39 private boolean openNew
= false;
40 private MouseListener mouseListener
;
42 private int horizontalAlignment
= SWT
.CENTER
;
43 private int verticalAlignment
= SWT
.CENTER
;
45 private String loggedInLabel
= null;
46 private String loggedInTarget
= null;
49 // private Boolean isUrl = false;
50 private Integer imageWidth
, imageHeight
;
56 public CmsLink(String label
, String target
) {
57 this(label
, target
, (String
) null);
60 public CmsLink(String label
, String target
, CmsStyle style
) {
61 this(label
, target
, style
!= null ? style
.style() : null);
64 public CmsLink(String label
, String target
, String style
) {
74 ImageData image
= loadImage();
75 if (imageHeight
== null && imageWidth
== null) {
76 imageWidth
= image
.width
;
77 imageHeight
= image
.height
;
78 } else if (imageHeight
== null) {
79 imageHeight
= (imageWidth
* image
.height
) / image
.width
;
80 } else if (imageWidth
== null) {
81 imageWidth
= (imageHeight
* image
.width
) / image
.height
;
86 /** @return {@link Composite} with a single {@link Label} child. */
88 public Control
createUi(final Composite parent
, Node context
) {
89 // if (image != null && (imageWidth == null || imageHeight == null)) {
90 // throw new CmsException("Image is not properly configured."
91 // + " Make sure bundleContext property is set and init() method has been called.");
94 Composite comp
= new Composite(parent
, SWT
.NONE
);
95 comp
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
97 Label link
= new Label(comp
, SWT
.NONE
);
98 CmsSwtUtils
.markup(link
);
99 GridData layoutData
= new GridData(horizontalAlignment
, verticalAlignment
, false, false);
101 if (imageHeight
!= null)
102 layoutData
.heightHint
= imageHeight
;
104 if (imageWidth
!= null)
105 layoutData
.widthHint
= imageWidth
;
108 link
.setLayoutData(layoutData
);
109 CmsSwtUtils
.style(comp
, style
!= null ? style
: getDefaultStyle());
110 CmsSwtUtils
.style(link
, style
!= null ? style
: getDefaultStyle());
113 StringBuilder labelText
= new StringBuilder();
114 if (loggedInTarget
!= null && isLoggedIn()) {
115 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
116 if (loggedInTarget
.equals("")) {
118 Node homeNode
= CmsJcrUtils
.getUserHome(context
.getSession());
119 String homePath
= homeNode
.getPath();
120 labelText
.append("/#" + homePath
);
121 } catch (RepositoryException e
) {
122 throw new JcrException("Cannot get home path", e
);
125 labelText
.append(loggedInTarget
);
127 labelText
.append("\">");
128 } else if (target
!= null) {
129 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href='");
130 labelText
.append(target
).append("'");
132 labelText
.append(" target='_blank'");
134 labelText
.append(">");
137 registerImageIfNeeded();
138 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
139 labelText
.append("<img");
140 if (imageWidth
!= null)
141 labelText
.append(" width='").append(imageWidth
).append('\'');
142 if (imageHeight
!= null)
143 labelText
.append(" height='").append(imageHeight
).append('\'');
144 labelText
.append(" src=\"").append(imageLocation
).append("\"/>");
148 if (loggedInLabel
!= null && isLoggedIn()) {
149 labelText
.append(' ').append(loggedInLabel
);
150 } else if (label
!= null) {
151 labelText
.append(' ').append(label
);
154 if ((loggedInTarget
!= null && isLoggedIn()) || target
!= null)
155 labelText
.append("</a>");
157 link
.setText(labelText
.toString());
159 if (mouseListener
!= null)
160 link
.addMouseListener(mouseListener
);
165 private void registerImageIfNeeded() {
166 ResourceManager resourceManager
= RWT
.getResourceManager();
167 if (!resourceManager
.isRegistered(image
)) {
168 URL res
= getImageUrl();
169 try (InputStream inputStream
= res
.openStream()) {
170 resourceManager
.register(image
, inputStream
);
171 if (log
.isTraceEnabled())
172 log
.trace("Registered image " + image
);
173 } catch (IOException e
) {
174 throw new RuntimeException("Cannot load image " + image
, e
);
179 private ImageData
loadImage() {
180 URL url
= getImageUrl();
181 ImageData result
= null;
182 try (InputStream inputStream
= url
.openStream()) {
183 result
= new ImageData(inputStream
);
184 if (log
.isTraceEnabled())
185 log
.trace("Loaded image " + image
);
186 } catch (IOException e
) {
187 throw new RuntimeException("Cannot load image " + image
, e
);
192 private URL
getImageUrl() {
196 url
= new URL(image
);
197 } catch (MalformedURLException e1
) {
198 url
= bundleContext
.getBundle().getResource(image
);
202 throw new IllegalStateException("No image " + image
+ " available.");
207 public void setBundleContext(BundleContext bundleContext
) {
208 this.bundleContext
= bundleContext
;
211 public void setLabel(String label
) {
215 public void setStyle(String style
) {
219 /** @deprecated Use {@link #setStyle(String)} instead. */
221 public void setCustom(String custom
) {
225 public void setTarget(String target
) {
226 this.target
= target
;
230 // } catch (MalformedURLException e1) {
235 public void setImage(String image
) {
239 public void setLoggedInLabel(String loggedInLabel
) {
240 this.loggedInLabel
= loggedInLabel
;
243 public void setLoggedInTarget(String loggedInTarget
) {
244 this.loggedInTarget
= loggedInTarget
;
247 public void setMouseListener(MouseListener mouseListener
) {
248 this.mouseListener
= mouseListener
;
251 public void setvAlign(String vAlign
) {
252 if ("bottom".equals(vAlign
)) {
253 verticalAlignment
= SWT
.BOTTOM
;
254 } else if ("top".equals(vAlign
)) {
255 verticalAlignment
= SWT
.TOP
;
256 } else if ("center".equals(vAlign
)) {
257 verticalAlignment
= SWT
.CENTER
;
259 throw new IllegalArgumentException(
260 "Unsupported vertical alignment " + vAlign
+ " (must be: top, bottom or center)");
264 protected boolean isLoggedIn() {
265 return !CurrentUser
.isAnonymous();
268 public void setImageWidth(Integer imageWidth
) {
269 this.imageWidth
= imageWidth
;
272 public void setImageHeight(Integer imageHeight
) {
273 this.imageHeight
= imageHeight
;
276 public void setOpenNew(boolean openNew
) {
277 this.openNew
= openNew
;
280 protected String
getDefaultStyle() {
281 return SimpleStyle
.link
.name();