Skip to main content
summaryrefslogtreecommitdiffstats
blob: 32686b873b43d86118769c62dc526e186b33a1e2 (plain) (blame)
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
package org.eclipse.fx.ecp.ui.controls;

import java.util.Date;
import java.util.Objects;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.HBox;
import jidefx.scene.control.field.DateField;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControl;

public class DateControl extends ECPControlBase {

	protected DateField dateField;
	protected boolean internalUpdate = false;

	public DateControl(IItemPropertyDescriptor propertyDescriptor, final EObject modelElement, final EditingDomain editingDomain) {
		super(propertyDescriptor, modelElement, editingDomain);

		setSkin(new Skin(this));

		dateField.valueProperty().addListener(new ChangeListener<Date>() {

			@Override
			public void changed(ObservableValue<? extends Date> observableValue, Date oldDate, Date newDate) {
				// only commit if the value has changed and it's not an internal update
				if (!Objects.equals(oldDate, newDate) && !internalUpdate) {
					Command command = SetCommand.create(editingDomain, modelElement, feature, newDate);
					if (command.canExecute())
						editingDomain.getCommandStack().execute(command);
				}
			}

		});

		update();
	}

	@Override
	public void update() {
		Date newDate = (Date) modelElement.eGet(feature);

		if (newDate == null)
			newDate = new Date();

		// set the date only if the value has changed
		if (!Objects.equals(newDate, dateField.getValue())) {
			internalUpdate = true;
			dateField.setValue(newDate);
			internalUpdate = false;
		}
	}

	protected class Skin extends SkinBase<DateControl> {

		private Skin(DateControl control) {
			super(control);
			HBox hBox = new HBox();
			getChildren().add(hBox);
			dateField = DateField.createDateField();
			dateField.setPopupButtonVisible(true);
			hBox.getChildren().add(dateField);
		}

	}

	public static class Factory implements ECPControl.Factory {

		@Override
		public ECPControlBase createControl(IItemPropertyDescriptor itemPropertyDescriptor, EObject modelElement, EditingDomain editingDomain) {
			return new DateControl(itemPropertyDescriptor, modelElement, editingDomain);
		}

	}

}

Back to the top