1
|
/**
|
2
|
* Copyright (C) 2014 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.ui.mvc.element;
|
10
|
|
11
|
import org.eclipse.swt.SWT;
|
12
|
import org.eclipse.swt.events.DisposeEvent;
|
13
|
import org.eclipse.swt.events.DisposeListener;
|
14
|
import org.eclipse.swt.events.SelectionAdapter;
|
15
|
import org.eclipse.swt.events.SelectionEvent;
|
16
|
import org.eclipse.swt.graphics.Image;
|
17
|
import org.eclipse.swt.layout.GridData;
|
18
|
import org.eclipse.swt.layout.GridLayout;
|
19
|
import org.eclipse.swt.widgets.Button;
|
20
|
import org.eclipse.swt.widgets.Composite;
|
21
|
import org.eclipse.swt.widgets.DateTime;
|
22
|
import org.eclipse.swt.widgets.Display;
|
23
|
import org.eclipse.swt.widgets.Shell;
|
24
|
import org.eclipse.swt.widgets.Text;
|
25
|
import org.eclipse.ui.forms.widgets.FormToolkit;
|
26
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
27
|
import org.joda.time.DateTimeZone;
|
28
|
|
29
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
30
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
31
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
32
|
import eu.etaxonomy.taxeditor.ui.mvc.AbstractCdmComposite;
|
33
|
|
34
|
/**
|
35
|
* @author pplitzner
|
36
|
* @date 16.06.2014
|
37
|
*/
|
38
|
public class DateElement extends AbstractCdmComposite<DateElementController> {
|
39
|
|
40
|
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
|
41
|
private org.joda.time.DateTime initialDateTime;
|
42
|
private final Text textDate;
|
43
|
private final Button openDateDialog;
|
44
|
|
45
|
private static final String pattern = "yyyy-MM-dd HH:mm";
|
46
|
|
47
|
|
48
|
/**
|
49
|
* Create the composite.
|
50
|
*/
|
51
|
public DateElement(Composite parent, org.joda.time.DateTime initDateTime, String label, int style, boolean editableText) {
|
52
|
super(parent, style);
|
53
|
if (initDateTime != null){
|
54
|
this.initialDateTime = initDateTime.toDateTime(DateTimeZone.UTC);
|
55
|
}
|
56
|
|
57
|
addDisposeListener(new DisposeListener() {
|
58
|
@Override
|
59
|
public void widgetDisposed(DisposeEvent e) {
|
60
|
toolkit.dispose();
|
61
|
}
|
62
|
});
|
63
|
toolkit.adapt(this);
|
64
|
toolkit.paintBordersFor(this);
|
65
|
{
|
66
|
TableWrapLayout tableWrapLayout = new TableWrapLayout();
|
67
|
tableWrapLayout.verticalSpacing = 0;
|
68
|
tableWrapLayout.topMargin = 0;
|
69
|
tableWrapLayout.rightMargin = 0;
|
70
|
tableWrapLayout.leftMargin = 0;
|
71
|
tableWrapLayout.horizontalSpacing = 0;
|
72
|
tableWrapLayout.bottomMargin = 0;
|
73
|
setLayout(tableWrapLayout);
|
74
|
}
|
75
|
|
76
|
Composite composite = new Composite (this, SWT.NONE);
|
77
|
GridLayout layout = new GridLayout ();
|
78
|
|
79
|
layout.numColumns = 2;
|
80
|
layout.marginWidth = 0;
|
81
|
|
82
|
composite.setLayout(layout);
|
83
|
|
84
|
|
85
|
textDate = toolkit.createText(composite, null, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
|
86
|
|
87
|
textDate.setEditable(editableText);
|
88
|
|
89
|
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
|
90
|
gridData.minimumWidth = 100;
|
91
|
|
92
|
textDate.setLayoutData(gridData);
|
93
|
openDateDialog = new Button (composite, SWT.PUSH);
|
94
|
Image image = ImageResources.getImage(ImageResources.DATE);
|
95
|
// openDateDialog.setText ("Open Date Dialog");
|
96
|
openDateDialog.setImage(image);
|
97
|
|
98
|
openDateDialog.addSelectionListener (new SelectionAdapter() {
|
99
|
@Override
|
100
|
public void widgetSelected(SelectionEvent e){
|
101
|
final Shell dialog = new Shell (parent.getShell(), SWT.DIALOG_TRIM);
|
102
|
|
103
|
dialog.setLayout (new GridLayout (3, false));
|
104
|
dialog.setText(label);
|
105
|
|
106
|
final DateTime calendar = new DateTime (dialog, SWT.CALENDAR | SWT.BORDER);
|
107
|
|
108
|
final DateTime time = new DateTime (dialog, SWT.TIME | SWT.SHORT);
|
109
|
if (initialDateTime != null){
|
110
|
calendar.setDate(initialDateTime.getYear(), initialDateTime.getMonthOfYear()-1, initialDateTime.getDayOfMonth());
|
111
|
time.setHours(initialDateTime.getHourOfDay());
|
112
|
time.setMinutes(initialDateTime.getMinuteOfHour());
|
113
|
// time.setSeconds(initialDateTime.getSecondOfMinute());
|
114
|
}
|
115
|
|
116
|
// new Label (dialog, SWT.NONE);
|
117
|
// new Label (dialog, SWT.NONE);
|
118
|
Button ok = new Button (dialog, SWT.PUSH);
|
119
|
ok.setText ("OK");
|
120
|
ok.setLayoutData(new GridData (SWT.FILL, SWT.CENTER, false, false));
|
121
|
ok.addSelectionListener (new SelectionAdapter() {
|
122
|
@Override
|
123
|
public void widgetSelected(SelectionEvent e){
|
124
|
setData(makeZonedTimeFromSWT(calendar, time));
|
125
|
dialog.close ();
|
126
|
}
|
127
|
});
|
128
|
dialog.setDefaultButton (ok);
|
129
|
dialog.pack ();
|
130
|
dialog.open ();
|
131
|
}
|
132
|
});
|
133
|
|
134
|
toolkit.adapt(textDate, true, true);
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
protected void initInternalController(CdmFormFactory formFactory, ICdmFormElement parentElement) {
|
139
|
controller = new DateElementController(this, formFactory, parentElement, initialDateTime);
|
140
|
formFactory.adapt(controller);
|
141
|
}
|
142
|
public Text getTextDate() {
|
143
|
return textDate;
|
144
|
}
|
145
|
|
146
|
public static org.joda.time.DateTime makeZonedTimeFromSWT(
|
147
|
org.eclipse.swt.widgets.DateTime widget,
|
148
|
org.eclipse.swt.widgets.DateTime dateTime) {
|
149
|
//the first month of the year is 0
|
150
|
return new org.joda.time.DateTime(widget.getYear(),
|
151
|
widget.getMonth()+1,
|
152
|
widget.getDay(),
|
153
|
dateTime.getHours(),
|
154
|
dateTime.getMinutes(),
|
155
|
0,
|
156
|
0,
|
157
|
DateTimeZone.UTC);
|
158
|
}
|
159
|
|
160
|
public static void updateSWTwithJoda(
|
161
|
org.eclipse.swt.widgets.DateTime widget,
|
162
|
DateTime dateTime) {
|
163
|
widget.setYear(dateTime.getYear());
|
164
|
widget.setMonth(dateTime.getMonth() -1);
|
165
|
widget.setDay(dateTime.getDay());
|
166
|
widget.setHours(dateTime.getHours());
|
167
|
widget.setMinutes(dateTime.getMinutes());
|
168
|
}
|
169
|
|
170
|
public void setData (org.joda.time.DateTime data) {
|
171
|
this.initialDateTime = data;
|
172
|
this.controller.setDateTime(data);
|
173
|
this.textDate.setText(initialDateTime.toString(pattern));
|
174
|
}
|
175
|
|
176
|
}
|