From 6768374b92cceb30ab853aae947087790586c919 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 3 Mar 2018 00:27:54 +0100 Subject: [PATCH] Add possibility to select in the interface the filtering of the arcs for ShortestPath. --- src/main/org/insa/graphics/MainWindow.java | 38 +++++++- .../org/insa/graphics/ShortestPathPanel.java | 95 ++++++++++++++++--- 2 files changed, 113 insertions(+), 20 deletions(-) diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index a5a29ec..10990be 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; +import java.util.prefs.Preferences; import javax.swing.BorderFactory; 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 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. protected Graph graph; @@ -144,7 +154,7 @@ public class MainWindow extends JFrame { public void actionPerformed(ActionEvent e) { StartActionEvent evt = (StartActionEvent) e; ShortestPathData data = new ShortestPathData(graph, evt.getOrigin(), evt.getDestination(), - evt.getMode()); + evt.getMode(), evt.getArcFilter()); ShortestPathAlgorithm spAlgorithm = null; try { @@ -451,11 +461,20 @@ public class MainWindow extends JFrame { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); 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); if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { graphFilePath = chooser.getSelectedFile().getAbsolutePath(); + // Check... + if (chooser.getSelectedFile().exists()) { + preferences.put(DEFAULT_MAP_FOLDER_KEY, chooser.getSelectedFile().getParent()); + } + DataInputStream stream; try { stream = new DataInputStream( @@ -478,11 +497,20 @@ public class MainWindow extends JFrame { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); - FileNameExtensionFilter filter = new FileNameExtensionFilter("Path & compressed path files", "path", - "path.gz"); - chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Path & compressed path files", "path"); + File pathFolder = new File(preferences.get(DEFAULT_PATH_FOLDER_KEY, DEFAULT_PATH_FOLDER_INSA)); + if (!pathFolder.exists()) { + pathFolder = new File(System.getProperty("user.dir")); + } + chooser.setCurrentDirectory(pathFolder); chooser.setFileFilter(filter); 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; try { reader = new BinaryPathReader(new DataInputStream( diff --git a/src/main/org/insa/graphics/ShortestPathPanel.java b/src/main/org/insa/graphics/ShortestPathPanel.java index e2c1ae3..27f273d 100644 --- a/src/main/org/insa/graphics/ShortestPathPanel.java +++ b/src/main/org/insa/graphics/ShortestPathPanel.java @@ -3,7 +3,8 @@ package org.insa.graphics; import java.awt.Color; import java.awt.Component; 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.ActionListener; 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.ShortestPathAlgorithmFactory; +import org.insa.algo.shortestpath.ShortestPathData.ArcFilter; import org.insa.algo.shortestpath.ShortestPathData.Mode; +import org.insa.graph.Arc; import org.insa.graph.Node; +import org.insa.graph.RoadInformation.AccessMode; import org.insa.graphics.NodesInputPanel.InputChangedEvent; public class ShortestPathPanel extends JPanel { @@ -51,12 +55,13 @@ public class ShortestPathPanel extends JPanel { private final Mode mode; 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, boolean graphicVisualization, - boolean textualVisualization) { + public StartActionEvent(Class algoClass, Node origin, Node destination, + Mode mode, ArcFilter arcFilter, boolean graphicVisualization, boolean textualVisualization) { super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND); this.origin = origin; this.destination = destination; @@ -64,6 +69,7 @@ public class ShortestPathPanel extends JPanel { this.algoClass = algoClass; this.graphicVisualization = graphicVisualization; this.textualVisualization = textualVisualization; + this.arcFilter = arcFilter; } /** @@ -87,6 +93,13 @@ public class ShortestPathPanel extends JPanel { return this.mode; } + /** + * @return Arc filter associated with this event. + */ + public ArcFilter getArcFilter() { + return this.arcFilter; + } + /** * @return Algorithm class associated with this event. */ @@ -161,10 +174,34 @@ public class ShortestPathPanel extends JPanel { add(this.nodesInputPanel); components.add(this.nodesInputPanel); + JComboBox 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 JPanel modeAndObserverPanel = new JPanel(); modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT); - modeAndObserverPanel.setLayout(new GridLayout(2, 3)); + modeAndObserverPanel.setLayout(new GridBagLayout()); JRadioButton lengthModeButton = new JRadioButton("Length"); lengthModeButton.setSelected(true); JRadioButton timeModeButton = new JRadioButton("Time"); @@ -176,15 +213,44 @@ public class ShortestPathPanel extends JPanel { 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); + GridBagConstraints c = new GridBagConstraints(); + + c.fill = GridBagConstraints.HORIZONTAL; + + c.gridx = 0; + 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(lengthModeButton); + components.add(arcFilterSelect); components.add(graphicObserver); components.add(textObserver); @@ -211,10 +277,9 @@ public class ShortestPathPanel extends JPanel { for (ActionListener lis: startActionListeners) { lis.actionPerformed(new StartActionEvent( - ShortestPathAlgorithmFactory.getAlgorithmClass( - (String) algoSelect.getSelectedItem()), - origin, destination, mode, graphicObserver.isSelected(), - textObserver.isSelected())); + ShortestPathAlgorithmFactory.getAlgorithmClass((String) algoSelect.getSelectedItem()), + origin, destination, mode, (ArcFilter) arcFilterSelect.getSelectedItem(), + graphicObserver.isSelected(), textObserver.isSelected())); } } });