Add possibility to select in the interface the filtering of the arcs for ShortestPath.

This commit is contained in:
Holt59 2018-03-03 00:27:54 +01:00
parent 5b3690b982
commit 6768374b92
2 changed files with 113 additions and 20 deletions

View File

@ -19,6 +19,7 @@ import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.JButton; import javax.swing.JButton;
@ -82,6 +83,15 @@ public class MainWindow extends JFrame {
*/ */
private static final int THREAD_TIMER_DELAY = 1000; // in milliseconds private static final int THREAD_TIMER_DELAY = 1000; // in milliseconds
private static final String DEFAULT_MAP_FOLDER_KEY = "DefaultMapFolder";
private static final String DEFAULT_MAP_FOLDER_INSA = "/home/commetud/...";
private static final String DEFAULT_PATH_FOLDER_KEY = "DefaultPathFolder";
private static final String DEFAULT_PATH_FOLDER_INSA = "/home/commetud/...";
// Preferences
private Preferences preferences = Preferences.userRoot().node(getClass().getName());
// Current graph. // Current graph.
protected Graph graph; protected Graph graph;
@ -144,7 +154,7 @@ public class MainWindow extends JFrame {
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.getOrigin(), evt.getDestination(),
evt.getMode()); evt.getMode(), evt.getArcFilter());
ShortestPathAlgorithm spAlgorithm = null; ShortestPathAlgorithm spAlgorithm = null;
try { try {
@ -451,11 +461,20 @@ public class MainWindow extends JFrame {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files", "mapgr"); FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files", "mapgr");
chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); File mapFolder = new File(preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
if (!mapFolder.exists()) {
mapFolder = new File(System.getProperty("user.dir"));
}
chooser.setCurrentDirectory(mapFolder);
chooser.setFileFilter(filter); chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
graphFilePath = chooser.getSelectedFile().getAbsolutePath(); graphFilePath = chooser.getSelectedFile().getAbsolutePath();
// Check...
if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_MAP_FOLDER_KEY, chooser.getSelectedFile().getParent());
}
DataInputStream stream; DataInputStream stream;
try { try {
stream = new DataInputStream( stream = new DataInputStream(
@ -478,11 +497,20 @@ public class MainWindow extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Path & compressed path files", "path", FileNameExtensionFilter filter = new FileNameExtensionFilter("Path & compressed path files", "path");
"path.gz"); File pathFolder = new File(preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA));
chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); if (!pathFolder.exists()) {
pathFolder = new File(System.getProperty("user.dir"));
}
chooser.setCurrentDirectory(pathFolder);
chooser.setFileFilter(filter); chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
// Check & Update
if (chooser.getSelectedFile().exists()) {
preferences.put(DEFAULT_PATH_FOLDER_KEY, chooser.getSelectedFile().getParent());
}
BinaryPathReader reader; BinaryPathReader reader;
try { try {
reader = new BinaryPathReader(new DataInputStream( reader = new BinaryPathReader(new DataInputStream(

View File

@ -3,7 +3,8 @@ 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.GridBagConstraints;
import java.awt.GridBagLayout;
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;
@ -25,8 +26,11 @@ import javax.swing.border.EmptyBorder;
import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm;
import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory; import org.insa.algo.shortestpath.ShortestPathAlgorithmFactory;
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.Node; import org.insa.graph.Node;
import org.insa.graph.RoadInformation.AccessMode;
import org.insa.graphics.NodesInputPanel.InputChangedEvent; import org.insa.graphics.NodesInputPanel.InputChangedEvent;
public class ShortestPathPanel extends JPanel { public class ShortestPathPanel extends JPanel {
@ -51,12 +55,13 @@ 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 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, public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin, Node destination,
Node destination, Mode mode, boolean graphicVisualization, Mode mode, ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) {
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;
@ -64,6 +69,7 @@ public class ShortestPathPanel extends JPanel {
this.algoClass = algoClass; this.algoClass = algoClass;
this.graphicVisualization = graphicVisualization; this.graphicVisualization = graphicVisualization;
this.textualVisualization = textualVisualization; this.textualVisualization = textualVisualization;
this.arcFilter = arcFilter;
} }
/** /**
@ -87,6 +93,13 @@ public class ShortestPathPanel extends JPanel {
return this.mode; return this.mode;
} }
/**
* @return Arc filter associated with this event.
*/
public ArcFilter getArcFilter() {
return this.arcFilter;
}
/** /**
* @return Algorithm class associated with this event. * @return Algorithm class associated with this event.
*/ */
@ -161,10 +174,34 @@ public class ShortestPathPanel extends JPanel {
add(this.nodesInputPanel); add(this.nodesInputPanel);
components.add(this.nodesInputPanel); components.add(this.nodesInputPanel);
JComboBox<ArcFilter> arcFilterSelect = new JComboBox<>(new ArcFilter[] { new ArcFilter() {
@Override
public boolean isAllowed(Arc arc) {
return true;
}
@Override
public String toString() {
return "All arcs are allowed";
}
}, new ArcFilter() {
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedFor(AccessMode.MOTORCAR)
&& !arc.getRoadInformation().getAccessRestrictions().isPrivate();
}
@Override
public String toString() {
return "Non-private roads allowed for motorcars";
}
} });
arcFilterSelect.setBackground(Color.WHITE);
// Add mode selection // Add mode selection
JPanel modeAndObserverPanel = new JPanel(); JPanel modeAndObserverPanel = new JPanel();
modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT); modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
modeAndObserverPanel.setLayout(new GridLayout(2, 3)); modeAndObserverPanel.setLayout(new GridBagLayout());
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");
@ -176,15 +213,44 @@ public class ShortestPathPanel extends JPanel {
graphicObserver.setSelected(true); graphicObserver.setSelected(true);
JCheckBox textObserver = new JCheckBox("Textual"); JCheckBox textObserver = new JCheckBox("Textual");
modeAndObserverPanel.add(new JLabel("Mode: ")); GridBagConstraints c = new GridBagConstraints();
modeAndObserverPanel.add(lengthModeButton);
modeAndObserverPanel.add(timeModeButton); c.fill = GridBagConstraints.HORIZONTAL;
modeAndObserverPanel.add(new JLabel("Visualization: "));
modeAndObserverPanel.add(graphicObserver); c.gridx = 0;
modeAndObserverPanel.add(textObserver); c.gridy = 0;
c.weightx = 0;
modeAndObserverPanel.add(new JLabel("Mode: "), c);
c.gridx = 1;
c.weightx = 1;
modeAndObserverPanel.add(lengthModeButton, c);
c.gridx = 2;
c.weightx = 1;
modeAndObserverPanel.add(timeModeButton, c);
c.gridy = 2;
c.gridx = 0;
c.weightx = 0;
modeAndObserverPanel.add(new JLabel("Visualization: "), c);
c.gridx = 1;
c.weightx = 1;
modeAndObserverPanel.add(graphicObserver, c);
c.gridx = 2;
c.weightx = 1;
modeAndObserverPanel.add(textObserver, c);
c.gridy = 1;
c.gridx = 0;
c.weightx = 0;
modeAndObserverPanel.add(new JLabel("Restrictions: "), c);
c.gridx = 1;
c.gridwidth = 2;
c.weightx = 1;
modeAndObserverPanel.add(arcFilterSelect, c);
components.add(timeModeButton); components.add(timeModeButton);
components.add(lengthModeButton); components.add(lengthModeButton);
components.add(arcFilterSelect);
components.add(graphicObserver); components.add(graphicObserver);
components.add(textObserver); components.add(textObserver);
@ -211,10 +277,9 @@ public class ShortestPathPanel extends JPanel {
for (ActionListener lis: startActionListeners) { for (ActionListener lis: startActionListeners) {
lis.actionPerformed(new StartActionEvent( lis.actionPerformed(new StartActionEvent(
ShortestPathAlgorithmFactory.getAlgorithmClass( ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()),
(String) algoSelect.getSelectedItem()), origin, destination, mode, (ArcFilter) arcFilterSelect.getSelectedItem(),
origin, destination, mode, graphicObserver.isSelected(), graphicObserver.isSelected(), textObserver.isSelected()));
textObserver.isSelected()));
} }
} }
}); });