Refactor EnumEditor and actually use it.

refs #32
This commit is contained in:
Fabian Becker 2014-11-05 20:00:00 +01:00
parent 62e436274c
commit 93aff6d876
3 changed files with 40 additions and 9 deletions

View File

@ -21,8 +21,7 @@ public class PropertyEditorProvider {
* So better use the one based on PropertyDescriptor if possible. * So better use the one based on PropertyDescriptor if possible.
*/ */
public static PropertyEditor findEditor(Class<?> cls) { public static PropertyEditor findEditor(Class<?> cls) {
PropertyEditor editor = null; PropertyEditor editor = PropertyEditorManager.findEditor(cls);
editor = PropertyEditorManager.findEditor(cls);
if ((editor == null) && useDefaultGOE) { if ((editor == null) && useDefaultGOE) {
if (cls.isArray()) { if (cls.isArray()) {
@ -46,6 +45,7 @@ public class PropertyEditorProvider {
PropertyEditor editor = null; PropertyEditor editor = null;
Class pec = prop.getPropertyEditorClass(); Class pec = prop.getPropertyEditorClass();
Class type = prop.getPropertyType(); Class type = prop.getPropertyType();
try { try {
if (pec != null) { if (pec != null) {
editor = (PropertyEditor) pec.newInstance(); editor = (PropertyEditor) pec.newInstance();
@ -56,7 +56,13 @@ public class PropertyEditorProvider {
if (editor == null) { if (editor == null) {
if (value != null) { if (value != null) {
editor = PropertyEditorManager.findEditor(value.getClass());
// ToDo: This should be handled by the registerEditor below. findEditor however always returns the sun.beans.editor stuff.
if (value instanceof Enum) {
editor = new EnumEditor();
} else {
editor = PropertyEditorManager.findEditor(value.getClass());
}
} }
if (editor == null && (BeanInspector.isJavaPrimitive(value.getClass()))) { if (editor == null && (BeanInspector.isJavaPrimitive(value.getClass()))) {
Class<?> prim = BeanInspector.getBoxedType(value.getClass()); Class<?> prim = BeanInspector.getBoxedType(value.getClass());
@ -103,6 +109,9 @@ public class PropertyEditorProvider {
/** /**
*/ */
public static void installEditors() { public static void installEditors() {
// First unregister the default EnumEditor provided by sun.*
PropertyEditorManager.registerEditor(Enum.class, null);
PropertyEditorManager.registerEditor(SelectedTag.class, TagEditor.class); PropertyEditorManager.registerEditor(SelectedTag.class, TagEditor.class);
PropertyEditorManager.registerEditor(Enum.class, EnumEditor.class); PropertyEditorManager.registerEditor(Enum.class, EnumEditor.class);
PropertyEditorManager.registerEditor(int[].class, GenericArrayEditor.class); PropertyEditorManager.registerEditor(int[].class, GenericArrayEditor.class);

View File

@ -9,11 +9,7 @@ import java.awt.event.WindowEvent;
import java.beans.PropertyEditorSupport; import java.beans.PropertyEditorSupport;
/** /**
* Created by IntelliJ IDEA. *
* User: streiche
* Date: 27.06.2003
* Time: 11:41:01
* To change this template use Options | File Templates.
*/ */
public class EnumEditor extends PropertyEditorSupport { public class EnumEditor extends PropertyEditorSupport {
/** /**
@ -92,4 +88,10 @@ public class EnumEditor extends PropertyEditorSupport {
} }
} }
enum TestEnum {asdf, sdf, asdfa} enum TestEnum {
asdf, sdf, asdfa;
public String toString() {
return "Foo";
}
}

View File

@ -4,6 +4,26 @@ public enum DEType {
DE1_Rand_1, DE2_CurrentToBest, DE_Best_1, DE_Best_2, TrigonometricDE, DE_CurrentToRand; DE1_Rand_1, DE2_CurrentToBest, DE_Best_1, DE_Best_2, TrigonometricDE, DE_CurrentToRand;
//", "DE2 - DE/current-to-best/1", "DE/best/2", "Trigonometric DE"}; //", "DE2 - DE/current-to-best/1", "DE/best/2", "Trigonometric DE"};
@Override
public String toString() {
switch(this) {
case DE1_Rand_1:
return "DE/rand/1";
case DE2_CurrentToBest:
return "DE/current-to-best/1";
case DE_Best_1:
return "DE/best/1";
case DE_Best_2:
return "DE/best/2";
case TrigonometricDE:
return this.name();
case DE_CurrentToRand:
return "DE/current-to-rand";
default:
return this.name();
}
}
/** /**
* A method to translate the "old" integer tags into the enum type. * A method to translate the "old" integer tags into the enum type.
* *