Properly show ToolTipText on the PropertySheetPanel.

Fix missing HTML for OptimizationParameters.
Clean up PropertySheetPanel.
This commit is contained in:
Fabian Becker 2014-10-23 17:11:49 +02:00
parent b13e81e59c
commit 2593ec2a70
6 changed files with 67 additions and 103 deletions

View File

@ -7,9 +7,11 @@ import eva2.tools.SelectedTag;
import eva2.tools.StringTools; import eva2.tools.StringTools;
import eva2.tools.Tag; import eva2.tools.Tag;
import eva2.util.annotation.Description; import eva2.util.annotation.Description;
import eva2.util.annotation.Parameter;
import java.beans.*; import java.beans.*;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -994,6 +996,16 @@ public class BeanInspector {
String result = ""; String result = "";
String tipName = name + "TipText"; String tipName = name + "TipText";
// Find by annotation
Parameter[] parameters= target.getClass().getAnnotationsByType(Parameter.class);
for (Field field : target.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Parameter.class) && field.getName().equals(name)) {
Parameter parameter = field.getAnnotation(Parameter.class);
return parameter.description();
}
}
// Find by deprecated TipText method
for (int j = 0; j < methods.length; j++) { for (int j = 0; j < methods.length; j++) {
String mname = methods[j].getDisplayName(); String mname = methods[j].getDisplayName();
Method meth = methods[j].getMethod(); Method meth = methods[j].getMethod();

View File

@ -1,14 +1,11 @@
package eva2.gui; package eva2.gui;
import eva2.EvAInfo; import eva2.EvAInfo;
import eva2.optimization.modules.*;
import eva2.util.ClassPreloader; import eva2.util.ClassPreloader;
import eva2.util.EvAComAdapter; import eva2.util.EvAComAdapter;
import eva2.optimization.OptimizationStateListener; import eva2.optimization.OptimizationStateListener;
import eva2.optimization.go.InterfaceOptimizationParameters; import eva2.optimization.go.InterfaceOptimizationParameters;
import eva2.optimization.modules.AbstractModuleAdapter;
import eva2.optimization.modules.GenericModuleAdapter;
import eva2.optimization.modules.ModuleAdapter;
import eva2.optimization.modules.OptimizationParameters;
import eva2.optimization.stat.AbstractStatistics; import eva2.optimization.stat.AbstractStatistics;
import eva2.optimization.stat.InterfaceStatisticsListener; import eva2.optimization.stat.InterfaceStatisticsListener;
import eva2.optimization.stat.InterfaceStatisticsParameter; import eva2.optimization.stat.InterfaceStatisticsParameter;
@ -772,7 +769,8 @@ public class Main extends JFrame implements OptimizationStateListener {
ModuleAdapter newModuleAdapter = null; ModuleAdapter newModuleAdapter = null;
// //
try { try {
newModuleAdapter = comAdapter.getModuleAdapter(selectedModule, optimizationParameters, withGUI ? null : "EvA2"); newModuleAdapter = new GOModuleAdapter(selectedModule, OptimizationParameters.getInstance(), withGUI ? null : "EvA2");
//newModuleAdapter = comAdapter.getModuleAdapter(selectedModule, optimizationParameters, withGUI ? null : "EvA2");
} catch (Exception e) { } catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error loading module.", e); LOGGER.log(Level.SEVERE, "Error loading module.", e);
EVAERROR.EXIT("Error while comAdapter.GetModuleAdapter Host: " + e.getMessage()); EVAERROR.EXIT("Error while comAdapter.GetModuleAdapter Host: " + e.getMessage());

View File

@ -8,9 +8,11 @@ import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer; import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.beans.*; import java.beans.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -82,7 +84,7 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
*/ */
// If true, tool tips are used up to the first point only. // If true, tool tips are used up to the first point only.
boolean stripToolTipToFirstPoint = false; boolean stripToolTipToFirstPoint = false;
private JTable propertyTable; private ToolTipTable propertyTable;
private DefaultTableModel propertyTableModel; private DefaultTableModel propertyTableModel;
/** /**
@ -155,6 +157,38 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
return view; return view;
} }
/**
* JTable extended to show ToolTips
*/
class ToolTipTable extends JTable {
private String[] toolTips;
public ToolTipTable(TableModel model) {
super(model);
}
public void setToolTips(String[] toolTips) {
this.toolTips = toolTips;
}
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
try {
if(this.toolTips != null && rowIndex <= this.toolTips.length){
tip = this.toolTips[rowIndex];
}
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
}
/** /**
* Sets a new target object for customisation. * Sets a new target object for customisation.
* *
@ -164,7 +198,7 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
propertyTableModel = new DefaultTableModel(); propertyTableModel = new DefaultTableModel();
propertyTableModel.addColumn("Key"); propertyTableModel.addColumn("Key");
propertyTableModel.addColumn("Value"); propertyTableModel.addColumn("Value");
propertyTable = new JTable(propertyTableModel); propertyTable = new ToolTipTable(propertyTableModel);
propertyTable.setDefaultRenderer(Object.class, new PropertyCellRenderer()); propertyTable.setDefaultRenderer(Object.class, new PropertyCellRenderer());
propertyTable.setDefaultEditor(Object.class, new PropertyCellEditor()); propertyTable.setDefaultEditor(Object.class, new PropertyCellEditor());
propertyTable.setRowHeight(20); propertyTable.setRowHeight(20);
@ -237,7 +271,7 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
propertyLabels = new JLabel[propertyDescriptors.length]; propertyLabels = new JLabel[propertyDescriptors.length];
toolTips = new String[propertyDescriptors.length]; toolTips = new String[propertyDescriptors.length];
int itemIndex = 0;
for (int i = 0; i < propertyDescriptors.length; i++) { for (int i = 0; i < propertyDescriptors.length; i++) {
// For each property do this // For each property do this
// Don't display hidden or expert properties. // Don't display hidden or expert properties.
@ -253,8 +287,9 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
if (propertyEditors[i] == null) { if (propertyEditors[i] == null) {
continue; continue;
} }
toolTips[i] = BeanInspector.getToolTipText(name, methodDescriptors, targetObject, stripToolTipToFirstPoint, tipTextLineLen);
toolTips[itemIndex] = BeanInspector.getToolTipText(name, methodDescriptors, targetObject, stripToolTipToFirstPoint, tipTextLineLen);
itemIndex++;
newView = getView(propertyEditors[i]); newView = getView(propertyEditors[i]);
if (newView == null) { if (newView == null) {
System.err.println("Warning: Property \"" + name + "\" has non-displayabale editor. Skipping."); System.err.println("Warning: Property \"" + name + "\" has non-displayabale editor. Skipping.");
@ -272,6 +307,8 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
propertyTableModel.addRow(new Object[]{name, newView}); propertyTableModel.addRow(new Object[]{name, newView});
} }
propertyTable.setToolTips(toolTips);
JScrollPane scrollableTable = new JScrollPane(propertyTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); JScrollPane scrollableTable = new JScrollPane(propertyTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
gbConstraints.gridx = 0; gbConstraints.gridx = 0;
gbConstraints.gridy = 1; gbConstraints.gridy = 1;
@ -359,27 +396,6 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
return values; return values;
} }
/**
* Create a label to be shown if no other properties are shown.
*
* @param componentOffset
* @param gbLayout
* @return
*/
private JLabel createDummyLabel(int componentOffset, GridBagLayout gbLayout) {
JLabel empty = new JLabel("No editable properties", SwingConstants.CENTER);
Dimension d = empty.getPreferredSize();
empty.setPreferredSize(new Dimension(d.width * 2, d.height * 2));
empty.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 10));
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.anchor = GridBagConstraints.CENTER;
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
gbConstraints.gridy = componentOffset;
gbConstraints.gridx = 0;
gbLayout.setConstraints(empty, gbConstraints);
return empty;
}
private PropertyEditor makeEditor(PropertyDescriptor property, String name, Object value) { private PropertyEditor makeEditor(PropertyDescriptor property, String name, Object value) {
PropertyEditor editor = PropertyEditorProvider.findEditor(property, value); PropertyEditor editor = PropertyEditorProvider.findEditor(property, value);
if (editor == null) { if (editor == null) {
@ -397,67 +413,10 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
} }
editor.setValue(value); editor.setValue(value);
// System.out.println("PSP editor class: " + editor.getClass());
editor.addPropertyChangeListener(this); editor.addPropertyChangeListener(this);
return editor; return editor;
} }
private void addLabelView(int componentOffset, GridBagLayout gbLayout,
int i, String name, JComponent newView) {
propertyLabels[i] = makeLabel(name);
views[i] = newView;
viewWrappers[i] = new JPanel();
viewWrappers[i].setLayout(new BorderLayout());
gbLayout.setConstraints(propertyLabels[i], makeLabelConstraints(i + componentOffset));
add(propertyLabels[i]);
JPanel newPanel = makeViewPanel(toolTips[i], propertyLabels[i], views[i], viewWrappers[i]);
gbLayout.setConstraints(newPanel, makeViewConstraints(i + componentOffset));
add(newPanel);
}
private JLabel makeLabel(String name) {
JLabel label = new JLabel(name, SwingConstants.RIGHT);
label.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 5));
return label;
}
private static JPanel makeViewPanel(String tipText, JLabel label,
JComponent view, JComponent viewWrapper) {
JPanel newPanel = new JPanel();
if (tipText != null) {
label.setToolTipText(tipText);
view.setToolTipText(tipText);
}
newPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 10));
newPanel.setLayout(new BorderLayout());
// @todo: Streiche here i could add the ViewWrapper
viewWrapper.add(view, BorderLayout.CENTER);
newPanel.add(viewWrapper, BorderLayout.CENTER);
return newPanel;
}
private GridBagConstraints makeLabelConstraints(int componentIndex) {
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.anchor = GridBagConstraints.EAST;
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
gbConstraints.gridy = componentIndex;
gbConstraints.gridx = 0;
return gbConstraints;
}
private GridBagConstraints makeViewConstraints(int componentIndex) {
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.anchor = GridBagConstraints.WEST;
gbConstraints.fill = GridBagConstraints.BOTH;
gbConstraints.gridy = componentIndex;
gbConstraints.gridx = 1;
gbConstraints.weightx = 100;
return gbConstraints;
}
/** /**
* Be sure to give a clone * Be sure to give a clone
* *
@ -543,8 +502,6 @@ public class PropertySheetPanel extends JPanel implements PropertyChangeListener
} }
private JPanel makeInfoPanel(String infoText, Object targ, int rowHeight) { private JPanel makeInfoPanel(String infoText, Object targ, int rowHeight) {
Object args[] = {};
className = targ.getClass().getName(); className = targ.getClass().getName();
helpButton = new JButton("Help"); helpButton = new JButton("Help");
helpButton.setToolTipText("More information about " + className); helpButton.setToolTipText("More information about " + className);
@ -1038,9 +995,7 @@ class PropertyCellRenderer implements TableCellRenderer {
} }
throw new UnsupportedOperationException("Not supported yet."); throw new UnsupportedOperationException("Not supported yet.");
} }
} }
class PropertyCellEditor extends AbstractCellEditor implements TableCellEditor { class PropertyCellEditor extends AbstractCellEditor implements TableCellEditor {

View File

@ -23,8 +23,7 @@ public class GOModuleAdapter extends GenericModuleAdapter implements ModuleAdapt
/** /**
* Starts a statistics GUI and the GOProcessor thread. * Starts a statistics GUI and the GOProcessor thread.
* *
* @param AdapterName the title of the ModulAdapter * @param adapterName the title of the ModuleAdapter
* @param Client the client instance
*/ */
public GOModuleAdapter(String adapterName) { public GOModuleAdapter(String adapterName) {
super(adapterName, "", OptimizationParameters.getInstance(), false); super(adapterName, "", OptimizationParameters.getInstance(), false);
@ -33,10 +32,11 @@ public class GOModuleAdapter extends GenericModuleAdapter implements ModuleAdapt
/** /**
* Starts a statistics GUI and the GOProcessor thread with a given OptimizationParameters file. * Starts a statistics GUI and the GOProcessor thread with a given OptimizationParameters file.
* *
* @param AdapterName the title of the ModulAdapter * @param adapterName the title of the ModuleAdapter
* @param Client the client instance * @param optimizationParameters the client instance
* @param noGuiLogFile
*/ */
public GOModuleAdapter(String adapterName, InterfaceOptimizationParameters goParams, String noGuiLogFile) { public GOModuleAdapter(String adapterName, InterfaceOptimizationParameters optimizationParameters, String noGuiLogFile) {
super(adapterName, "", goParams, false, noGuiLogFile); super(adapterName, "", optimizationParameters, false, noGuiLogFile);
} }
} }

