]> git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.swt/src/org/argeo/app/swt/forms/FormUtils.java
Remove legacy map overview
[gpl/argeo-suite.git] / swt / org.argeo.app.swt / src / org / argeo / app / swt / forms / FormUtils.java
1 package org.argeo.app.swt.forms;
2
3 import java.time.format.DateTimeFormatter;
4 import java.time.format.DateTimeParseException;
5 import java.time.temporal.TemporalAccessor;
6
7 import org.argeo.api.cms.CmsLog;
8 import org.argeo.eclipse.ui.EclipseUiUtils;
9
10 /** Utilitary methods to ease implementation of CMS forms */
11 public class FormUtils {
12 private final static CmsLog log = CmsLog.getLog(FormUtils.class);
13
14 public final static String DEFAULT_SHORT_DATE_FORMAT = "dd/MM/yyyy";
15
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)) {
19 try {
20 return dateFormat.parse(calStr);
21 // cal = new GregorianCalendar();
22 // cal.setTime(date);
23 } catch (DateTimeParseException pe) {
24 // Silent
25 log.warn("Unable to parse date: " + calStr + " - msg: " + pe.getMessage());
26 throw pe;
27 }
28 }
29 return null;
30 }
31
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() {
35 //
36 // @Override
37 // public void doubleClick(DoubleClickEvent event) {
38 // CmsView cmsView = CmsUiUtils.getCmsView();
39 // Node node = (Node) ((IStructuredSelection) event.getSelection())
40 // .getFirstElement();
41 // try {
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);
46 // }
47 // }
48 // });
49 // }
50
51 // MANAGE ERROR DECORATION
52
53 // public static ControlDecoration addDecoration(final Text text) {
54 // final ControlDecoration dynDecoration = new ControlDecoration(text,
55 // SWT.LEFT);
56 // Image icon = getDecorationImage(FieldDecorationRegistry.DEC_ERROR);
57 // dynDecoration.setImage(icon);
58 // dynDecoration.setMarginWidth(3);
59 // dynDecoration.hide();
60 // return dynDecoration;
61 // }
62 //
63 // public static void refreshDecoration(Text text, ControlDecoration deco,
64 // boolean isValid, boolean clean) {
65 // if (isValid || clean) {
66 // text.setBackground(null);
67 // deco.hide();
68 // } else {
69 // text.setBackground(new Color(text.getDisplay(), 250, 200, 150));
70 // deco.show();
71 // }
72 // }
73 //
74 // public static Image getDecorationImage(String image) {
75 // FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
76 // return registry.getFieldDecoration(image).getImage();
77 // }
78 //
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);
85 // }
86
87 // TODO the read only generation of read only links for various contact type
88 // should be factorised in the cms Utils.
89 /**
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
92 */
93 public static String getPhoneLink(String value) {
94 return getPhoneLink(value, value);
95 }
96
97 /**
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
100 *
101 * @param value
102 * @param label a potentially distinct label
103 * @return the link
104 */
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();
110 }
111
112 /**
113 * Creates the read-only HTML snippet to display in a label with styling enabled
114 * in order to provide a click-able mail
115 */
116 public static String getMailLink(String value) {
117 return getMailLink(value, value);
118 }
119
120 /**
121 * Creates the read-only HTML snippet to display in a label with styling enabled
122 * in order to provide a click-able mail
123 *
124 * @param value
125 * @param label a potentially distinct label
126 * @return the link
127 */
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();
134 }
135
136 /**
137 * Creates the read-only HTML snippet to display in a label with styling enabled
138 * in order to provide a click-able link
139 */
140 public static String getUrlLink(String value) {
141 return getUrlLink(value, value);
142 }
143
144 /**
145 * Creates the read-only HTML snippet to display in a label with styling enabled
146 * in order to provide a click-able link
147 */
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();
157 }
158
159 private static String AMPERSAND = "&#38;";
160
161 /**
162 * Cleans a String by replacing any '&#38;' by its HTML encoding '&#38;#38;' to
163 * avoid <code>SAXParseException</code> while rendering HTML with RWT
164 */
165 public static String replaceAmpersand(String value) {
166 value = value.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND);
167 return value;
168 }
169
170 // Prevents instantiation
171 private FormUtils() {
172 }
173 }