From bcdaf54a63609dd9fc87aeb7c107072b5e143bf3 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Wed, 7 Mar 2018 14:47:22 +0100 Subject: [PATCH] Add algorithms for CarPooling and PackageSwitch. --- .../algo/carpooling/CarPoolingAlgorithm.java | 24 ++++++++ .../insa/algo/carpooling/CarPoolingData.java | 12 ++++ .../carpooling/CarPoolingGraphicObserver.java | 5 ++ .../algo/carpooling/CarPoolingObserver.java | 5 ++ .../algo/carpooling/CarPoolingSolution.java | 11 ++++ .../carpooling/CarPoolingTextObserver.java | 5 ++ .../packageswitch/PackageSwitchAlgorithm.java | 29 +++++++++ .../algo/packageswitch/PackageSwitchData.java | 12 ++++ .../PackageSwitchGraphicObserver.java | 5 ++ .../packageswitch/PackageSwitchObserver.java | 5 ++ .../packageswitch/PackageSwitchSolution.java | 11 ++++ .../PackageSwitchTextObserver.java | 5 ++ src/main/org/insa/graphics/MainWindow.java | 59 +++++++++++++++---- .../org/insa/graphics/NodesInputPanel.java | 6 +- src/main/org/insa/graphics/PathsPanel.java | 4 -- 15 files changed, 181 insertions(+), 17 deletions(-) create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingAlgorithm.java create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingData.java create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingGraphicObserver.java create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingObserver.java create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingSolution.java create mode 100644 src/main/org/insa/algo/carpooling/CarPoolingTextObserver.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchAlgorithm.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchData.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchGraphicObserver.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchObserver.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchSolution.java create mode 100644 src/main/org/insa/algo/packageswitch/PackageSwitchTextObserver.java diff --git a/src/main/org/insa/algo/carpooling/CarPoolingAlgorithm.java b/src/main/org/insa/algo/carpooling/CarPoolingAlgorithm.java new file mode 100644 index 0000000..36f7a04 --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingAlgorithm.java @@ -0,0 +1,24 @@ +package org.insa.algo.carpooling; + +import org.insa.algo.AbstractAlgorithm; + +public abstract class CarPoolingAlgorithm extends AbstractAlgorithm { + + protected CarPoolingAlgorithm(CarPoolingData data) { + super(data); + } + + @Override + public CarPoolingSolution run() { + return (CarPoolingSolution) super.run(); + } + + @Override + protected abstract CarPoolingSolution doRun(); + + @Override + public CarPoolingData getInputData() { + return (CarPoolingData) super.getInputData(); + } + +} diff --git a/src/main/org/insa/algo/carpooling/CarPoolingData.java b/src/main/org/insa/algo/carpooling/CarPoolingData.java new file mode 100644 index 0000000..048ceeb --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingData.java @@ -0,0 +1,12 @@ +package org.insa.algo.carpooling; + +import org.insa.algo.AbstractInputData; +import org.insa.graph.Graph; + +public class CarPoolingData extends AbstractInputData { + + protected CarPoolingData(Graph graph, Mode mode, ArcFilter arcFilter) { + super(graph, mode, arcFilter); + } + +} diff --git a/src/main/org/insa/algo/carpooling/CarPoolingGraphicObserver.java b/src/main/org/insa/algo/carpooling/CarPoolingGraphicObserver.java new file mode 100644 index 0000000..a0ee5d9 --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingGraphicObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.carpooling; + +public class CarPoolingGraphicObserver implements CarPoolingObserver { + +} diff --git a/src/main/org/insa/algo/carpooling/CarPoolingObserver.java b/src/main/org/insa/algo/carpooling/CarPoolingObserver.java new file mode 100644 index 0000000..e7249e1 --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.carpooling; + +public interface CarPoolingObserver { + +} diff --git a/src/main/org/insa/algo/carpooling/CarPoolingSolution.java b/src/main/org/insa/algo/carpooling/CarPoolingSolution.java new file mode 100644 index 0000000..07efa75 --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingSolution.java @@ -0,0 +1,11 @@ +package org.insa.algo.carpooling; + +import org.insa.algo.AbstractSolution; + +public class CarPoolingSolution extends AbstractSolution { + + protected CarPoolingSolution(CarPoolingData data, Status status) { + super(data, status); + } + +} diff --git a/src/main/org/insa/algo/carpooling/CarPoolingTextObserver.java b/src/main/org/insa/algo/carpooling/CarPoolingTextObserver.java new file mode 100644 index 0000000..386664e --- /dev/null +++ b/src/main/org/insa/algo/carpooling/CarPoolingTextObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.carpooling; + +public class CarPoolingTextObserver implements CarPoolingObserver { + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchAlgorithm.java b/src/main/org/insa/algo/packageswitch/PackageSwitchAlgorithm.java new file mode 100644 index 0000000..76d4786 --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchAlgorithm.java @@ -0,0 +1,29 @@ +package org.insa.algo.packageswitch; + +import org.insa.algo.AbstractAlgorithm; + +public abstract class PackageSwitchAlgorithm extends AbstractAlgorithm { + + /** + * Create a new PackageSwitchAlgorithm with the given data. + * + * @param data + */ + protected PackageSwitchAlgorithm(PackageSwitchData data) { + super(data); + } + + @Override + public PackageSwitchSolution run() { + return (PackageSwitchSolution) super.run(); + } + + @Override + protected abstract PackageSwitchSolution doRun(); + + @Override + public PackageSwitchData getInputData() { + return (PackageSwitchData) super.getInputData(); + } + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchData.java b/src/main/org/insa/algo/packageswitch/PackageSwitchData.java new file mode 100644 index 0000000..8f7edcb --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchData.java @@ -0,0 +1,12 @@ +package org.insa.algo.packageswitch; + +import org.insa.algo.AbstractInputData; +import org.insa.graph.Graph; + +public class PackageSwitchData extends AbstractInputData { + + protected PackageSwitchData(Graph graph, Mode mode, ArcFilter arcFilter) { + super(graph, mode, arcFilter); + } + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchGraphicObserver.java b/src/main/org/insa/algo/packageswitch/PackageSwitchGraphicObserver.java new file mode 100644 index 0000000..1750ae6 --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchGraphicObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.packageswitch; + +public class PackageSwitchGraphicObserver implements PackageSwitchObserver { + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchObserver.java b/src/main/org/insa/algo/packageswitch/PackageSwitchObserver.java new file mode 100644 index 0000000..4f29e7b --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.packageswitch; + +public interface PackageSwitchObserver { + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchSolution.java b/src/main/org/insa/algo/packageswitch/PackageSwitchSolution.java new file mode 100644 index 0000000..5fa2f07 --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchSolution.java @@ -0,0 +1,11 @@ +package org.insa.algo.packageswitch; + +import org.insa.algo.AbstractSolution; + +public class PackageSwitchSolution extends AbstractSolution { + + protected PackageSwitchSolution(PackageSwitchData data, Status status) { + super(data, status); + } + +} diff --git a/src/main/org/insa/algo/packageswitch/PackageSwitchTextObserver.java b/src/main/org/insa/algo/packageswitch/PackageSwitchTextObserver.java new file mode 100644 index 0000000..ba5b584 --- /dev/null +++ b/src/main/org/insa/algo/packageswitch/PackageSwitchTextObserver.java @@ -0,0 +1,5 @@ +package org.insa.algo.packageswitch; + +public class PackageSwitchTextObserver implements PackageSwitchObserver { + +} diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index 6117b16..78efb21 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -44,6 +44,8 @@ import javax.swing.filechooser.FileNameExtensionFilter; import org.insa.algo.AbstractSolution; import org.insa.algo.AlgorithmFactory; +import org.insa.algo.carpooling.CarPoolingAlgorithm; +import org.insa.algo.packageswitch.PackageSwitchAlgorithm; import org.insa.algo.shortestpath.ShortestPathAlgorithm; import org.insa.algo.shortestpath.ShortestPathData; import org.insa.algo.shortestpath.ShortestPathGraphicObserver; @@ -112,7 +114,7 @@ public class MainWindow extends JFrame { // Algorithm panels private final List algoPanels = new ArrayList<>(); - private final AlgorithmPanel wccPanel, spPanel; + private final AlgorithmPanel wccPanel, spPanel, cpPanel, psPanel; // Path panel private final PathsPanel pathPanel; @@ -161,7 +163,7 @@ public class MainWindow extends JFrame { this.currentPalette = this.basicPalette; wccPanel = new AlgorithmPanel(this, WeaklyConnectedComponentsAlgorithm.class, - "Weakly-Connected Components", new String[] {}, false, false); + "Weakly-Connected Components", new String[]{}, false, false); wccPanel.addStartActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -206,7 +208,7 @@ public class MainWindow extends JFrame { }); spPanel = new AlgorithmPanel(this, ShortestPathAlgorithm.class, "Shortest-Path", - new String[] { "Origin", "Destination" }, true, true); + new String[]{ "Origin", "Destination" }, true, true); spPanel.addStartActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -258,21 +260,35 @@ public class MainWindow extends JFrame { } }); + cpPanel = new AlgorithmPanel( + this, CarPoolingAlgorithm.class, "Car-Pooling", new String[]{ "Origin Car", + "Origin Pedestrian", "Destination Car", "Destination Pedestrian" }, + true, true); + + psPanel = new AlgorithmPanel(this, PackageSwitchAlgorithm.class, "Car-Pooling", + new String[]{ "Oribin A", "Origin B", "Destination A", "Destination B" }, true, + true); + // add algorithm panels algoPanels.add(wccPanel); algoPanels.add(spPanel); + algoPanels.add(cpPanel); + algoPanels.add(psPanel); this.pathPanel = new PathsPanel(this); // Add click listeners to both drawing. - basicDrawing.addDrawingClickListener(spPanel.nodesInputPanel); - mapViewDrawing.addDrawingClickListener(spPanel.nodesInputPanel); - this.graphChangeListeneres.add(spPanel.nodesInputPanel); - this.graphChangeListeneres.add(spPanel.solutionPanel); + for (AlgorithmPanel panel: algoPanels) { + this.basicDrawing.addDrawingClickListener(panel.nodesInputPanel); + this.mapViewDrawing.addDrawingClickListener(panel.nodesInputPanel); + this.graphChangeListeneres.add(panel.nodesInputPanel); + this.graphChangeListeneres.add(panel.solutionPanel); + this.drawingChangeListeners.add(panel.nodesInputPanel); + this.drawingChangeListeners.add(panel.solutionPanel); + } + this.graphChangeListeneres.add(pathPanel); - this.drawingChangeListeners.add(spPanel.nodesInputPanel); - this.drawingChangeListeners.add(spPanel.solutionPanel); this.drawingChangeListeners.add(pathPanel); // Create action factory. @@ -738,19 +754,42 @@ public class MainWindow extends JFrame { })); // Shortest path - JMenuItem spItem = new JMenuItem("Shortest Path"); + JMenuItem spItem = new JMenuItem("Shortest-Path"); spItem.addActionListener(baf.createBlockingAction(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { enableAlgorithmPanel(spPanel); } })); + + // Car pooling + JMenuItem cpItem = new JMenuItem("Car Pooling"); + cpItem.addActionListener(baf.createBlockingAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + enableAlgorithmPanel(cpPanel); + } + })); + + // Car pooling + JMenuItem psItem = new JMenuItem("Package Switch"); + psItem.addActionListener(baf.createBlockingAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + enableAlgorithmPanel(psPanel); + } + })); + graphLockItems.add(wccItem); graphLockItems.add(spItem); + graphLockItems.add(cpItem); + graphLockItems.add(psItem); algoMenu.add(wccItem); algoMenu.addSeparator(); algoMenu.add(spItem); + algoMenu.add(cpItem); + algoMenu.add(psItem); // Create the menu bar. JMenuBar menuBar = new JMenuBar(); diff --git a/src/main/org/insa/graphics/NodesInputPanel.java b/src/main/org/insa/graphics/NodesInputPanel.java index b0af315..e0eed9e 100644 --- a/src/main/org/insa/graphics/NodesInputPanel.java +++ b/src/main/org/insa/graphics/NodesInputPanel.java @@ -59,7 +59,7 @@ public class NodesInputPanel extends JPanel * @param point * * @return the closest node to the given point, or null if no node is "close - * enough". + * enough". */ public Node findClosestNode(Point point) { Node minNode = null; @@ -293,7 +293,7 @@ public class NodesInputPanel extends JPanel /** * @return The node for the given text field, or null if the content of the text - * field is invalid. + * field is invalid. */ protected Node getNodeForInput(JTextField textfield) { try { @@ -307,7 +307,7 @@ public class NodesInputPanel extends JPanel /** * @return List of nodes associated with the input. Some nodes may be null if - * their associated input is invalid. + * their associated input is invalid. */ public List getNodeForInputs() { List nodes = new ArrayList<>(nodeInputs.size()); diff --git a/src/main/org/insa/graphics/PathsPanel.java b/src/main/org/insa/graphics/PathsPanel.java index 4f179a6..7bb0f6e 100644 --- a/src/main/org/insa/graphics/PathsPanel.java +++ b/src/main/org/insa/graphics/PathsPanel.java @@ -162,7 +162,6 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh colorButton = new JButton(icon); colorButton.setFocusable(false); colorButton.setFocusPainted(false); - colorButton.setBorderPainted(false); colorButton.setMinimumSize(size); colorButton.setPreferredSize(size); colorButton.setMaximumSize(size); @@ -211,7 +210,6 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh JButton saveButton = new JButton(UIManager.getIcon("FileView.floppyDriveIcon")); saveButton.setFocusPainted(false); saveButton.setFocusable(false); - saveButton.setBorderPainted(false); saveButton.setMinimumSize(size); saveButton.setPreferredSize(size); saveButton.setMaximumSize(size); @@ -249,7 +247,6 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh JButton deleteButton = new JButton(new ImageIcon(newimg)); deleteButton.setFocusPainted(false); deleteButton.setFocusable(false); - deleteButton.setBorderPainted(false); deleteButton.setMinimumSize(size); deleteButton.setPreferredSize(size); deleteButton.setMaximumSize(size); @@ -287,7 +284,6 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh /* * (non-Javadoc) - * * @see java.lang.Object#toString() */ public String toString() {