Refactoring

This commit is contained in:
2013-07-16 11:30:54 +02:00
parent bf1af304dd
commit 5297968fd2
5 changed files with 126 additions and 252 deletions

View File

@@ -295,8 +295,4 @@ public class OptimizerRunnable implements Runnable {
public void setOutputFullStatsToText(boolean addInfo) {
((AbstractStatistics)proc.getStatistics()).getStatisticsParameter().setOutputAllFieldsAsText(addInfo);
}
// public void configureStats(int verbosityLevel, int outputDirection, int multiRuns, boolean additionalInfo) {
// asdf
// }
}

View File

@@ -6,32 +6,31 @@ import java.beans.PropertyEditor;
import javax.swing.JCheckBox;
/**
* A checkbox for boolean editors.
* A checkbox for boolean editors.
*/
public class PropertyBoolSelector extends JCheckBox {
private static final long serialVersionUID = 8181005734895597714L;
private PropertyEditor m_Editor;
private static final long serialVersionUID = 8181005734895597714L;
private PropertyEditor propertyEditor;
public PropertyBoolSelector(PropertyEditor pe) {
super();
m_Editor = pe;
if (m_Editor.getAsText().equals("True")) {
setSelected(true);
}
else {
setSelected(false);
}
public PropertyBoolSelector(PropertyEditor pe) {
super();
propertyEditor = pe;
if (propertyEditor.getAsText().equals("True")) {
setSelected(true);
} else {
setSelected(false);
}
addItemListener(new ItemListener () {
addItemListener(new ItemListener() {
@Override
public void itemStateChanged (ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
m_Editor.setValue(Boolean.TRUE);
}
if (evt.getStateChange() == ItemEvent.DESELECTED) {
m_Editor.setValue(Boolean.FALSE);
}
}
});
}
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED) {
propertyEditor.setValue(Boolean.TRUE);
}
if (evt.getStateChange() == ItemEvent.DESELECTED) {
propertyEditor.setValue(Boolean.FALSE);
}
}
});
}
}

View File

