From 0b469c7fe0882f07d6541342cc3a26f163b5be87 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 1 May 2024 23:35:54 +0200 Subject: [PATCH] Add a new page to see the schedule of each stop, for each line --- pom.xml | 2 +- src/main/java/fr/u_paris/gla/project/App.java | 31 +++-- .../java/fr/u_paris/gla/project/gui/View.java | 129 ++++++++++++++++-- .../gla/project/idfm/CSVImageProvider.java | 20 ++- .../project/idfm/IDFMNetworkExtractor.java | 7 +- .../u_paris/gla/project/idfm/ImagePair.java | 6 + .../gla/project/itinerary/Connection.java | 5 + .../u_paris/gla/project/itinerary/Finder.java | 3 +- .../u_paris/gla/project/itinerary/Graph.java | 2 +- .../u_paris/gla/project/itinerary/Parse.java | 13 +- .../u_paris/gla/project/itinerary/Stop.java | 8 +- 11 files changed, 178 insertions(+), 48 deletions(-) diff --git a/pom.xml b/pom.xml index b036634..a8e9958 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ true ${project.build.finalName}.lib/ - fr.u_paris.gla.project.gui.View + fr.u_paris.gla.project.App diff --git a/src/main/java/fr/u_paris/gla/project/App.java b/src/main/java/fr/u_paris/gla/project/App.java index e771f9a..043ad3e 100644 --- a/src/main/java/fr/u_paris/gla/project/App.java +++ b/src/main/java/fr/u_paris/gla/project/App.java @@ -7,19 +7,15 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.imageio.ImageIO; -import javax.swing.ImageIcon; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.WindowConstants; +import javax.swing.*; -import fr.u_paris.gla.project.itinerary.Finder; -import fr.u_paris.gla.project.itinerary.Parse; -import fr.u_paris.gla.project.itinerary.Path; -import fr.u_paris.gla.project.itinerary.Stop; +import fr.u_paris.gla.project.gui.View; +import fr.u_paris.gla.project.itinerary.*; /** Simple application model. * @@ -67,11 +63,26 @@ public class App { } } } - else{ - testRelease(); + else { + // testRelease(); + run(); } } + public static void run() { + Parse parse = new Parse(); + parse.parseFiles(); + + Graph graph = parse.createGraph(); + Finder finder = parse.createFinder(graph); + + ArrayList s = new ArrayList<>(); + s.add(new Stop("M8", "Balard", 1.0315897, 3.0265513)); + s.add(new Stop("M14", "Gare de Lyon", 2.4658452681, 3.0265513)); + + SwingUtilities.invokeLater(() -> new View(graph, finder, s)); + } + public static void testRelease(){ Parse parse = new Parse(); parse.parseFiles(); diff --git a/src/main/java/fr/u_paris/gla/project/gui/View.java b/src/main/java/fr/u_paris/gla/project/gui/View.java index 28db6b4..540fd4c 100644 --- a/src/main/java/fr/u_paris/gla/project/gui/View.java +++ b/src/main/java/fr/u_paris/gla/project/gui/View.java @@ -3,10 +3,10 @@ package fr.u_paris.gla.project.gui; import fr.u_paris.gla.project.idfm.CSVImageProvider; import fr.u_paris.gla.project.idfm.IDFMNetworkExtractor; import fr.u_paris.gla.project.idfm.ImagePair; -import fr.u_paris.gla.project.itinerary.Path; -import fr.u_paris.gla.project.itinerary.Stop; +import fr.u_paris.gla.project.itinerary.*; import javax.swing.*; +import javax.swing.border.Border; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import java.awt.*; @@ -16,6 +16,9 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Set; import java.util.logging.Logger; public class View extends JFrame { @@ -25,7 +28,6 @@ public class View extends JFrame { private JPanel CardPanel; private JMenuItem Home; private JMenuItem Network; - private JMenuItem Favorites; private JPanel NetworkPanel; private JTextField TextLocation; @@ -55,6 +57,13 @@ public class View extends JFrame { private JComboBox LinesComboBox; private JButton ShowLineButton; + private JMenuItem Stops; + private JPanel StopsPanel; + private JLabel StopsLabel; + private JComboBox StopsComboBox; + private JComboBox StopsLinesComboBox; + private JButton SeeStopButton; + private JTextField TextCoord; private JButton ButtonCoord; private JPanel SearchCoordPanel; @@ -80,7 +89,7 @@ public class View extends JFrame { private int count = 0; - public View(ArrayList s) throws HeadlessException { + public View(Graph graph, Finder finder, ArrayList s) throws HeadlessException { setSize(800, 600); MainPanel = new JPanel(); GridLayout MainLayout = new GridLayout(1, 2, 50, 0); @@ -146,6 +155,7 @@ public class View extends JFrame { ShowLineButton.setAlignmentX(Component.CENTER_ALIGNMENT); LinesPanel = new JPanel(); + LinesPanel.setBackground(new Color(214,173,153)); LinesPanel.setLayout(new BoxLayout(LinesPanel, BoxLayout.Y_AXIS)); LinesPanel.add(Box.createHorizontalGlue()); LinesPanel.add(Box.createHorizontalStrut(2)); @@ -157,8 +167,35 @@ public class View extends JFrame { LinesPanel.add(Box.createHorizontalStrut(2)); LinesPanel.add(Box.createHorizontalGlue()); + StopsComboBox = new JComboBox(); + StopsComboBox.setMaximumSize(new Dimension(200, StopsComboBox.getPreferredSize().height)); + StopsComboBox.setPreferredSize(StopsComboBox.getPreferredSize()); + StopsLinesComboBox = new JComboBox(); + StopsLinesComboBox.setMaximumSize(new Dimension(200, StopsLinesComboBox.getPreferredSize().height)); + StopsLinesComboBox.setPreferredSize(StopsComboBox.getPreferredSize()); + StopsLabel = new JLabel("See stop schedules"); + StopsLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + SeeStopButton = new JButton("See Schedule"); + SeeStopButton.setAlignmentX(Component.CENTER_ALIGNMENT); + + StopsPanel = new JPanel(); + StopsPanel.setBackground(new Color(171,197,105)); + StopsPanel.setLayout(new BoxLayout(StopsPanel, BoxLayout.Y_AXIS)); + StopsPanel.add(Box.createHorizontalGlue()); + StopsPanel.add(Box.createHorizontalStrut(2)); + StopsPanel.add(StopsLabel); + StopsPanel.add(Box.createHorizontalStrut(10)); + StopsPanel.add(StopsComboBox); + StopsPanel.add(Box.createHorizontalStrut(10)); + StopsPanel.add(StopsLinesComboBox); + StopsPanel.add(Box.createHorizontalStrut(10)); + StopsPanel.add(SeeStopButton); + StopsPanel.add(Box.createHorizontalStrut(2)); + StopsPanel.add(Box.createHorizontalGlue()); + + JPanel buttonBarPanel = new JPanel(new BorderLayout()); ButtonBar = new JMenuBar(); - GridLayout ButtonLayout = new GridLayout(4, 1); + GridLayout ButtonLayout = new GridLayout(5, 1); ButtonBar.setLayout(ButtonLayout); Home = new JMenuItem("Home"); ButtonBar.add(Home); @@ -168,8 +205,12 @@ public class View extends JFrame { ButtonBar.add(Itinerary); Lines = new JMenuItem("Lines"); ButtonBar.add(Lines); + Stops = new JMenuItem("Stops"); + ButtonBar.add(Stops); + buttonBarPanel.add(ButtonBar, BorderLayout.CENTER); + buttonBarPanel.setVisible(true); - ButtonBar.setPreferredSize(new Dimension(50, 500)); + ButtonBar.setPreferredSize(new Dimension(50, MainPanel.getHeight())); MainPanel.add(ButtonBar); MainPanel.add(CardPanel); @@ -220,12 +261,39 @@ public class View extends JFrame { CardPanel.revalidate(); }); + Stops.addActionListener(e -> { + CardPanel.removeAll(); + CardPanel.add(StopsPanel); + CardPanel.repaint(); + CardPanel.revalidate(); + }); + CSVImageProvider.getLineImageMap().forEach(p -> LinesComboBox.addItem(p)); ShowLineButton.addActionListener(f -> { ImagePair item = (ImagePair) LinesComboBox.getSelectedItem(); openWebpage(item.getValue()); }); + Set nodes = graph.getNodes(); + List nodesList = nodes.stream().sorted(Comparator.comparing(Stop::getName)).toList(); + nodesList.forEach(stop -> StopsComboBox.addItem(stop)); + StopsComboBox.addItemListener(e -> { + if (e.getStateChange() == ItemEvent.SELECTED) { + Stop stop = (Stop) StopsComboBox.getSelectedItem(); + StopsLinesComboBox.removeAllItems(); + graph.getConnections(stop).forEach(c -> { + if (!c.toString().equals("WALK")) + StopsLinesComboBox.addItem(c); + }); + } + }); + SeeStopButton.addActionListener(f -> { + Connection c; + if ((c = (Connection) StopsLinesComboBox.getSelectedItem()) != null) { + createHourWindow(c.getSchedules()); + } + }); + TextLocation.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { @@ -278,7 +346,6 @@ public class View extends JFrame { super.mouseClicked(e); System.out.println("MouseClick: " + e.getX() + ";" + e.getY()); showOptionsDialog(tableStops, e.getX(), e.getY()); - } }); @@ -290,6 +357,38 @@ public class View extends JFrame { }); } + private void createHourWindow(ArrayList schedules) { + JFrame frame = new JFrame("Schedule"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + JPanel panel = new JPanel(new GridLayout(0, 1)); + panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + + JScrollPane scrollPane = new JScrollPane(panel); + frame.getContentPane().add(scrollPane); + + for (int time : schedules) { + int hours = time / 3600; + int minutes = (time % 3600) / 60; + JLabel label = new JLabel(String.format("%dh%d", hours, minutes)); + label.setBorder(BorderFactory.createCompoundBorder( + BorderFactory.createLineBorder(Color.BLACK), + BorderFactory.createEmptyBorder(5, 5, 5, 5))); + label.setHorizontalAlignment(SwingConstants.CENTER); + panel.add(label); + } + if (schedules.isEmpty()) { + panel.add(new JLabel("No time available")); + } + + scrollPane.repaint(); + frame.repaint(); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setSize(200, 400); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + frame.setResizable(false); + } + private void showOptionsDialog(JTable table, int x, int y) { int selectedRow = table.rowAtPoint(new Point(x, y)); if (selectedRow != -1) { // If a row is selected @@ -359,12 +458,14 @@ public class View extends JFrame { // Add new rows based on the search results count = 0; Path last = null; - for (Path path : paths) { - // Add a row to the table with Stop's line in the first column and Stop's name in the second column - model.addRow(new Object[]{path.getLine(), path.getCurrentStop(), path.getStartTime()}); + if (paths != null) { + for (Path path : paths) { + // Add a row to the table with Stop's line in the first column and Stop's name in the second column + model.addRow(new Object[]{path.getLine(), path.getCurrentStop(), path.getStartTime()}); - ++count; - last = path; + ++count; + last = path; + } } if (last != null) model.addRow(new Object[]{last.getLine(), last.getNextStop()}); @@ -416,10 +517,12 @@ public class View extends JFrame { } } + /* public static void main(String[] args) { ArrayList s = new ArrayList<>(); s.add(new Stop("M8", "Balard", 1.0315897, 3.0265513)); s.add(new Stop("M14", "Gare de Lyon", 2.4658452681, 3.0265513)); - SwingUtilities.invokeLater(() -> new View(s)); + SwingUtilities.invokeLater(() -> new View(graph, finder, s)); } + */ } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java index 093f64e..04341ee 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java @@ -66,12 +66,20 @@ public final class CSVImageProvider { lineImageMap = new ArrayList<>(); try { CSVTools.readCSVFromFile(FILE_NAME, - (String[] line) -> lineImageMap.add(new ImagePair( - line[ImageFormat.LINE_INDEX], - line[ImageFormat.LINE_DETAIL_INDEX], - line[ImageFormat.IMAGE_URL_INDEX] - ))); - } catch(IOException e){ + (String[] line) -> + { + String label = line[ImageFormat.LINE_INDEX]; + String detail = line[ImageFormat.LINE_DETAIL_INDEX]; + String imageUrl = line[ImageFormat.IMAGE_URL_INDEX]; + + int index = (int) lineImageMap.stream() + .filter(pair -> pair.getLine().equals(label)) + .count(); + + lineImageMap.add(new ImagePair(label, detail, imageUrl)); + }); + } + catch(IOException e){ LOGGER.severe("File is not generated yet"); } lineImageMap.sort(Comparator.comparing(ImagePair::getLabel)); diff --git a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java index 0afd529..6a9598a 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java @@ -102,18 +102,17 @@ public class IDFMNetworkExtractor { public static boolean checkFileExistence(String filePath) { File file = new File(filePath); if (file.exists()) { - LOGGER.severe(filePath+ " already exists."); + LOGGER.log(Level.INFO, filePath+ " already exists."); return true; } else { - LOGGER.severe(filePath + " does not exist."); + LOGGER.log(Level.INFO, filePath + " does not exist."); return false; } } public static void buildFiles() { - if (checkFileExistence("./"+HOURS_FILE_NAME) && checkFileExistence("./"+TRACE_FILE_NAME) && checkFileExistence(("./"+IMAGES_FILE_NAME))) { - LOGGER.severe("Files already exists."); + LOGGER.log(Level.INFO, "Files already exists."); return; } diff --git a/src/main/java/fr/u_paris/gla/project/idfm/ImagePair.java b/src/main/java/fr/u_paris/gla/project/idfm/ImagePair.java index 0fe2f9a..90d7d01 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/ImagePair.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/ImagePair.java @@ -5,10 +5,12 @@ package fr.u_paris.gla.project.idfm; * These getters ables a ComboBox to show the label returned by toString, and get a specific value when the object is returned */ public class ImagePair { + private final String line; private final String label; private final String value; public ImagePair(String label, String label_detail, String value){ + this.line = label; this.label = label + " - " + label_detail; this.value = value; } @@ -17,6 +19,10 @@ public class ImagePair { return this.label; } + public String getLine(){ + return this.line; + } + public String getValue(){ return this.value; } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java index ed9dde6..c9b8608 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java @@ -96,4 +96,9 @@ public class Connection{ if(nextTime < currentTime) { nextTime += 86400;} return nextTime - currentTime + this.time; } + + @Override + public String toString() { + return lineName; + } } diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java index 31fe948..2cd8bba 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java @@ -27,8 +27,7 @@ public class Finder { graph.addNode(fromNode); graph.addNode(toNode); - List res = findPath(fromNode, toNode, startTime); - return res; + return findPath(fromNode, toNode, startTime); } /** diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java index 2fdfa21..76af8dd 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java @@ -4,7 +4,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -public class Graph{ +public class Graph { private final Set nodes; private final Map> connections; diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java b/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java index 16d6cc7..13e4a23 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Parse.java @@ -212,11 +212,9 @@ public class Parse { } public void parseFiles(){ - IDFMNetworkExtractor.buildFiles(); try { - CSVTools.readCSVFromFile(TRACE_FILE_NAME, (String[] line) -> addLine(line, nodes, tmp, connections)); @@ -228,11 +226,18 @@ public class Parse { c.sortSchedule(); } } - - } catch (IOException e) { + } + catch (IOException e) { LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); } + } + public Graph createGraph() { + return new Graph(nodes, connections); + } + + public Finder createFinder(Graph graph) { + return new Finder(graph); } public List getItinerary(Stop src, Stop dst, double startTime ){ diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java index bae0289..c814f37 100644 --- a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java @@ -32,13 +32,7 @@ public class Stop { @Override public String toString() { - return "Stop{" + - "id=" + id + - ", lines=" + lines + - ", name='" + name + '\'' + - ", latitude=" + latitude + - ", longitude=" + longitude + - '}'; + return name; } public int getId(){