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