1 package org
.argeo
.cms
.ui
.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
.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
;
30 /** Utilitary methods to ease implementation of CMS forms */
31 public class FormUtils
{
32 private final static Log log
= LogFactory
.getLog(FormUtils
.class);
34 public final static String DEFAULT_SHORT_DATE_FORMAT
= "dd/MM/yyyy";
36 /** Best effort to convert a String to a calendar. Fails silently */
37 public static Calendar
parseDate(DateFormat dateFormat
, String calStr
) {
39 if (EclipseUiUtils
.notEmpty(calStr
)) {
41 Date date
= dateFormat
.parse(calStr
);
42 cal
= new GregorianCalendar();
44 } catch (ParseException pe
) {
46 log
.warn("Unable to parse date: " + calStr
+ " - msg: "
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() {
58 public void doubleClick(DoubleClickEvent event
) {
59 CmsView cmsView
= CmsUiUtils
.getCmsView();
60 Node node
= (Node
) ((IStructuredSelection
) event
.getSelection())
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
);
72 // MANAGE ERROR DECORATION
74 public static ControlDecoration
addDecoration(final Text text
) {
75 final ControlDecoration dynDecoration
= new ControlDecoration(text
,
77 Image icon
= getDecorationImage(FieldDecorationRegistry
.DEC_ERROR
);
78 dynDecoration
.setImage(icon
);
79 dynDecoration
.setMarginWidth(3);
84 public static void refreshDecoration(Text text
, ControlDecoration deco
,
85 boolean isValid
, boolean clean
) {
86 if (isValid
|| clean
) {
87 text
.setBackground(null);
90 text
.setBackground(new Color(text
.getDisplay(), 250, 200, 150));
95 public static Image
getDecorationImage(String image
) {
96 FieldDecorationRegistry registry
= FieldDecorationRegistry
.getDefault();
97 return registry
.getFieldDecoration(image
).getImage();
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);
108 // TODO the read only generation of read only links for various contact type
109 // should be factorised in the cms Utils.
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
114 public static String
getPhoneLink(String value
) {
115 return getPhoneLink(value
, value
);
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
124 * a potentially distinct label
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
)
132 return builder
.toString();
136 * Creates the read-only HTML snippet to display in a label with styling
137 * enabled in order to provide a click-able mail
139 public static String
getMailLink(String value
) {
140 return getMailLink(value
, value
);
144 * Creates the read-only HTML snippet to display in a label with styling
145 * enabled in order to provide a click-able mail
149 * a potentially distinct label
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();
161 * Creates the read-only HTML snippet to display in a label with styling
162 * enabled in order to provide a click-able link
164 public static String
getUrlLink(String value
) {
165 return getUrlLink(value
, value
);
169 * Creates the read-only HTML snippet to display in a label with styling
170 * enabled in order to provide a click-able link
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();
183 private static String AMPERSAND
= "&";
186 * Cleans a String by replacing any '&' by its HTML encoding '&#38;' to
187 * avoid <code>SAXParseException</code> while rendering HTML with RWT
189 public static String
replaceAmpersand(String value
) {
190 value
= value
.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND
);
194 // Prevents instantiation
195 private FormUtils() {