diff options
author | Torsten Sommer | 2013-06-22 16:52:06 +0000 |
---|---|---|
committer | Torsten Sommer | 2013-06-22 16:52:06 +0000 |
commit | f2d4c1a168f4ec4ada048409281e4f7c26618224 (patch) | |
tree | 2a0c83d34ef3ad107a65d332c64baf4709df5d61 | |
parent | cbc65cf73ef2eab383996051fe77ac9ee7b572ef (diff) | |
download | org.eclipse.efxclipse-f2d4c1a168f4ec4ada048409281e4f7c26618224.tar.gz org.eclipse.efxclipse-f2d4c1a168f4ec4ada048409281e4f7c26618224.tar.xz org.eclipse.efxclipse-f2d4c1a168f4ec4ada048409281e4f7c26618224.zip |
Character control added.
3 files changed, 139 insertions, 119 deletions
diff --git a/bundles/runtime/org.eclipse.fx.ecp.ui/plugin.xml b/bundles/runtime/org.eclipse.fx.ecp.ui/plugin.xml index 257c709a2..c7fbf0ee8 100644 --- a/bundles/runtime/org.eclipse.fx.ecp.ui/plugin.xml +++ b/bundles/runtime/org.eclipse.fx.ecp.ui/plugin.xml @@ -6,20 +6,6 @@ <extension point="org.eclipse.fx.ecp.ui.controls"> - <!-- - <factory - class="org.eclipse.fx.ecp.ui.controls.DummyControl$Factory" - controlClass="javafx.scene.Node" - id="org.eclipse.fx.ecp.ui.controls.dummy" - showLabel="true"> - <staticTest - priority="1" - singleValue="true" - supportedClassType="java.lang.Object"> - </staticTest> - </factory> - --> - <factory class="org.eclipse.fx.ecp.ui.controls.CheckBoxControl$Factory" id="org.eclipse.fx.ecp.ui.controls.checkBox" @@ -30,21 +16,24 @@ supportedClassType="java.lang.Boolean"> </staticTest> </factory> - <factory class="org.eclipse.fx.ecp.ui.controls.TextFieldControl$Factory" id="org.eclipse.fx.ecp.ui.controls.textField" showLabel="true"> - <staticTest priority="1" singleValue="true" - supportedClassType="java.lang.Character"> - </staticTest> + supportedClassType="java.lang.String"> + </staticTest> + </factory> + <factory + class="org.eclipse.fx.ecp.ui.controls.CharacterField$Factory" + id="org.eclipse.fx.ecp.ui.controls.characterField" + showLabel="true"> <staticTest priority="1" singleValue="true" - supportedClassType="java.lang.String"> + supportedClassType="java.lang.Character"> </staticTest> </factory> <factory diff --git a/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/CharacterField.java b/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/CharacterField.java new file mode 100644 index 000000000..a9cb6b07c --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/CharacterField.java @@ -0,0 +1,110 @@ +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.ecp.edit.ECPControlContext; +import org.eclipse.emf.edit.command.SetCommand; +import org.eclipse.emf.edit.provider.IItemPropertyDescriptor; +import org.eclipse.fx.ecp.ui.ECPControl; + +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 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); + } + + } + +} diff --git a/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/TextFieldControl.java b/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/TextFieldControl.java index 2564fcf2a..a89df6ffd 100644 --- a/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/TextFieldControl.java +++ b/bundles/runtime/org.eclipse.fx.ecp.ui/src/org/eclipse/fx/ecp/ui/controls/TextFieldControl.java @@ -4,16 +4,11 @@ import java.util.Objects; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; -import javafx.collections.ObservableList; import javafx.scene.control.SkinBase; import javafx.scene.control.TextField; -import javafx.scene.layout.VBox; +import javafx.scene.layout.HBox; import org.eclipse.emf.common.command.Command; -import org.eclipse.emf.common.util.Diagnostic; -import org.eclipse.emf.ecore.EDataType; -import org.eclipse.emf.ecore.util.Diagnostician; -import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecp.edit.ECPControlContext; import org.eclipse.emf.edit.command.SetCommand; import org.eclipse.emf.edit.provider.IItemPropertyDescriptor; @@ -22,51 +17,25 @@ import org.eclipse.fx.ecp.ui.ECPControl; public class TextFieldControl extends ECPControlBase { protected TextField textField; - protected EDataTypeValueHandler valueHandler; public TextFieldControl(IItemPropertyDescriptor propertyDescriptor, ECPControlContext context) { super(propertyDescriptor, context); - - setSkin(new Skin(this)); - - valueHandler = new EDataTypeValueHandler((EDataType) feature.getEType()); - - textField.textProperty().addListener(new ChangeListener<String>() { - - @Override - public void changed(ObservableValue<? extends String> observableValue, String oldText, String newText) { - final String message = valueHandler.isValid(newText); - ObservableList<String> styleClass = textField.getStyleClass(); - if (message == null) { - styleClass.remove("invalid"); - } else { - if (!styleClass.contains("invalid")) - styleClass.add("invalid"); - } - } - }); + setSkin(new Skin(this)); textField.focusedProperty().addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> observableValue, Boolean oldFocused, Boolean newFocused) { if (!newFocused) { - Object oldValue = modelElement.eGet(feature); - String text = textField.getText(); - String message = valueHandler.isValid(text); - - if (message == null) { - Object newValue = valueHandler.toValue(text); - - // only commit if the value has changed - if (!Objects.equals(oldValue, newValue)) { - Command command = SetCommand.create(editingDomain, modelElement, feature, newValue); - if (command.canExecute()) - editingDomain.getCommandStack().execute(command); - } - } else { - System.err.println(message); + 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); } } } @@ -78,22 +47,22 @@ public class TextFieldControl extends ECPControlBase { @Override protected void update() { - Object value = modelElement.eGet(feature); - textField.setText(valueHandler.toString(value)); + String value = (String) modelElement.eGet(feature); + textField.setText(value); } - class Skin extends SkinBase<TextFieldControl> { - - Skin(TextFieldControl control) { + protected class Skin extends SkinBase<TextFieldControl> { + + protected Skin(TextFieldControl control) { super(control); - - VBox vBox = new VBox(); - getChildren().add(vBox); - + + HBox hBox = new HBox(); + getChildren().add(hBox); + textField = new TextField(); - vBox.getChildren().add(textField); + hBox.getChildren().add(textField); } - + } public static class Factory implements ECPControl.Factory { @@ -105,52 +74,4 @@ public class TextFieldControl extends ECPControlBase { } - /** - * A delegate for handling validation and conversion for data type values. - */ - protected static class EDataTypeValueHandler { - protected EDataType eDataType; - - public EDataTypeValueHandler(EDataType eDataType) { - this.eDataType = eDataType; - } - - public String isValid(Object object) { - Object value; - try { - value = eDataType.getEPackage().getEFactoryInstance().createFromString(eDataType, (String) object); - } catch (Exception exception) { - String message = exception.getClass().getName(); - int index = message.lastIndexOf('.'); - if (index >= 0) { - message = message.substring(index + 1); - } - if (exception.getLocalizedMessage() != null) { - message = message + ": " + exception.getLocalizedMessage(); - } - return message; - } - Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eDataType, value); - if (diagnostic.getSeverity() == Diagnostic.OK) { - return null; - } else { - return (diagnostic.getChildren().get(0)).getMessage().replaceAll("'", "''").replaceAll("\\{", "'{'"); // }} - } - } - - public String isValid(String text) { - return isValid((Object) text); - } - - public Object toValue(String string) { - return EcoreUtil.createFromString(eDataType, string); - } - - public String toString(Object value) { - String result = EcoreUtil.convertToString(eDataType, value); - return result == null ? "" : result; - } - - } - } |