Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8d9d4fd89324a05ad5abd04622c48864fb6e02b (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.eclipse.fx.ecp.ui.controls.numeric;

import java.text.DecimalFormat;
import java.text.NumberFormat;
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.NumberField;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControl;
import org.eclipse.fx.ecp.ui.ECPControlContext;
import org.eclipse.fx.ecp.ui.controls.ECPControlBase;

public class DoubleField extends ECPControlBase implements ECPControl {

	protected NumberField numberField;
	protected boolean internalUpdate = false;

	public class DoubleFieldSkin extends SkinBase<DoubleField> {

		DoubleFieldSkin(DoubleField control) {
			
			super(control);

			HBox hBox = new HBox();
			getChildren().add(hBox);
			hBox.setFillHeight(true);

			numberField = new NumberField() {
				
				@Override
				protected Number fromString(String arg0) {
					return Double.parseDouble(arg0);
//					return super.fromString(arg0);
				}
				
			};
			
			numberField.setDefaultValue(0.0);
			
			numberField.setDecimalFormat(new DecimalFormat("f"));
			
			hBox.getChildren().add(numberField);

			numberField.valueProperty().addListener(new ChangeListener<Number>() {

				@Override
				public void changed(ObservableValue<? extends Number> arg0, Number oldValue, Number newValue) {
					// only commit if the value has changed
					if (!Objects.equals(oldValue, newValue) && !internalUpdate) {
						Command command = SetCommand.create(editingDomain, modelElement, feature, newValue);
						if (command.canExecute())
							editingDomain.getCommandStack().execute(command);
					}
				}

			});

			update();
		}

	}

	public DoubleField(IItemPropertyDescriptor propertyDescriptor, ECPControlContext context) {
		super(propertyDescriptor, context);
		setSkin(new DoubleFieldSkin(this));
		getStyleClass().add("spinner");
	}

	@Override
	protected String getUserAgentStylesheet() {
		return getClass().getResource("../ECPControls.css").toExternalForm();
	}

	@Override
	protected void update() {
		Double value = (Double) modelElement.eGet(feature);
		internalUpdate = true;
		numberField.setValue(value);
		internalUpdate = false;
	}

	@Override
	public void dispose() {
		modelElement.eAdapters().remove(modelElementAdapter);
	}
	
	public static class Factory implements ECPControl.Factory {

		@Override
		public ECPControlBase createControl(IItemPropertyDescriptor itemPropertyDescriptor, ECPControlContext context) {
			return new DoubleField(itemPropertyDescriptor, context);
		}

	}

}

Back to the top