Improve reflections to make the input panel more generic.

This commit is contained in:
Holt59 2018-03-03 19:26:56 +01:00
parent 738b23d253
commit 6d8c2462ab
3 changed files with 26 additions and 26 deletions

View File

@ -42,8 +42,8 @@ import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import org.insa.algo.AlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathData; import org.insa.algo.shortestpath.ShortestPathData;
import org.insa.algo.shortestpath.ShortestPathGraphicObserver; import org.insa.algo.shortestpath.ShortestPathGraphicObserver;
import org.insa.algo.shortestpath.ShortestPathSolution; import org.insa.algo.shortestpath.ShortestPathSolution;
@ -153,12 +153,13 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
StartActionEvent evt = (StartActionEvent) e; StartActionEvent evt = (StartActionEvent) e;
ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), evt.getDestination(), ShortestPathData data = new ShortestPathData(graph, evt.getNodes().get(0), evt.getNodes().get(1),
evt.getMode(), evt.getArcFilter()); evt.getMode(), evt.getArcFilter());
ShortestPathAlgorithm spAlgorithm = null; ShortestPathAlgorithm spAlgorithm = null;
try { try {
spAlgorithm = ShortestPathAlgorithmFactory.createAlgorithm(evt.getAlgorithmClass(), data); spAlgorithm = (ShortestPathAlgorithm) AlgorithmFactory.createAlgorithm(evt.getAlgorithmClass(),
data);
} }
catch (Exception e1) { catch (Exception e1) {
JOptionPane.showMessageDialog(MainWindow.this, JOptionPane.showMessageDialog(MainWindow.this,

View File

@ -281,6 +281,13 @@ public class NodesInputPanel extends JPanel
components.add(clickButton); components.add(clickButton);
} }
/**
* @return Current graph associated with this input panel.
*/
protected Graph getGraph() {
return this.graph;
}
/** /**
* @return The node for the given text field, or null if the content of the text * @return The node for the given text field, or null if the content of the text
* field is invalid. * field is invalid.

View File

@ -24,8 +24,9 @@ import javax.swing.JPanel;
import javax.swing.JRadioButton; import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import org.insa.algo.AbstractAlgorithm;
import org.insa.algo.AlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory;
import org.insa.algo.shortestpath.ShortestPathData.ArcFilter; import org.insa.algo.shortestpath.ShortestPathData.ArcFilter;
import org.insa.algo.shortestpath.ShortestPathData.Mode; import org.insa.algo.shortestpath.ShortestPathData.Mode;
import org.insa.graph.Arc; import org.insa.graph.Arc;
@ -51,20 +52,19 @@ public class ShortestPathPanel extends JPanel {
protected static final int START_EVENT_ID = 0x1; protected static final int START_EVENT_ID = 0x1;
private final Node origin, destination; private final List<Node> nodes;
private final Mode mode; private final Mode mode;
private final Class<? extends ShortestPathAlgorithm> algoClass; private final Class<? extends AbstractAlgorithm<?>> algoClass;
private final ArcFilter arcFilter; private final ArcFilter arcFilter;
private final boolean graphicVisualization; private final boolean graphicVisualization;
private final boolean textualVisualization; private final boolean textualVisualization;
public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin, Node destination, public StartActionEvent(Class<? extends AbstractAlgorithm<?>> algoClass, List<Node> nodes, Mode mode,
Mode mode, ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) { ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) {
super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND); super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND);
this.origin = origin; this.nodes = nodes;
this.destination = destination;
this.mode = mode; this.mode = mode;
this.algoClass = algoClass; this.algoClass = algoClass;
this.graphicVisualization = graphicVisualization; this.graphicVisualization = graphicVisualization;
@ -73,17 +73,10 @@ public class ShortestPathPanel extends JPanel {
} }
/** /**
* @return Origin node associated with this event. * @return Nodes associated with this event.
*/ */
public Node getOrigin() { public List<Node> getNodes() {
return this.origin; return this.nodes;
}
/**
* @return Destination node associated with this event.
*/
public Node getDestination() {
return this.destination;
} }
/** /**
@ -103,7 +96,7 @@ public class ShortestPathPanel extends JPanel {
/** /**
* @return Algorithm class associated with this event. * @return Algorithm class associated with this event.
*/ */
public Class<? extends ShortestPathAlgorithm> getAlgorithmClass() { public Class<? extends AbstractAlgorithm<?>> getAlgorithmClass() {
return this.algoClass; return this.algoClass;
} }
@ -159,7 +152,7 @@ public class ShortestPathPanel extends JPanel {
// Add algorithm selection // Add algorithm selection
JComboBox<String> algoSelect = new JComboBox<>( JComboBox<String> algoSelect = new JComboBox<>(
ShortestPathAlgorithmFactory.getAlgorithmNames().toArray(new String[0])); AlgorithmFactory.getAlgorithmNames(ShortestPathAlgorithm.class).toArray(new String[0]));
algoSelect.setBackground(Color.WHITE); algoSelect.setBackground(Color.WHITE);
algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT); algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
add(algoSelect); add(algoSelect);
@ -272,14 +265,13 @@ public class ShortestPathPanel extends JPanel {
startAlgoButton.addActionListener(new ActionListener() { startAlgoButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
List<Node> nodes = nodesInputPanel.getNodeForInputs();
Node origin = nodes.get(0), destination = nodes.get(1);
Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME; Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME;
for (ActionListener lis: startActionListeners) { for (ActionListener lis: startActionListeners) {
lis.actionPerformed(new StartActionEvent( lis.actionPerformed(new StartActionEvent(
ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()), AlgorithmFactory.getAlgorithmClass(ShortestPathAlgorithm.class,
origin, destination, mode, (ArcFilter) arcFilterSelect.getSelectedItem(), (String) algoSelect.getSelectedItem()),
nodesInputPanel.getNodeForInputs(), mode, (ArcFilter) arcFilterSelect.getSelectedItem(),
graphicObserver.isSelected(), textObserver.isSelected())); graphicObserver.isSelected(), textObserver.isSelected()));
} }
} }