Move Item to own class, remove ObjectArrayEditor (will be merged with other class)

This commit is contained in:
Fabian Becker 2015-12-17 15:38:20 +01:00
parent 2cd3017ae7
commit 2242367832
4 changed files with 59 additions and 142 deletions

View File

@ -0,0 +1,34 @@
package eva2.gui;
public class Item
{
private String id;
private String displayName;
private String description;
public Item(String id, String displayName, String description)
{
this.id = id;
this.displayName = displayName;
this.description = description;
}
public String getId()
{
return id;
}
public String getDescription()
{
return description;
}
public String getDisplayName() {
return displayName;
}
public String toString()
{
return id;
}
}

View File

@ -1,110 +0,0 @@
package eva2.gui;
import eva2.problems.AbstractProblemDouble;
import javax.swing.*;
import java.awt.*;
/**
* Created by fabian on 16/12/15.
*/
public class ObjectArrayEditor<T> extends JPanel {
private JList<T> objectList;
private DefaultListModel<T> listModel;
public ObjectArrayEditor(Class<T> type) {
listModel = new DefaultListModel<>();
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
TypeSelector typeSelector = new TypeSelector();
typeSelector.updateClassType(type.getName());
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(typeSelector, c);
JButton addButton = new JButton("Add");
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
add(addButton, c);
JButton removeButton = new JButton("Remove");
c.gridx = 2;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
add(removeButton, c);
JButton button;
button = new JButton("Config");
c.gridx = 2;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 0.0;
add(button, c);
objectList = new JList<>(listModel);
objectList.setVisibleRowCount(10);
//objectList.setFixedCellHeight(15);
//objectList.setFixedCellWidth(100);
c.gridwidth = 2;
c.gridheight = 5;
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
JScrollPane scrollPane = new JScrollPane(objectList);
add(scrollPane, c);
addButton.addActionListener(event -> {
String className = ((Item) typeSelector.getSelectedItem()).getId();
try {
T n = (T) Class.forName(className).newInstance();
listModel.addElement(n);
} catch (Exception ex) {
System.err.println("Exception in itemStateChanged " + ex.getMessage());
System.err.println("Classpath is " + System.getProperty("java.class.path"));
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Could not create an example of\n"
+ className + "\n"
+ "from the current classpath. Is the resource folder at the right place?\nIs the class abstract or the default constructor missing?",
"GenericObjectEditor",
JOptionPane.ERROR_MESSAGE);
}
});
removeButton.addActionListener(event -> {
if (!objectList.isSelectionEmpty()) {
listModel.remove(objectList.getSelectedIndex());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("ObjectArrayEditor");
frame.add(new ObjectArrayEditor<>(AbstractProblemDouble.class), BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@ -0,0 +1,25 @@
package eva2.gui;
import javax.swing.*;
import java.awt.*;
/**
* Created by halfdan on 17/12/15.
*/
public final class TitledSeparator extends JPanel {
public TitledSeparator(String title) {
setLayout(new GridBagLayout());
GridBagConstraints gbConstraints = new GridBagConstraints();
gbConstraints.gridx = 0;
gbConstraints.gridy = 0;
add(new JLabel("<html><b>" + title), gbConstraints);
gbConstraints.gridx = 1;
gbConstraints.gridy = 0;
gbConstraints.weightx = 1.0;
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
add(new JSeparator(JSeparator.HORIZONTAL), gbConstraints);
}
}

View File

@ -112,35 +112,3 @@ class ToolTipComboBoxRenderer extends BasicComboBoxRenderer {
}
}
class Item
{
private String id;
private String displayName;
private String description;
public Item(String id, String displayName, String description)
{
this.id = id;
this.displayName = displayName;
this.description = description;
}
public String getId()
{
return id;
}
public String getDescription()
{
return description;
}
public String getDisplayName() {
return displayName;
}
public String toString()
{
return id;
}
}