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
.NodeUtils
;
14 import org
.argeo
.cms
.auth
.CurrentUser
;
15 import org
.argeo
.cms
.ui
.CmsUiProvider
;
16 import org
.argeo
.jcr
.JcrException
;
17 import org
.eclipse
.rap
.rwt
.RWT
;
18 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.MouseListener
;
21 import org
.eclipse
.swt
.graphics
.ImageData
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Label
;
26 import org
.osgi
.framework
.BundleContext
;
28 /** A link to an internal or external location. */
29 public class CmsLink
implements CmsUiProvider
{
30 private final static Log log
= LogFactory
.getLog(CmsLink
.class);
31 private BundleContext bundleContext
;
35 private String target
;
37 private boolean openNew
= false;
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
, (String
) null);
58 public CmsLink(String label
, String target
, CmsStyle style
) {
59 this(label
, target
, style
!= null ? style
.style() : null);
62 public CmsLink(String label
, String target
, String style
) {
72 ImageData image
= loadImage();
73 if (imageHeight
== null && imageWidth
== null) {
74 imageWidth
= image
.width
;
75 imageHeight
= image
.height
;
76 } else if (imageHeight
== null) {
77 imageHeight
= (imageWidth
* image
.height
) / image
.width
;
78 } else if (imageWidth
== null) {
79 imageWidth
= (imageHeight
* image
.width
) / image
.height
;
84 /** @return {@link Composite} with a single {@link Label} child. */
86 public Control
createUi(final Composite parent
, Node context
) {
87 // if (image != null && (imageWidth == null || imageHeight == null)) {
88 // throw new CmsException("Image is not properly configured."
89 // + " Make sure bundleContext property is set and init() method has been called.");
92 Composite comp
= new Composite(parent
, SWT
.NONE
);
93 comp
.setLayout(CmsUiUtils
.noSpaceGridLayout());
95 Label link
= new Label(comp
, SWT
.NONE
);
96 CmsUiUtils
.markup(link
);
97 GridData layoutData
= new GridData(horizontalAlignment
, verticalAlignment
, false, false);
99 if (imageHeight
!= null)
100 layoutData
.heightHint
= imageHeight
;
102 if (imageWidth
!= null)
103 layoutData
.widthHint
= imageWidth
;
106 link
.setLayoutData(layoutData
);
107 CmsUiUtils
.style(comp
, style
!= null ? style
: getDefaultStyle());
108 CmsUiUtils
.style(link
, style
!= null ? style
: getDefaultStyle());
111 StringBuilder labelText
= new StringBuilder();
112 if (loggedInTarget
!= null && isLoggedIn()) {
113 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
114 if (loggedInTarget
.equals("")) {
116 Node homeNode
= NodeUtils
.getUserHome(context
.getSession());
117 String homePath
= homeNode
.getPath();
118 labelText
.append("/#" + homePath
);
119 } catch (RepositoryException e
) {
120 throw new JcrException("Cannot get home path", e
);
123 labelText
.append(loggedInTarget
);
125 labelText
.append("\">");
126 } else if (target
!= null) {
127 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href='");
128 labelText
.append(target
).append("'");
130 labelText
.append(" target='_blank'");
132 labelText
.append(">");
135 registerImageIfNeeded();
136 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
137 labelText
.append("<img");
138 if (imageWidth
!= null)
139 labelText
.append(" width='").append(imageWidth
).append('\'');
140 if (imageHeight
!= null)
141 labelText
.append(" height='").append(imageHeight
).append('\'');
142 labelText
.append(" src=\"").append(imageLocation
).append("\"/>");
146 if (loggedInLabel
!= null && isLoggedIn()) {
147 labelText
.append(' ').append(loggedInLabel
);
148 } else if (label
!= null) {
149 labelText
.append(' ').append(label
);
152 if ((loggedInTarget
!= null && isLoggedIn()) || target
!= null)
153 labelText
.append("</a>");
155 link
.setText(labelText
.toString());
157 if (mouseListener
!= null)
158 link
.addMouseListener(mouseListener
);
163 private void registerImageIfNeeded() {
164 ResourceManager resourceManager
= RWT
.getResourceManager();
165 if (!resourceManager
.isRegistered(image
)) {
166 URL res
= getImageUrl();
167 try (InputStream inputStream
= res
.openStream()) {
168 resourceManager
.register(image
, inputStream
);
169 if (log
.isTraceEnabled())
170 log
.trace("Registered image " + image
);
171 } catch (IOException e
) {
172 throw new RuntimeException("Cannot load image " + image
, e
);
177 private ImageData
loadImage() {
178 URL url
= getImageUrl();
179 ImageData result
= null;
180 try (InputStream inputStream
= url
.openStream()) {
181 result
= new ImageData(inputStream
);
182 if (log
.isTraceEnabled())
183 log
.trace("Loaded image " + image
);
184 } catch (IOException e
) {
185 throw new RuntimeException("Cannot load image " + image
, e
);
190 private URL
getImageUrl() {
194 url
= new URL(image
);
195 } catch (MalformedURLException e1
) {
196 url
= bundleContext
.getBundle().getResource(image
);
200 throw new IllegalStateException("No image " + image
+ " available.");
205 public void setBundleContext(BundleContext bundleContext
) {
206 this.bundleContext
= bundleContext
;
209 public void setLabel(String label
) {
213 public void setStyle(String style
) {
217 /** @deprecated Use {@link #setStyle(String)} instead. */
219 public void setCustom(String custom
) {
223 public void setTarget(String target
) {
224 this.target
= target
;
228 // } catch (MalformedURLException e1) {
233 public void setImage(String image
) {
237 public void setLoggedInLabel(String loggedInLabel
) {
238 this.loggedInLabel
= loggedInLabel
;
241 public void setLoggedInTarget(String loggedInTarget
) {
242 this.loggedInTarget
= loggedInTarget
;
245 public void setMouseListener(MouseListener mouseListener
) {
246 this.mouseListener
= mouseListener
;
249 public void setvAlign(String vAlign
) {
250 if ("bottom".equals(vAlign
)) {
251 verticalAlignment
= SWT
.BOTTOM
;
252 } else if ("top".equals(vAlign
)) {
253 verticalAlignment
= SWT
.TOP
;
254 } else if ("center".equals(vAlign
)) {
255 verticalAlignment
= SWT
.CENTER
;
257 throw new IllegalArgumentException(
258 "Unsupported vertical alignment " + 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
;
274 public void setOpenNew(boolean openNew
) {
275 this.openNew
= openNew
;
278 protected String
getDefaultStyle() {
279 return SimpleStyle
.link
.name();