View File

@ -583,7 +583,7 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial
} }
public void optimizeSteadyState() { public void optimizeSteadyState() {
AbstractEAIndividual indy = null, orig; AbstractEAIndividual indy, orig;
int index; int index;
int nextDoomed = getNextDoomed(population, 0); int nextDoomed = getNextDoomed(population, 0);
@ -591,7 +591,6 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial
// required for dynamic problems especially // required for dynamic problems especially
optimizationProblem.evaluatePopulationStart(population); optimizationProblem.evaluatePopulationStart(population);
/** /**
* Reevalutation mechanism for dynamically changing problems * Reevalutation mechanism for dynamically changing problems
*/ */
@ -654,7 +653,7 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial
* *
* @param pop Population to search * @param pop Population to search
* @param startIndex index to start the search from * @param startIndex index to start the search from
* @return index of an overaged individual or -1 * @return index of an over aged individual or -1
*/ */
protected int getNextDoomed(Population pop, int startIndex) { protected int getNextDoomed(Population pop, int startIndex) {
if (maximumAge > 0) { if (maximumAge > 0) {
@ -689,12 +688,12 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial
/** /**
* Something has changed * Something has changed
* *
* @param name * @param name Event name
*/ */
protected void firePropertyChangedEvent(String name) { protected void firePropertyChangedEvent(String name) {
if (this.populationChangedEventListeners != null) { if (this.populationChangedEventListeners != null) {
for (int i = 0; i < this.populationChangedEventListeners.size(); i++) { for (InterfacePopulationChangedEventListener listener : this.populationChangedEventListeners) {
this.populationChangedEventListeners.get(i).registerPopulationStateChanged(this, name); listener.registerPopulationStateChanged(this, name);
} }
} }
} }