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
.argeo
.api
.cms
.CmsStyle
;
12 import org
.argeo
.api
.cms
.CmsLog
;
13 import org
.argeo
.cms
.auth
.CurrentUser
;
14 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
15 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
16 import org
.argeo
.cms
.ui
.CmsUiProvider
;
17 import org
.argeo
.jcr
.JcrException
;
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 CmsLog log
= CmsLog
.getLog(CmsLink
.class);
32 private BundleContext bundleContext
;
36 private String target
;
38 private boolean openNew
= false;
39 private MouseListener mouseListener
;
41 private int horizontalAlignment
= SWT
.CENTER
;
42 private int verticalAlignment
= SWT
.CENTER
;
44 private String loggedInLabel
= null;
45 private String loggedInTarget
= null;
48 // private Boolean isUrl = false;
49 private Integer imageWidth
, imageHeight
;
55 public CmsLink(String label
, String target
) {
56 this(label
, target
, (String
) null);
59 public CmsLink(String label
, String target
, CmsStyle style
) {
60 this(label
, target
, style
!= null ? style
.style() : null);
63 public CmsLink(String label
, String target
, String style
) {
73 ImageData image
= loadImage();
74 if (imageHeight
== null && imageWidth
== null) {
75 imageWidth
= image
.width
;
76 imageHeight
= image
.height
;
77 } else if (imageHeight
== null) {
78 imageHeight
= (imageWidth
* image
.height
) / image
.width
;
79 } else if (imageWidth
== null) {
80 imageWidth
= (imageHeight
* image
.width
) / image
.height
;
85 /** @return {@link Composite} with a single {@link Label} child. */
87 public Control
createUi(final Composite parent
, Node context
) {
88 // if (image != null && (imageWidth == null || imageHeight == null)) {
89 // throw new CmsException("Image is not properly configured."
90 // + " Make sure bundleContext property is set and init() method has been called.");
93 Composite comp
= new Composite(parent
, SWT
.NONE
);
94 comp
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
96 Label link
= new Label(comp
, SWT
.NONE
);
97 CmsSwtUtils
.markup(link
);
98 GridData layoutData
= new GridData(horizontalAlignment
, verticalAlignment
, false, false);
100 if (imageHeight
!= null)
101 layoutData
.heightHint
= imageHeight
;
103 if (imageWidth
!= null)
104 layoutData
.widthHint
= imageWidth
;
107 link
.setLayoutData(layoutData
);
108 CmsSwtUtils
.style(comp
, style
!= null ? style
: getDefaultStyle());
109 CmsSwtUtils
.style(link
, style
!= null ? style
: getDefaultStyle());
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
= CmsJcrUtils
.getUserHome(context
.getSession());
118 String homePath
= homeNode
.getPath();
119 labelText
.append("/#" + homePath
);
120 } catch (RepositoryException e
) {
121 throw new JcrException("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
).append("'");
131 labelText
.append(" target='_blank'");
133 labelText
.append(">");
136 registerImageIfNeeded();
137 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
138 labelText
.append("<img");
139 if (imageWidth
!= null)
140 labelText
.append(" width='").append(imageWidth
).append('\'');
141 if (imageHeight
!= null)
142 labelText
.append(" height='").append(imageHeight
).append('\'');
143 labelText
.append(" src=\"").append(imageLocation
).append("\"/>");
147 if (loggedInLabel
!= null && isLoggedIn()) {
148 labelText
.append(' ').append(loggedInLabel
);
149 } else if (label
!= null) {
150 labelText
.append(' ').append(label
);
153 if ((loggedInTarget
!= null && isLoggedIn()) || target
!= null)
154 labelText
.append("</a>");
156 link
.setText(labelText
.toString());
158 if (mouseListener
!= null)
159 link
.addMouseListener(mouseListener
);
164 private void registerImageIfNeeded() {
165 ResourceManager resourceManager
= RWT
.getResourceManager();
166 if (!resourceManager
.isRegistered(image
)) {
167 URL res
= getImageUrl();
168 try (InputStream inputStream
= res
.openStream()) {
169 resourceManager
.register(image
, inputStream
);
170 if (log
.isTraceEnabled())
171 log
.trace("Registered image " + image
);
172 } catch (IOException e
) {
173 throw new RuntimeException("Cannot load image " + image
, e
);
178 private ImageData
loadImage() {
179 URL url
= getImageUrl();
180 ImageData result
= null;
181 try (InputStream inputStream
= url
.openStream()) {
182 result
= new ImageData(inputStream
);
183 if (log
.isTraceEnabled())
184 log
.trace("Loaded image " + image
);
185 } catch (IOException e
) {
186 throw new RuntimeException("Cannot load image " + image
, e
);
191 private URL
getImageUrl() {
195 url
= new URL(image
);
196 } catch (MalformedURLException e1
) {
197 url
= bundleContext
.getBundle().getResource(image
);
201 throw new IllegalStateException("No image " + image
+ " available.");
206 public void setBundleContext(BundleContext bundleContext
) {
207 this.bundleContext
= bundleContext
;
210 public void setLabel(String label
) {
214 public void setStyle(String style
) {
218 /** @deprecated Use {@link #setStyle(String)} instead. */
220 public void setCustom(String 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 IllegalArgumentException(
259 "Unsupported vertical alignment " + vAlign
+ " (must be: top, bottom or center)");
263 protected boolean isLoggedIn() {
264 return !CurrentUser
.isAnonymous();
267 public void setImageWidth(Integer imageWidth
) {
268 this.imageWidth
= imageWidth
;
271 public void setImageHeight(Integer imageHeight
) {
272 this.imageHeight
= imageHeight
;
275 public void setOpenNew(boolean openNew
) {
276 this.openNew
= openNew
;
279 protected String
getDefaultStyle() {
280 return SimpleStyle
.link
.name();