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
|
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.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.edit.command.SetCommand;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControl;
import org.eclipse.fx.ecp.ui.ECPControlContext;
public class CharacterField extends ECPControlBase {
protected TextField textField;
public CharacterField(IItemPropertyDescriptor propertyDescriptor, ECPControlContext context) {
super(propertyDescriptor, context);
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) {
Character oldCharacter = (Character) modelElement.eGet(feature);
String text = textField.getText();
Character newCharacter = text != null && text.length() > 0 ? text.charAt(0) : null;
// only commit if the value has changed
if (!Objects.equals(oldCharacter, newCharacter)) {
Command command = SetCommand.create(editingDomain, modelElement, feature, newCharacter);
if (command.canExecute())
editingDomain.getCommandStack().execute(command);
}
}
}
});
update();
}
@Override
protected void update() {
Object value = modelElement.eGet(feature);
textField.setText(value.toString());
}
protected class Skin extends SkinBase<CharacterField> {
protected Skin(CharacterField control) {
super(control);
HBox hBox = new HBox();
getChildren().add(hBox);
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);
}
private boolean validate(String tmp) {
return tmp.length() <= 1;
}
};
hBox.getChildren().add(textField);
textField.setPrefColumnCount(1);
}
}
public static class Factory implements ECPControl.Factory {
@Override
public ECPControlBase createControl(IItemPropertyDescriptor itemPropertyDescriptor, ECPControlContext context) {
return new CharacterField(itemPropertyDescriptor, context);
}
}
}
|