From e111b5f0e6e4afb8b19abe6e286e153728422620 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 2 Mar 2018 11:45:43 +0100 Subject: [PATCH] Add option to specify observers for shortest-path. --- src/main/org/insa/graphics/MainWindow.java | 20 +++++-- .../org/insa/graphics/ShortestPathPanel.java | 58 ++++++++++++++----- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index aeb9853..5027833 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -46,6 +46,7 @@ import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathData; import org.insa.algo.shortestpath.ShortestPathGraphicObserver; import org.insa.algo.shortestpath.ShortestPathSolution; +import org.insa.algo.shortestpath.ShortestPathTextObserver; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentTextObserver; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm; @@ -145,11 +146,11 @@ public class MainWindow extends JFrame { StartActionEvent evt = (StartActionEvent) e; ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), evt.getDestination(), evt.getMode()); + + ShortestPathAlgorithm spAlgorithm = null; try { - ShortestPathAlgorithm spAlgorithm = ShortestPathAlgorithmFactory + spAlgorithm = ShortestPathAlgorithmFactory .createAlgorithm(evt.getAlgorithmClass(), data); - spPanel.setEnabled(false); - launchShortestPathThread(spAlgorithm); } catch (Exception e1) { JOptionPane.showMessageDialog(MainWindow.this, @@ -157,6 +158,17 @@ public class MainWindow extends JFrame { "Internal error: Algorithm instantiation failure", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); + return; + } + + spPanel.setEnabled(false); + launchShortestPathThread(spAlgorithm); + + if (evt.isGraphicVisualizationEnabled()) { + spAlgorithm.addObserver(new ShortestPathGraphicObserver(drawing)); + } + if (evt.isTextualVisualizationEnabled()) { + spAlgorithm.addObserver(new ShortestPathTextObserver(printStream)); } } }); @@ -274,8 +286,6 @@ public class MainWindow extends JFrame { } private void launchShortestPathThread(ShortestPathAlgorithm spAlgorithm) { - spAlgorithm.addObserver(new ShortestPathGraphicObserver(drawing)); - // algo.addObserver(new ShortestPathTextObserver(printStream)); launchThread(new Runnable() { @Override public void run() { diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index 9636056..e2c1ae3 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -3,6 +3,7 @@ package org.insa.graphics; import java.awt.Color; import java.awt.Component; import java.awt.Font; +import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; @@ -14,6 +15,7 @@ import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; @@ -49,13 +51,19 @@ public class ShortestPathPanel extends JPanel { private final Mode mode; private final Class algoClass; + private final boolean graphicVisualization; + private final boolean textualVisualization; + public StartActionEvent(Class algoClass, Node origin, - Node destination, Mode mode) { + Node destination, Mode mode, boolean graphicVisualization, + boolean textualVisualization) { super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND); this.origin = origin; this.destination = destination; this.mode = mode; this.algoClass = algoClass; + this.graphicVisualization = graphicVisualization; + this.textualVisualization = textualVisualization; } /** @@ -86,6 +94,20 @@ public class ShortestPathPanel extends JPanel { return this.algoClass; } + /** + * @return true if graphic visualization is enabled. + */ + public boolean isGraphicVisualizationEnabled() { + return this.graphicVisualization; + } + + /** + * @return true if textual visualization is enabled. + */ + public boolean isTextualVisualizationEnabled() { + return this.textualVisualization; + } + }; // Input panels for node. @@ -140,24 +162,33 @@ public class ShortestPathPanel extends JPanel { components.add(this.nodesInputPanel); // Add mode selection - JPanel modePanel = new JPanel(); - modePanel.setAlignmentX(Component.LEFT_ALIGNMENT); - modePanel.setLayout(new BoxLayout(modePanel, BoxLayout.LINE_AXIS)); + JPanel modeAndObserverPanel = new JPanel(); + modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT); + modeAndObserverPanel.setLayout(new GridLayout(2, 3)); JRadioButton lengthModeButton = new JRadioButton("Length"); lengthModeButton.setSelected(true); JRadioButton timeModeButton = new JRadioButton("Time"); ButtonGroup group = new ButtonGroup(); group.add(lengthModeButton); group.add(timeModeButton); - modePanel.add(Box.createHorizontalGlue()); - modePanel.add(lengthModeButton); - modePanel.add(Box.createHorizontalGlue()); - modePanel.add(timeModeButton); - modePanel.add(Box.createHorizontalGlue()); - add(modePanel); + JCheckBox graphicObserver = new JCheckBox("Graphic"); + graphicObserver.setSelected(true); + JCheckBox textObserver = new JCheckBox("Textual"); + + modeAndObserverPanel.add(new JLabel("Mode: ")); + modeAndObserverPanel.add(lengthModeButton); + modeAndObserverPanel.add(timeModeButton); + modeAndObserverPanel.add(new JLabel("Visualization: ")); + modeAndObserverPanel.add(graphicObserver); + modeAndObserverPanel.add(textObserver); + components.add(timeModeButton); components.add(lengthModeButton); + components.add(graphicObserver); + components.add(textObserver); + + add(modeAndObserverPanel); solutionPanel = new ShortestPathSolutionPanel(parent); solutionPanel.setAlignmentX(Component.LEFT_ALIGNMENT); @@ -180,9 +211,10 @@ public class ShortestPathPanel extends JPanel { for (ActionListener lis: startActionListeners) { lis.actionPerformed(new StartActionEvent( - ShortestPathAlgorithmFactory - .getAlgorithmClass((String) algoSelect.getSelectedItem()), - origin, destination, mode)); + ShortestPathAlgorithmFactory.getAlgorithmClass( + (String) algoSelect.getSelectedItem()), + origin, destination, mode, graphicObserver.isSelected(), + textObserver.isSelected())); } } });