]> git.argeo.org Git - lgpl/argeo-commons.git/blob - forms/FormUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / forms / FormUtils.java
1 package org.argeo.cms.ui.forms;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8
9 import javax.jcr.Node;
10 import javax.jcr.RepositoryException;
11
12 import org.argeo.api.cms.CmsLog;
13 import org.argeo.api.cms.ux.CmsView;
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;
28
29 /** Utilitary methods to ease implementation of CMS forms */
30 public class FormUtils {
31 private final static CmsLog log = CmsLog.getLog(FormUtils.class);
32
33 public final static String DEFAULT_SHORT_DATE_FORMAT = "dd/MM/yyyy";
34
35 /** Best effort to convert a String to a calendar. Fails silently */
36 public static Calendar parseDate(DateFormat dateFormat, String calStr) {
37 Calendar cal = null;
38 if (EclipseUiUtils.notEmpty(calStr)) {
39 try {
40 Date date = dateFormat.parse(calStr);
41 cal = new GregorianCalendar();
42 cal.setTime(date);
43 } catch (ParseException pe) {
44 // Silent
45 log.warn("Unable to parse date: " + calStr + " - msg: "
46 + pe.getMessage());
47 }
48 }
49 return cal;
50 }
51
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() {
55
56 @Override
57 public void doubleClick(DoubleClickEvent event) {
58 CmsView cmsView = CmsUiUtils.getCmsView();
59 Node node = (Node) ((IStructuredSelection) event.getSelection())
60 .getFirstElement();
61 try {
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);
66 }
67 }
68 });
69 }
70
71 // MANAGE ERROR DECORATION
72
73 public static ControlDecoration addDecoration(final Text text) {
74 final ControlDecoration dynDecoration = new ControlDecoration(text,
75 SWT.LEFT);
76 Image icon = getDecorationImage(FieldDecorationRegistry.DEC_ERROR);
77 dynDecoration.setImage(icon);
78 dynDecoration.setMarginWidth(3);
79 dynDecoration.hide();
80 return dynDecoration;
81 }
82
83 public static void refreshDecoration(Text text, ControlDecoration deco,
84 boolean isValid, boolean clean) {
85 if (isValid || clean) {
86 text.setBackground(null);
87 deco.hide();
88 } else {
89 text.setBackground(new Color(text.getDisplay(), 250, 200, 150));
90 deco.show();
91 }
92 }
93
94 public static Image getDecorationImage(String image) {
95 FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
96 return registry.getFieldDecoration(image).getImage();
97 }
98
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);
105 }
106
107 // TODO the read only generation of read only links for various contact type
108 // should be factorised in the cms Utils.
109 /**
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
112 */
113 public static String getPhoneLink(String value) {
114 return getPhoneLink(value, value);
115 }
116
117 /**
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
120 *
121 * @param value
122 * @param label
123 * a potentially distinct label
124 * @return
125 */
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)
130 .append("</a>");
131 return builder.toString();
132 }
133
134 /**
135 * Creates the read-only HTML snippet to display in a label with styling
136 * enabled in order to provide a click-able mail
137 */
138 public static String getMailLink(String value) {
139 return getMailLink(value, value);
140 }
141
142 /**
143 * Creates the read-only HTML snippet to display in a label with styling
144 * enabled in order to provide a click-able mail
145 *
146 * @param value
147 * @param label
148 * a potentially distinct label
149 * @return
150 */
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();
157 }
158
159 /**
160 * Creates the read-only HTML snippet to display in a label with styling
161 * enabled in order to provide a click-able link
162 */
163 public static String getUrlLink(String value) {
164 return getUrlLink(value, value);
165 }
166
167 /**
168 * Creates the read-only HTML snippet to display in a label with styling
169 * enabled in order to provide a click-able link
170 */
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();
180 }
181
182 private static String AMPERSAND = "&#38;";
183
184 /**
185 * Cleans a String by replacing any '&#38;' by its HTML encoding '&#38;#38;' to
186 * avoid <code>SAXParseException</code> while rendering HTML with RWT
187 */
188 public static String replaceAmpersand(String value) {
189 value = value.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND);
190 return value;
191 }
192
193 // Prevents instantiation
194 private FormUtils() {
195 }
196 }