Add option to specify observers for shortest-path.

This commit is contained in:
Mikael Capelle 2018-03-02 11:45:43 +01:00
parent 19832321aa
commit e111b5f0e6
2 changed files with 60 additions and 18 deletions

View File

@ -46,6 +46,7 @@ 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;
import org.insa.algo.shortestpath.ShortestPathTextObserver;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentTextObserver; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentTextObserver;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
@ -145,11 +146,11 @@ public class MainWindow extends JFrame {
StartActionEvent evt = (StartActionEvent) e; StartActionEvent evt = (StartActionEvent) e;
ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(),
evt.getDestination(), evt.getMode()); evt.getDestination(), evt.getMode());
ShortestPathAlgorithm spAlgorithm = null;
try { try {
ShortestPathAlgorithm spAlgorithm = ShortestPathAlgorithmFactory spAlgorithm = ShortestPathAlgorithmFactory
.createAlgorithm(evt.getAlgorithmClass(), data); .createAlgorithm(evt.getAlgorithmClass(), data);
spPanel.setEnabled(false);
launchShortestPathThread(spAlgorithm);
} }
catch (Exception e1) { catch (Exception e1) {
JOptionPane.showMessageDialog(MainWindow.this, JOptionPane.showMessageDialog(MainWindow.this,
@ -157,6 +158,17 @@ public class MainWindow extends JFrame {
"Internal error: Algorithm instantiation failure", "Internal error: Algorithm instantiation failure",
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
e1.printStackTrace(); 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) { private void launchShortestPathThread(ShortestPathAlgorithm spAlgorithm) {
spAlgorithm.addObserver(new ShortestPathGraphicObserver(drawing));
// algo.addObserver(new ShortestPathTextObserver(printStream));
launchThread(new Runnable() { launchThread(new Runnable() {
@Override @Override
public void run() { public void run() {

View File

@ -3,6 +3,7 @@ package org.insa.graphics;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Font; import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
@ -14,6 +15,7 @@ import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
@ -49,13 +51,19 @@ public class ShortestPathPanel extends JPanel {
private final Mode mode; private final Mode mode;
private final Class<? extends ShortestPathAlgorithm> algoClass; private final Class<? extends ShortestPathAlgorithm> algoClass;
private final boolean graphicVisualization;
private final boolean textualVisualization;
public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin, public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin,
Node destination, Mode mode) { Node destination, Mode mode, 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.origin = origin;
this.destination = destination; this.destination = destination;
this.mode = mode; this.mode = mode;
this.algoClass = algoClass; this.algoClass = algoClass;
this.graphicVisualization = graphicVisualization;
this.textualVisualization = textualVisualization;
} }
/** /**
@ -86,6 +94,20 @@ public class ShortestPathPanel extends JPanel {
return this.algoClass; 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. // Input panels for node.
@ -140,24 +162,33 @@ public class ShortestPathPanel extends JPanel {
components.add(this.nodesInputPanel); components.add(this.nodesInputPanel);
// Add mode selection // Add mode selection
JPanel modePanel = new JPanel(); JPanel modeAndObserverPanel = new JPanel();
modePanel.setAlignmentX(Component.LEFT_ALIGNMENT); modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
modePanel.setLayout(new BoxLayout(modePanel, BoxLayout.LINE_AXIS)); modeAndObserverPanel.setLayout(new GridLayout(2, 3));
JRadioButton lengthModeButton = new JRadioButton("Length"); JRadioButton lengthModeButton = new JRadioButton("Length");
lengthModeButton.setSelected(true); lengthModeButton.setSelected(true);
JRadioButton timeModeButton = new JRadioButton("Time"); JRadioButton timeModeButton = new JRadioButton("Time");
ButtonGroup group = new ButtonGroup(); ButtonGroup group = new ButtonGroup();
group.add(lengthModeButton); group.add(lengthModeButton);
group.add(timeModeButton); 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(timeModeButton);
components.add(lengthModeButton); components.add(lengthModeButton);
components.add(graphicObserver);
components.add(textObserver);
add(modeAndObserverPanel);
solutionPanel = new ShortestPathSolutionPanel(parent); solutionPanel = new ShortestPathSolutionPanel(parent);
solutionPanel.setAlignmentX(Component.LEFT_ALIGNMENT); solutionPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
@ -180,9 +211,10 @@ public class ShortestPathPanel extends JPanel {
for (ActionListener lis: startActionListeners) { for (ActionListener lis: startActionListeners) {
lis.actionPerformed(new StartActionEvent( lis.actionPerformed(new StartActionEvent(
ShortestPathAlgorithmFactory ShortestPathAlgorithmFactory.getAlgorithmClass(
.getAlgorithmClass((String) algoSelect.getSelectedItem()), (String) algoSelect.getSelectedItem()),
origin, destination, mode)); origin, destination, mode, graphicObserver.isSelected(),
textObserver.isSelected()));
} }
} }
}); });