From 6cc57631ad26ed7b5d6f7ac7b03553c1ea173ed3 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Wed, 28 Feb 2018 22:24:04 +0100 Subject: [PATCH] Rename binary readers, and separate the button in two. --- ...er.java => BinaryGraphReaderInsa2016.java} | 4 +- ...V2.java => BinaryGraphReaderInsa2018.java} | 4 +- src/main/org/insa/graphics/MainWindow.java | 125 +++++++++++------- .../insa/graph/io/BinaryGraphReaderTest.java | 2 +- 4 files changed, 79 insertions(+), 56 deletions(-) rename src/main/org/insa/graph/io/{BinaryGraphReader.java => BinaryGraphReaderInsa2016.java} (97%) rename src/main/org/insa/graph/io/{BinaryGraphReaderV2.java => BinaryGraphReaderInsa2018.java} (97%) diff --git a/src/main/org/insa/graph/io/BinaryGraphReader.java b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java similarity index 97% rename from src/main/org/insa/graph/io/BinaryGraphReader.java rename to src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java index cfe7fe8..f19d874 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReader.java +++ b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java @@ -12,7 +12,7 @@ import org.insa.graph.Point; import org.insa.graph.RoadInformation; import org.insa.graph.RoadInformation.RoadType; -public class BinaryGraphReader extends BinaryReader implements GraphReader { +public class BinaryGraphReaderInsa2016 extends BinaryReader implements GraphReader { // Map version and magic number targeted for this reader. private static final int VERSION = 4; @@ -70,7 +70,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { * * @param dis */ - public BinaryGraphReader(DataInputStream dis) { + public BinaryGraphReaderInsa2016(DataInputStream dis) { super(MAGIC_NUMBER, VERSION, dis); } diff --git a/src/main/org/insa/graph/io/BinaryGraphReaderV2.java b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java similarity index 97% rename from src/main/org/insa/graph/io/BinaryGraphReaderV2.java rename to src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java index c7788f9..225b260 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReaderV2.java +++ b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java @@ -12,7 +12,7 @@ import org.insa.graph.Point; import org.insa.graph.RoadInformation; import org.insa.graph.RoadInformation.RoadType; -public class BinaryGraphReaderV2 extends BinaryReader implements GraphReader { +public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphReader { // Map version and magic number targeted for this reader. private static final int VERSION = 5; @@ -70,7 +70,7 @@ public class BinaryGraphReaderV2 extends BinaryReader implements GraphReader { * * @param dis */ - public BinaryGraphReaderV2(DataInputStream dis) { + public BinaryGraphReaderInsa2018(DataInputStream dis) { super(MAGIC_NUMBER, VERSION, dis); } diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index df8e4d0..1a99c36 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -58,8 +58,8 @@ import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm; import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsData; import org.insa.graph.Graph; import org.insa.graph.Path; -import org.insa.graph.io.BinaryGraphReader; -import org.insa.graph.io.BinaryGraphReaderV2; +import org.insa.graph.io.BinaryGraphReaderInsa2016; +import org.insa.graph.io.BinaryGraphReaderInsa2018; import org.insa.graph.io.BinaryPathReader; import org.insa.graph.io.BinaryPathWriter; import org.insa.graph.io.GraphReader; @@ -107,9 +107,6 @@ public class MainWindow extends JFrame { // Shortest path panel private ShortestPathPanel spPanel; - // List of item for the top menus. - private JMenuItem openMapItem; - // List of items that cannot be used without a graph private ArrayList graphLockItems = new ArrayList(); @@ -475,74 +472,98 @@ public class MainWindow extends JFrame { } + /** + * @param newClass + */ private void drawGraph(Class newClass) { drawGraph(newClass, new BasicGraphPalette()); } + /** + * + */ private void drawGraph() { drawGraph(null, new BasicGraphPalette()); } + private void loadGraph(GraphReader reader) { + launchThread(new Runnable() { + @Override + public void run() { + GraphReaderProgressBar progressBar = new GraphReaderProgressBar(MainWindow.this); + progressBar.setLocationRelativeTo(mainPanel.getLeftComponent()); + reader.addObserver(progressBar); + try { + graph = reader.read(); + } + catch (Exception exception) { + JOptionPane.showMessageDialog(MainWindow.this, "Unable to read graph from the selected file."); + exception.printStackTrace(System.out); + return; + } + notifyNewGraphLoaded(); + + graphInfoPanel + .setText(String.format("Map ID: %#x, %d nodes", graph.getMapId(), graph.getNodes().size())); + drawGraph(); + + for (JMenuItem item: graphLockItems) { + item.setEnabled(true); + } + } + }, false); + } + private JMenuBar createMenuBar() { // Open Map item... - openMapItem = new JMenuItem("Open Map... ", KeyEvent.VK_O); + JMenuItem openMapItem = new JMenuItem("Open Map... ", KeyEvent.VK_O); openMapItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK)); openMapItem.addActionListener(baf.createBlockingAction(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); - FileNameExtensionFilter filter = new FileNameExtensionFilter("Map & compressed map files", "map", - "map2", "mapgr", "map.gz"); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files", "mapgr"); chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); chooser.setFileFilter(filter); if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { - launchThread(new Runnable() { - @Override - public void run() { - String path = chooser.getSelectedFile().getAbsolutePath(); - DataInputStream stream; - try { - stream = new DataInputStream( - new BufferedInputStream(new FileInputStream(chooser.getSelectedFile()))); - } - catch (IOException e1) { - JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); - return; - } - GraphReader reader; - if (path.endsWith(".map2") || path.endsWith("mapgr")) { - reader = new BinaryGraphReaderV2(stream); - } - else { - reader = new BinaryGraphReader(stream); - } - GraphReaderProgressBar progressBar = new GraphReaderProgressBar(MainWindow.this); - progressBar.setLocationRelativeTo(mainPanel.getLeftComponent()); - reader.addObserver(progressBar); - try { - graph = reader.read(); - } - catch (Exception exception) { - JOptionPane.showMessageDialog(MainWindow.this, - "Unable to read graph from the selected file."); - exception.printStackTrace(System.out); - return; - } - notifyNewGraphLoaded(); + graphFilePath = chooser.getSelectedFile().getAbsolutePath(); - // Save file path. - graphFilePath = path; + DataInputStream stream; + try { + stream = new DataInputStream( + new BufferedInputStream(new FileInputStream(chooser.getSelectedFile()))); + } + catch (IOException e1) { + JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); + return; + } + loadGraph(new BinaryGraphReaderInsa2018(stream)); + } + } + })); - graphInfoPanel.setText( - String.format("Map ID: %#x, %d nodes", graph.getMapId(), graph.getNodes().size())); - drawGraph(); + JMenuItem openOldMapItem = new JMenuItem("Open Map (Old version)... "); + openOldMapItem.addActionListener(baf.createBlockingAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Map & compressed map files", "map"); + chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); + chooser.setFileFilter(filter); + if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { + graphFilePath = chooser.getSelectedFile().getAbsolutePath(); - for (JMenuItem item: graphLockItems) { - item.setEnabled(true); - } - } - }, false); + DataInputStream stream; + try { + stream = new DataInputStream( + new BufferedInputStream(new FileInputStream(chooser.getSelectedFile()))); + } + catch (IOException e1) { + JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); + return; + } + loadGraph(new BinaryGraphReaderInsa2016(stream)); } } })); @@ -551,6 +572,7 @@ public class MainWindow extends JFrame { JMenuItem openPathItem = new JMenuItem("Open Path... ", KeyEvent.VK_P); openPathItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK)); openPathItem.addActionListener(baf.createBlockingAction(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); @@ -599,6 +621,7 @@ public class MainWindow extends JFrame { // Build the first menu. JMenu fileMenu = new JMenu("File"); fileMenu.add(openMapItem); + fileMenu.add(openOldMapItem); // TODO: Remove this for Students. fileMenu.add(openPathItem); fileMenu.addSeparator(); fileMenu.add(closeItem); diff --git a/src/test/org/insa/graph/io/BinaryGraphReaderTest.java b/src/test/org/insa/graph/io/BinaryGraphReaderTest.java index 468a3d7..e7cb9a0 100644 --- a/src/test/org/insa/graph/io/BinaryGraphReaderTest.java +++ b/src/test/org/insa/graph/io/BinaryGraphReaderTest.java @@ -23,7 +23,7 @@ public class BinaryGraphReaderTest { @BeforeClass public static void initAll() throws IOException { - BinaryGraphReader reader = new BinaryGraphReader( + BinaryGraphReaderInsa2016 reader = new BinaryGraphReaderInsa2016( new DataInputStream(new BufferedInputStream(new FileInputStream("Maps/midip.map")))); midip = reader.read(); }