]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/CmsLink.java
Extract log method from asynchronous publisher
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / widgets / CmsLink.java
1 package org.argeo.cms.swt.widgets;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7
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;
21
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;
26
27 private String label;
28 private String style;
29 private String target;
30 private String image;
31 private boolean openNew = false;
32 private MouseListener mouseListener;
33
34 private int horizontalAlignment = SWT.CENTER;
35 private int verticalAlignment = SWT.CENTER;
36
37 // internal
38 // private Boolean isUrl = false;
39 private Integer imageWidth, imageHeight;
40
41 public CmsLink() {
42 super();
43 }
44
45 public CmsLink(String label, String target) {
46 this(label, target, (String) null);
47 }
48
49 public CmsLink(String label, String target, CmsStyle style) {
50 this(label, target, style != null ? style.style() : null);
51 }
52
53 public CmsLink(String label, String target, String style) {
54 super();
55 this.label = label;
56 this.target = target;
57 this.style = style;
58 init();
59 }
60
61 public void init() {
62 if (image != null) {
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;
71 }
72 }
73 }
74
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.");
80 // }
81
82 Composite comp = new Composite(parent, SWT.NONE);
83 comp.setLayout(CmsSwtUtils.noSpaceGridLayout());
84
85 Label link = new Label(comp, SWT.NONE);
86 CmsSwtUtils.markup(link);
87 GridData layoutData = new GridData(horizontalAlignment, verticalAlignment, false, false);
88 if (image != null) {
89 if (imageHeight != null)
90 layoutData.heightHint = imageHeight;
91 if (label == null)
92 if (imageWidth != null)
93 layoutData.widthHint = imageWidth;
94 }
95
96 link.setLayoutData(layoutData);
97 CmsSwtUtils.style(comp, style != null ? style : getDefaultStyle());
98 CmsSwtUtils.style(link, style != null ? style : getDefaultStyle());
99
100 // label
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("'");
105 if (openNew) {
106 labelText.append(" target='_blank'");
107 }
108 labelText.append(">");
109 }
110 if (image != null) {
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("\"/>");
119
120 }
121
122 if (label != null) {
123 labelText.append(' ').append(label);
124 }
125
126 if (target != null)
127 labelText.append("</a>");
128
129 link.setText(labelText.toString());
130
131 if (mouseListener != null)
132 link.addMouseListener(mouseListener);
133
134 return comp;
135 }
136
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);
147 }
148 }
149 }
150
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);
160 }
161 return result;
162 }
163
164 private URL getImageUrl() {
165 URL url;
166 try {
167 // pure URL
168 url = new URL(image);
169 } catch (MalformedURLException e1) {
170 url = bundleContext.getBundle().getResource(image);
171 }
172
173 if (url == null)
174 throw new IllegalStateException("No image " + image + " available.");
175
176 return url;
177 }
178
179 public void setBundleContext(BundleContext bundleContext) {
180 this.bundleContext = bundleContext;
181 }
182
183 public void setLabel(String label) {
184 this.label = label;
185 }
186
187 public void setStyle(String style) {
188 this.style = style;
189 }
190
191 /** @deprecated Use {@link #setStyle(String)} instead. */
192 @Deprecated
193 public void setCustom(String custom) {
194 this.style = custom;
195 }
196
197 public void setTarget(String target) {
198 this.target = target;
199 }
200
201 public void setImage(String image) {
202 this.image = image;
203 }
204
205 public void setMouseListener(MouseListener mouseListener) {
206 this.mouseListener = mouseListener;
207 }
208
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;
216 } else {
217 throw new IllegalArgumentException(
218 "Unsupported vertical alignment " + vAlign + " (must be: top, bottom or center)");
219 }
220 }
221
222 public void setImageWidth(Integer imageWidth) {
223 this.imageWidth = imageWidth;
224 }
225
226 public void setImageHeight(Integer imageHeight) {
227 this.imageHeight = imageHeight;
228 }
229
230 public void setOpenNew(boolean openNew) {
231 this.openNew = openNew;
232 }
233
234 protected String getDefaultStyle() {
235 return "link";
236 // return SimpleStyle.link.name();
237 }
238 }