Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85eb6c301fc947e109f7c60ba1adf98be1d7637e (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.Objects;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.SkinBase;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;

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.ECPModelElementOpener;

public class TextFieldControl extends ECPControlBase {

	protected TextField textField;

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

		setSkin(new Skin(this));
		
		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) {
					Object oldText = modelElement.eGet(feature);
					String newText = textField.getText();

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

		});

		update();
	}

	@Override
	protected void update() {
		String value = (String) modelElement.eGet(feature);
		textField.setText(value);
	}

	protected class Skin extends SkinBase<TextFieldControl> {

		protected Skin(TextFieldControl control) {
			super(control);

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

			textField = new TextField();
			hBox.getChildren().add(textField);
			HBox.setHgrow(textField, Priority.ALWAYS);
		}

	}

	public static class Factory implements ECPControl.Factory {

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

	}

}

Back to the top