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.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;
29
30
31 public class FormUtils {
32 private final static Log log = LogFactory.getLog(FormUtils.class);
33
34 public final static String DEFAULT_SHORT_DATE_FORMAT = "dd/MM/yyyy";
35
36
37 public static Calendar parseDate(DateFormat dateFormat, String calStr) {
38 Calendar cal = null;
39 if (EclipseUiUtils.notEmpty(calStr)) {
40 try {
41 Date date = dateFormat.parse(calStr);
42 cal = new GregorianCalendar();
43 cal.setTime(date);
44 } catch (ParseException pe) {
45
46 log.warn("Unable to parse date: " + calStr + " - msg: "
47 + pe.getMessage());
48 }
49 }
50 return cal;
51 }
52
53
54 public static void addCanonicalDoubleClickListener(final TableViewer v) {
55 v.addDoubleClickListener(new IDoubleClickListener() {
56
57 @Override
58 public void doubleClick(DoubleClickEvent event) {
59 CmsView cmsView = CmsUiUtils.getCmsView();
60 Node node = (Node) ((IStructuredSelection) event.getSelection())
61 .getFirstElement();
62 try {
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);
67 }
68 }
69 });
70 }
71
72
73
74 public static ControlDecoration addDecoration(final Text text) {
75 final ControlDecoration dynDecoration = new ControlDecoration(text,
76 SWT.LEFT);
77 Image icon = getDecorationImage(FieldDecorationRegistry.DEC_ERROR);
78 dynDecoration.setImage(icon);
79 dynDecoration.setMarginWidth(3);
80 dynDecoration.hide();
81 return dynDecoration;
82 }
83
84 public static void refreshDecoration(Text text, ControlDecoration deco,
85 boolean isValid, boolean clean) {
86 if (isValid || clean) {
87 text.setBackground(null);
88 deco.hide();
89 } else {
90 text.setBackground(new Color(text.getDisplay(), 250, 200, 150));
91 deco.show();
92 }
93 }
94
95 public static Image getDecorationImage(String image) {
96 FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
97 return registry.getFieldDecoration(image).getImage();
98 }
99
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);
106 }
107
108
109
110
111
112
113
114 public static String getPhoneLink(String value) {
115 return getPhoneLink(value, value);
116 }
117
118
119
120
121
122
123
124
125
126
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)
131 .append("</a>");
132 return builder.toString();
133 }
134
135
136
137
138
139 public static String getMailLink(String value) {
140 return getMailLink(value, value);
141 }
142
143
144
145
146
147
148
149
150
151
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();
158 }
159
160
161
162
163
164 public static String getUrlLink(String value) {
165 return getUrlLink(value, value);
166 }
167
168
169
170
171
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();
181 }
182
183 private static String AMPERSAND = "&";
184
185
186
187
188
189 public static String replaceAmpersand(String value) {
190 value = value.replaceAll("&(?![#a-zA-Z0-9]+;)", AMPERSAND);
191 return value;
192 }
193
194
195 private FormUtils() {
196 }
197 }