From 05c4f0da2acade661ca165ae219d95510218f7c7 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 21:39:57 +0100 Subject: [PATCH] Try to improve mapsforge auto file detection. --- src/main/org/insa/graph/io/Openfile.java | 83 ------------ src/main/org/insa/graphics/MainWindow.java | 118 ++++++++++++++---- .../insa/graphics/drawing/MapViewDrawing.java | 52 ++------ .../insa/graph/io/BinaryGraphReaderTest.java | 6 +- 4 files changed, 104 insertions(+), 155 deletions(-) delete mode 100644 src/main/org/insa/graph/io/Openfile.java diff --git a/src/main/org/insa/graph/io/Openfile.java b/src/main/org/insa/graph/io/Openfile.java deleted file mode 100644 index 3932dc1..0000000 --- a/src/main/org/insa/graph/io/Openfile.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.insa.graph.io ; - -import java.io.* ; -import java.util.zip.* ; - -/** - * Class that can be used to open (compressed) files from a specified - * set of folders or for a full path. - * - */ -public class Openfile { - - /** - * These folders will be looked up for the files. - * - */ - private static final String[] datadirs = { - - // INSA folder containing maps. - "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps", - - // INSA folder containing paths. - "/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/", - - // Maps sub-folder. - "Maps", - - // Current folder. - "." - }; - - /** - * Available extensions. - * - */ - private static final String[] extensions = { ".map", ".gz", ".map.gz", ".path", ".path.gz", "" }; - - /** - * Open the given file and return a corresponding DataInputStream. - * - * @param filename Name of the file to open (without extension) or full path to the given file. - * @throws IOException - */ - public static DataInputStream open(String filename) throws IOException { - - File file = null; - String fullpath = null; - - // If the filename containing only a name (not a path): - if (filename.equals (new File(filename).getName())) { - - - for (String ext: extensions) { - String fname = filename + ext; - for (int index = 0; file == null && index < datadirs.length; ++index) { - fullpath = datadirs[index] + File.separator + fname; - file = new File(fullpath); - if (!file.exists()) { - file = null; - } - } - } - - } - else { - fullpath = filename; - file = new File(filename); - } - - InputStream fileInput = new FileInputStream(new File(fullpath)); - - // If the file is compressed. - if (fullpath.endsWith(".gz")) { - fileInput = new GZIPInputStream(fileInput) ; - } - else { - fileInput = new BufferedInputStream(fileInput) ; - } - - return new DataInputStream(fileInput) ; - } - -} diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index 64a5f93..0143077 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -11,10 +11,12 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -61,11 +63,12 @@ import org.insa.graph.io.BinaryPathReader; import org.insa.graph.io.BinaryPathWriter; import org.insa.graph.io.GraphReader; import org.insa.graph.io.MapMismatchException; -import org.insa.graph.io.Openfile; import org.insa.graphics.ShortestPathPanel.StartActionEvent; import org.insa.graphics.drawing.BasicDrawing; +import org.insa.graphics.drawing.BasicGraphPalette; import org.insa.graphics.drawing.BlackAndWhiteGraphPalette; import org.insa.graphics.drawing.Drawing; +import org.insa.graphics.drawing.GraphPalette; import org.insa.graphics.drawing.MapViewDrawing; import org.insa.graphics.drawing.overlays.PathOverlay; @@ -89,11 +92,13 @@ public class MainWindow extends JFrame { // Current graph. protected Graph graph; - // Current loaded path. - // private Path currentPath; + // Path to the last opened graph file. + private String graphFilePath; // Drawing and click adapter. protected Drawing drawing; + private MapViewDrawing mapViewDrawing; + private BasicDrawing basicDrawing; // Main panel. private JSplitPane mainPanel; @@ -133,7 +138,10 @@ public class MainWindow extends JFrame { setLayout(new BorderLayout()); // Create drawing and action listeners... - this.drawing = new BasicDrawing(); + this.basicDrawing = new BasicDrawing(); + this.mapViewDrawing = new MapViewDrawing(); + + this.drawing = this.basicDrawing; spPanel = new ShortestPathPanel(MainWindow.this); spPanel.addStartActionListener(new ActionListener() { @@ -158,6 +166,9 @@ public class MainWindow extends JFrame { }); spPanel.setVisible(false); + basicDrawing.addDrawingClickListener(spPanel.nodesInputPanel); + mapViewDrawing.addDrawingClickListener(spPanel.nodesInputPanel); + this.currentThread = new ThreadWrapper(this); this.baf = new BlockingActionFactory(this); this.baf.addAction(currentThread); @@ -359,23 +370,76 @@ public class MainWindow extends JFrame { } private void addDrawingClickListeners() { - drawing.addDrawingClickListener(spPanel.nodesInputPanel); } - private void updateDrawing(Class newClass) { + /** + * Draw the stored graph on the drawing. + */ + private void drawGraph(Class newClass, GraphPalette palette) { + // Save old divider location int oldLocation = mainPanel.getDividerLocation(); - drawing.clear(); - if (drawing == null || !newClass.isInstance(drawing)) { - try { - drawing = newClass.newInstance(); + + boolean isNewGraph = newClass == null; + boolean isMapView = (isNewGraph && drawing == mapViewDrawing) + || (!isNewGraph && newClass.equals(MapViewDrawing.class)); + + // We need to draw MapView, we have to check if the file exists. + File mfile = null; + if (isMapView) { + String mfpath = graphFilePath.substring(0, graphFilePath.lastIndexOf(".map")) + ".mapfg"; + mfile = new File(mfpath); + if (!mfile.exists()) { + if (JOptionPane.showConfirmDialog(this, + "The associated mapsforge (.mapfg) file has not been found, do you want to specify it manually?", + "File not found", JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) { + JFileChooser chooser = new JFileChooser(mfile.getParentFile()); + if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { + mfile = chooser.getSelectedFile(); + } + else { + mfile = null; + } + } + else { + mfile = null; + } } - catch (InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - } - addDrawingClickListeners(); - mainPanel.setLeftComponent((Component) drawing); - mainPanel.setDividerLocation(oldLocation); } + + if (isMapView && mfile != null) { + // It is a mapview drawing and the file was found, so: + // 1. We create the drawing if necessary. + if (drawing != mapViewDrawing) { + drawing.clear(); + drawing = mapViewDrawing; + mainPanel.setLeftComponent(mapViewDrawing); + mainPanel.setDividerLocation(oldLocation); + } + + // 2. We draw the graph. + drawing.clear(); + ((MapViewDrawing) drawing).drawGraph(mfile); + } + else if (!isMapView || (isMapView && mfile == null && isNewGraph)) { + if (drawing == mapViewDrawing) { + mapViewDrawing.clear(); + drawing = basicDrawing; + addDrawingClickListeners(); + mainPanel.setLeftComponent(basicDrawing); + mainPanel.setDividerLocation(oldLocation); + } + drawing.clear(); + drawing.drawGraph(graph, palette); + } + + } + + private void drawGraph(Class newClass) { + drawGraph(newClass, new BasicGraphPalette()); + } + + private void drawGraph() { + drawGraph(null, new BasicGraphPalette()); } private JMenuBar createMenuBar() { @@ -398,7 +462,8 @@ public class MainWindow extends JFrame { String path = chooser.getSelectedFile().getAbsolutePath(); DataInputStream stream; try { - stream = Openfile.open(path); + stream = new DataInputStream( + new BufferedInputStream(new FileInputStream(chooser.getSelectedFile()))); } catch (IOException e1) { JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); @@ -420,8 +485,11 @@ public class MainWindow extends JFrame { exception.printStackTrace(System.out); return; } - drawing.clear(); - drawing.drawGraph(graph); + + // Save file path. + graphFilePath = path; + + drawGraph(); for (JMenuItem item: graphLockItems) { item.setEnabled(true); @@ -447,7 +515,8 @@ public class MainWindow extends JFrame { if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) { BinaryPathReader reader; try { - reader = new BinaryPathReader(Openfile.open(chooser.getSelectedFile().getAbsolutePath())); + reader = new BinaryPathReader(new DataInputStream( + new BufferedInputStream(new FileInputStream(chooser.getSelectedFile())))); } catch (IOException e1) { JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file."); @@ -497,8 +566,7 @@ public class MainWindow extends JFrame { launchThread(new Runnable() { @Override public void run() { - updateDrawing(BasicDrawing.class); - drawing.drawGraph(graph); + drawGraph(BasicDrawing.class); } }); } @@ -512,8 +580,7 @@ public class MainWindow extends JFrame { launchThread(new Runnable() { @Override public void run() { - updateDrawing(BasicDrawing.class); - drawing.drawGraph(graph, new BlackAndWhiteGraphPalette()); + drawGraph(BasicDrawing.class, new BlackAndWhiteGraphPalette()); } }); } @@ -527,8 +594,7 @@ public class MainWindow extends JFrame { launchThread(new Runnable() { @Override public void run() { - updateDrawing(MapViewDrawing.class); - drawing.drawGraph(graph); + drawGraph(MapViewDrawing.class); } }); } diff --git a/src/main/org/insa/graphics/drawing/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/MapViewDrawing.java index 8caff84..b7e20b9 100644 --- a/src/main/org/insa/graphics/drawing/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/MapViewDrawing.java @@ -3,13 +3,8 @@ package org.insa.graphics.drawing; import java.awt.Color; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; import java.util.UUID; -import javax.swing.JFileChooser; -import javax.swing.filechooser.FileNameExtensionFilter; - import org.insa.graph.Arc; import org.insa.graph.Graph; import org.insa.graph.Path; @@ -141,41 +136,6 @@ public class MapViewDrawing extends MapView implements Drawing { model.setFixedTileSize(this.tileSize); } - /** - * - * @param color - * @return - */ - private File getMapsforgeFileFromGraph(Graph graph) { - // TODO: Find a way to change this... - Map idToNames = new HashMap(); - idToNames.put(0x100, "insa"); - idToNames.put(0x110, "paris"); - idToNames.put(0x200, "mayotte"); - idToNames.put(0x250, "newzealand"); - idToNames.put(0x300, "reunion"); - idToNames.put(0x400, "midip"); - idToNames.put(0x410, "morbihan"); - - File file = null; - if (idToNames.containsKey(graph.getMapId())) { - file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg"); - if (!file.exists()) { - file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg"); - } - } - - if (file == null || !file.exists()) { - JFileChooser fileChooser = new JFileChooser("Maps/"); - fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg")); - if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) { - file = fileChooser.getSelectedFile(); - } - } - - return file; - } - protected LatLong convertPoint(Point point) { return new LatLong(point.getLatitude(), point.getLongitude()); } @@ -235,10 +195,7 @@ public class MapViewDrawing extends MapView implements Drawing { // TODO: } - @Override - public void drawGraph(Graph graph, GraphPalette palette) { - - File graphFile = getMapsforgeFileFromGraph(graph); + public void drawGraph(File file) { // Tile cache TileCache tileCache = AwtUtil.createTileCache(tileSize, getModel().frameBufferModel.getOverdrawFactor(), 1024, @@ -247,7 +204,7 @@ public class MapViewDrawing extends MapView implements Drawing { // Layers Layers layers = getLayerManager().getLayers(); - MapDataStore mapDataStore = new MapFile(graphFile); + MapDataStore mapDataStore = new MapFile(file); TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, getModel().mapViewPosition, null); layers.add(tileRendererLayer); @@ -261,6 +218,11 @@ public class MapViewDrawing extends MapView implements Drawing { } } + @Override + public void drawGraph(Graph graph, GraphPalette palette) { + // TODO: Unimplemented for now... + } + @Override public void drawGraph(Graph graph) { drawGraph(graph, null); diff --git a/src/test/org/insa/graph/io/BinaryGraphReaderTest.java b/src/test/org/insa/graph/io/BinaryGraphReaderTest.java index 33e39de..12dee09 100644 --- a/src/test/org/insa/graph/io/BinaryGraphReaderTest.java +++ b/src/test/org/insa/graph/io/BinaryGraphReaderTest.java @@ -2,6 +2,9 @@ package org.insa.graph.io; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; import java.io.IOException; import java.util.List; @@ -20,7 +23,8 @@ public class BinaryGraphReaderTest { @BeforeAll static void initAll() throws IOException { - BinaryGraphReader reader = new BinaryGraphReader(Openfile.open("midip")); + BinaryGraphReader reader = new BinaryGraphReader( + new DataInputStream(new BufferedInputStream(new FileInputStream("Maps/midip.map")))); midip = reader.read(); }