1 package org
.argeo
.app
.swt
.forms
;
3 import java
.time
.format
.DateTimeFormatter
;
4 import java
.time
.format
.DateTimeParseException
;
5 import java
.time
.temporal
.TemporalAccessor
;
7 import org
.argeo
.api
.cms
.CmsLog
;
8 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
10 /** Utilitary methods to ease implementation of CMS forms */
11 public class FormUtils
{
12 private final static CmsLog log
= CmsLog
.getLog(FormUtils
.class);
14 public final static String DEFAULT_SHORT_DATE_FORMAT
= "dd/MM/yyyy";
16 /** Best effort to convert a String to a calendar. Fails silently */
17 public static TemporalAccessor
parseDate(DateTimeFormatter dateFormat
, String calStr
) {
18 if (EclipseUiUtils
.notEmpty(calStr
)) {
20 return dateFormat
.parse(calStr
);
21 // cal = new GregorianCalendar();
23 } catch (DateTimeParseException pe
) {
25 log
.warn("Unable to parse date: " + calStr
+ " - msg: " + pe
.getMessage());
32 // /** Add a double click listener on tables that display a JCR node list */
33 // public static void addCanonicalDoubleClickListener(final TableViewer v) {
34 // v.addDoubleClickListener(new IDoubleClickListener() {
37 // public void doubleClick(DoubleClickEvent event) {
38 // CmsView cmsView = CmsUiUtils.getCmsView();
39 // Node node = (Node) ((IStructuredSelection) event.getSelection())
40 // .getFirstElement();
42 // cmsView.navigateTo(node.getPath());
43 // } catch (RepositoryException e) {
44 // throw new CmsException("Unable to get path for node "
45 // + node + " before calling navigateTo(path)", e);
51 // MANAGE ERROR DECORATION
53 // public static ControlDecoration addDecoration(final Text text) {
54 // final ControlDecoration dynDecoration = new ControlDecoration(text,
56 // Image icon = getDecorationImage(FieldDecorationRegistry.DEC_ERROR);
57 // dynDecoration.setImage(icon);
58 // dynDecoration.setMarginWidth(3);
59 // dynDecoration.hide();
60 // return dynDecoration;
63 // public static void refreshDecoration(Text text, ControlDecoration deco,
64 // boolean isValid, boolean clean) {
65 // if (isValid || clean) {
66 // text.setBackground(null);
69 // text.setBackground(new Color(text.getDisplay(), 250, 200, 150));
74 // public static Image getDecorationImage(String image) {
75 // FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
76 // return registry.getFieldDecoration(image).getImage();
79 // public static void addCompulsoryDecoration(Label label) {
80 // final ControlDecoration dynDecoration = new ControlDecoration(label,
81 // SWT.RIGHT | SWT.TOP);
82 // Image icon = getDecorationImage(FieldDecorationRegistry.DEC_REQUIRED);
83 // dynDecoration.setImage(icon);
84 // dynDecoration.setMarginWidth(3);
87 // TODO the read only generation of read only links for various contact type
88 // should be factorised in the cms Utils.
90 * Creates the read-only HTML snippet to display in a label with styling enabled
91 * in order to provide a click-able phone number
93 public static String
getPhoneLink(String value
) {
94 return getPhoneLink(value
, value
);
98 * Creates the read-only HTML snippet to display in a label with styling enabled
99 * in order to provide a click-able phone number
102 * @param label a potentially distinct label
105 public static String
getPhoneLink(String value
, String label
) {
106 StringBuilder builder
= new StringBuilder();
107 builder
.append("<a href=\"tel:");
108 builder
.append(value
).append("\" target=\"_blank\" >").append(label
).append("</a>");
109 return builder
.toString();
113 * Creates the read-only HTML snippet to display in a label with styling enabled
114 * in order to provide a click-able mail
116 public static String
getMailLink(String value
) {
117 return getMailLink(value
, value
);
121 * Creates the read-only HTML snippet to display in a label with styling enabled
122 * in order to provide a click-able mail
125 * @param label a potentially distinct label
128 public static String
getMailLink(String value
, String label
) {
129 StringBuilder builder
= new StringBuilder();
130 value
= replaceAmpersand(value
);
131 builder
.append("<a href=\"mailto:");
132 builder
.append(value
).append("\" >").append(label
).append("</a>");
133 return builder
.toString();
137 * Creates the read-only HTML snippet to display in a label with styling enabled
138 * in order to provide a click-able link
140 public static String
getUrlLink(String value
) {
141 return getUrlLink(value
, value
);
145 * Creates the read-only HTML snippet to display in a label with styling enabled
146 * in order to provide a click-able link
148 public static String
getUrlLink(String value
, String label
) {
149 StringBuilder builder
= new StringBuilder();
150 value
= replaceAmpersand(value
);
151 label
= replaceAmpersand(label
);
152 if (!(value
.startsWith("http://") || value
.startsWith("https://")))
153 value
= "http://" + value
;
154 builder
.append("<a href=\"");
155 builder
.append(value
+ "\" target=\"_blank\" >" + label
+ "</a>");
156 return builder
.toString();
159 private static String AMPERSAND
= "&";
162 * Cleans a String by replacing any '&' by its HTML encoding '&#38;' to
163 * avoid <code>SAXParseException</code> while rendering HTML with RWT
165 public static String
replaceAmpersand(String value
) {
166 value
= value
.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND
);
170 // Prevents instantiation
171 private FormUtils() {