diff --git a/src/eva2/EvAInfo.java b/src/eva2/EvAInfo.java index 441bab90..01a9d0ee 100644 --- a/src/eva2/EvAInfo.java +++ b/src/eva2/EvAInfo.java @@ -4,6 +4,8 @@ package eva2; * Main product and version information strings. * * --- Changelog + * 2.036: New graph selection mode for statistic plots: every property may be selected independently. + * A simple plot legend is produced in the graph window, which can be deactivated. * 2.035: Reactivated some of the model-based functionality within the GO framework. Minor bugfixes. * 2.034: Adding a generic parameter control method for optimizers, currently used by PSO to adapt inertness depending * on EvaluationTerminator or GenerationTerminator instances defining the number of function evaluations. @@ -17,7 +19,7 @@ package eva2; * is not available seems to never show up (as opposed to being printed to the console when X-forwarding is on) * and silently fill up the JVM-memory. I havent the faintest idea why there havnt been OutOfMemory exceptions * earlier or whether and how the deadlocks have to do with it. - * The ingeniuos solution was: dont print anything to System.out, which is now done at verbosity 0. + * The ingenious solution was: dont print anything to System.out, which is now done at verbosity 0. * 2.032: Some cosmetics, e.g. to AbstractEAIndividualComparator and older MOCCO classes. * 2.031: Some updates to the OptimizerFactory. Review of the MatlabInterface with adding an own options structure. * Better access to the EvAClient, which now may have a RemoteStateListener added monitoring the optimization run. @@ -56,7 +58,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.035"); + public static final String versionNum = new String ("2.036"); 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/AbstractListSelectionEditor.java b/src/eva2/gui/AbstractListSelectionEditor.java index f87f5fd7..115f3add 100644 --- a/src/eva2/gui/AbstractListSelectionEditor.java +++ b/src/eva2/gui/AbstractListSelectionEditor.java @@ -1,22 +1,24 @@ package eva2.gui; -import javax.swing.*; - -import eva2.server.go.individuals.codings.gp.AbstractGPNode; -import eva2.server.go.individuals.codings.gp.GPArea; -import eva2.tools.EVAHELP; - +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; -import java.beans.PropertyEditorSupport; -import java.beans.PropertyEditor; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; -import java.awt.*; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; -import java.lang.reflect.Array; -import java.util.ArrayList; +import java.beans.PropertyEditor; + +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.SwingConstants; /** * Created by IntelliJ IDEA. @@ -30,21 +32,22 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop /** Handles property change notification */ private PropertyChangeSupport m_Support = new PropertyChangeSupport(this); /** The label for when we can't edit that type */ - private JLabel m_Label = new JLabel("Can't edit", SwingConstants.CENTER); + protected JLabel m_Label = new JLabel("Can't edit", SwingConstants.CENTER); - /** The gaphix stuff */ + /** The graphics stuff */ private JPanel m_CustomEditor, m_NodePanel; protected JCheckBox[] m_BlackCheck; - public AbstractListSelectionEditor() { - } + + public AbstractListSelectionEditor() { + } /** This method will init the CustomEditor Panel */ private void initCustomEditor() { this.m_CustomEditor = new JPanel(); this.m_CustomEditor.setLayout(new BorderLayout()); - this.m_CustomEditor.add(new JLabel("Choose the area:"), BorderLayout.NORTH); + this.m_CustomEditor.add(new JLabel("Choose:"), BorderLayout.NORTH); this.m_NodePanel = new JPanel(); this.m_CustomEditor.add(new JScrollPane(this.m_NodePanel), BorderLayout.CENTER); this.updateEditor(); @@ -65,12 +68,12 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop protected abstract String getElementName(int i); /** - * Get the initial selection state of an element. + * Get the selection state of an element. * * @param i * @return */ - protected abstract boolean isElementAllowed(int i); + protected abstract boolean isElementSelected(int i); /** * The object may have changed update the editor. This notifies change listeners automatically. @@ -81,10 +84,10 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop this.m_NodePanel.setLayout(new GridLayout(getElementCount(), 1)); this.m_BlackCheck = new JCheckBox[getElementCount()]; for (int i = 0; i < getElementCount(); i++) { - this.m_BlackCheck[i] = new JCheckBox(getElementName(i), isElementAllowed(i)); + this.m_BlackCheck[i] = new JCheckBox(getElementName(i), isElementSelected(i)); this.m_BlackCheck[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { - if (performOnAction()) m_Support.firePropertyChange("AbstractListSelectionEditor", null, this); + if (actionOnSelect()) m_Support.firePropertyChange("AbstractListSelectionEditor", null, this); } }); this.m_NodePanel.add(this.m_BlackCheck[i]); @@ -95,7 +98,7 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop /** * Perform actions when the selection state changes. Return true if there was an actual change. */ - protected abstract boolean performOnAction(); + protected abstract boolean actionOnSelect(); /** * Set the base object, return true on success. Make sure that the editor instance is @@ -113,45 +116,36 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop if (setObject(o)) updateEditor(); } - /** Retruns the current object. + /** + * Returns the current object. * @return the current object */ public abstract Object getValue(); public String getJavaInitializationString() { - return "TEST"; + return ""; } - /** - * - */ public String getAsText() { return null; } - /** - * - */ + public void setAsText(String text) throws IllegalArgumentException { throw new IllegalArgumentException(text); } - /** - * - */ + public String[] getTags() { return null; } - /** - * - */ + public void addPropertyChangeListener(PropertyChangeListener l) { m_Support.addPropertyChangeListener(l); } - /** - * - */ + public void removePropertyChangeListener(PropertyChangeListener l) { m_Support.removePropertyChangeListener(l); } + /** Returns true since the Object can be shown * @return true */ @@ -170,9 +164,11 @@ public abstract class AbstractListSelectionEditor extends JPanel implements Prop String rep = "Select from list"; gfx.drawString(rep, 2, fm.getHeight() + vpad - 3 ); } - /** Returns true because we do support a custom editor. - * @return true - */ + + /** + * Returns true because we do support a custom editor. + * @return true + */ public boolean supportsCustomEditor() { return true; } diff --git a/src/eva2/gui/BeanInspector.java b/src/eva2/gui/BeanInspector.java index c71093f8..6b0dd7c8 100644 --- a/src/eva2/gui/BeanInspector.java +++ b/src/eva2/gui/BeanInspector.java @@ -326,6 +326,7 @@ public class BeanInspector { return meth.invoke(obj, args); } catch(Exception e) { System.err.println("Error on calling method "+mName + " on " + obj.getClass().getName()); + e.printStackTrace(); return null; } } else return null; diff --git a/src/eva2/gui/EnumEditor.java b/src/eva2/gui/EnumEditor.java index 71831b59..bbd5c5a0 100644 --- a/src/eva2/gui/EnumEditor.java +++ b/src/eva2/gui/EnumEditor.java @@ -16,7 +16,7 @@ import javax.swing.JFrame; */ public class EnumEditor extends PropertyEditorSupport { /** The Enum values that may be chosen */ - private Object[] values = null; + private Enum[] enumConstants; public String getAsText() { return getValue().toString(); @@ -24,29 +24,33 @@ public class EnumEditor extends PropertyEditorSupport { public void setValue(Object value) { if (value instanceof Enum) { - values = ((Enum)value).getClass().getEnumConstants(); + enumConstants = ((Enum)value).getClass().getEnumConstants(); +// enumType = ((Enum)value); + super.setValue(value); + } else if (value.getClass().isArray() && value.getClass().getComponentType().isEnum() ) { +// values = value.getClass().getComponentType().getEnumConstants(); + Enum[] e = (Enum[])(value); + enumConstants =(Enum[]) e.getClass().getComponentType().getEnumConstants(); super.setValue(value); } } - /** - * - */ + @Override public void setAsText(String text) throws IllegalArgumentException { - for (int i=0; i m_ClassesLongName; + private GenericObjectEditor m_goe = null; +// private String[] m_ClassesShortName; +// private SourceCodeEditor m_SourceCodeEditor; +// private PropertyDialog m_SourceCodeEditorFrame; + + /** + * + */ + public GOEPanel(Object target, Object backup, PropertyChangeSupport support, GenericObjectEditor goe) { + Object m_Object = target; + m_Backup = backup; + m_Support = support; + m_goe = goe; + + //System.out.println("GOEPanel.Constructor !!"); + if (!(Proxy.isProxyClass(m_Object.getClass()))) m_Backup = copyObject(m_Object); + m_ObjectNames = new DefaultComboBoxModel(new String [0]); + m_ObjectChooser = new JComboBox(m_ObjectNames); + m_ObjectChooser.setEditable(false); + m_ChildPropertySheet = new PropertySheetPanel(); + m_ChildPropertySheet.addPropertyChangeListener( + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) { + m_Support.firePropertyChange("", m_Backup, m_goe.getValue()); + } + }); + m_OpenBut = new JButton("Open..."); + m_OpenBut.setToolTipText("Load a configured object"); + m_OpenBut.setEnabled(true); + m_OpenBut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Object object = openObject(); + if (object != null) { + // setValue takes care of: Making sure obj is of right type, + // and firing property change. + m_goe.setValue(object); + // Need a second setValue to get property values filled in OK. + // Not sure why. + m_goe.setValue(object); // <- Hannes ?!?!? + } + } + }); + + m_SaveBut = new JButton("Save..."); + m_SaveBut.setToolTipText("Save the current configured object"); + m_SaveBut.setEnabled(true); + m_SaveBut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + saveObject(m_goe.getValue()); + } + }); +// +// m_editSourceBut = new JButton("Edit Source"); +// m_editSourceBut.setToolTipText("Edit the Source"); +// m_editSourceBut.setEnabled(false); +// m_editSourceBut.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// m_editSourceBut.setEnabled(false); +// m_SourceCodeEditor = new SourceCodeEditor(); +// String className = m_Object.getClass().getName(); +// m_SourceCodeEditor.editSource(EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className)); +// m_SourceCodeEditorFrame = new PropertyDialog(m_SourceCodeEditor, "test", 50, 50); +// m_SourceCodeEditorFrame.pack(); +// m_SourceCodeEditorFrame.addWindowListener(new WindowAdapter() { +// public void windowClosing (WindowEvent e) { +// m_SourceCodeEditor = null; +// m_editSourceBut.setEnabled(true); +// } +// }); +// m_SourceCodeEditor.addPropertyChangeListener(new +// PropertyChangeListener() { +// public void propertyChange(PropertyChangeEvent evt) { +// sourceChanged(); +// } +// } +// ); +// } +// }); + + m_okBut = new JButton("OK"); + m_okBut.setEnabled(true); + m_okBut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + m_Backup = copyObject(m_goe.getValue()); + if ((getTopLevelAncestor() != null) && (getTopLevelAncestor() instanceof Window)) { + Window w = (Window) getTopLevelAncestor(); + w.dispose(); + } + } + }); + + m_cancelBut = new JButton("Cancel"); + m_cancelBut.setEnabled(false); + m_cancelBut.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (m_Backup != null) { +// m_Object = copyObject(m_Backup); + // TODO m_goe.setObject(m_Object); + m_goe.setValue(copyObject(m_Backup)); + updateClassType(); + updateChooser(); + updateChildPropertySheet(); + } + if ((getTopLevelAncestor() != null) + && (getTopLevelAncestor() instanceof Window)) { + Window w = (Window) getTopLevelAncestor(); + w.dispose(); + } + } + }); + + setLayout(new BorderLayout()); + add(m_ObjectChooser, BorderLayout.NORTH); // important + add(m_ChildPropertySheet, BorderLayout.CENTER); + // Since we resize to the size of the property sheet, a scrollpane isn't + // typically needed + // add(new JScrollPane(m_ChildPropertySheet), BorderLayout.CENTER); + + JPanel okcButs = new JPanel(); + okcButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + okcButs.setLayout(new GridLayout(1, 4, 5, 5)); + okcButs.add(m_OpenBut); + okcButs.add(m_SaveBut); + okcButs.add(m_okBut); +// okcButs.add(m_editSourceBut); + //okcButs.add(m_cancelBut); + add(okcButs, BorderLayout.SOUTH); + + if (m_goe.getClassType() != null) { + updateClassType(); + updateChooser(); + updateChildPropertySheet(); + } + m_ObjectChooser.addItemListener(this); + } + + /** + * Opens an object from a file selected by the user. + * + * @return the loaded object, or null if the operation was cancelled + */ + protected Object openObject() { + if (m_FileChooser == null) { + createFileChooser(); + } + int returnVal = m_FileChooser.showOpenDialog(this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File selected = m_FileChooser.getSelectedFile(); + try { + ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); + Object obj = oi.readObject(); + oi.close(); + if (!m_goe.getClassType().isAssignableFrom(obj.getClass())) { + throw new Exception("Object not of type: " + m_goe.getClassType().getName()); + } + return obj; + } catch (Exception ex) { + JOptionPane.showMessageDialog(this, + "Couldn't read object: " + + selected.getName() + + "\n" + ex.getMessage(), + "Open object file", + JOptionPane.ERROR_MESSAGE); + } + } + return null; + } + + /** Saves the current object to a file selected by the user. + * @param object The object to save. + */ + protected void saveObject(Object object) { + + if (m_FileChooser == null) { + createFileChooser(); + } + int returnVal = m_FileChooser.showSaveDialog(this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + File sFile = m_FileChooser.getSelectedFile(); + try { + ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); + oo.writeObject(object); + oo.close(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(this, + "Couldn't write to file: " + + sFile.getName() + + "\n" + ex.getMessage(), + "Save object", + JOptionPane.ERROR_MESSAGE); + } + } + } + protected void createFileChooser() { + m_FileChooser = new JFileChooser(new File("/resources")); + m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + } + + /** + * Makes a copy of an object using serialization + * @param source the object to copy + * @return a copy of the source object + */ + protected Object copyObject(Object source) { + Object result = null; + try { + SerializedObject so = new SerializedObject(source); + result = so.getObject(); + } catch (Exception ex) { + System.err.println("GenericObjectEditor: Problem making backup object"); + System.err.println(source.getClass().getName()); + ex.printStackTrace(); + } + return result; + } + + /** + * This is used to hook an action listener to the ok button + * @param a The action listener. + */ + public void addOkListener(ActionListener a) { + m_okBut.addActionListener(a); + } + + /** + * This is used to hook an action listener to the cancel button + * @param a The action listener. + */ + public void addCancelListener(ActionListener a) { + m_cancelBut.addActionListener(a); + } + + /** + * This is used to remove an action listener from the ok button + * @param a The action listener + */ + public void removeOkListener(ActionListener a) { + m_okBut.removeActionListener(a); + } + + /** + * This is used to remove an action listener from the cancel button + * @param a The action listener + */ + public void removeCancelListener(ActionListener a) { + m_cancelBut.removeActionListener(a); + } + /** + * + */ + protected void updateClassType() { + if (TRACE) System.out.println("# updating class "+m_goe.getClassType().getName()); + + if (Proxy.isProxyClass(m_goe.getClassType())) { + if (TRACE) System.out.println("PROXY! original was " + ((RMIProxyLocal)Proxy.getInvocationHandler(((Proxy)m_goe.getValue()))).getOriginalClass().getName()); + m_ClassesLongName = new Vector(GenericObjectEditor.getClassesFromProperties(((RMIProxyLocal)Proxy.getInvocationHandler(((Proxy)m_goe.getValue()))).getOriginalClass().getName())); + } else { + m_ClassesLongName = new Vector(GenericObjectEditor.getClassesFromProperties(m_goe.getClassType().getName())); + } + m_ObjectChooser.setModel(new DefaultComboBoxModel(m_ClassesLongName)); + if (m_ClassesLongName.size() > 1) // testhu + add(m_ObjectChooser, BorderLayout.NORTH); + else + remove(m_ObjectChooser); + if (TRACE) System.out.println("# done updating class "+m_goe.getClassType().getName()); + } + + protected void updateChooser() { + String objectName = /*EVAHELP.cutClassName*/ (m_goe.getValue().getClass().getName()); + boolean found = false; + for (int i = 0; i < m_ObjectNames.getSize(); i++) { + if (TRACE) System.out.println("in updateChooser: looking at "+(String)m_ObjectNames.getElementAt(i)); + if (objectName.equals((String)m_ObjectNames.getElementAt(i))) { + found = true; + break; + } + } + if (!found) + m_ObjectNames.addElement(objectName); + m_ObjectChooser.getModel().setSelectedItem(objectName); + } + + + /** Updates the child property sheet, and creates if needed */ + public void updateChildPropertySheet() { + //System.err.println("GOE::updateChildPropertySheet()"); + // Set the object as the target of the propertysheet + m_ChildPropertySheet.setTarget(m_goe.getValue()); + // Adjust size of containing window if possible + if ((getTopLevelAncestor() != null) + && (getTopLevelAncestor() instanceof Window)) { + ((Window) getTopLevelAncestor()).pack(); + } + } + +// +// public void sourceChanged() { +// +// //System.out.println("SOURCESTATECHANGED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "); +// String className = (String) m_ObjectChooser.getSelectedItem(); +// +//// @todohannes: hack! ausbessern +// className = (String) m_ObjectChooser.getSelectedItem(); +// try { +// if (m_userdefclasses == true) { +// className = m_Object.getClass().getName(); +// Object[] para = new Object[] {}; +// Object n = (Object) CompileAndLoad.getInstanceFull( +// EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className), +// className, +// para); +// setObject(n); +// } +// else { +// System.out.println("m_userdefclasses == false!!!!!"); +// } +// } +// catch (Exception ex) { +// } +// +// } + + + /** + * When the chooser selection is changed, ensures that the Object + * is changed appropriately. + * + * @param e a value of type 'ItemEvent' + */ + + public void itemStateChanged(ItemEvent e) { + String className = (String)m_ObjectChooser.getSelectedItem(); +// m_editSourceBut.setEnabled(false); +// @todohannes: hack! ausbessern +// try { +// if (EvAClient.DYNAMICCLASSES_PROPERTIES.containsKey(className) && m_userdefclasses) { +// m_editSourceBut.setEnabled(true); +// } +// } catch (Exception e1) { +// System.out.println("Fehler !!! " + e1); +// } + +// @todohannes: hack! ausbessern +// +// if (this.m_SourceCodeEditorFrame != null) { +// m_SourceCodeEditorFrame.setVisible(false); +// m_SourceCodeEditorFrame = null; +// m_SourceCodeEditor = null; +// } + + if (TRACE) System.out.println("Event-Quelle: " + e.getSource().toString()); + if ((e.getSource() == m_ObjectChooser) && (e.getStateChange() == ItemEvent.SELECTED)){ + className = (String)m_ObjectChooser.getSelectedItem(); + try { +// if (EvAClient.DYNAMICCLASSES_PROPERTIES.containsKey(className) && m_userdefclasses) { +// Object[] para = new Object[] {}; +// String source = EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className); +// Object dummy = CompileAndLoad.getInstanceFull(source,className,para); +// setObject(dummy); +// } else { + if (TRACE) System.out.println(className); +// Object n = (Object)Class.forName(className, true, this.getClass().getClassLoader()).newInstance(); + Object n = (Object)Class.forName(className).newInstance(); + m_goe.setValue(n); + // TODO ? setObject(n); +// } + } catch (Exception ex) { + System.err.println("Exeption in itemStateChanged "+ex.getMessage()); + System.err.println("Classpath is " + System.getProperty("java.class.path")); + ex.printStackTrace(); + m_ObjectChooser.hidePopup(); + m_ObjectChooser.setSelectedIndex(0); + JOptionPane.showMessageDialog(this, + "Could not create an example of\n" + + className + "\n" + + "from the current classpath. Is it abstract? Is the default constructor missing?", + "GenericObjectEditor", + JOptionPane.ERROR_MESSAGE); + EVAHELP.getSystemPropertyString(); + } + } + } +} // end of inner class + diff --git a/src/eva2/gui/GenericAreaEditor.java b/src/eva2/gui/GenericAreaEditor.java index 8ca993be..9c94b387 100644 --- a/src/eva2/gui/GenericAreaEditor.java +++ b/src/eva2/gui/GenericAreaEditor.java @@ -11,9 +11,9 @@ import eva2.server.go.individuals.codings.gp.GPArea; * To change this template use Options | File Templates. */ public class GenericAreaEditor extends AbstractListSelectionEditor { - /** The GPArea that is to be edited*/ private GPArea m_AreaObject; + public GenericAreaEditor() { // compiled code } @@ -30,12 +30,12 @@ public class GenericAreaEditor extends AbstractListSelectionEditor { } @Override - protected boolean isElementAllowed(int i) { + protected boolean isElementSelected(int i) { return ((Boolean)m_AreaObject.getBlackList().get(i)).booleanValue(); } @Override - protected boolean performOnAction() { + protected boolean actionOnSelect() { /** This method checks the current BlackList and compiles it * to a new ReducedList. */ diff --git a/src/eva2/gui/GenericArrayEditor.java b/src/eva2/gui/GenericArrayEditor.java index 5a54fe4c..3e343f16 100644 --- a/src/eva2/gui/GenericArrayEditor.java +++ b/src/eva2/gui/GenericArrayEditor.java @@ -31,6 +31,7 @@ import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.beans.PropertyEditor; import java.lang.reflect.Array; +import java.lang.reflect.Method; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; @@ -64,6 +65,8 @@ implements PropertyEditor { private DefaultListModel m_ListModel; /** The property editor for the class we are editing */ private PropertyEditor m_ElementEditor; + /** Cheat to handle selectable lists as well */ + private PropertySelectableList selectableList = null; /** Click this to delete the selected array values */ private JButton m_DeleteBut = new JButton("Delete"); /** Click to add the current object configuration to the array */ @@ -84,6 +87,8 @@ implements PropertyEditor { } m_ElementList.setModel(m_ListModel); } + + if (selectableList!=null) selectableList.setObjects(modelToArray(m_ListModel)); m_Support.firePropertyChange("", null, null); } if (m_ElementList.getSelectedIndex() == -1) { @@ -103,6 +108,7 @@ implements PropertyEditor { m_ListModel.addElement(addObj); } m_ElementList.setModel(m_ListModel); + if (selectableList!=null) selectableList.setObjects(modelToArray(m_ListModel)); m_Support.firePropertyChange("", null, null); } catch (Exception ex) { JOptionPane.showMessageDialog(GenericArrayEditor.this,"Could not create an object copy",null,JOptionPane.ERROR_MESSAGE); @@ -110,7 +116,15 @@ implements PropertyEditor { } } }; - + + private Object[] modelToArray(DefaultListModel listModel) { + Object[] os= new Object[listModel.size()]; + for (int i=0; i m_ClassType; private GOEPanel m_EditorComponent; private boolean m_Enabled = true; - /** - * - */ - public class GOEPanel extends JPanel implements ItemListener { - /** The chooser component */ - private JComboBox m_ObjectChooser; - /** The component that performs classifier customization */ - private PropertySheetPanel m_ChildPropertySheet; - /** The model containing the list of names to select from */ - private DefaultComboBoxModel m_ObjectNames; - /** Open object from disk */ - private JButton m_OpenBut; - /** Save object to disk */ - private JButton m_SaveBut; - /** ok button */ - public JButton m_okBut; - /** cancel button */ - private JButton m_cancelBut; - /** edit source button */ -// private JButton m_editSourceBut; - /** The filechooser for opening and saving object files */ - private JFileChooser m_FileChooser; - /** Creates the GUI editor component */ - private Vector m_ClassesLongName; -// private String[] m_ClassesShortName; -// private SourceCodeEditor m_SourceCodeEditor; -// private PropertyDialog m_SourceCodeEditorFrame; - - /** - * - */ - public GOEPanel() { - //System.out.println("GOEPanel.Constructor !!"); - if (!(Proxy.isProxyClass(m_Object.getClass()))) m_Backup = copyObject(m_Object); - m_ObjectNames = new DefaultComboBoxModel(new String [0]); - m_ObjectChooser = new JComboBox(m_ObjectNames); - m_ObjectChooser.setEditable(false); - m_ChildPropertySheet = new PropertySheetPanel(); - m_ChildPropertySheet.addPropertyChangeListener( - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) { - m_Support.firePropertyChange("", m_Backup, m_Object); - } - }); - m_OpenBut = new JButton("Open..."); - m_OpenBut.setToolTipText("Load a configured object"); - m_OpenBut.setEnabled(true); - m_OpenBut.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - Object object = openObject(); - if (object != null) { - // setValue takes care of: Making sure obj is of right type, - // and firing property change. - setValue(object); - // Need a second setValue to get property values filled in OK. - // Not sure why. - setValue(object); // <- Hannes ?!?!? - } - } - }); - - m_SaveBut = new JButton("Save..."); - m_SaveBut.setToolTipText("Save the current configured object"); - m_SaveBut.setEnabled(true); - m_SaveBut.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - saveObject(m_Object); - } - }); -// -// m_editSourceBut = new JButton("Edit Source"); -// m_editSourceBut.setToolTipText("Edit the Source"); -// m_editSourceBut.setEnabled(false); -// m_editSourceBut.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// m_editSourceBut.setEnabled(false); -// m_SourceCodeEditor = new SourceCodeEditor(); -// String className = m_Object.getClass().getName(); -// m_SourceCodeEditor.editSource(EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className)); -// m_SourceCodeEditorFrame = new PropertyDialog(m_SourceCodeEditor, "test", 50, 50); -// m_SourceCodeEditorFrame.pack(); -// m_SourceCodeEditorFrame.addWindowListener(new WindowAdapter() { -// public void windowClosing (WindowEvent e) { -// m_SourceCodeEditor = null; -// m_editSourceBut.setEnabled(true); -// } -// }); -// m_SourceCodeEditor.addPropertyChangeListener(new -// PropertyChangeListener() { -// public void propertyChange(PropertyChangeEvent evt) { -// sourceChanged(); -// } -// } -// ); -// } -// }); - - m_okBut = new JButton("OK"); - m_okBut.setEnabled(true); - m_okBut.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - m_Backup = copyObject(m_Object); - if ((getTopLevelAncestor() != null) && (getTopLevelAncestor() instanceof Window)) { - Window w = (Window) getTopLevelAncestor(); - w.dispose(); - } - } - }); - - m_cancelBut = new JButton("Cancel"); - m_cancelBut.setEnabled(false); - m_cancelBut.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (m_Backup != null) { - m_Object = copyObject(m_Backup); - setObject(m_Object); - updateClassType(); - updateChooser(); - updateChildPropertySheet(); - } - if ((getTopLevelAncestor() != null) - && (getTopLevelAncestor() instanceof Window)) { - Window w = (Window) getTopLevelAncestor(); - w.dispose(); - } - } - }); - - setLayout(new BorderLayout()); - add(m_ObjectChooser, BorderLayout.NORTH); // important - add(m_ChildPropertySheet, BorderLayout.CENTER); - // Since we resize to the size of the property sheet, a scrollpane isn't - // typically needed - // add(new JScrollPane(m_ChildPropertySheet), BorderLayout.CENTER); - - JPanel okcButs = new JPanel(); - okcButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - okcButs.setLayout(new GridLayout(1, 4, 5, 5)); - okcButs.add(m_OpenBut); - okcButs.add(m_SaveBut); - okcButs.add(m_okBut); -// okcButs.add(m_editSourceBut); - //okcButs.add(m_cancelBut); - add(okcButs, BorderLayout.SOUTH); - - if (m_ClassType != null) { - updateClassType(); - updateChooser(); - updateChildPropertySheet(); - } - m_ObjectChooser.addItemListener(this); - } - - /** - * Opens an object from a file selected by the user. - * - * @return the loaded object, or null if the operation was cancelled - */ - protected Object openObject() { - if (m_FileChooser == null) { - createFileChooser(); - } - int returnVal = m_FileChooser.showOpenDialog(this); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File selected = m_FileChooser.getSelectedFile(); - try { - ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); - Object obj = oi.readObject(); - oi.close(); - if (!m_ClassType.isAssignableFrom(obj.getClass())) { - throw new Exception("Object not of type: " + m_ClassType.getName()); - } - return obj; - } catch (Exception ex) { - JOptionPane.showMessageDialog(this, - "Couldn't read object: " - + selected.getName() - + "\n" + ex.getMessage(), - "Open object file", - JOptionPane.ERROR_MESSAGE); - } - } - return null; - } - - /** Saves the current object to a file selected by the user. - * @param object The object to save. - */ - protected void saveObject(Object object) { - - if (m_FileChooser == null) { - createFileChooser(); - } - int returnVal = m_FileChooser.showSaveDialog(this); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File sFile = m_FileChooser.getSelectedFile(); - try { - ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); - oo.writeObject(object); - oo.close(); - } catch (Exception ex) { - JOptionPane.showMessageDialog(this, - "Couldn't write to file: " - + sFile.getName() - + "\n" + ex.getMessage(), - "Save object", - JOptionPane.ERROR_MESSAGE); - } - } - } - protected void createFileChooser() { - m_FileChooser = new JFileChooser(new File("/resources")); - m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - } - - /** - * Makes a copy of an object using serialization - * @param source the object to copy - * @return a copy of the source object - */ - protected Object copyObject(Object source) { - Object result = null; - try { - SerializedObject so = new SerializedObject(source); - result = so.getObject(); - } catch (Exception ex) { - System.err.println("GenericObjectEditor: Problem making backup object"); - System.err.println(source.getClass().getName()); - ex.printStackTrace(); - } - return result; - } - - /** - * This is used to hook an action listener to the ok button - * @param a The action listener. - */ - public void addOkListener(ActionListener a) { - m_okBut.addActionListener(a); - } - - /** - * This is used to hook an action listener to the cancel button - * @param a The action listener. - */ - public void addCancelListener(ActionListener a) { - m_cancelBut.addActionListener(a); - } - - /** - * This is used to remove an action listener from the ok button - * @param a The action listener - */ - public void removeOkListener(ActionListener a) { - m_okBut.removeActionListener(a); - } - - /** - * This is used to remove an action listener from the cancel button - * @param a The action listener - */ - public void removeCancelListener(ActionListener a) { - m_cancelBut.removeActionListener(a); - } - /** - * - */ - protected void updateClassType() { - if (TRACE) System.out.println("# updating class "+m_ClassType.getName()); - - if (Proxy.isProxyClass(m_ClassType)) { - if (TRACE) System.out.println("PROXY! original was " + ((RMIProxyLocal)Proxy.getInvocationHandler(((Proxy)m_Object))).getOriginalClass().getName()); - m_ClassesLongName = new Vector(getClassesFromProperties(((RMIProxyLocal)Proxy.getInvocationHandler(((Proxy)m_Object))).getOriginalClass().getName())); - } else { - m_ClassesLongName = new Vector(getClassesFromProperties(m_ClassType.getName())); - } - m_ObjectChooser.setModel(new DefaultComboBoxModel(m_ClassesLongName)); - if (m_ClassesLongName.size() > 1) // testhu - add(m_ObjectChooser, BorderLayout.NORTH); - else - remove(m_ObjectChooser); - if (TRACE) System.out.println("# done updating class "+m_ClassType.getName()); - } - - protected void updateChooser() { - String objectName = /*EVAHELP.cutClassName*/ (m_Object.getClass().getName()); - boolean found = false; - for (int i = 0; i < m_ObjectNames.getSize(); i++) { - if (TRACE) System.out.println("in updateChooser: looking at "+(String)m_ObjectNames.getElementAt(i)); - if (objectName.equals((String)m_ObjectNames.getElementAt(i))) { - found = true; - break; - } - } - if (!found) - m_ObjectNames.addElement(objectName); - m_ObjectChooser.getModel().setSelectedItem(objectName); - } - - - /** Updates the child property sheet, and creates if needed */ - public void updateChildPropertySheet() { - //System.err.println("GOE::updateChildPropertySheet()"); - // Set the object as the target of the propertysheet - m_ChildPropertySheet.setTarget(m_Object); - // Adjust size of containing window if possible - if ((getTopLevelAncestor() != null) - && (getTopLevelAncestor() instanceof Window)) { - ((Window) getTopLevelAncestor()).pack(); - } - } - -// -// public void sourceChanged() { -// -// //System.out.println("SOURCESTATECHANGED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "); -// String className = (String) m_ObjectChooser.getSelectedItem(); -// -//// @todohannes: hack! ausbessern -// className = (String) m_ObjectChooser.getSelectedItem(); -// try { -// if (m_userdefclasses == true) { -// className = m_Object.getClass().getName(); -// Object[] para = new Object[] {}; -// Object n = (Object) CompileAndLoad.getInstanceFull( -// EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className), -// className, -// para); -// setObject(n); -// } -// else { -// System.out.println("m_userdefclasses == false!!!!!"); -// } -// } -// catch (Exception ex) { -// } -// -// } - - - /** - * When the chooser selection is changed, ensures that the Object - * is changed appropriately. - * - * @param e a value of type 'ItemEvent' - */ - - public void itemStateChanged(ItemEvent e) { - String className = (String)m_ObjectChooser.getSelectedItem(); -// m_editSourceBut.setEnabled(false); -// @todohannes: hack! ausbessern -// try { -// if (EvAClient.DYNAMICCLASSES_PROPERTIES.containsKey(className) && m_userdefclasses) { -// m_editSourceBut.setEnabled(true); -// } -// } catch (Exception e1) { -// System.out.println("Fehler !!! " + e1); -// } - -// @todohannes: hack! ausbessern -// -// if (this.m_SourceCodeEditorFrame != null) { -// m_SourceCodeEditorFrame.setVisible(false); -// m_SourceCodeEditorFrame = null; -// m_SourceCodeEditor = null; -// } - - if (TRACE) System.out.println("Event-Quelle: " + e.getSource().toString()); - if ((e.getSource() == m_ObjectChooser) && (e.getStateChange() == ItemEvent.SELECTED)){ - className = (String)m_ObjectChooser.getSelectedItem(); - try { -// if (EvAClient.DYNAMICCLASSES_PROPERTIES.containsKey(className) && m_userdefclasses) { -// Object[] para = new Object[] {}; -// String source = EvAClient.DYNAMICCLASSES_PROPERTIES.getProperty(className); -// Object dummy = CompileAndLoad.getInstanceFull(source,className,para); -// setObject(dummy); -// } else { - if (TRACE) System.out.println(className); -// Object n = (Object)Class.forName(className, true, this.getClass().getClassLoader()).newInstance(); - Object n = (Object)Class.forName(className).newInstance(); - setObject(n); -// } - } catch (Exception ex) { - System.err.println("Exeption in itemStateChanged "+ex.getMessage()); - System.err.println("Classpath is " + System.getProperty("java.class.path")); - ex.printStackTrace(); - m_ObjectChooser.hidePopup(); - m_ObjectChooser.setSelectedIndex(0); - JOptionPane.showMessageDialog(this, - "Could not create an example of\n" - + className + "\n" - + "from the current classpath. Is it abstract? Is the default constructor missing?", - "GenericObjectEditor", - JOptionPane.ERROR_MESSAGE); - EVAHELP.getSystemPropertyString(); - } - } - } - } // end of inner class /** * Read the classes available for user selection from the properties or the classpath respectively @@ -677,6 +278,10 @@ public class GenericObjectEditor implements PropertyEditor { } + public Class getClassType() { + return m_ClassType; + } + /** * Sets the current object to be the default, taken as the first item in * the chooser @@ -807,7 +412,7 @@ public class GenericObjectEditor implements PropertyEditor { if (methods[i].getName().equalsIgnoreCase("getName")) getNameMethod = i; } } catch (IntrospectionException ex) { - System.out.println("PropertySheetPanel.setTarget(): Couldn't introspect"); + System.err.println("PropertySheetPanel.setTarget(): Couldn't introspect"); return; } if (getNameMethod >= 0) { @@ -878,7 +483,7 @@ public class GenericObjectEditor implements PropertyEditor { */ public Component getCustomEditor() { if (m_EditorComponent == null) - m_EditorComponent = new GOEPanel(); + m_EditorComponent = new GOEPanel(m_Object, m_Backup, m_Support, this); return m_EditorComponent; } /** @@ -886,7 +491,7 @@ public class GenericObjectEditor implements PropertyEditor { */ public void disableOK() { if (m_EditorComponent == null) - m_EditorComponent = new GOEPanel(); + m_EditorComponent = new GOEPanel(m_Object, m_Backup, m_Support, this); m_EditorComponent.m_okBut.setEnabled(false); } public void addPropertyChangeListener(PropertyChangeListener l) { diff --git a/src/eva2/gui/GenericObjectListSelectionEditor.java b/src/eva2/gui/GenericObjectListSelectionEditor.java index 21e0b86d..37b10226 100644 --- a/src/eva2/gui/GenericObjectListSelectionEditor.java +++ b/src/eva2/gui/GenericObjectListSelectionEditor.java @@ -7,9 +7,7 @@ package eva2.gui; public class GenericObjectListSelectionEditor extends AbstractListSelectionEditor { private PropertySelectableList objList; - public GenericObjectListSelectionEditor() { - // compiled code - } + public GenericObjectListSelectionEditor() {} @Override protected int getElementCount() { @@ -22,12 +20,12 @@ public class GenericObjectListSelectionEditor extends AbstractListSelectionEdito } @Override - protected boolean isElementAllowed(int i) { + protected boolean isElementSelected(int i) { return objList.isSelected(i); } @Override - protected boolean performOnAction() { + protected boolean actionOnSelect() { boolean changed=false; for (int i = 0; i < this.m_BlackCheck.length; i++) { if (objList.isSelected(i)!=this.m_BlackCheck[i].isSelected()) { diff --git a/src/eva2/gui/GenericSelectableArrayEditor.java b/src/eva2/gui/GenericSelectableArrayEditor.java new file mode 100644 index 00000000..9a4c3718 --- /dev/null +++ b/src/eva2/gui/GenericSelectableArrayEditor.java @@ -0,0 +1,83 @@ +package eva2.gui; +/* + * Title: EvA2 + * Description: + * Copyright: Copyright (c) 2003 + * Company: University of Tuebingen, Computer Architecture + * @author Holger Ulmer, Felix Streichert, Hannes Planatscher + * @version: $Revision: 235 $ + * $Date: 2007-11-08 13:53:51 +0100 (Thu, 08 Nov 2007) $ + * $Author: mkron $ + */ +/*==========================================================================* + * IMPORTS + *==========================================================================*/ + + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Insets; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.beans.PropertyEditor; +import java.lang.reflect.Array; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.ListCellRenderer; +import javax.swing.SwingConstants; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +import eva2.tools.EVAHELP; +import eva2.tools.SelectedTag; +/*==========================================================================* + * CLASS DECLARATION + *==========================================================================*/ +public class GenericSelectableArrayEditor extends GenericArrayEditor { + protected JCheckBox[] m_BlackCheck; + + public static void main(String [] args) { + try { + java.beans.PropertyEditorManager.registerEditor(SelectedTag.class,TagEditor.class); + java.beans.PropertyEditorManager.registerEditor(int [].class,GenericArrayEditor.class); + java.beans.PropertyEditorManager.registerEditor(double [].class,GenericArrayEditor.class); + GenericArrayEditor editor = new GenericArrayEditor(); + + + int[] initial = { 3,45, 7}; + editor.setValue(initial); + PropertyDialog pd = new PropertyDialog(editor,EVAHELP.cutClassName(editor.getClass().getName()) + , 100, 100); + pd.setSize(200,200); + pd.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + editor.setValue(initial); + //ce.validate(); + } catch (Exception ex) { + ex.printStackTrace(); + System.err.println(ex.getMessage()); + } + } +} + diff --git a/src/eva2/gui/PropertyEditorProvider.java b/src/eva2/gui/PropertyEditorProvider.java index de1f2c92..11f16b83 100644 --- a/src/eva2/gui/PropertyEditorProvider.java +++ b/src/eva2/gui/PropertyEditorProvider.java @@ -18,6 +18,7 @@ import sun.beans.editors.StringEditor; import eva2.server.go.InterfaceTerminator; import eva2.server.go.individuals.codings.gp.GPArea; import eva2.tools.SelectedTag; +import eva2.tools.StringSelection; public class PropertyEditorProvider { final static boolean TRACE = false; @@ -162,6 +163,7 @@ public class PropertyEditorProvider { // The Editor for the new GO + PropertyEditorManager.registerEditor(StringSelection.class , StringSelectionEditor.class); // // Traveling Salesman problem PropertyEditorManager.registerEditor(GPArea.class , GenericAreaEditor.class); PropertyEditorManager.registerEditor(PropertyDoubleArray.class , GenericDoubleArrayEditor.class); @@ -175,6 +177,6 @@ public class PropertyEditorProvider { PropertyEditorManager.registerEditor(PropertyOptimizationObjectives.class , GenericOptimizationObjectivesEditor.class); PropertyEditorManager.registerEditor(PropertyOptimizationObjectivesWithParam.class , GenericOptimizationObjectivesWithParamEditor.class); PropertyEditorManager.registerEditor(eva2.gui.MultiLineString.class, eva2.gui.MultiLineStringEditor.class); - PropertyEditorManager.registerEditor(PropertySelectableList.class, GenericObjectListSelectionEditor.class); + PropertyEditorManager.registerEditor(PropertySelectableList.class, GenericArrayEditor.class); } } diff --git a/src/eva2/gui/PropertySelectableList.java b/src/eva2/gui/PropertySelectableList.java index 6fb35a41..bd97e958 100644 --- a/src/eva2/gui/PropertySelectableList.java +++ b/src/eva2/gui/PropertySelectableList.java @@ -10,19 +10,23 @@ import java.beans.PropertyChangeSupport; * Time: 15:04:05 * To change this template use File | Settings | File Templates. */ -public class PropertySelectableList implements java.io.Serializable { +public class PropertySelectableList implements java.io.Serializable { - private Object[] m_Objects; + private T[] m_Objects; private boolean[] m_Selection; - private PropertyChangeSupport m_Support; + private PropertyChangeSupport m_Support = new PropertyChangeSupport(this); - public PropertySelectableList() { +// public PropertySelectableList() { +// } +// + public PropertySelectableList(T[] initial) { + m_Objects = initial; + m_Selection = new boolean[initial.length]; } - public PropertySelectableList(PropertySelectableList b) { + public PropertySelectableList(PropertySelectableList b) { if (b.m_Objects != null) { - this.m_Objects = new Object[b.m_Objects.length]; - System.arraycopy(b.m_Objects, 0, this.m_Objects, 0, this.m_Objects.length); + this.m_Objects = b.m_Objects.clone(); } if (b.m_Selection != null) { this.m_Selection = new boolean[b.m_Selection.length]; @@ -31,15 +35,16 @@ public class PropertySelectableList implements java.io.Serializable { } public Object clone() { - return (Object) new PropertySelectableList(this); + return (Object) new PropertySelectableList(this); } - public void setObjects(Object[] o) { + public void setObjects(T[] o) { this.m_Objects = o; this.m_Selection = new boolean[o.length]; m_Support.firePropertyChange("PropertySelectableList", null, this); } - public Object[] getStrings() { + + public T[] getObjects() { return this.m_Objects; } @@ -64,7 +69,7 @@ public class PropertySelectableList implements java.io.Serializable { else return m_Objects.length; } - public Object get(int i) { + public T get(int i) { return m_Objects[i]; } @@ -78,6 +83,27 @@ public class PropertySelectableList implements java.io.Serializable { m_Support.firePropertyChange("PropertySelectableList", null, this); } +// /** +// * Append an object at the end of the list and immediately select it. +// * @param o +// */ +// public void addObject(T o) { +// if (m_Objects==null) { +// m_Objects=(T[]) new Object[]{o}; +// m_Selection = new boolean[1]; +// } else { +// T[] newOs = (T[])new Object[m_Objects.length+1]; +// boolean[] newSs = new boolean[m_Selection.length+1]; +// System.arraycopy(m_Objects, 0, newOs, 0, this.m_Objects.length); +// System.arraycopy(m_Selection, 0, newSs, 0, this.m_Selection.length); +// newOs[m_Objects.length]=o; +// newSs[m_Objects.length]=true; +// m_Objects=newOs; +// m_Selection=newSs; +// } +// m_Support.firePropertyChange("PropertySelectableList", null, this); +// } + public void addPropertyChangeListener(PropertyChangeListener l) { m_Support.addPropertyChangeListener(l); } diff --git a/src/eva2/gui/PropertySheetPanel.java b/src/eva2/gui/PropertySheetPanel.java index e169988c..591bb7f2 100644 --- a/src/eva2/gui/PropertySheetPanel.java +++ b/src/eva2/gui/PropertySheetPanel.java @@ -33,10 +33,8 @@ import java.beans.PropertyChangeSupport; import java.beans.PropertyDescriptor; import java.beans.PropertyEditor; import java.beans.PropertyVetoException; -import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Arrays; import javax.swing.BorderFactory; import javax.swing.JButton; @@ -46,9 +44,7 @@ import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.SwingConstants; -import eva2.gui.GenericObjectEditor.GOEPanel; import eva2.tools.EVAHELP; -import eva2.tools.Mathematics; import eva2.tools.StringTools; /*==========================================================================* * CLASS DECLARATION @@ -761,6 +757,8 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener o = getter.invoke(m_Target, args); } catch (Exception ex) { o = null; + System.err.println(ex.getMessage()); + ex.printStackTrace(); } if (TRACE) System.out.println("# cmp " + BeanInspector.toString(o) + "\n# vs. " + BeanInspector.toString(m_Values[i])); if (o == m_Values[i] && (BeanInspector.isJavaPrimitive(o.getClass()))) { diff --git a/src/eva2/gui/StringSelectionEditor.java b/src/eva2/gui/StringSelectionEditor.java new file mode 100644 index 00000000..2399c20f --- /dev/null +++ b/src/eva2/gui/StringSelectionEditor.java @@ -0,0 +1,76 @@ +package eva2.gui; + +import eva2.tools.StringSelection; + +public class StringSelectionEditor extends AbstractListSelectionEditor { + StringSelection strs; + + public StringSelectionEditor() { + strs = new StringSelection(new String[]{}); + } + + @Override + protected boolean actionOnSelect() { + for (int i = 0; i < this.m_BlackCheck.length; i++) { + strs.setSelected(i, this.m_BlackCheck[i].isSelected()); + } + return true; + } + + @Override + protected int getElementCount() { + return strs.getLength(); + } + + @Override + protected String getElementName(int i) { + return strs.getElement(i); + } + + @Override + public Object getValue() { + return strs; + } + + @Override + protected boolean isElementSelected(int i) { + return strs.isSelected(i); + } + + @Override + protected boolean setObject(Object o) { + if (o instanceof StringSelection) { + strs = (StringSelection) o; +// m_AreaObject.addPropertyChangeListener(this); + return true; + } else return false; + } + + @Override + public String getName() { + return "StringSelection"; + } + + @Override + public String getAsText() { + StringBuffer sbuf = new StringBuffer("{"); + boolean first = true; + for (int i=0; i getPlotDescriptions(); - public SelectedTag getPlotData(); - public void setPlotData(SelectedTag newMethod); - +// public SelectedTag getPlotData(); +// public void setPlotData(SelectedTag newMethod); + + public StringSelection getGraphSelection(); + public void setGraphSelection(StringSelection v); + public String getResultFilePrefix(); public void SetResultFilePrefix(String x); diff --git a/src/eva2/server/stat/StatisticsWithGUI.java b/src/eva2/server/stat/StatisticsWithGUI.java index 2190d1a2..fe55cf15 100644 --- a/src/eva2/server/stat/StatisticsWithGUI.java +++ b/src/eva2/server/stat/StatisticsWithGUI.java @@ -32,6 +32,7 @@ import eva2.server.EvAServer; import eva2.server.go.PopulationInterface; import eva2.server.go.problems.InterfaceAdditionalPopulationInformer; import eva2.tools.EVAERROR; +import eva2.tools.StringSelection; /*==========================================================================* * CLASS DECLARATION @@ -54,6 +55,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl private MainAdapterClient m_MainAdapterClient; // the connection to the client MainAdapter private JTextoutputFrameInterface m_ProxyPrinter; + private StringSelection lastGraphSelection; ////////////// protected static String m_MyHostName = null; @@ -103,7 +105,10 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl public synchronized void startOptPerformed(String infoString, int runNumber, Object goParams) { super.startOptPerformed(infoString, runNumber, goParams); m_GraphInfoString = infoString; - + if (runNumber == 0) { + lastGraphSelection = (StringSelection)m_StatsParams.getGraphSelection().clone(); + } + // m_TextCounter = m_StatisticsParameter.GetTextoutput(); m_PlotCounter = m_StatsParams.GetPlotoutput(); if ((m_FitnessFrame!=null) && (m_FitnessFrame[0]!=null)) { @@ -151,7 +156,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl } public void maybeShowProxyPrinter() { - if (m_ProxyPrinter != null) m_ProxyPrinter.setShow((m_StatsParams).isShowTextOutput()); + if (m_ProxyPrinter != null) m_ProxyPrinter.setShow(m_StatsParams.isShowTextOutput()); } protected void initPlots(List description) { @@ -198,11 +203,11 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl private void plotFitnessPoint(int graph, int subGraph, int x, double y) { if (m_FitnessGraph == null) { - EVAERROR.WARNING("fitness graph is null!"); + EVAERROR.WARNING("fitness graph is null! (StatisticsWithGUI)"); return; } if (graph >= m_FitnessGraph.length || subGraph >= m_FitnessGraph[graph].length) { - EVAERROR.WARNING("tried to plot to invalid graph!"); + EVAERROR.WARNING("tried to plot to invalid graph! (StatisticsWithGUI)"); return; } boolean isValidGraph = m_FitnessFrame[graph].isValid(); @@ -219,40 +224,39 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl // Plots m_PlotCounter--; - int fitnessplot_setting = m_StatsParams.getPlotData().getSelectedTag().getID(); +// int fitnessplot_setting = m_StatsParams.getPlotData().getSelectedTag().getID(); if (m_PlotCounter == 0) { m_PlotCounter = m_StatsParams.GetPlotoutput(); - boolean doPlotBest = (fitnessplot_setting == StatsParameter.PLOT_BEST) - || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_WORST) - || (fitnessplot_setting == StatsParameter.PLOT_CURBEST_AND_RUNBEST) - || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_MEASURES); - boolean doPlotWorst = (fitnessplot_setting == StatsParameter.PLOT_WORST) - || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_WORST); - boolean doPlotMeasures = (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_MEASURES); - boolean doPlotBestFeasible = false; - // TODO <-- das hier besser? -// Oder erstmal ganz weg und dafür gesamtergebnis berechnen (textfenster) -// ZB: wie oft wurde feasible sol. gefunden (analog convergence counter) -// Durchschnitt: erste feasible sol., fitness der feasible sol... +// boolean doPlotBest = (fitnessplot_setting == StatsParameter.PLOT_BEST) +// || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_WORST) +// || (fitnessplot_setting == StatsParameter.PLOT_CURBEST_AND_RUNBEST) +// || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_MEASURES); +// boolean doPlotWorst = (fitnessplot_setting == StatsParameter.PLOT_WORST) +// || (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_WORST); +// boolean doPlotMeasures = (fitnessplot_setting == StatsParameter.PLOT_BEST_AND_MEASURES); + + boolean doPlotCurrentBest = GraphSelectionEnum.doPlotCurrentBest(lastGraphSelection); + boolean doPlotRunBest = GraphSelectionEnum.doPlotRunBest(lastGraphSelection); + boolean doPlotWorst= GraphSelectionEnum.doPlotWorst(lastGraphSelection); + boolean doPlotBestFeasible = GraphSelectionEnum.doPlotBestFeasible(lastGraphSelection); + boolean doPlotAvgDist= GraphSelectionEnum.doPlotAvgDist(lastGraphSelection); + boolean doPlotMaxPopDist= GraphSelectionEnum.doPlotMaxPopDist(lastGraphSelection); + int subGraph=0; - if (doPlotBest) { - plotFitnessPoint(0, subGraph++, functionCalls, currentBestFit[0]); - if (fitnessplot_setting == StatsParameter.PLOT_CURBEST_AND_RUNBEST) plotFitnessPoint(0, subGraph++, functionCalls, bestRunIndividual.getFitness()[0]); - } - if (doPlotWorst) { - // schlechteste Fitness plotten - m_PlotCounter = m_StatsParams.GetPlotoutput(); + if (doPlotCurrentBest) plotFitnessPoint(0, subGraph++, functionCalls, currentBestFit[0]); + if (doPlotRunBest) plotFitnessPoint(0, subGraph++, functionCalls, bestRunIndividual.getFitness()[0]); + + if (doPlotWorst) {// schlechteste Fitness plotten if (currentWorstFit == null) { System.err.println("m_WorstFitness==null in plotStatisticsPerformed"); return; } plotFitnessPoint(0, subGraph++ , functionCalls, currentWorstFit[0]); } - if (doPlotMeasures) { - plotFitnessPoint(0, subGraph++, functionCalls, avgPopDist); - plotFitnessPoint(0, subGraph++, functionCalls, maxPopDist); - } + if (doPlotAvgDist) plotFitnessPoint(0, subGraph++, functionCalls, avgPopDist); + if (doPlotMaxPopDist) plotFitnessPoint(0, subGraph++, functionCalls, maxPopDist); + if (doPlotBestFeasible && currentBestFeasibleFit!=null) { plotFitnessPoint(0, subGraph++, functionCalls, currentBestFeasibleFit[0]); } diff --git a/src/eva2/server/stat/StatsParameter.java b/src/eva2/server/stat/StatsParameter.java index 84c8e262..24e69a5f 100644 --- a/src/eva2/server/stat/StatsParameter.java +++ b/src/eva2/server/stat/StatsParameter.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import eva2.gui.GenericObjectEditor; import eva2.tools.SelectedTag; import eva2.tools.Serializer; +import eva2.tools.StringSelection; import eva2.tools.Tag; @@ -29,19 +30,19 @@ import eva2.tools.Tag; * */ public class StatsParameter implements InterfaceStatisticsParameter, Serializable { - public final static int PLOT_BEST = 0; - public final static int PLOT_WORST = 1; - public final static int PLOT_BEST_AND_WORST = 2; - public final static int PLOT_BEST_AND_MEASURES = 3; - public final static int PLOT_CURBEST_AND_RUNBEST = 4; - public final static Tag[] TAGS_PLOT_FITNESS = { - new Tag(PLOT_BEST, "plot best fitness"), - new Tag(PLOT_WORST, "plot worst fitness"), - new Tag(PLOT_BEST_AND_WORST, "both best and worst"), - new Tag(PLOT_BEST_AND_MEASURES, "both best and population measures"), - new Tag(PLOT_CURBEST_AND_RUNBEST, "current best and best of run") - }; - +// public final static int PLOT_BEST = 0; +// public final static int PLOT_WORST = 1; +// public final static int PLOT_BEST_AND_WORST = 2; +// public final static int PLOT_BEST_AND_MEASURES = 3; +// public final static int PLOT_CURBEST_AND_RUNBEST = 4; +// public final static Tag[] TAGS_PLOT_FITNESS = { +// new Tag(PLOT_BEST, "plot best fitness"), +// new Tag(PLOT_WORST, "plot worst fitness"), +// new Tag(PLOT_BEST_AND_WORST, "both best and worst"), +// new Tag(PLOT_BEST_AND_MEASURES, "both best and population measures"), +// new Tag(PLOT_CURBEST_AND_RUNBEST, "current best and best of run") +// }; + public final static int VERBOSITY_NONE = 0; public final static int VERBOSITY_FINAL = 1; public final static int VERBOSITY_KTH_IT = 2; @@ -54,7 +55,7 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl SelectedTag outputTo = new SelectedTag("File (current dir.)", "Text-window", "Both file and text-window"); private int verboK = 10; - private int m_PlotFitness = PLOT_BEST; +// private int m_PlotFitness = PLOT_BEST; private int m_Textoutput = 0; private int m_Plotoutput = 1; private int m_MultiRuns = 1; @@ -65,6 +66,8 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl private boolean showAdditionalProblemInfo = false; private double m_ConvergenceRateThreshold=0.001; + private StringSelection graphSel = new StringSelection(GraphSelectionEnum.currentBest); + /** * */ @@ -101,24 +104,30 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl * @return a list of String arrays describing the selected plot options */ public ArrayList getPlotDescriptions() { - ArrayList desc = new ArrayList(); - switch (getPlotData().getSelectedTagID()) { - case StatsParameter.PLOT_BEST_AND_WORST: - desc.add(new String[] {"Best", "Worst"}); - break; - case StatsParameter.PLOT_BEST: - desc.add(new String[] {"Best"}); - break; - case StatsParameter.PLOT_WORST: - desc.add(new String[] {"Worst"}); - break; - case StatsParameter.PLOT_BEST_AND_MEASURES: - desc.add(new String[] {"Best", "AvgDist", "MaxDist"}); - break; - case StatsParameter.PLOT_CURBEST_AND_RUNBEST: - desc.add(new String[] {"Cur.Best", "Run Best"}); - break; } - return desc; + ArrayList desc = new ArrayList(); + for (int i=0; i alist = new ArrayList(); + alist.add(desc.toArray(new String[desc.size()])); + return alist; +// switch (getPlotData().getSelectedTagID()) { +// case StatsParameter.PLOT_BEST_AND_WORST: +// desc.add(new String[] {"Best", "Worst"}); +// break; +// case StatsParameter.PLOT_BEST: +// desc.add(new String[] {"Best"}); +// break; +// case StatsParameter.PLOT_WORST: +// desc.add(new String[] {"Worst"}); +// break; +// case StatsParameter.PLOT_BEST_AND_MEASURES: +// desc.add(new String[] {"Best", "AvgDist", "MaxDist"}); +// break; +// case StatsParameter.PLOT_CURBEST_AND_RUNBEST: +// desc.add(new String[] {"Cur.Best", "Run Best"}); +// break; } +// return desc; } /** @@ -136,7 +145,7 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl m_useStatPlot = Source.m_useStatPlot; m_Textoutput = Source.m_Textoutput; m_Plotoutput = Source.m_Plotoutput; - m_PlotFitness = Source.m_PlotFitness; +// m_PlotFitness = Source.m_PlotFitness; m_MultiRuns = Source.m_MultiRuns; m_ResultFilePrefix = Source.m_ResultFilePrefix; verboK = Source.verboK; @@ -251,35 +260,6 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl return "Plotting each fitness graph separate if multiruns > 1."; } - /** - * - */ - public SelectedTag getPlotData() { - return new SelectedTag(m_PlotFitness, TAGS_PLOT_FITNESS); - } - - /** - * - */ - public void setPlotData(SelectedTag newMethod) { - m_PlotFitness = newMethod.getSelectedTag().getID(); - } - - /** - * - */ - public String plotDataTipText() { - return "The data to be plotted: best fitness, worst fitness or average/max distance in population."; - } - -// /** -// * -// */ -// public String plotObjectivesTipText() { -// return "The individual of which the objectives are plotted."; -// } - - /** * */ @@ -413,4 +393,15 @@ public class StatsParameter implements InterfaceStatisticsParameter, Serializabl return "Set the output destination; to deactivate output, set verbosity to none."; } + public StringSelection getGraphSelection() { + return graphSel; + } + + public void setGraphSelection(StringSelection v) { + graphSel = v; + } + + public String graphSelectionTipText() { + return "Select the graphs to be plotted."; + } } \ No newline at end of file diff --git a/src/eva2/tools/Mathematics.java b/src/eva2/tools/Mathematics.java index ef1854d7..649417d6 100644 --- a/src/eva2/tools/Mathematics.java +++ b/src/eva2/tools/Mathematics.java @@ -819,6 +819,18 @@ public class Mathematics { return true; } + /** + * Check whether the given value lies within the interval in every dimension. + * + * @param x + * @param range + * @return true if the vector lies within the range, else false + */ + public static boolean isInRange(double v, double lower, double upper) { + if (vupper)) return false; + return true; + } + /** * Return the vector of interval length values in any dimension. * ret[i]=range[i][1]-range[i][0]; @@ -860,6 +872,21 @@ public class Mathematics { return viols; } + /** + * Project the value to the range given. + * + * @param v + * @param min + * @param max + * @return the closest value to v within [min,max] + */ + public static double projectValue(double v, double min, double max) { + if (vmax) { + return max; + } else return v; + } /** * Calculate the average length of the range intervals over all dimensions. diff --git a/src/eva2/tools/Pair.java b/src/eva2/tools/Pair.java index 355f83b9..83c42242 100644 --- a/src/eva2/tools/Pair.java +++ b/src/eva2/tools/Pair.java @@ -15,6 +15,10 @@ public class Pair implements Serializable { private static final long serialVersionUID = -3620465393975181451L; public S head; public T tail; + + public Object clone() { + return new Pair(head, tail); + } public Pair(S head, T tail) { this.head = head; diff --git a/src/eva2/tools/StringSelection.java b/src/eva2/tools/StringSelection.java new file mode 100644 index 00000000..e2c8b4c6 --- /dev/null +++ b/src/eva2/tools/StringSelection.java @@ -0,0 +1,79 @@ +package eva2.tools; + +import java.io.Serializable; + +/** + * An array of Strings that can be selected and deselected. May be created directly from an Enum. + * + * @author mkron + * + */ +public class StringSelection implements Serializable { + private String[] strObjects; + boolean[] selStates; + + public StringSelection(String[] sArr) { + strObjects = sArr; + selStates=new boolean[sArr.length]; + } + + public StringSelection(String[] sArr, int initialSel) { + this(sArr); + if (initialSel e) { + strObjects = new String[e.getClass().getEnumConstants().length]; + selStates = new boolean[strObjects.length]; + for (int i = 0; i < strObjects.length; i++) { + strObjects[i] = e.getClass().getEnumConstants()[i].toString(); + } + setSelected(e.ordinal(), true); + } + + public StringSelection(StringSelection stringSelection) { + strObjects = stringSelection.strObjects.clone(); + selStates = stringSelection.selStates.clone(); + } + + public Object clone() { + return new StringSelection(this); + } + + public int getLength() { + return strObjects.length; + } + + public String getElement(int i) { + return strObjects[i]; + } + + public String[] getStrings() { + return strObjects; + } + + public boolean isSelected(int i) { + return selStates[i]; + } + + /** + * Returns true if the given enum is selected (as its string representation) + * within the instance. + * + * @param e + * @return + */ + public boolean isSelected(Enum e) { + if (isSelected(e.ordinal())) { + return e.toString().equals(strObjects[e.ordinal()]); + } else return false; + } + + public void setSelected(int i, boolean v) { + selStates[i]=v; + } + + public void toggleSelected(int i) { + selStates[i]=!selStates[i]; + } +} \ No newline at end of file