From 2a215953eb4748e4d31136a7af0ca570ae1a3611 Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Tue, 26 Aug 2008 15:02:41 +0000 Subject: [PATCH] Adding an enum editor for the GUI. Merge from MK branch rev. 157 --- src/eva2/EvAInfo.java | 3 +- src/eva2/gui/EnumEditor.java | 81 ++++++++++++++++++++++++ src/eva2/gui/PropertyEditorProvider.java | 3 + src/eva2/gui/TagEditor.java | 10 +-- 4 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/eva2/gui/EnumEditor.java diff --git a/src/eva2/EvAInfo.java b/src/eva2/EvAInfo.java index 4fab4713..18f64807 100644 --- a/src/eva2/EvAInfo.java +++ b/src/eva2/EvAInfo.java @@ -4,6 +4,7 @@ package eva2; * Main product and version information strings. * * --- Changelog + * 2.030: Added an EnumEditor to access enums easily through the GUI. * 2.029: Tuned the 2d-graphs which now paints quicker and changes size depending on the * surrounding plot window. Added a preloader-thread to accelerate the GUI at starting time. * 2.028: Tuned the Population to sort only when necessary on calls to getBestN... Added StatisticsDummy. @@ -29,7 +30,7 @@ package eva2; public class EvAInfo { public static final String productName = "EvA 2"; public static final String productLongName = "Evolutionary Algorithms Workbench 2"; - public static final String versionNum = new String ("2.029"); + public static final String versionNum = new String ("2.030"); public static final String url = "http://www.ra.cs.uni-tuebingen.de/software/EvA2"; public static final String propertyFile = "resources/EvA2.props"; diff --git a/src/eva2/gui/EnumEditor.java b/src/eva2/gui/EnumEditor.java new file mode 100644 index 00000000..71831b59 --- /dev/null +++ b/src/eva2/gui/EnumEditor.java @@ -0,0 +1,81 @@ +package eva2.gui; + +import java.awt.BorderLayout; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.beans.PropertyEditorSupport; + +import javax.swing.JFrame; + +/** + * 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 { + /** The Enum values that may be chosen */ + private Object[] values = null; + + public String getAsText() { + return getValue().toString(); + } + + public void setValue(Object value) { + if (value instanceof Enum) { + values = ((Enum)value).getClass().getEnumConstants(); + super.setValue(value); + } + } + + /** + * + */ + public void setAsText(String text) throws IllegalArgumentException { + for (int i=0; i initial = TestEnum.asdf; + EnumEditor ed = new EnumEditor(); + ed.setValue(initial); + PropertyValueSelector ps = new PropertyValueSelector(ed); + JFrame f = new JFrame(); + f.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + f.getContentPane().setLayout(new BorderLayout()); + f.getContentPane().add(ps, BorderLayout.CENTER); + f.pack(); + f.setVisible(true); + } catch (Exception ex) { + ex.printStackTrace(); + System.err.println(ex.getMessage()); + } + } +} + +enum TestEnum { asdf, sdf, asdfa}; \ No newline at end of file diff --git a/src/eva2/gui/PropertyEditorProvider.java b/src/eva2/gui/PropertyEditorProvider.java index 449fa786..1f7ccc6b 100644 --- a/src/eva2/gui/PropertyEditorProvider.java +++ b/src/eva2/gui/PropertyEditorProvider.java @@ -38,6 +38,7 @@ public class PropertyEditorProvider { // if (TRACE) System.out.println((editor == null ) ? "No editor from PEM" : ("Found " + editor.getClass())); if ((editor == null) && useDefaultGOE ) { if (cls.isArray()) editor = new GenericArrayEditor(); + else if (cls.isEnum()) editor = new EnumEditor(); else editor = new GenericObjectEditor(); // if (TRACE) System.out.println("using GOE/GAE"); } @@ -119,6 +120,7 @@ public class PropertyEditorProvider { if (TRACE) System.out.println((editor == null ) ? "No editor from PEM by type" : ("Found " + editor.getClass())); if ((editor == null) && useDefaultGOE ) { if (type.isArray()) editor = new GenericArrayEditor(); + else if (type.isEnum()) editor = new EnumEditor(); else editor = new GenericObjectEditor(); if (TRACE) System.out.println("using GOE/GAE"); } @@ -144,6 +146,7 @@ public class PropertyEditorProvider { */ public static void installEditors() { PropertyEditorManager.registerEditor(SelectedTag.class, TagEditor.class); + PropertyEditorManager.registerEditor(Enum.class, EnumEditor.class); PropertyEditorManager.registerEditor(int[].class, GenericArrayEditor.class); PropertyEditorManager.registerEditor(double[].class, GenericArrayEditor.class); PropertyEditorManager.registerEditor(InterfaceTerminator[].class, GenericArrayEditor.class); diff --git a/src/eva2/gui/TagEditor.java b/src/eva2/gui/TagEditor.java index 6d41ba81..da48a34b 100644 --- a/src/eva2/gui/TagEditor.java +++ b/src/eva2/gui/TagEditor.java @@ -114,11 +114,11 @@ public class TagEditor extends PropertyEditorSupport { try { PropertyEditorManager.registerEditor(SelectedTag.class,TagEditor.class); Tag [] tags = { - new Tag(1, "First option"), - new Tag(2, "Second option"), - new Tag(3, "Third option"), - new Tag(4, "Fourth option"), - new Tag(5, "Fifth option"), + new Tag(0, "First option"), + new Tag(1, "Second option"), + new Tag(2, "Third option"), + new Tag(3, "Fourth option"), + new Tag(4, "Fifth option"), }; SelectedTag initial = new SelectedTag(1, tags); TagEditor ce = new TagEditor();