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