]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/CmsLink.java
Ignore target
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / CmsLink.java
1 package org.argeo.cms.util;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9
10 import org.apache.commons.io.IOUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.cms.CmsException;
14 import org.argeo.cms.auth.CurrentUser;
15 import org.argeo.cms.ui.CmsStyles;
16 import org.argeo.cms.ui.CmsUiProvider;
17 import org.argeo.node.NodeUtils;
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;
28
29 /** A link to an internal or external location. */
30 public class CmsLink implements CmsUiProvider {
31 private final static Log log = LogFactory.getLog(CmsLink.class);
32 private BundleContext bundleContext;
33
34 private String label;
35 private String custom;
36 private String target;
37 private String image;
38 private MouseListener mouseListener;
39
40 private int verticalAlignment = SWT.CENTER;
41
42 private String loggedInLabel = null;
43 private String loggedInTarget = null;
44
45 // internal
46 // private Boolean isUrl = false;
47 private Integer imageWidth, imageHeight;
48
49 public CmsLink() {
50 super();
51 }
52
53 public CmsLink(String label, String target) {
54 this(label, target, null);
55 }
56
57 public CmsLink(String label, String target, String custom) {
58 super();
59 this.label = label;
60 this.target = target;
61 this.custom = custom;
62 init();
63 }
64
65 public void init() {
66 if (image != null) {
67 ImageData image = loadImage();
68 imageWidth = image.width;
69 imageHeight = image.height;
70 }
71 }
72
73 /** @return {@link Composite} with a single {@link Label} child. */
74 @Override
75 public Control createUi(final Composite parent, Node context) {
76 if (image != null && (imageWidth == null || imageHeight == null)) {
77 throw new CmsException("Image is not properly configured."
78 + " Make sure bundleContext property is set and init() method has been called.");
79 }
80
81 Composite comp = new Composite(parent, SWT.BOTTOM);
82 comp.setLayout(CmsUtils.noSpaceGridLayout());
83
84 Label link = new Label(comp, SWT.NONE);
85 link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
86 GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true, true);
87 if (image != null) {
88 layoutData.heightHint = imageHeight;
89 if (label == null)
90 layoutData.widthHint = imageWidth;
91 }
92
93 link.setLayoutData(layoutData);
94 if (custom != null) {
95 comp.setData(RWT.CUSTOM_VARIANT, custom);
96 link.setData(RWT.CUSTOM_VARIANT, custom);
97 } else {
98 comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
99 link.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LINK);
100 }
101
102 // label
103 StringBuilder labelText = new StringBuilder();
104 if (loggedInTarget != null && isLoggedIn()) {
105 labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
106 if (loggedInTarget.equals("")) {
107 try {
108 Node homeNode = NodeUtils.getUserHome(context.getSession());
109 String homePath = homeNode.getPath();
110 labelText.append("/#" + homePath);
111 } catch (RepositoryException e) {
112 throw new CmsException("Cannot get home path", e);
113 }
114 } else {
115 labelText.append(loggedInTarget);
116 }
117 labelText.append("\">");
118 } else if (target != null) {
119 labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
120 labelText.append(target);
121 labelText.append("\">");
122 }
123 if (image != null) {
124 registerImageIfNeeded();
125 String imageLocation = RWT.getResourceManager().getLocation(image);
126 labelText.append("<img width='").append(imageWidth).append("' height='").append(imageHeight)
127 .append("' src=\"").append(imageLocation).append("\"/>");
128
129 }
130
131 if (loggedInLabel != null && isLoggedIn()) {
132 labelText.append(' ').append(loggedInLabel);
133 } else if (label != null) {
134 labelText.append(' ').append(label);
135 }
136
137 if ((loggedInTarget != null && isLoggedIn()) || target != null)
138 labelText.append("</a>");
139
140 link.setText(labelText.toString());
141
142 if (mouseListener != null)
143 link.addMouseListener(mouseListener);
144
145 return comp;
146 }
147
148 private void registerImageIfNeeded() {
149 ResourceManager resourceManager = RWT.getResourceManager();
150 if (!resourceManager.isRegistered(image)) {
151 URL res = getImageUrl();
152 InputStream inputStream = null;
153 try {
154 IOUtils.closeQuietly(inputStream);
155 inputStream = res.openStream();
156 resourceManager.register(image, inputStream);
157 if (log.isTraceEnabled())
158 log.trace("Registered image " + image);
159 } catch (Exception e) {
160 throw new CmsException("Cannot load image " + image, e);
161 } finally {
162 IOUtils.closeQuietly(inputStream);
163 }
164 }
165 }
166
167 private ImageData loadImage() {
168 URL url = getImageUrl();
169 ImageData result = null;
170 InputStream inputStream = null;
171 try {
172 inputStream = url.openStream();
173 result = new ImageData(inputStream);
174 if (log.isTraceEnabled())
175 log.trace("Loaded image " + image);
176 } catch (Exception e) {
177 throw new CmsException("Cannot load image " + image, e);
178 } finally {
179 IOUtils.closeQuietly(inputStream);
180 }
181 return result;
182 }
183
184 private URL getImageUrl() {
185 URL url;
186 try {
187 // pure URL
188 url = new URL(image);
189 } catch (MalformedURLException e1) {
190 url = bundleContext.getBundle().getResource(image);
191 }
192
193 if (url == null)
194 throw new CmsException("No image " + image + " available.");
195
196 return url;
197 }
198
199 public void setBundleContext(BundleContext bundleContext) {
200 this.bundleContext = bundleContext;
201 }
202
203 public void setLabel(String label) {
204 this.label = label;
205 }
206
207 public void setCustom(String custom) {
208 this.custom = custom;
209 }
210
211 public void setTarget(String target) {
212 this.target = target;
213 // try {
214 // new URL(target);
215 // isUrl = true;
216 // } catch (MalformedURLException e1) {
217 // isUrl = false;
218 // }
219 }
220
221 public void setImage(String image) {
222 this.image = image;
223 }
224
225 public void setLoggedInLabel(String loggedInLabel) {
226 this.loggedInLabel = loggedInLabel;
227 }
228
229 public void setLoggedInTarget(String loggedInTarget) {
230 this.loggedInTarget = loggedInTarget;
231 }
232
233 public void setMouseListener(MouseListener mouseListener) {
234 this.mouseListener = mouseListener;
235 }
236
237 public void setvAlign(String vAlign) {
238 if ("bottom".equals(vAlign)) {
239 verticalAlignment = SWT.BOTTOM;
240 } else if ("top".equals(vAlign)) {
241 verticalAlignment = SWT.TOP;
242 } else if ("center".equals(vAlign)) {
243 verticalAlignment = SWT.CENTER;
244 } else {
245 throw new CmsException("Unsupported vertical allignment " + vAlign + " (must be: top, bottom or center)");
246 }
247 }
248
249 protected boolean isLoggedIn() {
250 return !CurrentUser.isAnonymous();
251 }
252
253 }