1 package org
.argeo
.cms
.forms
;
3 import java
.text
.DateFormat
;
4 import java
.text
.ParseException
;
5 import java
.util
.Calendar
;
7 import java
.util
.GregorianCalendar
;
10 import javax
.jcr
.RepositoryException
;
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
;
29 /** Utilitary methods to ease implementation of CMS forms */
30 public class FormUtils
{
31 private final static Log log
= LogFactory
.getLog(FormUtils
.class);
33 public final static String DEFAULT_SHORT_DATE_FORMAT
= "dd/MM/yyyy";
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());
40 /** Best effort to convert a String to a calendar. Fails silently */
41 public static Calendar
parseDate(DateFormat dateFormat
, String calStr
) {
43 if (notEmpty(calStr
)) {
45 Date date
= dateFormat
.parse(calStr
);
46 cal
= new GregorianCalendar();
48 } catch (ParseException pe
) {
50 log
.warn("Unable to parse date: " + calStr
+ " - msg: "
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() {
62 public void doubleClick(DoubleClickEvent event
) {
63 CmsView cmsView
= CmsUtils
.getCmsView();
64 Node node
= (Node
) ((IStructuredSelection
) event
.getSelection())
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
);
76 // MANAGE ERROR DECORATION
78 public static ControlDecoration
addDecoration(final Text text
) {
79 final ControlDecoration dynDecoration
= new ControlDecoration(text
,
81 Image icon
= getDecorationImage(FieldDecorationRegistry
.DEC_ERROR
);
82 dynDecoration
.setImage(icon
);
83 dynDecoration
.setMarginWidth(3);
88 public static void refreshDecoration(Text text
, ControlDecoration deco
,
89 boolean isValid
, boolean clean
) {
90 if (isValid
|| clean
) {
91 text
.setBackground(null);
94 text
.setBackground(new Color(text
.getDisplay(), 250, 200, 150));
99 public static Image
getDecorationImage(String image
) {
100 FieldDecorationRegistry registry
= FieldDecorationRegistry
.getDefault();
101 return registry
.getFieldDecoration(image
).getImage();
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);
112 // TODO the read only generation of read only links for various contact type
113 // should be factorised in the cms Utils.
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
118 public static String
getPhoneLink(String value
) {
119 return getPhoneLink(value
, value
);
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
128 * a potentially distinct label
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
)
136 return builder
.toString();
140 * Creates the read-only HTML snippet to display in a label with styling
141 * enabled in order to provide a click-able mail
143 public static String
getMailLink(String value
) {
144 return getMailLink(value
, value
);
148 * Creates the read-only HTML snippet to display in a label with styling
149 * enabled in order to provide a click-able mail
153 * a potentially distinct label
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();
165 * Creates the read-only HTML snippet to display in a label with styling
166 * enabled in order to provide a click-able link
168 public static String
getUrlLink(String value
) {
169 return getUrlLink(value
, value
);
173 * Creates the read-only HTML snippet to display in a label with styling
174 * enabled in order to provide a click-able link
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();
187 private static String AMPERSAND
= "&";
190 * Cleans a String by replacing any '&' by its HTML encoding '&' to
191 * avoid <code>SAXParseException</code> while rendering HTML with RWT
193 public static String
replaceAmpersand(String value
) {
194 value
= value
.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND
);
198 // Prevents instantiation
199 private FormUtils() {