@@ -4,197 +4,101 @@ import eva2.optimization.go.InterfaceTerminator;
import eva2.optimization.individuals.codings.gp.GPArea;
import eva2.tools.SelectedTag;
import eva2.tools.StringSelection;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
public class PropertyEditorProvider {
final static boolean TRACE = false;
// if true, we use the GenericObjectEditor whenever no specific one is registered, so keep it true
// unless you want to register every single possibility.
public static boolean useDefaultGOE = true;
public static boolean useDefaultGOE = true;
/**
* Retrieve an editor object for a given class.
* This method seems unable to retrieve a primitive editor for obscure reasons.
* So better use the one based on PropertyDescriptor if possible.
*/
public static PropertyEditor findEditor(Class<?> cls) {
PropertyEditor editor = null;
PropertyEditor editor = null;
editor = PropertyEditorManager.findEditor(cls);
// if (TRACE) System.out.println((editor == null ) ? "No editor from PEM" : ("Found " + editor.getClass()));
if ((editor == null) && useDefaultGOE ) {
if (cls.isArray()) {
if ((editor == null) && useDefaultGOE) {
if (cls.isArray()) {
editor = new GenericArrayEditor();
}
else if (cls.isEnum()) {
} else if (cls.isEnum()) {
editor = new EnumEditor();
}
else {
} else {
editor = new GenericObjectEditor();
}
// if (TRACE) System.out.println("using GOE/GAE");
}
if (TRACE) {
System.out.println("# using "+ editor.getClass().getName() + " for " + cls.getName());
}
return editor;
}
/**
private PropertyEditor makeEditor(PropertyDescriptor prop, Object value) {
Class type = prop.getPropertyType();
Class pec = prop.getPropertyEditorClass();
PropertyEditor editor = null;
//Class pec = m_Properties[i].getPropertyEditorClass();
if (pec != null) {
try {
editor = (PropertyEditor)pec.newInstance();
} catch (Exception ex) {
}
}
if (editor == null) {
if (TRACE) System.out.println("PropertySheetPanel.setTarget(): No editor from pec.");
if (STREICHE) {
//@todo Streiche: Here i'm looking for a specialized editor
//if (TRACE) System.out.println("PropertySheetPanel.setTarget(): trying to find specialised editor for "+value.getClass()+".");
if (value != null) editor = PropertyEditorManager.findEditor(value.getClass());
if (TRACE) {
if (editor == null) System.out.println("PropertySheetPanel.setTarget(): Found no editor.");
else System.out.println("PropertySheetPanel.setTarget(): Found " + editor.getClass()+".");
}
if (editor == null) editor = PropertyEditorManager.findEditor(type);
} else {
editor = PropertyEditorManager.findEditor(type);
}
}
if ((TRACE) && (editor != null)) System.out.println("PropertySheetPanel.setTarget(): editor="+editor.getClass().getName());
if (editor == null) {
// If it's a user-defined property we give a warning.
String getterClass = prop.getReadMethod().getDeclaringClass().getName();
if (getterClass.indexOf("java.") != 0) {
System.err.println("Warning: Can't find public property editor"
+ " for property \"" + prop.getDisplayName() + "\" (class \""
+ type.getName() + "\"). Skipping.");
}
} else if (editor instanceof GenericObjectEditor) ((GenericObjectEditor) editor).setClassType(type);
return editor;
}
*/
/**
*
* @param prop
* @param value
* @return
*/
public static PropertyEditor findEditor(PropertyDescriptor prop, Object value) {
PropertyEditor editor = null;
Class pec = prop.getPropertyEditorClass();
Class type = prop.getPropertyType();
if (TRACE) {
System.out.println("PropertyEditorProvider: Searching editor for " + value.getClass());
}
try {
if (pec != null) {
editor = (PropertyEditor)pec.newInstance();
PropertyEditor editor = null;
Class pec = prop.getPropertyEditorClass();
Class type = prop.getPropertyType();
try {
if (pec != null) {
editor = (PropertyEditor) pec.newInstance();
}
} catch (Exception e) {
editor = null;
}
} catch (Exception e) {
editor = null;
}
if (editor == null) {
if (TRACE) {
System.out.println("PropertySheetPanel.makeEditor(): No editor from PEC.");
}
//@todo Streiche: Here i'm looking for a specialized editor
//if (TRACE) System.out.println("PropertySheetPanel.setTarget(): trying to find specialised editor for "+value.getClass()+".");
if (TRACE) {
System.out.println("A PropertySheetPanel.makeEditor(): checking " + value.getClass());
}
if (value != null) {
if (editor == null) {
if (value != null) {
editor = PropertyEditorManager.findEditor(value.getClass());
}
if (TRACE) {
System.out.println((editor == null ) ? "No editor from PEM" : ("Found " + editor.getClass()));
}
if (editor == null && (BeanInspector.isJavaPrimitive(value.getClass()))) {
Class<?> prim = BeanInspector.getBoxedType(value.getClass());
if (prim != null) {
editor = PropertyEditorManager.findEditor(prim);
}
if (editor == null) {
if (editor == null && (BeanInspector.isJavaPrimitive(value.getClass()))) {
Class<?> prim = BeanInspector.getBoxedType(value.getClass());
if (TRACE) {
System.out.println("B1 PropertySheetPanel.makeEditor(): checking " + prim);
}
if (prim!=null) {
prim = BeanInspector.getUnboxedType(value.getClass());
if (prim != null) {
editor = PropertyEditorManager.findEditor(prim);
}
if (editor ==null) {
if (TRACE) {
System.out.println((editor == null ) ? "No editor from PEM by boxed type " : ("Found " + editor.getClass()));
}
}
}
prim = BeanInspector.getUnboxedType(value.getClass());
if (TRACE) {
System.out.println("B2 PropertySheetPanel.makeEditor(): checking " + prim);
}
if (prim!=null) {
editor = PropertyEditorManager.findEditor(prim);
}
if (TRACE) {
System.out.println((editor == null ) ? "No editor from PEM by unboxed type " : ("Found " + editor.getClass()));
}
}
}
if (editor == null) {
editor = PropertyEditorManager.findEditor(type);
}
if (editor == null) {
if (TRACE) {
System.out.println("C PropertySheetPanel.makeEditor(): checking " + type);
}
editor = PropertyEditorManager.findEditor(type);
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");
}
}
}
if (editor == null) {
// If it's a user-defined property we give a warning.
String getterClass = prop.getReadMethod().getDeclaringClass().getName();
if (getterClass.indexOf("java.") != 0) {
System.err.println("Warning: Can't find public property editor"
+ " for property \"" + prop.getDisplayName() + "\" (class \""
+ type.getName() + "\"). Skipping.");
}
} else if (editor instanceof GenericObjectEditor) {
// hier erst noch das object setzen?
// ((GenericObjectEditor) editor).getCustomEditor();
((GenericObjectEditor) editor).setClassType(type);
}
if (TRACE) {
System.out.println("+ using "+ editor.getClass().getName() + " for " + value.getClass().getName());
if ((editor == null) && useDefaultGOE) {
if (type.isArray()) {
editor = new GenericArrayEditor();
} else if (type.isEnum()) {
editor = new EnumEditor();
} else {
editor = new GenericObjectEditor();
}
}
}
return editor;
if (editor == null) {
// If it's a user-defined property we give a warning.
String getterClass = prop.getReadMethod().getDeclaringClass().getName();
if (getterClass.indexOf("java.") != 0) {
System.err.println("Warning: Can't find public property editor"
+ " for property \"" + prop.getDisplayName() + "\" (class \""
+ type.getName() + "\"). Skipping.");
}
} else if (editor instanceof GenericObjectEditor) {
((GenericObjectEditor) editor).setClassType(type);
}
return editor;
}
/**
*/
public static void installEditors() {
@@ -203,31 +107,21 @@ public class PropertyEditorProvider {
PropertyEditorManager.registerEditor(int[].class, GenericArrayEditor.class);
PropertyEditorManager.registerEditor(double[].class, GenericArrayEditor.class);
PropertyEditorManager.registerEditor(InterfaceTerminator[].class, GenericArrayEditor.class);
// PropertyEditorManager.registerEditor(Double.class, DoubleEditor.class);
// PropertyEditorManager.registerEditor(Integer.class, IntEditor.class);
// PropertyEditorManager.registerEditor(Boolean.class, BoolEditor.class);
// PropertyEditorManager.registerEditor(byte.class, ByteEditor.class);
// PropertyEditorManager.registerEditor(Color.class, ColorEditor.class);
// PropertyEditorManager.registerEditor(short.class, ShortEditor.class);
// PropertyEditorManager.registerEditor(float.class, FloatEditor.class);
// PropertyEditorManager.registerEditor(long.class, LongEditor.class);
// PropertyEditorManager.registerEditor(String.class, StringEditor.class);
// 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);
PropertyEditorManager.registerEditor(PropertyIntArray.class , GenericIntArrayEditor.class);
PropertyEditorManager.registerEditor(PropertyEpsilonThreshold.class , GenericEpsilonThresholdEditor.class);
PropertyEditorManager.registerEditor(PropertyEpsilonConstraint.class , GenericEpsilonConstraintEditor.class);
PropertyEditorManager.registerEditor(StringSelection.class, StringSelectionEditor.class);
// Traveling Salesman problem
PropertyEditorManager.registerEditor(GPArea.class, GenericAreaEditor.class);
PropertyEditorManager.registerEditor(PropertyDoubleArray.class, GenericDoubleArrayEditor.class);
PropertyEditorManager.registerEditor(PropertyIntArray.class, GenericIntArrayEditor.class);
PropertyEditorManager.registerEditor(PropertyEpsilonThreshold.class, GenericEpsilonThresholdEditor.class);
PropertyEditorManager.registerEditor(PropertyEpsilonConstraint.class, GenericEpsilonConstraintEditor.class);
PropertyEditorManager.registerEditor(PropertyWeightedLPTchebycheff.class, GenericWeigthedLPTchebycheffEditor.class);
// PropertyEditorManager.registerEditor(PropertyStringList.class , GenericStringListSelectionEditor.class);
PropertyEditorManager.registerEditor(PropertyFilePath.class , GenericFilePathEditor.class);
PropertyEditorManager.registerEditor(PropertyOptimizationObjectives.class , GenericOptimizationObjectivesEditor.class);
PropertyEditorManager.registerEditor(PropertyOptimizationObjectivesWithParam.class , GenericOptimizationObjectivesWithParamEditor.class);
PropertyEditorManager.registerEditor(PropertyFilePath.class, GenericFilePathEditor.class);
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, GenericArrayEditor.class);
}

View File

@@ -42,35 +42,22 @@ public class PropertyPanel extends JPanel {
propertyEditor = editor;
textLabel = new JLabel();
add(textLabel, gbConstraints);
JButton dialogButton = new JButton("...");
dialogButton.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.LIGHT_GRAY));
dialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
if (propertyEditor.getValue() != null) {
showDialog(getLocationOnScreen().x, getLocationOnScreen().y);
}
}
});
gbConstraints.weighty = 1.0;
gbConstraints.fill = GridBagConstraints.VERTICAL;
gbConstraints.anchor = GridBagConstraints.LINE_END;
gbConstraints.gridx = 1;
add(dialogButton, gbConstraints);
}
public void showDialog(int initX, int initY) {
if (propertyDialog == null) {
propertyDialog = new PropertyDialog(propertyEditor, EVAHELP.cutClassName(propertyEditor.getClass().getName()) , initX, initY);
propertyDialog.setPreferredSize(new Dimension(500,300));
propertyDialog.setModal(true);
propertyDialog.setVisible(true);
}
else {
propertyDialog.updateFrameTitle(propertyEditor);
propertyDialog.setVisible(false);
propertyDialog.setVisible(true);
propertyDialog.requestFocus();
}
}
@@ -98,10 +85,6 @@ public class PropertyPanel extends JPanel {
getSize().width - i.right - i.left,
getSize().height - i.bottom - i.top);
propertyEditor.paintValue(g, box);
// Rectangle box = new Rectangle(i.left,i.top,
// this.getWidth() - i.right,
// this.getHeight() - i.bottom );
}
public PropertyEditor getEditor() {

View File

@@ -176,6 +176,7 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
propertyTable.setDefaultRenderer(Object.class, new PropertyCellRenderer());
propertyTable.setDefaultEditor(Object.class, new PropertyCellEditor());
propertyTable.setRowHeight(20);
propertyTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
// Close any child windows at this point
removeAll();
@@ -763,14 +764,14 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
JComponent newView = null;
newView = getView(tmpEdit);
if (newView == null) {
System.err.println("Warning: Property \"" + propertyDescriptors[i].getDisplayName() + "\" has non-displayabale editor. Skipping.");
System.err.println("Warning: Property \"" + propertyDescriptors[i].getDisplayName() + "\" has non-displayable editor. Skipping.");
return false;
}
propertyEditors[i].addPropertyChangeListener(this);
views[i] = newView;
if (toolTips[i] != null) {
views[i].setToolTipText(toolTips[i]);
}
}
viewWrappers[i].removeAll();
viewWrappers[i].setLayout(new BorderLayout());
viewWrappers[i].add(views[i], BorderLayout.CENTER);
@@ -796,8 +797,6 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
// This could check whether i have to set the value back to
// the editor, this would allow to check myu and lambda
// why shouldn't i do this for every property!?
// System.out.println("value: "+((Integer)value).intValue());
// System.out.println(" m_Values[i]: "+ ((Integer) m_Values[i]).intValue());
if (((Integer) newValue).intValue() != ((Integer) objectValues[i]).intValue()) {
propertyEditors[i].setValue(objectValues[i]);
}
@@ -813,7 +812,6 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
} catch (Exception ex) {
System.out.println("PropertySheetPanel.wasModified(): Unexpected exception while updating " + property.getName());
}
//revalidate();
if (views[i] != null && views[i] instanceof PropertyPanel) {
//System.err.println("Trying to repaint the property canvas");
views[i].repaint();
@@ -944,7 +942,7 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
* @return
*/
private boolean updateFieldView(int i) {
// looking at another field (not changed explicitly, maybe implicitely
// looking at another field (not changed explicitly, maybe implicitly
boolean valChanged = false;
boolean doRepaint = false;
Object args[] = {};
@@ -1071,20 +1069,43 @@ class PropertyCellRenderer implements TableCellRenderer {
}
class PropertyCellEditor implements TableCellEditor {
class PropertyCellEditor extends AbstractCellEditor implements TableCellEditor {
private JLabel empty = new JLabel();
private Object value;
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
public JComponent getTableCellEditorComponent(JTable table, final Object value, boolean isSelected, int row, int column) {
this.value = value;
Component component;
JComponent component;
if (value == null) {
component = empty;
} else if (value instanceof String) {
component = new JLabel(value.toString());
} else if (value instanceof eva2.gui.PropertyPanel) {
component = (PropertyPanel) value;
} else if (value instanceof PropertyPanel) {
component = new JPanel();
component.setLayout(new GridBagLayout());
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.gridx = 0;
gbConstraints.gridy = 0;
gbConstraints.weightx = 1.0;
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
component.add((PropertyPanel) value, gbConstraints);
JButton dialogButton = new JButton("...");
dialogButton.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.LIGHT_GRAY));
dialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
((PropertyPanel) value).showDialog(0, 0);
fireEditingStopped();
}
});
gbConstraints = new GridBagConstraints();
gbConstraints.weighty = 1.0;
gbConstraints.fill = GridBagConstraints.VERTICAL;
gbConstraints.anchor = GridBagConstraints.LINE_END;
gbConstraints.gridy = 0;
gbConstraints.gridx = 1;
component.add(dialogButton, gbConstraints);
} else if (value instanceof PropertyText) {
component = (PropertyText) value;
} else if (value instanceof PropertyBoolSelector) {
@@ -1094,6 +1115,7 @@ class PropertyCellEditor implements TableCellEditor {
} else {
throw new UnsupportedOperationException("Not supported yet.");
}
if (isSelected) {
component.setForeground(table.getSelectionForeground());
component.setBackground(table.getSelectionBackground());
@@ -1122,26 +1144,6 @@ class PropertyCellEditor implements TableCellEditor {
return true;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override
public boolean stopCellEditing() {
return true;
}
@Override
public void cancelCellEditing() {
}
@Override
public void addCellEditorListener(CellEditorListener l) {
}
@Override
public void removeCellEditorListener(CellEditorListener l) {
}
}