Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85c6c2d3347aba9f353e11ab8a9d31381629fe6e (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
package org.eclipse.fx.ecp.ui.controls.multi;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.SkinBase;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControlContext;
import org.eclipse.fx.ecp.ui.ECPUtil;
import org.eclipse.fx.ecp.ui.controls.AutoSelector;
import org.eclipse.fx.ecp.ui.controls.ECPControlBase;

public class TextFieldAddControl extends ECPControlBase {

	protected EDataTypeTextField textField;
	protected Button button;
	protected Command addCommand;
	protected EDataTypeValueHandler valueHandler = new EDataTypeValueHandler((EDataType) feature.getEType());

	public TextFieldAddControl(IItemPropertyDescriptor propertyDescriptor, ECPControlContext context) {
		super(propertyDescriptor, context);

		setSkin(new Skin(this));

		update();
	}

	protected void doAdd() {
		if (addCommand != null && addCommand.canExecute()) {
			editingDomain.getCommandStack().execute(addCommand);
			textField.selectAll();
			textField.requestFocus();
		}
	}

	@Override
	protected void update() {
		String text = textField.textProperty().getValue();
		String message = valueHandler.isValid(text);

		ObservableList<String> styleClass = textField.getStyleClass();

		if (message == null) {
			Object value = valueHandler.toValue(text);
			addCommand = AddCommand.create(editingDomain, modelElement, feature, value);
			styleClass.remove("error");
		} else {
			addCommand = null;
			if (!styleClass.contains("error"))
				styleClass.add("error");
		}

		button.setDisable(addCommand == null || !addCommand.canExecute());
	}

	protected class Skin extends SkinBase<TextFieldAddControl> {
		
		protected Skin(TextFieldAddControl control) {
			super(control);
			
			HBox hBox = new HBox();
			getChildren().add(hBox);

			textField = new EDataTypeTextField((EDataType) feature.getEType());
			hBox.getChildren().add(textField);
			textField.getStyleClass().add("left-pill");
			
			textField.focusedProperty().addListener(new AutoSelector(textField));
			
			textField.setText(feature.getDefaultValueLiteral());

			HBox.setHgrow(textField, Priority.ALWAYS);

			textField.textProperty().addListener(new ChangeListener<String>() {

				@Override
				public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
					update();
				}

			});

			textField.focusedProperty().addListener(new ChangeListener<Boolean>() {

				@Override
				public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
					if (arg2) {
						textField.requestFocus();
						textField.selectAll();
					}
				}

			});

			button = new Button();
			hBox.getChildren().add(button);
			button.setMaxHeight(Double.MAX_VALUE);
			button.getStyleClass().addAll("right-pill", "text-field-add-button");
			ECPUtil.addMark(button, "plus");

			button.setOnAction(new EventHandler<ActionEvent>() {

				@Override
				public void handle(ActionEvent arg0) {
					doAdd();
				}

			});

			textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
				public void handle(final KeyEvent keyEvent) {
					if (keyEvent.getCode() == KeyCode.ENTER) {
						doAdd();
						keyEvent.consume();
					}
				}
			});
		}
		
	}

}

Back to the top