]> git.argeo.org Git - lgpl/argeo-commons.git/blob - EditablePropertyDate.java
8591a925f1a2ef1be55adcd79b41846ba9d785bc
[lgpl/argeo-commons.git] / EditablePropertyDate.java
1 package org.argeo.cms.ui.forms;
2
3 import java.text.DateFormat;
4 import java.util.Calendar;
5 import java.util.GregorianCalendar;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9
10 import org.argeo.cms.swt.CmsSwtUtils;
11 import org.argeo.cms.ui.viewers.EditablePart;
12 import org.argeo.cms.ui.widgets.StyledControl;
13 import org.argeo.eclipse.ui.EclipseUiUtils;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.MouseEvent;
16 import org.eclipse.swt.events.MouseListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.ShellAdapter;
20 import org.eclipse.swt.events.ShellEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.DateTime;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30
31 /** CMS form part to display and edit a date */
32 public class EditablePropertyDate extends StyledControl implements EditablePart {
33 private static final long serialVersionUID = 2500215515778162468L;
34
35 // Context
36 private String propertyName;
37 private String message;
38 private DateFormat dateFormat;
39
40 // UI Objects
41 private Text dateTxt;
42 private Button openCalBtn;
43
44 // TODO manage within the CSS
45 private int fieldBtnSpacing = 5;
46
47 /**
48 *
49 * @param parent
50 * @param style
51 * @param node
52 * @param propertyName
53 * @param message
54 * @param dateFormat provide a {@link DateFormat} as contract to be able to
55 * read/write dates as strings
56 * @throws RepositoryException
57 */
58 public EditablePropertyDate(Composite parent, int style, Node node, String propertyName, String message,
59 DateFormat dateFormat) throws RepositoryException {
60 super(parent, style, node, false);
61
62 this.propertyName = propertyName;
63 this.message = message;
64 this.dateFormat = dateFormat;
65
66 if (node.hasProperty(propertyName)) {
67 this.setStyle(FormStyle.propertyText.style());
68 this.setText(dateFormat.format(node.getProperty(propertyName).getDate().getTime()));
69 } else {
70 this.setStyle(FormStyle.propertyMessage.style());
71 this.setText(message);
72 }
73 }
74
75 public void setText(String text) {
76 Control child = getControl();
77 if (child instanceof Label) {
78 Label lbl = (Label) child;
79 if (EclipseUiUtils.isEmpty(text))
80 lbl.setText(message);
81 else
82 lbl.setText(text);
83 } else if (child instanceof Text) {
84 Text txt = (Text) child;
85 if (EclipseUiUtils.isEmpty(text)) {
86 txt.setText("");
87 } else
88 txt.setText(text);
89 }
90 }
91
92 public synchronized void startEditing() {
93 // if (dateTxt != null && !dateTxt.isDisposed())
94 CmsSwtUtils.style(getControl(), FormStyle.propertyText);
95 // getControl().setData(STYLE, FormStyle.propertyText.style());
96 super.startEditing();
97 }
98
99 public synchronized void stopEditing() {
100 if (EclipseUiUtils.isEmpty(dateTxt.getText()))
101 CmsSwtUtils.style(getControl(), FormStyle.propertyMessage);
102 // getControl().setData(STYLE, FormStyle.propertyMessage.style());
103 else
104 CmsSwtUtils.style(getControl(), FormStyle.propertyText);
105 // getControl().setData(STYLE, FormStyle.propertyText.style());
106 super.stopEditing();
107 }
108
109 public String getPropertyName() {
110 return propertyName;
111 }
112
113 @Override
114 protected Control createControl(Composite box, String style) {
115 if (isEditing()) {
116 return createCustomEditableControl(box, style);
117 } else
118 return createLabel(box, style);
119 }
120
121 protected Label createLabel(Composite box, String style) {
122 Label lbl = new Label(box, getStyle() | SWT.WRAP);
123 lbl.setLayoutData(CmsSwtUtils.fillWidth());
124 CmsSwtUtils.style(lbl, style);
125 CmsSwtUtils.markup(lbl);
126 if (mouseListener != null)
127 lbl.addMouseListener(mouseListener);
128 return lbl;
129 }
130
131 private Control createCustomEditableControl(Composite box, String style) {
132 box.setLayoutData(CmsSwtUtils.fillWidth());
133 Composite dateComposite = new Composite(box, SWT.NONE);
134 GridLayout gl = EclipseUiUtils.noSpaceGridLayout(new GridLayout(2, false));
135 gl.horizontalSpacing = fieldBtnSpacing;
136 dateComposite.setLayout(gl);
137 dateTxt = new Text(dateComposite, SWT.BORDER);
138 CmsSwtUtils.style(dateTxt, style);
139 dateTxt.setLayoutData(new GridData(120, SWT.DEFAULT));
140 dateTxt.setToolTipText(
141 "Enter a date with form \"" + FormUtils.DEFAULT_SHORT_DATE_FORMAT + "\" or use the calendar");
142 openCalBtn = new Button(dateComposite, SWT.FLAT);
143 CmsSwtUtils.style(openCalBtn, FormStyle.calendar.style() + FormStyle.BUTTON_SUFFIX);
144 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
145 gd.heightHint = 17;
146 openCalBtn.setLayoutData(gd);
147 // openCalBtn.setImage(PeopleRapImages.CALENDAR_BTN);
148
149 openCalBtn.addSelectionListener(new SelectionAdapter() {
150 private static final long serialVersionUID = 1L;
151
152 public void widgetSelected(SelectionEvent event) {
153 CalendarPopup popup = new CalendarPopup(dateTxt);
154 popup.open();
155 }
156 });
157
158 // dateTxt.addFocusListener(new FocusListener() {
159 // private static final long serialVersionUID = 1L;
160 //
161 // @Override
162 // public void focusLost(FocusEvent event) {
163 // String newVal = dateTxt.getText();
164 // // Enable reset of the field
165 // if (FormUtils.notNull(newVal))
166 // calendar = null;
167 // else {
168 // try {
169 // Calendar newCal = parseDate(newVal);
170 // // DateText.this.setText(newCal);
171 // calendar = newCal;
172 // } catch (ParseException pe) {
173 // // Silent. Manage error popup?
174 // if (calendar != null)
175 // EditablePropertyDate.this.setText(calendar);
176 // }
177 // }
178 // }
179 //
180 // @Override
181 // public void focusGained(FocusEvent event) {
182 // }
183 // });
184 return dateTxt;
185 }
186
187 protected void clear(boolean deep) {
188 Control child = getControl();
189 if (deep || child instanceof Label)
190 super.clear(deep);
191 else {
192 child.getParent().dispose();
193 }
194 }
195
196 /** Enable setting a custom tooltip on the underlying text */
197 @Deprecated
198 public void setToolTipText(String toolTipText) {
199 dateTxt.setToolTipText(toolTipText);
200 }
201
202 @Deprecated
203 /** Enable setting a custom message on the underlying text */
204 public void setMessage(String message) {
205 dateTxt.setMessage(message);
206 }
207
208 @Deprecated
209 public void setText(Calendar cal) {
210 String newValueStr = "";
211 if (cal != null)
212 newValueStr = dateFormat.format(cal.getTime());
213 if (!newValueStr.equals(dateTxt.getText()))
214 dateTxt.setText(newValueStr);
215 }
216
217 // UTILITIES TO MANAGE THE CALENDAR POPUP
218 // TODO manage the popup shell in a cleaner way
219 private class CalendarPopup extends Shell {
220 private static final long serialVersionUID = 1L;
221 private DateTime dateTimeCtl;
222
223 public CalendarPopup(Control source) {
224 super(source.getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
225 populate();
226 // Add border and shadow style
227 CmsSwtUtils.markup(CalendarPopup.this);
228 CmsSwtUtils.style(CalendarPopup.this, FormStyle.popupCalendar.style());
229 pack();
230 layout();
231 setLocation(source.toDisplay((source.getLocation().x - 2), (source.getSize().y) + 3));
232
233 addShellListener(new ShellAdapter() {
234 private static final long serialVersionUID = 5178980294808435833L;
235
236 @Override
237 public void shellDeactivated(ShellEvent e) {
238 close();
239 dispose();
240 }
241 });
242 open();
243 }
244
245 private void setProperty() {
246 // Direct set does not seems to work. investigate
247 // cal.set(dateTimeCtl.getYear(), dateTimeCtl.getMonth(),
248 // dateTimeCtl.getDay(), 12, 0);
249 Calendar cal = new GregorianCalendar();
250 cal.set(Calendar.YEAR, dateTimeCtl.getYear());
251 cal.set(Calendar.MONTH, dateTimeCtl.getMonth());
252 cal.set(Calendar.DAY_OF_MONTH, dateTimeCtl.getDay());
253 String dateStr = dateFormat.format(cal.getTime());
254 dateTxt.setText(dateStr);
255 }
256
257 protected void populate() {
258 setLayout(EclipseUiUtils.noSpaceGridLayout());
259
260 dateTimeCtl = new DateTime(this, SWT.CALENDAR);
261 dateTimeCtl.setLayoutData(EclipseUiUtils.fillAll());
262
263 Calendar calendar = FormUtils.parseDate(dateFormat, dateTxt.getText());
264
265 if (calendar != null)
266 dateTimeCtl.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
267 calendar.get(Calendar.DAY_OF_MONTH));
268
269 dateTimeCtl.addSelectionListener(new SelectionAdapter() {
270 private static final long serialVersionUID = -8414377364434281112L;
271
272 @Override
273 public void widgetSelected(SelectionEvent e) {
274 setProperty();
275 }
276 });
277
278 dateTimeCtl.addMouseListener(new MouseListener() {
279 private static final long serialVersionUID = 1L;
280
281 @Override
282 public void mouseUp(MouseEvent e) {
283 }
284
285 @Override
286 public void mouseDown(MouseEvent e) {
287 }
288
289 @Override
290 public void mouseDoubleClick(MouseEvent e) {
291 setProperty();
292 close();
293 dispose();
294 }
295 });
296 }
297 }
298 }