1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package org.eclipse.fx.ecp.ui.controls;
import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.DatePicker;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.HBox;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecp.edit.ECPControlContext;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControl;
public class DateControl extends ECPControlBase {
private DatePicker datePicker;
public DateControl(IItemPropertyDescriptor propertyDescriptor, ECPControlContext context) {
super(propertyDescriptor, context);
setSkin(new Skin(this));
datePicker.valueProperty().addListener(new ChangeListener<LocalDate>() {
@Override
public void changed(ObservableValue<? extends LocalDate> observableValue, LocalDate oldDate, LocalDate newDate) {
// only commit if the value has changed
if (!Objects.equals(oldDate, newDate)) {
@SuppressWarnings("deprecation")
Date date = new Date(newDate.getYear() - 1900, newDate.getMonthValue(), newDate.getDayOfMonth());
Command command = SetCommand.create(editingDomain, modelElement, feature, date);
if (command.canExecute())
editingDomain.getCommandStack().execute(command);
}
}
});
update();
}
@Override
public void update() {
Date newDate = (Date) modelElement.eGet(feature);
if (newDate == null)
newDate = new Date();
@SuppressWarnings("deprecation")
LocalDate newLocalDate = LocalDate.of(newDate.getYear() + 1900, newDate.getMonth(), newDate.getDate());
// set the date only if the value has changed
if (!Objects.equals(newLocalDate, datePicker.getValue()))
datePicker.valueProperty().set(newLocalDate);
}
private final class Skin extends SkinBase<DateControl> {
private Skin(DateControl control) {
super(control);
HBox hBox = new HBox();
getChildren().add(hBox);
datePicker = new DatePicker();
hBox.getChildren().add(datePicker);
}
}
public static class Factory implements ECPControl.Factory {
@Override
public ECPControlBase createControl(IItemPropertyDescriptor itemPropertyDescriptor, ECPControlContext context) {
return new DateControl(itemPropertyDescriptor, context);
}
}
}
|