]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/util/CmsLink.java
Rename remote auth interfaces
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / CmsLink.java
1 package org.argeo.cms.ui.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7
8 import javax.jcr.Node;
9 import javax.jcr.RepositoryException;
10
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;
29
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;
34
35 private String label;
36 private String style;
37 private String target;
38 private String image;
39 private boolean openNew = false;
40 private MouseListener mouseListener;
41
42 private int horizontalAlignment = SWT.CENTER;
43 private int verticalAlignment = SWT.CENTER;
44
45 private String loggedInLabel = null;
46 private String loggedInTarget = null;
47
48 // internal
49 // private Boolean isUrl = false;
50 private Integer imageWidth, imageHeight;
51
52 public CmsLink() {
53 super();
54 }
55
56 public CmsLink(String label, String target) {
57 this(label, target, (String) null);
58 }
59
60 public CmsLink(String label, String target, CmsStyle style) {
61 this(label, target, style != null ? style.style() : null);
62 }
63
64 public CmsLink(String label, String target, String style) {
65 super();
66 this.label = label;
67 this.target = target;
68 this.style = style;
69 init();
70 }
71
72 public void init() {
73 if (image != null) {
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;
82 }
83 }
84 }
85
86 /** @return {@link Composite} with a single {@link Label} child. */
87 @Override
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.");
92 // }
93
94 Composite comp = new Composite(parent, SWT.NONE);
95 comp.setLayout(CmsSwtUtils.noSpaceGridLayout());
96
97 Label link = new Label(comp, SWT.NONE);
98 CmsSwtUtils.markup(link);
99 GridData layoutData = new GridData(horizontalAlignment, verticalAlignment, false, false);
100 if (image != null) {
101 if (imageHeight != null)
102 layoutData.heightHint = imageHeight;
103 if (label == null)
104 if (imageWidth != null)
105 layoutData.widthHint = imageWidth;
106 }
107
108 link.setLayoutData(layoutData);
109 CmsSwtUtils.style(comp, style != null ? style : getDefaultStyle());
110 CmsSwtUtils.style(link, style != null ? style : getDefaultStyle());
111
112 // label
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("")) {
117 try {
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);
123 }
124 } else {
125 labelText.append(loggedInTarget);
126 }
127 labelText.append("\">");
128 } else if (target != null) {
129 labelText.append("<a style='color:inherit;text-decoration:inherit;' href='");
130 labelText.append(target).append("'");
131 if (openNew) {
132 labelText.append(" target='_blank'");
133 }
134 labelText.append(">");
135 }
136 if (image != null) {
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("\"/>");
145
146 }
147
148 if (loggedInLabel != null && isLoggedIn()) {
149 labelText.append(' ').append(loggedInLabel);
150 } else if (label != null) {
151 labelText.append(' ').append(label);
152 }
153
154 if ((loggedInTarget != null && isLoggedIn()) || target != null)
155 labelText.append("</a>");
156
157 link.setText(labelText.toString());
158
159 if (mouseListener != null)
160 link.addMouseListener(mouseListener);
161
162 return comp;
163 }
164
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);
175 }
176 }
177 }
178
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);
188 }
189 return result;
190 }
191
192 private URL getImageUrl() {
193 URL url;
194 try {
195 // pure URL
196 url = new URL(image);
197 } catch (MalformedURLException e1) {
198 url = bundleContext.getBundle().getResource(image);
199 }
200
201 if (url == null)
202 throw new IllegalStateException("No image " + image + " available.");
203
204 return url;
205 }
206
207 public void setBundleContext(BundleContext bundleContext) {
208 this.bundleContext = bundleContext;
209 }
210
211 public void setLabel(String label) {
212 this.label = label;
213 }
214
215 public void setStyle(String style) {
216 this.style = style;
217 }
218
219 /** @deprecated Use {@link #setStyle(String)} instead. */
220 @Deprecated
221 public void setCustom(String custom) {
222 this.style = custom;
223 }
224
225 public void setTarget(String target) {
226 this.target = target;
227 // try {
228 // new URL(target);
229 // isUrl = true;
230 // } catch (MalformedURLException e1) {
231 // isUrl = false;
232 // }
233 }
234
235 public void setImage(String image) {
236 this.image = image;
237 }
238
239 public void setLoggedInLabel(String loggedInLabel) {
240 this.loggedInLabel = loggedInLabel;
241 }
242
243 public void setLoggedInTarget(String loggedInTarget) {
244 this.loggedInTarget = loggedInTarget;
245 }
246
247 public void setMouseListener(MouseListener mouseListener) {
248 this.mouseListener = mouseListener;
249 }
250
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;
258 } else {
259 throw new IllegalArgumentException(
260 "Unsupported vertical alignment " + vAlign + " (must be: top, bottom or center)");
261 }
262 }
263
264 protected boolean isLoggedIn() {
265 return !CurrentUser.isAnonymous();
266 }
267
268 public void setImageWidth(Integer imageWidth) {
269 this.imageWidth = imageWidth;
270 }
271
272 public void setImageHeight(Integer imageHeight) {
273 this.imageHeight = imageHeight;
274 }
275
276 public void setOpenNew(boolean openNew) {
277 this.openNew = openNew;
278 }
279
280 protected String getDefaultStyle() {
281 return SimpleStyle.link.name();
282 }
283 }