1 package org
.argeo
.cms
.swt
.widgets
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
8 import org
.argeo
.api
.cms
.CmsLog
;
9 import org
.argeo
.api
.cms
.ux
.CmsStyle
;
10 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
11 import org
.eclipse
.rap
.rwt
.RWT
;
12 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.MouseListener
;
15 import org
.eclipse
.swt
.graphics
.ImageData
;
16 import org
.eclipse
.swt
.layout
.GridData
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Control
;
19 import org
.eclipse
.swt
.widgets
.Label
;
20 import org
.osgi
.framework
.BundleContext
;
22 /** A link to an internal or external location. */
23 public class CmsLink
{
24 private final static CmsLog log
= CmsLog
.getLog(CmsLink
.class);
25 private BundleContext bundleContext
;
29 private String target
;
31 private boolean openNew
= false;
32 private MouseListener mouseListener
;
34 private int horizontalAlignment
= SWT
.CENTER
;
35 private int verticalAlignment
= SWT
.CENTER
;
38 // private Boolean isUrl = false;
39 private Integer imageWidth
, imageHeight
;
45 public CmsLink(String label
, String target
) {
46 this(label
, target
, (String
) null);
49 public CmsLink(String label
, String target
, CmsStyle style
) {
50 this(label
, target
, style
!= null ? style
.style() : null);
53 public CmsLink(String label
, String target
, String style
) {
63 ImageData image
= loadImage();
64 if (imageHeight
== null && imageWidth
== null) {
65 imageWidth
= image
.width
;
66 imageHeight
= image
.height
;
67 } else if (imageHeight
== null) {
68 imageHeight
= (imageWidth
* image
.height
) / image
.width
;
69 } else if (imageWidth
== null) {
70 imageWidth
= (imageHeight
* image
.width
) / image
.height
;
75 /** @return {@link Composite} with a single {@link Label} child. */
76 public Control
createUi(Composite parent
) {
77 // if (image != null && (imageWidth == null || imageHeight == null)) {
78 // throw new CmsException("Image is not properly configured."
79 // + " Make sure bundleContext property is set and init() method has been called.");
82 Composite comp
= new Composite(parent
, SWT
.NONE
);
83 comp
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
85 Label link
= new Label(comp
, SWT
.NONE
);
86 CmsSwtUtils
.markup(link
);
87 GridData layoutData
= new GridData(horizontalAlignment
, verticalAlignment
, false, false);
89 if (imageHeight
!= null)
90 layoutData
.heightHint
= imageHeight
;
92 if (imageWidth
!= null)
93 layoutData
.widthHint
= imageWidth
;
96 link
.setLayoutData(layoutData
);
97 CmsSwtUtils
.style(comp
, style
!= null ? style
: getDefaultStyle());
98 CmsSwtUtils
.style(link
, style
!= null ? style
: getDefaultStyle());
101 StringBuilder labelText
= new StringBuilder();
102 if (target
!= null) {
103 labelText
.append("<a style='color:inherit;text-decoration:inherit;' href='");
104 labelText
.append(target
).append("'");
106 labelText
.append(" target='_blank'");
108 labelText
.append(">");
111 registerImageIfNeeded();
112 String imageLocation
= RWT
.getResourceManager().getLocation(image
);
113 labelText
.append("<img");
114 if (imageWidth
!= null)
115 labelText
.append(" width='").append(imageWidth
).append('\'');
116 if (imageHeight
!= null)
117 labelText
.append(" height='").append(imageHeight
).append('\'');
118 labelText
.append(" src=\"").append(imageLocation
).append("\"/>");
123 labelText
.append(' ').append(label
);
127 labelText
.append("</a>");
129 link
.setText(labelText
.toString());
131 if (mouseListener
!= null)
132 link
.addMouseListener(mouseListener
);
137 private void registerImageIfNeeded() {
138 ResourceManager resourceManager
= RWT
.getResourceManager();
139 if (!resourceManager
.isRegistered(image
)) {
140 URL res
= getImageUrl();
141 try (InputStream inputStream
= res
.openStream()) {
142 resourceManager
.register(image
, inputStream
);
143 if (log
.isTraceEnabled())
144 log
.trace("Registered image " + image
);
145 } catch (IOException e
) {
146 throw new RuntimeException("Cannot load image " + image
, e
);
151 private ImageData
loadImage() {
152 URL url
= getImageUrl();
153 ImageData result
= null;
154 try (InputStream inputStream
= url
.openStream()) {
155 result
= new ImageData(inputStream
);
156 if (log
.isTraceEnabled())
157 log
.trace("Loaded image " + image
);
158 } catch (IOException e
) {
159 throw new RuntimeException("Cannot load image " + image
, e
);
164 private URL
getImageUrl() {
168 url
= new URL(image
);
169 } catch (MalformedURLException e1
) {
170 url
= bundleContext
.getBundle().getResource(image
);
174 throw new IllegalStateException("No image " + image
+ " available.");
179 public void setBundleContext(BundleContext bundleContext
) {
180 this.bundleContext
= bundleContext
;
183 public void setLabel(String label
) {
187 public void setStyle(String style
) {
191 /** @deprecated Use {@link #setStyle(String)} instead. */
193 public void setCustom(String custom
) {
197 public void setTarget(String target
) {
198 this.target
= target
;
201 public void setImage(String image
) {
205 public void setMouseListener(MouseListener mouseListener
) {
206 this.mouseListener
= mouseListener
;
209 public void setvAlign(String vAlign
) {
210 if ("bottom".equals(vAlign
)) {
211 verticalAlignment
= SWT
.BOTTOM
;
212 } else if ("top".equals(vAlign
)) {
213 verticalAlignment
= SWT
.TOP
;
214 } else if ("center".equals(vAlign
)) {
215 verticalAlignment
= SWT
.CENTER
;
217 throw new IllegalArgumentException(
218 "Unsupported vertical alignment " + vAlign
+ " (must be: top, bottom or center)");
222 public void setImageWidth(Integer imageWidth
) {
223 this.imageWidth
= imageWidth
;
226 public void setImageHeight(Integer imageHeight
) {
227 this.imageHeight
= imageHeight
;
230 public void setOpenNew(boolean openNew
) {
231 this.openNew
= openNew
;
234 protected String
getDefaultStyle() {
236 // return SimpleStyle.link.name();