View Javadoc
1   package org.argeo.cms.ui.forms;
2   
3   import java.text.DateFormat;
4   import java.text.ParseException;
5   import java.util.Calendar;
6   import java.util.Date;
7   import java.util.GregorianCalendar;
8   
9   import javax.jcr.Node;
10  import javax.jcr.RepositoryException;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.argeo.cms.CmsException;
15  import org.argeo.cms.ui.CmsView;
16  import org.argeo.cms.ui.util.CmsUiUtils;
17  import org.argeo.eclipse.ui.EclipseUiUtils;
18  import org.eclipse.jface.fieldassist.ControlDecoration;
19  import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
20  import org.eclipse.jface.viewers.DoubleClickEvent;
21  import org.eclipse.jface.viewers.IDoubleClickListener;
22  import org.eclipse.jface.viewers.IStructuredSelection;
23  import org.eclipse.jface.viewers.TableViewer;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.graphics.Color;
26  import org.eclipse.swt.graphics.Image;
27  import org.eclipse.swt.widgets.Label;
28  import org.eclipse.swt.widgets.Text;
29  
30  /** Utilitary methods to ease implementation of CMS forms */
31  public class FormUtils {
32  	private final static Log log = LogFactory.getLog(FormUtils.class);
33  
34  	public final static String DEFAULT_SHORT_DATE_FORMAT = "dd/MM/yyyy";
35  
36  	/** Best effort to convert a String to a calendar. Fails silently */
37  	public static Calendar parseDate(DateFormat dateFormat, String calStr) {
38  		Calendar cal = null;
39  		if (EclipseUiUtils.notEmpty(calStr)) {
40  			try {
41  				Date date = dateFormat.parse(calStr);
42  				cal = new GregorianCalendar();
43  				cal.setTime(date);
44  			} catch (ParseException pe) {
45  				// Silent
46  				log.warn("Unable to parse date: " + calStr + " - msg: "
47  						+ pe.getMessage());
48  			}
49  		}
50  		return cal;
51  	}
52  
53  	/** Add a double click listener on tables that display a JCR node list */
54  	public static void addCanonicalDoubleClickListener(final TableViewer v) {
55  		v.addDoubleClickListener(new IDoubleClickListener() {
56  
57  			@Override
58  			public void doubleClick(DoubleClickEvent event) {
59  				CmsView cmsView = CmsUiUtils.getCmsView();
60  				Node node = (Node) ((IStructuredSelection) event.getSelection())
61  						.getFirstElement();
62  				try {
63  					cmsView.navigateTo(node.getPath());
64  				} catch (RepositoryException e) {
65  					throw new CmsException("Unable to get path for node "
66  							+ node + " before calling navigateTo(path)", e);
67  				}
68  			}
69  		});
70  	}
71  
72  	// MANAGE ERROR DECORATION
73  
74  	public static ControlDecoration addDecoration(final Text text) {
75  		final ControlDecoration dynDecoration = new ControlDecoration(text,
76  				SWT.LEFT);
77  		Image icon = getDecorationImage(FieldDecorationRegistry.DEC_ERROR);
78  		dynDecoration.setImage(icon);
79  		dynDecoration.setMarginWidth(3);
80  		dynDecoration.hide();
81  		return dynDecoration;
82  	}
83  
84  	public static void refreshDecoration(Text text, ControlDecoration deco,
85  			boolean isValid, boolean clean) {
86  		if (isValid || clean) {
87  			text.setBackground(null);
88  			deco.hide();
89  		} else {
90  			text.setBackground(new Color(text.getDisplay(), 250, 200, 150));
91  			deco.show();
92  		}
93  	}
94  
95  	public static Image getDecorationImage(String image) {
96  		FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
97  		return registry.getFieldDecoration(image).getImage();
98  	}
99  
100 	public static void addCompulsoryDecoration(Label label) {
101 		final ControlDecoration dynDecoration = new ControlDecoration(label,
102 				SWT.RIGHT | SWT.TOP);
103 		Image icon = getDecorationImage(FieldDecorationRegistry.DEC_REQUIRED);
104 		dynDecoration.setImage(icon);
105 		dynDecoration.setMarginWidth(3);
106 	}
107 
108 	// TODO the read only generation of read only links for various contact type
109 	// should be factorised in the cms Utils.
110 	/**
111 	 * Creates the read-only HTML snippet to display in a label with styling
112 	 * enabled in order to provide a click-able phone number
113 	 */
114 	public static String getPhoneLink(String value) {
115 		return getPhoneLink(value, value);
116 	}
117 
118 	/**
119 	 * Creates the read-only HTML snippet to display in a label with styling
120 	 * enabled in order to provide a click-able phone number
121 	 * 
122 	 * @param value
123 	 * @param label
124 	 *            a potentially distinct label
125 	 * @return
126 	 */
127 	public static String getPhoneLink(String value, String label) {
128 		StringBuilder builder = new StringBuilder();
129 		builder.append("<a href=\"tel:");
130 		builder.append(value).append("\" target=\"_blank\" >").append(label)
131 				.append("</a>");
132 		return builder.toString();
133 	}
134 
135 	/**
136 	 * Creates the read-only HTML snippet to display in a label with styling
137 	 * enabled in order to provide a click-able mail
138 	 */
139 	public static String getMailLink(String value) {
140 		return getMailLink(value, value);
141 	}
142 
143 	/**
144 	 * Creates the read-only HTML snippet to display in a label with styling
145 	 * enabled in order to provide a click-able mail
146 	 * 
147 	 * @param value
148 	 * @param label
149 	 *            a potentially distinct label
150 	 * @return
151 	 */
152 	public static String getMailLink(String value, String label) {
153 		StringBuilder builder = new StringBuilder();
154 		value = replaceAmpersand(value);
155 		builder.append("<a href=\"mailto:");
156 		builder.append(value).append("\" >").append(label).append("</a>");
157 		return builder.toString();
158 	}
159 
160 	/**
161 	 * Creates the read-only HTML snippet to display in a label with styling
162 	 * enabled in order to provide a click-able link
163 	 */
164 	public static String getUrlLink(String value) {
165 		return getUrlLink(value, value);
166 	}
167 
168 	/**
169 	 * Creates the read-only HTML snippet to display in a label with styling
170 	 * enabled in order to provide a click-able link
171 	 */
172 	public static String getUrlLink(String value, String label) {
173 		StringBuilder builder = new StringBuilder();
174 		value = replaceAmpersand(value);
175 		label = replaceAmpersand(label);
176 		if (!(value.startsWith("http://") || value.startsWith("https://")))
177 			value = "http://" + value;
178 		builder.append("<a href=\"");
179 		builder.append(value + "\" target=\"_blank\" >" + label + "</a>");
180 		return builder.toString();
181 	}
182 
183 	private static String AMPERSAND = "&#38;";
184 
185 	/**
186 	 * Cleans a String by replacing any '&#38;' by its HTML encoding '&#38;#38;' to
187 	 * avoid <code>SAXParseException</code> while rendering HTML with RWT
188 	 */
189 	public static String replaceAmpersand(String value) {
190 		value = value.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND);
191 		return value;
192 	}
193 
194 	// Prevents instantiation
195 	private FormUtils() {
196 	}
197 }