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
.argeo
.api
.cms
.CmsView
;
13 import org
.argeo
.api
.cms
.CmsLog
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
16 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
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 CmsLog log
= CmsLog
.getLog(FormUtils
.class);
33 public final static String DEFAULT_SHORT_DATE_FORMAT
= "dd/MM/yyyy";
35 /** Best effort to convert a String to a calendar. Fails silently */
36 public static Calendar
parseDate(DateFormat dateFormat
, String calStr
) {
38 if (EclipseUiUtils
.notEmpty(calStr
)) {
40 Date date
= dateFormat
.parse(calStr
);
41 cal
= new GregorianCalendar();
43 } catch (ParseException pe
) {
45 log
.warn("Unable to parse date: " + calStr
+ " - msg: "
52 /** Add a double click listener on tables that display a JCR node list */
53 public static void addCanonicalDoubleClickListener(final TableViewer v
) {
54 v
.addDoubleClickListener(new IDoubleClickListener() {
57 public void doubleClick(DoubleClickEvent event
) {
58 CmsView cmsView
= CmsUiUtils
.getCmsView();
59 Node node
= (Node
) ((IStructuredSelection
) event
.getSelection())
62 cmsView
.navigateTo(node
.getPath());
63 } catch (RepositoryException e
) {
64 throw new CmsException("Unable to get path for node "
65 + node
+ " before calling navigateTo(path)", e
);
71 // MANAGE ERROR DECORATION
73 public static ControlDecoration
addDecoration(final Text text
) {
74 final ControlDecoration dynDecoration
= new ControlDecoration(text
,
76 Image icon
= getDecorationImage(FieldDecorationRegistry
.DEC_ERROR
);
77 dynDecoration
.setImage(icon
);
78 dynDecoration
.setMarginWidth(3);
83 public static void refreshDecoration(Text text
, ControlDecoration deco
,
84 boolean isValid
, boolean clean
) {
85 if (isValid
|| clean
) {
86 text
.setBackground(null);
89 text
.setBackground(new Color(text
.getDisplay(), 250, 200, 150));
94 public static Image
getDecorationImage(String image
) {
95 FieldDecorationRegistry registry
= FieldDecorationRegistry
.getDefault();
96 return registry
.getFieldDecoration(image
).getImage();
99 public static void addCompulsoryDecoration(Label label
) {
100 final ControlDecoration dynDecoration
= new ControlDecoration(label
,
101 SWT
.RIGHT
| SWT
.TOP
);
102 Image icon
= getDecorationImage(FieldDecorationRegistry
.DEC_REQUIRED
);
103 dynDecoration
.setImage(icon
);
104 dynDecoration
.setMarginWidth(3);
107 // TODO the read only generation of read only links for various contact type
108 // should be factorised in the cms Utils.
110 * Creates the read-only HTML snippet to display in a label with styling
111 * enabled in order to provide a click-able phone number
113 public static String
getPhoneLink(String value
) {
114 return getPhoneLink(value
, value
);
118 * Creates the read-only HTML snippet to display in a label with styling
119 * enabled in order to provide a click-able phone number
123 * a potentially distinct label
126 public static String
getPhoneLink(String value
, String label
) {
127 StringBuilder builder
= new StringBuilder();
128 builder
.append("<a href=\"tel:");
129 builder
.append(value
).append("\" target=\"_blank\" >").append(label
)
131 return builder
.toString();
135 * Creates the read-only HTML snippet to display in a label with styling
136 * enabled in order to provide a click-able mail
138 public static String
getMailLink(String value
) {
139 return getMailLink(value
, value
);
143 * Creates the read-only HTML snippet to display in a label with styling
144 * enabled in order to provide a click-able mail
148 * a potentially distinct label
151 public static String
getMailLink(String value
, String label
) {
152 StringBuilder builder
= new StringBuilder();
153 value
= replaceAmpersand(value
);
154 builder
.append("<a href=\"mailto:");
155 builder
.append(value
).append("\" >").append(label
).append("</a>");
156 return builder
.toString();
160 * Creates the read-only HTML snippet to display in a label with styling
161 * enabled in order to provide a click-able link
163 public static String
getUrlLink(String value
) {
164 return getUrlLink(value
, value
);
168 * Creates the read-only HTML snippet to display in a label with styling
169 * enabled in order to provide a click-able link
171 public static String
getUrlLink(String value
, String label
) {
172 StringBuilder builder
= new StringBuilder();
173 value
= replaceAmpersand(value
);
174 label
= replaceAmpersand(label
);
175 if (!(value
.startsWith("http://") || value
.startsWith("https://")))
176 value
= "http://" + value
;
177 builder
.append("<a href=\"");
178 builder
.append(value
+ "\" target=\"_blank\" >" + label
+ "</a>");
179 return builder
.toString();
182 private static String AMPERSAND
= "&";
185 * Cleans a String by replacing any '&' by its HTML encoding '&#38;' to
186 * avoid <code>SAXParseException</code> while rendering HTML with RWT
188 public static String
replaceAmpersand(String value
) {
189 value
= value
.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND
);
193 // Prevents instantiation
194 private FormUtils() {