From 6d8c2462ab2ab248734fb38acbedd49ed5368589 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 3 Mar 2018 19:26:56 +0100 Subject: [PATCH] Improve reflections to make the input panel more generic. --- src/main/org/insa/graphics/MainWindow.java | 7 ++-- .../org/insa/graphics/NodesInputPanel.java | 7 ++++ .../org/insa/graphics/ShortestPathPanel.java | 38 ++++++++----------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index ff41e02..c4c6522 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -42,8 +42,8 @@ import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileNameExtensionFilter; +import org.insa.algo.AlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathAlgorithm; -import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathData; import org.insa.algo.shortestpath.ShortestPathGraphicObserver; import org.insa.algo.shortestpath.ShortestPathSolution; @@ -153,12 +153,13 @@ public class MainWindow extends JFrame { @Override public void actionPerformed(ActionEvent 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()); ShortestPathAlgorithm spAlgorithm = null; try { - spAlgorithm = ShortestPathAlgorithmFactory.createAlgorithm(evt.getAlgorithmClass(), data); + spAlgorithm = (ShortestPathAlgorithm) AlgorithmFactory.createAlgorithm(evt.getAlgorithmClass(), + data); } catch (Exception e1) { JOptionPane.showMessageDialog(MainWindow.this, diff --git a/src/main/org/insa/graphics/NodesInputPanel.java b/src/main/org/insa/graphics/NodesInputPanel.java index 9dc0010..cbbf9b3 100644 --- a/src/main/org/insa/graphics/NodesInputPanel.java +++ b/src/main/org/insa/graphics/NodesInputPanel.java @@ -281,6 +281,13 @@ public class NodesInputPanel extends JPanel 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 * field is invalid. diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index 7d34846..5ff48b7 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -24,8 +24,9 @@ import javax.swing.JPanel; import javax.swing.JRadioButton; 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.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathData.ArcFilter; import org.insa.algo.shortestpath.ShortestPathData.Mode; import org.insa.graph.Arc; @@ -51,20 +52,19 @@ public class ShortestPathPanel extends JPanel { protected static final int START_EVENT_ID = 0x1; - private final Node origin, destination; + private final List nodes; private final Mode mode; - private final Class algoClass; + private final Class> algoClass; private final ArcFilter arcFilter; private final boolean graphicVisualization; private final boolean textualVisualization; - public StartActionEvent(Class algoClass, Node origin, Node destination, - Mode mode, ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) { + public StartActionEvent(Class> algoClass, List nodes, Mode mode, + ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) { super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND); - this.origin = origin; - this.destination = destination; + this.nodes = nodes; this.mode = mode; this.algoClass = algoClass; 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() { - return this.origin; - } - - /** - * @return Destination node associated with this event. - */ - public Node getDestination() { - return this.destination; + public List getNodes() { + return this.nodes; } /** @@ -103,7 +96,7 @@ public class ShortestPathPanel extends JPanel { /** * @return Algorithm class associated with this event. */ - public Class getAlgorithmClass() { + public Class> getAlgorithmClass() { return this.algoClass; } @@ -159,7 +152,7 @@ public class ShortestPathPanel extends JPanel { // Add algorithm selection JComboBox algoSelect = new JComboBox<>( - ShortestPathAlgorithmFactory.getAlgorithmNames().toArray(new String[0])); + AlgorithmFactory.getAlgorithmNames(ShortestPathAlgorithm.class).toArray(new String[0])); algoSelect.setBackground(Color.WHITE); algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT); add(algoSelect); @@ -272,14 +265,13 @@ public class ShortestPathPanel extends JPanel { startAlgoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - List nodes = nodesInputPanel.getNodeForInputs(); - Node origin = nodes.get(0), destination = nodes.get(1); Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME; for (ActionListener lis: startActionListeners) { lis.actionPerformed(new StartActionEvent( - ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()), - origin, destination, mode, (ArcFilter) arcFilterSelect.getSelectedItem(), + AlgorithmFactory.getAlgorithmClass(ShortestPathAlgorithm.class, + (String) algoSelect.getSelectedItem()), + nodesInputPanel.getNodeForInputs(), mode, (ArcFilter) arcFilterSelect.getSelectedItem(), graphicObserver.isSelected(), textObserver.isSelected())); } }