Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e7d931bc82cce5d21494da9006d36719ba7e264 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.eclipse.fx.ecp.ui.controls.numeric;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.IndexRange;
import javafx.scene.control.SkinBase;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

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;
import org.eclipse.fx.ecp.ui.ECPUtil;
import org.eclipse.fx.ecp.ui.controls.AutoSelector;
import org.eclipse.fx.ecp.ui.controls.ECPControlBase;

public abstract class NumberSpinner extends ECPControlBase implements ECPControl {

	private Button decreaseButton;
	private TextField textField;
	private Button increaseButton;

	abstract class NumberSpinnerSkin<C extends Control, T extends Number> extends SkinBase<C> {

		@SuppressWarnings("unchecked")
		NumberSpinnerSkin(C control) {
			super(control);

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

			decreaseButton = new Button();
			hBox.getChildren().add(decreaseButton);
			decreaseButton.getStyleClass().add("decrease-button");
			decreaseButton.getStyleClass().add("left-pill");
			ECPUtil.addMark(decreaseButton, "minus");
			decreaseButton.setOnAction(new EventHandler<ActionEvent>() {

				@Override
				public void handle(ActionEvent arg0) {
					T value = (T) modelElement.eGet(feature);
					Command command = SetCommand.create(editingDomain, modelElement, feature, decrease(value));
					if (command.canExecute())
						editingDomain.getCommandStack().execute(command);
				}

			});

			textField = new TextField() {

				@Override
				public void replaceText(int start, int end, String text) {
					String currentText = getText();
					StringBuffer sb = new StringBuffer(currentText);
					sb.replace(start, end, text);
					String tmp = sb.toString();

					if (validate(tmp))
						super.replaceText(start, end, text);
				}

				@Override
				public void replaceSelection(String text) {
					IndexRange selection = getSelection();
					int start = selection.getStart();
					int end = selection.getEnd();
					StringBuffer sb = new StringBuffer(getText());
					sb.replace(start, end, text);
					String tmp = sb.toString();

					if (validate(tmp))
						super.replaceSelection(text);
				}

			};
			hBox.getChildren().add(textField);
			textField.getStyleClass().add("center-pill");
			textField.focusedProperty().addListener(new AutoSelector(textField));
			textField.focusedProperty().addListener(new ChangeListener<Boolean>() {

				@Override
				public void changed(ObservableValue<? extends Boolean> observableValue, Boolean oldFocused, Boolean newFocused) {
					if (!newFocused) {
						T oldValue = (T) modelElement.eGet(feature);
						T newValue = parseValue(textField.getText());

						// only commit if the value has changed
						if (oldValue != newValue) {
							Command command = SetCommand.create(editingDomain, modelElement, feature, newValue);
							if (command.canExecute())
								editingDomain.getCommandStack().execute(command);
						}
					}
				}

			});

			increaseButton = new Button();
			hBox.getChildren().add(increaseButton);
			increaseButton.getStyleClass().add("increase-button");
			increaseButton.getStyleClass().add("right-pill");
			ECPUtil.addMark(increaseButton, "plus");
			increaseButton.setOnAction(new EventHandler<ActionEvent>() {

				@Override
				public void handle(ActionEvent arg0) {
					T value = (T) modelElement.eGet(feature);
					Command command = SetCommand.create(editingDomain, modelElement, feature, increase(value));
					if (command.canExecute())
						editingDomain.getCommandStack().execute(command);
				}

			});

			update();
		}

		abstract T decrease(T value);

		abstract T increase(T value);

		abstract T parseValue(String literal);

		abstract boolean validate(String literal);

	}

	public NumberSpinner(IItemPropertyDescriptor propertyDescriptor, final EObject modelElement, final EditingDomain editingDomain) {
		super(propertyDescriptor, modelElement, editingDomain);
		getStyleClass().add("spinner");
	}

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

	@Override
	protected void update() {
		Object value = modelElement.eGet(feature);
		textField.setText(value != null ? value.toString() : "0");
	}

	@Override
	public void dispose() {
		modelElement.eAdapters().remove(modelElementAdapter);
	}

}

Back to the top