Browse Source

Fix issue with MapViewDrawing and generalize ClickListener for all Drawings.

Holt59 6 years ago
parent
commit
9db2ee36dd

+ 63
- 69
.classpath View File

@@ -1,69 +1,63 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<classpath>
3
-	<classpathentry kind="src" output="target/classes" path="src/main">
4
-		<attributes>
5
-			<attribute name="optional" value="true"/>
6
-			<attribute name="maven.pomderived" value="true"/>
7
-		</attributes>
8
-	</classpathentry>
9
-	<classpathentry kind="src" output="target/test-classes" path="src/test">
10
-		<attributes>
11
-			<attribute name="optional" value="true"/>
12
-			<attribute name="maven.pomderived" value="true"/>
13
-		</attributes>
14
-	</classpathentry>
15
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
16
-		<attributes>
17
-			<attribute name="maven.pomderived" value="true"/>
18
-		</attributes>
19
-	</classpathentry>
20
-	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
21
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22
-		<attributes>
23
-			<attribute name="maven.pomderived" value="true"/>
24
-		</attributes>
25
-	</classpathentry>
26
-	<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar">
27
-		<attributes>
28
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-core-0.9.1-javadoc.jar!/"/>
29
-		</attributes>
30
-	</classpathentry>
31
-	<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar">
32
-		<attributes>
33
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-0.9.1-javadoc.jar!/"/>
34
-		</attributes>
35
-	</classpathentry>
36
-	<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar">
37
-		<attributes>
38
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/>
39
-		</attributes>
40
-	</classpathentry>
41
-	<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar">
42
-		<attributes>
43
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/>
44
-		</attributes>
45
-	</classpathentry>
46
-	<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar">
47
-		<attributes>
48
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/>
49
-		</attributes>
50
-	</classpathentry>
51
-	<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar">
52
-		<attributes>
53
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/>
54
-		</attributes>
55
-	</classpathentry>
56
-	<classpathentry kind="lib" path="libs/mapsforge-themes-0.9.1.jar">
57
-		<attributes>
58
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-themes-0.9.1-javadoc.jar!/"/>
59
-		</attributes>
60
-	</classpathentry>
61
-	<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/>
62
-	<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar">
63
-		<attributes>
64
-			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/svg-salamander-1.0-javadoc.jar!/"/>
65
-		</attributes>
66
-	</classpathentry>
67
-	<classpathentry kind="lib" path="libs/mapsforge-map-writer-0.9.1-jar-with-dependencies.jar"/>
68
-	<classpathentry kind="output" path="target/classes"/>
69
-</classpath>
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" output="target/classes" path="src/main">
4
+		<attributes>
5
+			<attribute name="optional" value="true"/>
6
+			<attribute name="maven.pomderived" value="true"/>
7
+		</attributes>
8
+	</classpathentry>
9
+	<classpathentry kind="src" output="target/test-classes" path="src/test">
10
+		<attributes>
11
+			<attribute name="optional" value="true"/>
12
+			<attribute name="maven.pomderived" value="true"/>
13
+		</attributes>
14
+	</classpathentry>
15
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
16
+		<attributes>
17
+			<attribute name="maven.pomderived" value="true"/>
18
+		</attributes>
19
+	</classpathentry>
20
+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
21
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22
+		<attributes>
23
+			<attribute name="maven.pomderived" value="true"/>
24
+		</attributes>
25
+	</classpathentry>
26
+	<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar">
27
+		<attributes>
28
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-core-0.9.1-javadoc.jar!/"/>
29
+		</attributes>
30
+	</classpathentry>
31
+	<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar">
32
+		<attributes>
33
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-0.9.1-javadoc.jar!/"/>
34
+		</attributes>
35
+	</classpathentry>
36
+	<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar">
37
+		<attributes>
38
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/>
39
+		</attributes>
40
+	</classpathentry>
41
+	<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar">
42
+		<attributes>
43
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/>
44
+		</attributes>
45
+	</classpathentry>
46
+	<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar">
47
+		<attributes>
48
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/>
49
+		</attributes>
50
+	</classpathentry>
51
+	<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar">
52
+		<attributes>
53
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/>
54
+		</attributes>
55
+	</classpathentry>
56
+	<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/>
57
+	<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar">
58
+		<attributes>
59
+			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/svg-salamander-1.0-javadoc.jar!/"/>
60
+		</attributes>
61
+	</classpathentry>
62
+	<classpathentry kind="output" path="target/classes"/>
63
+</classpath>

+ 5
- 0
.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,5 @@
1
+eclipse.preferences.version=1
2
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3
+org.eclipse.jdt.core.compiler.compliance=1.8
4
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5
+org.eclipse.jdt.core.compiler.source=1.8

+ 4
- 0
.settings/org.eclipse.m2e.core.prefs View File

@@ -0,0 +1,4 @@
1
+activeProfiles=
2
+eclipse.preferences.version=1
3
+resolveWorkspaceProjects=true
4
+version=1

+ 26
- 28
src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentGraphicObserver.java View File

@@ -7,33 +7,31 @@ import org.insa.drawing.Drawing;
7 7
 import org.insa.graph.Node;
8 8
 
9 9
 public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver {
10
-	
11
-	private static final Color[] COLORS = {
12
-		Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED
13
-	};
14
-
15
-	// Drawing + Graph drawing
16
-	private Drawing drawing;
17
-	
18
-	// Current index color
19
-	private int cindex = -1;
20
-	
21
-	public WeaklyConnectedComponentGraphicObserver(Drawing drawing) {
22
-		this.drawing = drawing;
23
-	}
24
-
25
-	@Override
26
-	public void notifyStartComponent(Node curNode) {
27
-		cindex = (cindex + 1) % COLORS.length;
28
-	}
29
-
30
-	@Override
31
-	public void notifyNewNodeInComponent(Node node) {
32
-		this.drawing.drawMarker(node.getPoint(), COLORS[cindex]);
33
-	}
34
-
35
-	@Override
36
-	public void notifyEndComponent(ArrayList<Node> nodes) {
37
-	}
10
+
11
+    private static final Color[] COLORS = { Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED };
12
+
13
+    // Drawing + Graph drawing
14
+    private Drawing drawing;
15
+
16
+    // Current index color
17
+    private int cindex = -1;
18
+
19
+    public WeaklyConnectedComponentGraphicObserver(Drawing drawing) {
20
+        this.drawing = drawing;
21
+    }
22
+
23
+    @Override
24
+    public void notifyStartComponent(Node curNode) {
25
+        cindex = (cindex + 1) % COLORS.length;
26
+    }
27
+
28
+    @Override
29
+    public void notifyNewNodeInComponent(Node node) {
30
+        this.drawing.drawPoint(node.getPoint(), 1, COLORS[cindex]);
31
+    }
32
+
33
+    @Override
34
+    public void notifyEndComponent(ArrayList<Node> nodes) {
35
+    }
38 36
 
39 37
 }

+ 625
- 651
src/main/org/insa/base/MainWindow.java
File diff suppressed because it is too large
View File


+ 0
- 302
src/main/org/insa/base/Samples.java View File

@@ -1,302 +0,0 @@
1
-/*
2
- * Copyright 2010, 2011, 2012, 2013 mapsforge.org
3
- * Copyright 2014 Christian Pesch
4
- * Copyright 2014 Ludwig M Brinckmann
5
- * Copyright 2014-2018 devemux86
6
- * Copyright 2017 usrusr
7
- *
8
- * This program is free software: you can redistribute it and/or modify it under the
9
- * terms of the GNU Lesser General Public License as published by the Free Software
10
- * Foundation, either version 3 of the License, or (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY
13
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU Lesser General Public License along with
17
- * this program. If not, see <http://www.gnu.org/licenses/>.
18
- */
19
-package org.insa.base;
20
-
21
-import java.awt.Dimension;
22
-import java.awt.event.WindowAdapter;
23
-import java.awt.event.WindowEvent;
24
-import java.io.File;
25
-import java.util.ArrayList;
26
-import java.util.Arrays;
27
-import java.util.List;
28
-import java.util.UUID;
29
-import java.util.prefs.Preferences;
30
-
31
-import javax.swing.JFrame;
32
-import javax.swing.JOptionPane;
33
-import javax.swing.WindowConstants;
34
-
35
-import org.insa.graph.Arc;
36
-import org.insa.graph.Graph;
37
-import org.insa.graph.Path;
38
-import org.insa.graph.io.BinaryGraphReader;
39
-import org.insa.graph.io.BinaryPathReader;
40
-import org.insa.graph.io.Openfile;
41
-import org.mapsforge.core.graphics.Color;
42
-import org.mapsforge.core.graphics.GraphicFactory;
43
-import org.mapsforge.core.graphics.Paint;
44
-import org.mapsforge.core.graphics.Style;
45
-import org.mapsforge.core.model.BoundingBox;
46
-import org.mapsforge.core.model.LatLong;
47
-import org.mapsforge.core.model.MapPosition;
48
-import org.mapsforge.core.model.Point;
49
-import org.mapsforge.core.util.LatLongUtils;
50
-import org.mapsforge.core.util.Parameters;
51
-import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
52
-import org.mapsforge.map.awt.util.AwtUtil;
53
-import org.mapsforge.map.awt.util.JavaPreferences;
54
-import org.mapsforge.map.awt.view.MapView;
55
-import org.mapsforge.map.datastore.MapDataStore;
56
-import org.mapsforge.map.datastore.MultiMapDataStore;
57
-import org.mapsforge.map.layer.Layers;
58
-import org.mapsforge.map.layer.cache.TileCache;
59
-import org.mapsforge.map.layer.debug.TileCoordinatesLayer;
60
-import org.mapsforge.map.layer.debug.TileGridLayer;
61
-import org.mapsforge.map.layer.download.TileDownloadLayer;
62
-import org.mapsforge.map.layer.download.tilesource.OpenStreetMapMapnik;
63
-import org.mapsforge.map.layer.download.tilesource.TileSource;
64
-import org.mapsforge.map.layer.hills.DiffuseLightShadingAlgorithm;
65
-import org.mapsforge.map.layer.hills.HillsRenderConfig;
66
-import org.mapsforge.map.layer.hills.MemoryCachingHgtReaderTileSource;
67
-import org.mapsforge.map.layer.overlay.Polyline;
68
-import org.mapsforge.map.layer.renderer.TileRendererLayer;
69
-import org.mapsforge.map.model.MapViewPosition;
70
-import org.mapsforge.map.model.Model;
71
-import org.mapsforge.map.model.common.PreferencesFacade;
72
-import org.mapsforge.map.reader.MapFile;
73
-import org.mapsforge.map.rendertheme.InternalRenderTheme;
74
-
75
-public final class Samples {
76
-	private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
77
-	private static final boolean SHOW_DEBUG_LAYERS = false;
78
-	private static final boolean SHOW_RASTER_MAP = false;
79
-
80
-	private static final String MESSAGE = "Are you sure you want to exit the application?";
81
-	private static final String TITLE = "Confirm close";
82
-
83
-	/**
84
-	 * Starts the {@code Samples}.
85
-	 *
86
-	 * @param args
87
-	 *            command line args: expects the map files as multiple parameters
88
-	 *            with possible SRTM hgt folder as 1st argument.
89
-	 * @throws Exception
90
-	 */
91
-	public static void main(String[] args) throws Exception {
92
-
93
-		// Multithreaded map rendering
94
-		Parameters.NUMBER_OF_THREADS = 2;
95
-
96
-		// Square frame buffer
97
-		Parameters.SQUARE_FRAME_BUFFER = false;
98
-
99
-		HillsRenderConfig hillsCfg = null;
100
-		File demFolder = getDemFolder(args);
101
-		if (demFolder != null) {
102
-			MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource(
103
-					demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE);
104
-			tileSource.setEnableInterpolationOverlap(true);
105
-			hillsCfg = new HillsRenderConfig(tileSource);
106
-			hillsCfg.indexOnThread();
107
-			args = Arrays.copyOfRange(args, 1, args.length);
108
-		}
109
-
110
-		List<File> mapFiles = getMapFiles(args);
111
-		final MapView mapView = createMapView();
112
-		final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg);
113
-
114
-		// addAPath(mapView);
115
-
116
-		final PreferencesFacade preferencesFacade = new JavaPreferences(
117
-				Preferences.userNodeForPackage(Samples.class));
118
-
119
-		final JFrame frame = new JFrame();
120
-		frame.setTitle("Mapsforge Samples");
121
-		frame.add(mapView);
122
-		frame.pack();
123
-		frame.setSize(new Dimension(800, 600));
124
-		frame.setLocationRelativeTo(null);
125
-		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
126
-		frame.addWindowListener(new WindowAdapter() {
127
-			@Override
128
-			public void windowClosing(WindowEvent e) {
129
-				int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE,
130
-						JOptionPane.YES_NO_OPTION);
131
-				if (result == JOptionPane.YES_OPTION) {
132
-					mapView.getModel().save(preferencesFacade);
133
-					mapView.destroyAll();
134
-					AwtGraphicFactory.clearResourceMemoryCache();
135
-					frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
136
-				}
137
-			}
138
-
139
-			@Override
140
-			public void windowOpened(WindowEvent e) {
141
-				final Model model = mapView.getModel();
142
-				model.init(preferencesFacade);
143
-				if (model.mapViewPosition.getZoomLevel() == 0
144
-						|| !boundingBox.contains(model.mapViewPosition.getCenter())) {
145
-					byte zoomLevel = LatLongUtils.zoomForBounds(
146
-							model.mapViewDimension.getDimension(), boundingBox,
147
-							model.displayModel.getTileSize());
148
-					model.mapViewPosition.setMapPosition(
149
-							new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
150
-				}
151
-			}
152
-		});
153
-		frame.setVisible(true);
154
-	}
155
-
156
-	private static void addAPath(MapView mapView) throws Exception {
157
-
158
-		Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read();
159
-		Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path")))
160
-				.readPath(gr);
161
-
162
-		Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
163
-		paintStroke.setColor(Color.GREEN);
164
-		paintStroke.setStrokeWidth(3);
165
-		paintStroke.setStyle(Style.STROKE);
166
-
167
-		Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
168
-
169
-		for (Arc arc : path.getArcs()) {
170
-			ArrayList<org.insa.graph.Point> points = arc.getPoints();
171
-			for (int i = 0; i < points.size(); ++i) {
172
-				line.getLatLongs().add(
173
-						new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
174
-			}
175
-		}
176
-
177
-		mapView.getLayerManager().getLayers().add(line);
178
-	}
179
-
180
-	private static BoundingBox addLayers(MapView mapView, List<File> mapFiles,
181
-			HillsRenderConfig hillsRenderConfig) {
182
-		Layers layers = mapView.getLayerManager().getLayers();
183
-
184
-		int tileSize = SHOW_RASTER_MAP ? 256 : 512;
185
-
186
-		// Tile cache
187
-		TileCache tileCache = AwtUtil.createTileCache(tileSize,
188
-				mapView.getModel().frameBufferModel.getOverdrawFactor(), 1024,
189
-				new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
190
-
191
-		final BoundingBox boundingBox;
192
-		if (SHOW_RASTER_MAP) {
193
-			// Raster
194
-			mapView.getModel().displayModel.setFixedTileSize(tileSize);
195
-			TileSource tileSource = OpenStreetMapMapnik.INSTANCE;
196
-			TileDownloadLayer tileDownloadLayer = createTileDownloadLayer(tileCache,
197
-					mapView.getModel().mapViewPosition, tileSource);
198
-			layers.add(tileDownloadLayer);
199
-			tileDownloadLayer.start();
200
-			mapView.setZoomLevelMin(tileSource.getZoomLevelMin());
201
-			mapView.setZoomLevelMax(tileSource.getZoomLevelMax());
202
-			boundingBox = new BoundingBox(LatLongUtils.LATITUDE_MIN, LatLongUtils.LONGITUDE_MIN,
203
-					LatLongUtils.LATITUDE_MAX, LatLongUtils.LONGITUDE_MAX);
204
-		}
205
-		else {
206
-			// Vector
207
-			mapView.getModel().displayModel.setFixedTileSize(tileSize);
208
-			MultiMapDataStore mapDataStore = new MultiMapDataStore(
209
-					MultiMapDataStore.DataPolicy.RETURN_ALL);
210
-			for (File file : mapFiles) {
211
-				mapDataStore.addMapDataStore(new MapFile(file), false, false);
212
-			}
213
-			TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
214
-					mapView.getModel().mapViewPosition, hillsRenderConfig);
215
-			layers.add(tileRendererLayer);
216
-			boundingBox = mapDataStore.boundingBox();
217
-		}
218
-
219
-		// Debug
220
-		if (SHOW_DEBUG_LAYERS) {
221
-			layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
222
-			layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel));
223
-		}
224
-
225
-		return boundingBox;
226
-	}
227
-
228
-	private static MapView createMapView() {
229
-		MapView mapView = new MapView();
230
-		mapView.getMapScaleBar().setVisible(true);
231
-		if (SHOW_DEBUG_LAYERS) {
232
-			mapView.getFpsCounter().setVisible(true);
233
-		}
234
-
235
-		return mapView;
236
-	}
237
-
238
-	@SuppressWarnings("unused")
239
-	private static TileDownloadLayer createTileDownloadLayer(TileCache tileCache,
240
-			MapViewPosition mapViewPosition, TileSource tileSource) {
241
-		return new TileDownloadLayer(tileCache, mapViewPosition, tileSource, GRAPHIC_FACTORY) {
242
-			@Override
243
-			public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
244
-				System.out.println("Tap on: " + tapLatLong);
245
-				return true;
246
-			}
247
-		};
248
-	}
249
-
250
-	private static TileRendererLayer createTileRendererLayer(TileCache tileCache,
251
-			MapDataStore mapDataStore, MapViewPosition mapViewPosition,
252
-			HillsRenderConfig hillsRenderConfig) {
253
-		TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
254
-				mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
255
-			@Override
256
-			public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
257
-				System.out.println("Tap on: " + tapLatLong);
258
-				return true;
259
-			}
260
-		};
261
-		tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
262
-		return tileRendererLayer;
263
-	}
264
-
265
-	private static File getDemFolder(String[] args) {
266
-		if (args.length == 0) {
267
-			throw new IllegalArgumentException("missing argument: <mapFile>");
268
-		}
269
-
270
-		File demFolder = new File(args[0]);
271
-		if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) {
272
-			return demFolder;
273
-		}
274
-		return null;
275
-	}
276
-
277
-	private static List<File> getMapFiles(String[] args) {
278
-		if (args.length == 0) {
279
-			throw new IllegalArgumentException("missing argument: <mapFile>");
280
-		}
281
-
282
-		List<File> result = new ArrayList<>();
283
-		for (String arg : args) {
284
-			File mapFile = new File(arg);
285
-			if (!mapFile.exists()) {
286
-				throw new IllegalArgumentException("file does not exist: " + mapFile);
287
-			}
288
-			else if (!mapFile.isFile()) {
289
-				throw new IllegalArgumentException("not a file: " + mapFile);
290
-			}
291
-			else if (!mapFile.canRead()) {
292
-				throw new IllegalArgumentException("cannot read file: " + mapFile);
293
-			}
294
-			result.add(mapFile);
295
-		}
296
-		return result;
297
-	}
298
-
299
-	private Samples() {
300
-		throw new IllegalStateException();
301
-	}
302
-}

+ 317
- 286
src/main/org/insa/drawing/BasicDrawing.java View File

@@ -5,12 +5,16 @@ import java.awt.Color;
5 5
 import java.awt.Graphics;
6 6
 import java.awt.Graphics2D;
7 7
 import java.awt.Image;
8
+import java.awt.event.MouseAdapter;
8 9
 import java.awt.event.MouseEvent;
10
+import java.awt.event.MouseListener;
9 11
 import java.awt.geom.NoninvertibleTransformException;
10 12
 import java.awt.geom.Point2D;
11
-import java.awt.image.*;
13
+import java.awt.image.BufferedImage;
12 14
 import java.util.ArrayList;
15
+import java.util.IdentityHashMap;
13 16
 import java.util.Iterator;
17
+import java.util.Map;
14 18
 
15 19
 import javax.swing.JPanel;
16 20
 
@@ -21,294 +25,321 @@ import org.insa.graph.Path;
21 25
 import org.insa.graph.Point;
22 26
 
23 27
 /**
24
- *   Cette implementation de la classe Dessin produit vraiment un affichage
25
- *   (au contraire de la classe DessinInvisible).
28
+ * Cette implementation de la classe Dessin produit vraiment un affichage (au
29
+ * contraire de la classe DessinInvisible).
26 30
  */
27 31
 
28 32
 public class BasicDrawing extends JPanel implements Drawing {
29 33
 
30
-	/**
31
-	 * 
32
-	 */
33
-	private static final long serialVersionUID = 96779785877771827L;
34
-	
35
-	// Default path color.
36
-	public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255);
37
-
38
-	// Default palette.
39
-	public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette();
40
-	
41
-	// Default marker width
42
-	private static final int DEFAULT_MARKER_WIDTH = 10;
43
-	
44
-	// 
45
-	private final Graphics2D gr;
46
-
47
-	private double long1, long2, lat1, lat2;
48
-	
49
-	// Width and height of the image
50
-	private final int width, height;
51
-	
52
-	private Image image;
53
-	private ZoomAndPanListener zoomAndPanListener;
54
-	
55
-	/**
56
-	 * Create a new BasicDrawing.
57
-	 * 
58
-	 */
59
-	public BasicDrawing() {
60
-		
61
-		this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
62
-		this.addMouseListener(zoomAndPanListener);
63
-		this.addMouseMotionListener(zoomAndPanListener);
64
-		this.addMouseWheelListener(zoomAndPanListener);
65
-		
66
-		this.width = 2000;
67
-		this.height = 1600;
68
-		
69
-		BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
70
-		
71
-		this.image = img;
72
-		this.gr = img.createGraphics();
73
-		
74
-		this.zoomAndPanListener.setCoordTransform(this.gr.getTransform());
75
-
76
-		this.long1 = -180;
77
-		this.long2 = 180;
78
-		this.lat1  = -90;
79
-		this.lat2  = 90;
80
-
81
-		this.clear();
82
-		this.repaint();
83
-
84
-	}
85
-
86
-	@Override
87
-	public void paintComponent(Graphics g1) {
88
-		Graphics2D g = (Graphics2D)g1;
89
-		g.clearRect(0, 0, getWidth(), getHeight());
90
-		g.setTransform(zoomAndPanListener.getCoordTransform());
91
-		g.drawImage(image, 0, 0, this);
92
-	}
93
-
94
-	protected void setBB(double long1, double long2, double lat1, double lat2) {	
95
-
96
-		if (long1 > long2 || lat1 > lat2) {
97
-			throw new Error("DessinVisible.setBB : mauvaises coordonnees.");
98
-		}
99
-				
100
-		this.long1 = long1;
101
-		this.long2 = long2;
102
-		this.lat1= lat1;
103
-		this.lat2 = lat2;
104
-				
105
-		double scale = 1 / Math.max(this.width / (double)this.getWidth(),  this.height / (double)this.getHeight());
106
-		
107
-		this.zoomAndPanListener.getCoordTransform().setToIdentity();
108
-		this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2, 
109
-				(this.getHeight() - this.height * scale) / 2);
110
-		this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
111
-		this.zoomAndPanListener.setZoomLevel(0);
112
-		this.repaint();
113
-		
114
-	}
115
-
116
-	private int projx(double lon) {
117
-		return (int)(width * (lon - this.long1) / (this.long2 - this.long1)) ;
118
-	}
119
-
120
-	private int projy(double lat) {
121
-		return (int)(height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))) ;
122
-	}
123
-	
124
-	/**
125
-	 * Return the longitude and latitude corresponding to the given
126
-	 * position of the MouseEvent.
127
-	 * 
128
-	 * @param event
129
-	 * 
130
-	 * @return
131
-	 */
132
-	public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
133
-		// Get the point using the inverse transform of the Zoom/Pan object, this gives us
134
-		// a point within the drawing box (between [0, 0] and [width, height]).
135
-		Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null);
136
-		
137
-		// Inverse the "projection" on x/y to get longitude and latitude.
138
-		double lon = ptDst.getX();
139
-		double lat = ptDst.getY();
140
-		lon = (lon / this.width) * (this.long2 - this.long1) + this.long1;
141
-		lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1;
142
-		
143
-		// Return a new point.
144
-		return new Point(lon, lat);			
145
-	}
146
-	
147
-	protected void setWidth(int width) {
148
-		this.gr.setStroke(new BasicStroke(width));
149
-	}
150
-
151
-	protected void setColor(Color col) {
152
-		this.gr.setColor(col);
153
-	}
154
-	
155
-	@Override
156
-	public void clear() {
157
-		this.gr.setColor(Color.WHITE);
158
-		this.gr.fillRect(0, 0, this.width, this.height);
159
-	}
160
-
161
-	@Override
162
-	public void drawLine(Point from, Point to) {
163
-		int x1 = this.projx(from.getLongitude()) ;
164
-		int x2 = this.projx(to.getLongitude()) ;
165
-		int y1 = this.projy(from.getLatitude()) ;
166
-		int y2 = this.projy(to.getLatitude()) ;
167
-
168
-		gr.drawLine(x1, y1, x2, y2) ;
169
-		this.repaint();	
170
-	}
171
-
172
-	@Override
173
-	public void drawLine(Point from, Point to, int width) {
174
-		setWidth(width);
175
-		drawLine(from, to);		
176
-	}
177
-
178
-	@Override
179
-	public void drawLine(Point from, Point to, int width, Color color) {
180
-		setWidth(width);
181
-		setColor(color);	
182
-		drawLine(from, to);
183
-	}
184
-	
185
-	@Override
186
-	public void drawMarker(Point point) {
187
-		drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor());
188
-	}
189
-
190
-
191
-	@Override
192
-	public void drawMarker(Point point, Color color) {
193
-		setColor(color);
194
-		drawMarker(point);
195
-	}
196
-	
197
-	@Override
198
-	public void drawPoint(Point point, int width, Color color) {
199
-		setWidth(width);
200
-		setColor(color);
201
-		int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2;
202
-		int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2;
203
-		gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH);
204
-		this.repaint();
205
-	}
206
-	
207
-	/**
208
-	 * Draw the given arc.
209
-	 * 
210
-	 * @param arc Arc to draw.
211
-	 * @param palette Palette to use to retrieve color and width for arc,
212
-	 * or null to use current settings.
213
-	 */
214
-	public void drawArc(Arc arc, GraphPalette palette) {
215
-		ArrayList<Point> pts = arc.getPoints();
216
-		if (!pts.isEmpty()) {
217
-			if (palette != null) {
218
-				setColor(palette.getColorForType(arc.getInfo().getType()));
219
-				setWidth(palette.getWidthForType(arc.getInfo().getType()));
220
-			}
221
-			Iterator<Point> it1 = pts.iterator();
222
-			Point prev = it1.next();
223
-			while (it1.hasNext()) {
224
-				Point curr = it1.next();
225
-				drawLine(prev, curr);
226
-				prev = curr;
227
-			}
228
-		}
229
-	}
230
-	
231
-	/**
232
-	 * Initialize the drawing for the given graph.
233
-	 * 
234
-	 * @param graph
235
-	 */
236
-	public void initialize(Graph graph) {
237
-		double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, 
238
-				maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY;
239
-		for (Node node: graph.getNodes()) {
240
-			Point pt = node.getPoint();
241
-			if (pt.getLatitude() < minLat) {
242
-				minLat = pt.getLatitude();
243
-			}
244
-			if (pt.getLatitude() > maxLat) {
245
-				maxLat = pt.getLatitude();
246
-			}
247
-			if (pt.getLongitude() < minLon) {
248
-				minLon = pt.getLongitude();
249
-			}
250
-			if (pt.getLongitude() > maxLon) {
251
-				maxLon = pt.getLongitude();
252
-			}
253
-		}
254
-		
255
-		double deltaLon = 0.02 * (maxLon - minLon),
256
-				deltaLat = 0.02 * (maxLat - minLat);
257
-
258
-		setBB(minLon - deltaLon, maxLon + deltaLon, 
259
-			minLat - deltaLat, maxLat + deltaLat);
260
-	}
261
-
262
-	@Override
263
-	public void drawGraph(Graph graph, GraphPalette palette) {
264
-		clear();
265
-		initialize(graph);
266
-		for (Node node: graph.getNodes()) {
267
-			for (Arc arc: node.getSuccessors()) {
268
-				drawArc(arc, palette);
269
-			}
270
-		}
271
-	}
272
-
273
-	@Override
274
-	public void drawGraph(Graph graph) {
275
-		drawGraph(graph, DEFAULT_PALETTE);		
276
-	}
277
-
278
-	@Override
279
-	public void drawPath(Path path, Color color, boolean markers) {
280
-		setColor(color);
281
-		setWidth(2);
282
-		for (Arc arc: path.getArcs()) {
283
-			drawArc(arc, null);
284
-		}
285
-		if (markers) {
286
-			drawMarker(path.getOrigin().getPoint(), color);
287
-			drawMarker(path.getDestination().getPoint(), color);
288
-		}
289
-	}
290
-	
291
-	@Override
292
-	public void drawPath(Path path, Color color) {
293
-		drawPath(path, color, true);
294
-	}
295
-
296
-	@Override
297
-	public void drawPath(Path path) {
298
-		drawPath(path, DEFAULT_PATH_COLOR);
299
-	}
300
-	
301
-	@Override
302
-	public void drawPath(Path path, boolean markers) {
303
-		drawPath(path, DEFAULT_PATH_COLOR, markers);
304
-	}
305
-	
306
-	@SuppressWarnings("unused")
307
-	private void putText(Point point, String txt) {
308
-		int x = this.projx(point.getLongitude());
309
-		int y = this.projy(point.getLatitude());
310
-		gr.drawString(txt, x, y);
311
-		this.repaint();	
312
-	}
34
+    /**
35
+     * 
36
+     */
37
+    private static final long serialVersionUID = 96779785877771827L;
38
+
39
+    // Default path color.
40
+    public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255);
41
+
42
+    // Default palette.
43
+    public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette();
44
+
45
+    // Default marker width
46
+    private static final int DEFAULT_MARKER_WIDTH = 10;
47
+
48
+    //
49
+    private final Graphics2D gr;
50
+
51
+    private double long1, long2, lat1, lat2;
52
+
53
+    // Width and height of the image
54
+    private final int width, height;
55
+
56
+    //
57
+    private Image image;
58
+    private ZoomAndPanListener zoomAndPanListener;
59
+
60
+    // Mapping DrawingClickListener -> MouseEventListener
61
+    private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>();
62
+
63
+    /**
64
+     * Create a new BasicDrawing.
65
+     * 
66
+     */
67
+    public BasicDrawing() {
68
+
69
+        this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
70
+        this.addMouseListener(zoomAndPanListener);
71
+        this.addMouseMotionListener(zoomAndPanListener);
72
+        this.addMouseWheelListener(zoomAndPanListener);
73
+
74
+        this.width = 2000;
75
+        this.height = 1600;
76
+
77
+        BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
78
+
79
+        this.image = img;
80
+        this.gr = img.createGraphics();
81
+
82
+        this.zoomAndPanListener.setCoordTransform(this.gr.getTransform());
83
+
84
+        this.long1 = -180;
85
+        this.long2 = 180;
86
+        this.lat1 = -90;
87
+        this.lat2 = 90;
88
+
89
+        this.clear();
90
+        this.repaint();
91
+
92
+    }
93
+
94
+    @Override
95
+    public void paintComponent(Graphics g1) {
96
+        Graphics2D g = (Graphics2D) g1;
97
+        g.clearRect(0, 0, getWidth(), getHeight());
98
+        g.setTransform(zoomAndPanListener.getCoordTransform());
99
+        g.drawImage(image, 0, 0, this);
100
+    }
101
+
102
+    protected void setBB(double long1, double long2, double lat1, double lat2) {
103
+
104
+        if (long1 > long2 || lat1 > lat2) {
105
+            throw new Error("DessinVisible.setBB : mauvaises coordonnees.");
106
+        }
107
+
108
+        this.long1 = long1;
109
+        this.long2 = long2;
110
+        this.lat1 = lat1;
111
+        this.lat2 = lat2;
112
+
113
+        double scale = 1 / Math.max(this.width / (double) this.getWidth(), this.height / (double) this.getHeight());
114
+
115
+        this.zoomAndPanListener.getCoordTransform().setToIdentity();
116
+        this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2,
117
+                (this.getHeight() - this.height * scale) / 2);
118
+        this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
119
+        this.zoomAndPanListener.setZoomLevel(0);
120
+        this.repaint();
121
+
122
+    }
123
+
124
+    private int projx(double lon) {
125
+        return (int) (width * (lon - this.long1) / (this.long2 - this.long1));
126
+    }
127
+
128
+    private int projy(double lat) {
129
+        return (int) (height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1)));
130
+    }
131
+
132
+    /**
133
+     * Return the longitude and latitude corresponding to the given position of the
134
+     * MouseEvent.
135
+     * 
136
+     * @param event
137
+     * 
138
+     * @return
139
+     */
140
+    public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
141
+        // Get the point using the inverse transform of the Zoom/Pan object, this gives
142
+        // us
143
+        // a point within the drawing box (between [0, 0] and [width, height]).
144
+        Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null);
145
+
146
+        // Inverse the "projection" on x/y to get longitude and latitude.
147
+        double lon = ptDst.getX();
148
+        double lat = ptDst.getY();
149
+        lon = (lon / this.width) * (this.long2 - this.long1) + this.long1;
150
+        lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1;
151
+
152
+        // Return a new point.
153
+        return new Point(lon, lat);
154
+    }
155
+
156
+    @Override
157
+    public void addDrawingClickListener(DrawingClickListener listener) {
158
+        MouseListener mListener = new MouseAdapter() {
159
+            @Override
160
+            public void mouseClicked(MouseEvent evt) {
161
+                System.out.println(evt);
162
+                try {
163
+                    listener.mouseClicked(getLongitudeLatitude(evt));
164
+                }
165
+                catch (NoninvertibleTransformException e) {
166
+                    e.printStackTrace();
167
+                }
168
+            }
169
+        };
170
+        this.addMouseListener(mListener);
171
+        this.listenerMapping.put(listener, mListener);
172
+    }
173
+
174
+    @Override
175
+    public void removeDrawingClickListener(DrawingClickListener listener) {
176
+        this.removeMouseListener(this.listenerMapping.get(listener));
177
+    }
178
+
179
+    protected void setWidth(int width) {
180
+        this.gr.setStroke(new BasicStroke(width));
181
+    }
182
+
183
+    protected void setColor(Color col) {
184
+        this.gr.setColor(col);
185
+    }
186
+
187
+    @Override
188
+    public void clear() {
189
+        this.gr.setColor(Color.WHITE);
190
+        this.gr.fillRect(0, 0, this.width, this.height);
191
+    }
192
+
193
+    @Override
194
+    public void drawLine(Point from, Point to) {
195
+        int x1 = this.projx(from.getLongitude());
196
+        int x2 = this.projx(to.getLongitude());
197
+        int y1 = this.projy(from.getLatitude());
198
+        int y2 = this.projy(to.getLatitude());
199
+
200
+        gr.drawLine(x1, y1, x2, y2);
201
+        this.repaint();
202
+    }
203
+
204
+    @Override
205
+    public void drawLine(Point from, Point to, int width) {
206
+        setWidth(width);
207
+        drawLine(from, to);
208
+    }
209
+
210
+    @Override
211
+    public void drawLine(Point from, Point to, int width, Color color) {
212
+        setWidth(width);
213
+        setColor(color);
214
+        drawLine(from, to);
215
+    }
216
+
217
+    @Override
218
+    public void drawMarker(Point point) {
219
+        drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor());
220
+    }
221
+
222
+    @Override
223
+    public void drawMarker(Point point, Color color) {
224
+        setColor(color);
225
+        drawMarker(point);
226
+    }
227
+
228
+    @Override
229
+    public void drawPoint(Point point, int width, Color color) {
230
+        setWidth(width);
231
+        setColor(color);
232
+        int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2;
233
+        int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2;
234
+        gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH);
235
+        this.repaint();
236
+    }
237
+
238
+    /**
239
+     * Draw the given arc.
240
+     * 
241
+     * @param arc
242
+     *            Arc to draw.
243
+     * @param palette
244
+     *            Palette to use to retrieve color and width for arc, or null to use
245
+     *            current settings.
246
+     */
247
+    public void drawArc(Arc arc, GraphPalette palette) {
248
+        ArrayList<Point> pts = arc.getPoints();
249
+        if (!pts.isEmpty()) {
250
+            if (palette != null) {
251
+                setColor(palette.getColorForType(arc.getInfo().getType()));
252
+                setWidth(palette.getWidthForType(arc.getInfo().getType()));
253
+            }
254
+            Iterator<Point> it1 = pts.iterator();
255
+            Point prev = it1.next();
256
+            while (it1.hasNext()) {
257
+                Point curr = it1.next();
258
+                drawLine(prev, curr);
259
+                prev = curr;
260
+            }
261
+        }
262
+    }
263
+
264
+    /**
265
+     * Initialize the drawing for the given graph.
266
+     * 
267
+     * @param graph
268
+     */
269
+    public void initialize(Graph graph) {
270
+        double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, maxLon = Double.NEGATIVE_INFINITY,
271
+                maxLat = Double.NEGATIVE_INFINITY;
272
+        for (Node node: graph.getNodes()) {
273
+            Point pt = node.getPoint();
274
+            if (pt.getLatitude() < minLat) {
275
+                minLat = pt.getLatitude();
276
+            }
277
+            if (pt.getLatitude() > maxLat) {
278
+                maxLat = pt.getLatitude();
279
+            }
280
+            if (pt.getLongitude() < minLon) {
281
+                minLon = pt.getLongitude();
282
+            }
283
+            if (pt.getLongitude() > maxLon) {
284
+                maxLon = pt.getLongitude();
285
+            }
286
+        }
287
+
288
+        double deltaLon = 0.02 * (maxLon - minLon), deltaLat = 0.02 * (maxLat - minLat);
289
+
290
+        setBB(minLon - deltaLon, maxLon + deltaLon, minLat - deltaLat, maxLat + deltaLat);
291
+    }
292
+
293
+    @Override
294
+    public void drawGraph(Graph graph, GraphPalette palette) {
295
+        clear();
296
+        initialize(graph);
297
+        for (Node node: graph.getNodes()) {
298
+            for (Arc arc: node.getSuccessors()) {
299
+                drawArc(arc, palette);
300
+            }
301
+        }
302
+    }
303
+
304
+    @Override
305
+    public void drawGraph(Graph graph) {
306
+        drawGraph(graph, DEFAULT_PALETTE);
307
+    }
308
+
309
+    @Override
310
+    public void drawPath(Path path, Color color, boolean markers) {
311
+        setColor(color);
312
+        setWidth(2);
313
+        for (Arc arc: path.getArcs()) {
314
+            drawArc(arc, null);
315
+        }
316
+        if (markers) {
317
+            drawMarker(path.getOrigin().getPoint(), color);
318
+            drawMarker(path.getDestination().getPoint(), color);
319
+        }
320
+    }
321
+
322
+    @Override
323
+    public void drawPath(Path path, Color color) {
324
+        drawPath(path, color, true);
325
+    }
326
+
327
+    @Override
328
+    public void drawPath(Path path) {
329
+        drawPath(path, DEFAULT_PATH_COLOR);
330
+    }
331
+
332
+    @Override
333
+    public void drawPath(Path path, boolean markers) {
334
+        drawPath(path, DEFAULT_PATH_COLOR, markers);
335
+    }
336
+
337
+    @SuppressWarnings("unused")
338
+    private void putText(Point point, String txt) {
339
+        int x = this.projx(point.getLongitude());
340
+        int y = this.projy(point.getLatitude());
341
+        gr.drawString(txt, x, y);
342
+        this.repaint();
343
+    }
313 344
 
314 345
 }

+ 124
- 109
src/main/org/insa/drawing/Drawing.java View File

@@ -7,113 +7,128 @@ import org.insa.graph.Path;
7 7
 import org.insa.graph.Point;
8 8
 
9 9
 public interface Drawing {
10
-	
11
-	/**
12
-	 * Clear the drawing.
13
-	 */
14
-	public void clear();
15
-
16
-	/**
17
-	 * Draw a line between the two given points with the default color
18
-	 * and width.
19
-	 * 
20
-	 * @param from
21
-	 * @param to
22
-	 */
23
-	public void drawLine(Point from, Point to);
24
-	
25
-	/**
26
-	 * Draw a line between the two given points with the default color
27
-	 * and the given width.
28
-	 * 
29
-	 * @param from
30
-	 * @param to
31
-	 * @param width
32
-	 */
33
-	public void drawLine(Point from, Point to, int width);
34
-	
35
-	/**
36
-	 * Draw a line between the two given points with the given color
37
-	 * and the given width.
38
-	 * 
39
-	 * @param from
40
-	 * @param to
41
-	 * @param width
42
-	 * @param color
43
-	 */
44
-	public void drawLine(Point from, Point to, int width, Color color);
45
-	
46
-	/**
47
-	 * Draw a marker at the given point with the default color.
48
-	 * 
49
-	 * @param point
50
-	 */
51
-	public void drawMarker(Point point);
52
-	
53
-	/**
54
-	 * Draw the given point with the given color.
55
-	 * 
56
-	 * @param point
57
-	 */
58
-	public void drawMarker(Point point, Color color);
59
-	
60
-	/**
61
-	 * Draw a point width the given width and color. Do not use this to mark location,
62
-	 * use drawMarker.
63
-	 * 
64
-	 * @param point
65
-	 * @param width
66
-	 * @param color
67
-	 */
68
-	public void drawPoint(Point point, int width, Color color);
69
-
70
-	/**
71
-	 * Draw the given graph using the given palette.
72
-	 * 
73
-	 * @param graph
74
-	 * @param palette
75
-	 */
76
-	public void drawGraph(Graph graph, GraphPalette palette);
77
-	
78
-	/**
79
-	 * Draw the given graph using a default palette specific to the implementation.
80
-	 * 
81
-	 * @param graph
82
-	 */
83
-	public void drawGraph(Graph graph);
84
-	
85
-	/**
86
-	 * Draw a path using the given color.
87
-	 * 
88
-	 * @param path
89
-	 * @param color
90
-	 * @param markers Show origin and destination markers.
91
-	 */
92
-	public void drawPath(Path path, Color color, boolean markers);
93
-	
94
-	/**
95
-	 * Draw a path using the given color with markers.
96
-	 * 
97
-	 * @param path
98
-	 * @param color
99
-	 */
100
-	public void drawPath(Path path, Color color);
101
-	
102
-	/**
103
-	 * Draw a path using a default color specific to the implementation
104
-	 * 
105
-	 * 
106
-	 * @param path
107
-	 * @param markers Show origin and destination markers.
108
-	 */
109
-	public void drawPath(Path path, boolean markers);
110
-	
111
-	/**
112
-	 * Draw a path using a default color specific to the implementation
113
-	 * 
114
-	 * 
115
-	 * @param path
116
-	 */
117
-	public void drawPath(Path path);
118
-	
10
+
11
+    /**
12
+     * Add a listener to click to this drawing.
13
+     * 
14
+     * @param listener
15
+     */
16
+    public void addDrawingClickListener(DrawingClickListener listener);
17
+
18
+    /**
19
+     * Remove the given listener from the drawing.
20
+     * 
21
+     * @param listener
22
+     */
23
+    public void removeDrawingClickListener(DrawingClickListener listener);
24
+
25
+    /**
26
+     * Clear the drawing.
27
+     */
28
+    public void clear();
29
+
30
+    /**
31
+     * Draw a line between the two given points with the default color and width.
32
+     * 
33
+     * @param from
34
+     * @param to
35
+     */
36
+    public void drawLine(Point from, Point to);
37
+
38
+    /**
39
+     * Draw a line between the two given points with the default color and the given
40
+     * width.
41
+     * 
42
+     * @param from
43
+     * @param to
44
+     * @param width
45
+     */
46
+    public void drawLine(Point from, Point to, int width);
47
+
48
+    /**
49
+     * Draw a line between the two given points with the given color and the given
50
+     * width.
51
+     * 
52
+     * @param from
53
+     * @param to
54
+     * @param width
55
+     * @param color
56
+     */
57
+    public void drawLine(Point from, Point to, int width, Color color);
58
+
59
+    /**
60
+     * Draw a marker at the given point with the default color.
61
+     * 
62
+     * @param point
63
+     */
64
+    public void drawMarker(Point point);
65
+
66
+    /**
67
+     * Draw the given point with the given color.
68
+     * 
69
+     * @param point
70
+     */
71
+    public void drawMarker(Point point, Color color);
72
+
73
+    /**
74
+     * Draw a point width the given width and color. Do not use this to mark
75
+     * location, use drawMarker.
76
+     * 
77
+     * @param point
78
+     * @param width
79
+     * @param color
80
+     */
81
+    public void drawPoint(Point point, int width, Color color);
82
+
83
+    /**
84
+     * Draw the given graph using the given palette.
85
+     * 
86
+     * @param graph
87
+     * @param palette
88
+     */
89
+    public void drawGraph(Graph graph, GraphPalette palette);
90
+
91
+    /**
92
+     * Draw the given graph using a default palette specific to the implementation.
93
+     * 
94
+     * @param graph
95
+     */
96
+    public void drawGraph(Graph graph);
97
+
98
+    /**
99
+     * Draw a path using the given color.
100
+     * 
101
+     * @param path
102
+     * @param color
103
+     * @param markers
104
+     *            Show origin and destination markers.
105
+     */
106
+    public void drawPath(Path path, Color color, boolean markers);
107
+
108
+    /**
109
+     * Draw a path using the given color with markers.
110
+     * 
111
+     * @param path
112
+     * @param color
113
+     */
114
+    public void drawPath(Path path, Color color);
115
+
116
+    /**
117
+     * Draw a path using a default color specific to the implementation
118
+     * 
119
+     * 
120
+     * @param path
121
+     * @param markers
122
+     *            Show origin and destination markers.
123
+     */
124
+    public void drawPath(Path path, boolean markers);
125
+
126
+    /**
127
+     * Draw a path using a default color specific to the implementation
128
+     * 
129
+     * 
130
+     * @param path
131
+     */
132
+    public void drawPath(Path path);
133
+
119 134
 }

+ 14
- 0
src/main/org/insa/drawing/DrawingClickListener.java View File

@@ -0,0 +1,14 @@
1
+package org.insa.drawing;
2
+
3
+import org.insa.graph.Point;
4
+
5
+public interface DrawingClickListener {
6
+
7
+    /**
8
+     * Event triggered when a click is made on the map.
9
+     * 
10
+     * @param point
11
+     */
12
+    public void mouseClicked(Point point);
13
+
14
+}

+ 324
- 272
src/main/org/insa/drawing/MapViewDrawing.java View File

@@ -1,272 +1,324 @@
1
-package org.insa.drawing;
2
-
3
-import java.awt.Color;
4
-import java.awt.event.MouseAdapter;
5
-import java.awt.event.MouseWheelEvent;
6
-import java.io.File;
7
-import java.util.ArrayList;
8
-import java.util.HashMap;
9
-import java.util.Map;
10
-import java.util.UUID;
11
-
12
-import javax.swing.JFileChooser;
13
-import javax.swing.filechooser.FileNameExtensionFilter;
14
-
15
-import org.insa.graph.Arc;
16
-import org.insa.graph.Graph;
17
-import org.insa.graph.Path;
18
-import org.insa.graph.Point;
19
-import org.mapsforge.core.graphics.GraphicFactory;
20
-import org.mapsforge.core.graphics.Paint;
21
-import org.mapsforge.core.graphics.Style;
22
-import org.mapsforge.core.model.BoundingBox;
23
-import org.mapsforge.core.model.LatLong;
24
-import org.mapsforge.core.model.MapPosition;
25
-import org.mapsforge.core.util.LatLongUtils;
26
-import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
27
-import org.mapsforge.map.awt.util.AwtUtil;
28
-import org.mapsforge.map.awt.view.MapView;
29
-import org.mapsforge.map.datastore.MapDataStore;
30
-import org.mapsforge.map.layer.Layers;
31
-import org.mapsforge.map.layer.cache.TileCache;
32
-import org.mapsforge.map.layer.hills.HillsRenderConfig;
33
-import org.mapsforge.map.layer.overlay.Marker;
34
-import org.mapsforge.map.layer.overlay.Polyline;
35
-import org.mapsforge.map.layer.renderer.TileRendererLayer;
36
-import org.mapsforge.map.model.DisplayModel;
37
-import org.mapsforge.map.model.MapViewPosition;
38
-import org.mapsforge.map.model.Model;
39
-import org.mapsforge.map.reader.MapFile;
40
-import org.mapsforge.map.rendertheme.InternalRenderTheme;
41
-import org.mapsforge.map.rendertheme.XmlRenderTheme;
42
-
43
-public class MapViewDrawing extends MapView implements Drawing {
44
-
45
-    /**
46
-     * 
47
-     */
48
-    private static final long serialVersionUID = 8606967833704938092L;
49
-
50
-    // Default path color.
51
-    public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
52
-
53
-    // Graphic factory.
54
-    private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
55
-
56
-    // Default tile size.
57
-    private static final int DEFAULT_TILE_SIZE = 512;
58
-
59
-    // Tile size.
60
-    int tileSize;
61
-
62
-    ArrayList<Paint> extraLayers;
63
-
64
-    public MapViewDrawing() {
65
-        getMapScaleBar().setVisible(true);
66
-        this.tileSize = DEFAULT_TILE_SIZE;
67
-        DisplayModel model = getModel().displayModel;
68
-        model.setFixedTileSize(tileSize);
69
-        // model.setBackgroundColor(convertColor(Color.WHITE));
70
-
71
-        extraLayers = new ArrayList<Paint>();
72
-        addMouseWheelListener(new MouseAdapter() {
73
-            @Override
74
-            public void mouseWheelMoved(MouseWheelEvent e) {
75
-                byte zoomLevelDiff = (byte) -e.getWheelRotation();
76
-                for (Paint p: extraLayers) {
77
-                    p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff);
78
-                }
79
-            }
80
-        });
81
-    }
82
-
83
-    protected int convertColor(Color color) {
84
-        return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(),
85
-                color.getBlue());
86
-    }
87
-
88
-    private int getStrokeWidth(int width) {
89
-        byte zoomLevel = getModel().mapViewPosition.getZoomLevel();
90
-        return width * (2 - (8 - zoomLevel));
91
-    }
92
-
93
-    private Paint createPaintStroke(int width, Color color) {
94
-        Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
95
-        paintStroke.setStyle(Style.STROKE);
96
-        if (width != 0) {
97
-            paintStroke.setStrokeWidth(getStrokeWidth(width));
98
-        }
99
-        if (color != null) {
100
-            paintStroke.setColor(convertColor(color));
101
-        }
102
-        return paintStroke;
103
-    }
104
-
105
-    /**
106
-     * 
107
-     * @param color
108
-     * @return
109
-     */
110
-    private File getMapsforgeFileFromGraph(Graph graph) {
111
-        // TODO: Find a way to change this...
112
-        Map<Integer, String> idToNames = new HashMap<Integer, String>();
113
-        idToNames.put(0x100, "insa");
114
-        idToNames.put(0x110, "paris");
115
-        idToNames.put(0x200, "mayotte");
116
-        idToNames.put(0x250, "newzealand");
117
-        idToNames.put(0x300, "reunion");
118
-        idToNames.put(0x400, "midip");
119
-        idToNames.put(0x410, "morbihan");
120
-
121
-        File file = null;
122
-        if (idToNames.containsKey(graph.getMapId())) {
123
-            file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
124
-            if (!file.exists()) {
125
-                file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg");
126
-            }
127
-        }
128
-
129
-        if (file == null || !file.exists()) {
130
-            JFileChooser fileChooser = new JFileChooser("Maps/");
131
-            fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg"));
132
-            if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
133
-                file = fileChooser.getSelectedFile();
134
-            }
135
-        }
136
-
137
-        return file;
138
-    }
139
-
140
-    protected LatLong convertPoint(Point point) {
141
-        return new LatLong(point.getLatitude(), point.getLongitude());
142
-    }
143
-
144
-    private static TileRendererLayer createTileRendererLayer(TileCache tileCache,
145
-            MapDataStore mapDataStore, MapViewPosition mapViewPosition,
146
-            HillsRenderConfig hillsRenderConfig) {
147
-        TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
148
-                mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
149
-            @Override
150
-            public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
151
-                    org.mapsforge.core.model.Point tapXY) {
152
-                System.out.println("Tap on: " + tapLatLong);
153
-                return true;
154
-            }
155
-        };
156
-        XmlRenderTheme renderTheme = InternalRenderTheme.DEFAULT;
157
-        tileRendererLayer.setXmlRenderTheme(renderTheme);
158
-        return tileRendererLayer;
159
-    }
160
-
161
-    @Override
162
-    public void clear() {
163
-        getLayerManager().getLayers().clear();
164
-        extraLayers.clear();
165
-        repaint();
166
-    }
167
-
168
-    @Override
169
-    public void drawLine(Point from, Point to) {
170
-        drawLine(from, to, 0, null);
171
-    }
172
-
173
-    @Override
174
-    public void drawLine(Point from, Point to, int width) {
175
-        drawLine(from, to, width, null);
176
-    }
177
-
178
-    @Override
179
-    public void drawLine(Point from, Point to, int width, Color color) {
180
-        Paint paintStroke = createPaintStroke(width, color);
181
-        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
182
-        line.getLatLongs().add(convertPoint(from));
183
-        line.getLatLongs().add(convertPoint(to));
184
-        getLayerManager().getLayers().add(line);
185
-    }
186
-
187
-    @Override
188
-    public void drawMarker(Point point) {
189
-        drawMarker(point, null);
190
-    }
191
-
192
-    @Override
193
-    public void drawMarker(Point point, Color color) {
194
-        Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2);
195
-        getLayerManager().getLayers().add(marker);
196
-    }
197
-
198
-    @Override
199
-    public void drawPoint(Point point, int width, Color color) {
200
-        // TODO: Maybe do something?
201
-    }
202
-
203
-    @Override
204
-    public void drawGraph(Graph graph, GraphPalette palette) {
205
-
206
-        File graphFile = getMapsforgeFileFromGraph(graph);
207
-
208
-        // Tile cache
209
-        TileCache tileCache = AwtUtil.createTileCache(tileSize,
210
-                getModel().frameBufferModel.getOverdrawFactor(), 1024,
211
-                new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
212
-
213
-        // Layers
214
-        Layers layers = getLayerManager().getLayers();
215
-
216
-        MapDataStore mapDataStore = new MapFile(graphFile);
217
-        TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
218
-                getModel().mapViewPosition, null);
219
-        layers.add(tileRendererLayer);
220
-        BoundingBox boundingBox = mapDataStore.boundingBox();
221
-
222
-        final Model model = getModel();
223
-        if (model.mapViewPosition.getZoomLevel() == 0
224
-                || !boundingBox.contains(model.mapViewPosition.getCenter())) {
225
-            byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(),
226
-                    boundingBox, model.displayModel.getTileSize());
227
-            model.mapViewPosition
228
-                    .setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
229
-            model.mapViewPosition.setZoomLevelMin(zoomLevel);
230
-        }
231
-    }
232
-
233
-    @Override
234
-    public void drawGraph(Graph graph) {
235
-        drawGraph(graph, null);
236
-    }
237
-
238
-    @Override
239
-    public void drawPath(Path path, Color color, boolean markers) {
240
-        Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR);
241
-        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
242
-        for (Arc arc: path.getArcs()) {
243
-            ArrayList<Point> points = arc.getPoints();
244
-            for (int i = 0; i < points.size(); ++i) {
245
-                line.getLatLongs().add(
246
-                        new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
247
-            }
248
-        }
249
-        getLayerManager().getLayers().add(line);
250
-        extraLayers.add(paintStroke);
251
-        if (markers) {
252
-            drawMarker(path.getOrigin().getPoint());
253
-            drawMarker(path.getDestination().getPoint());
254
-        }
255
-    }
256
-
257
-    @Override
258
-    public void drawPath(Path path, Color color) {
259
-        drawPath(path, color, true);
260
-    }
261
-
262
-    @Override
263
-    public void drawPath(Path path) {
264
-        drawPath(path, DEFAULT_PATH_COLOR, true);
265
-    }
266
-
267
-    @Override
268
-    public void drawPath(Path path, boolean markers) {
269
-        drawPath(path, DEFAULT_PATH_COLOR, markers);
270
-    }
271
-
272
-}
1
+package org.insa.drawing;
2
+
3
+import java.awt.Color;
4
+import java.awt.event.MouseAdapter;
5
+import java.awt.event.MouseWheelEvent;
6
+import java.io.File;
7
+import java.io.FileNotFoundException;
8
+import java.util.ArrayList;
9
+import java.util.HashMap;
10
+import java.util.Map;
11
+import java.util.UUID;
12
+
13
+import javax.swing.JFileChooser;
14
+import javax.swing.filechooser.FileNameExtensionFilter;
15
+
16
+import org.insa.drawing.utils.MarkerUtils;
17
+import org.insa.graph.Arc;
18
+import org.insa.graph.Graph;
19
+import org.insa.graph.Path;
20
+import org.insa.graph.Point;
21
+import org.mapsforge.core.graphics.Bitmap;
22
+import org.mapsforge.core.graphics.GraphicFactory;
23
+import org.mapsforge.core.graphics.Paint;
24
+import org.mapsforge.core.graphics.Style;
25
+import org.mapsforge.core.model.BoundingBox;
26
+import org.mapsforge.core.model.LatLong;
27
+import org.mapsforge.core.model.MapPosition;
28
+import org.mapsforge.core.util.LatLongUtils;
29
+import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
30
+import org.mapsforge.map.awt.util.AwtUtil;
31
+import org.mapsforge.map.awt.view.MapView;
32
+import org.mapsforge.map.datastore.MapDataStore;
33
+import org.mapsforge.map.layer.Layers;
34
+import org.mapsforge.map.layer.cache.TileCache;
35
+import org.mapsforge.map.layer.hills.HillsRenderConfig;
36
+import org.mapsforge.map.layer.overlay.Marker;
37
+import org.mapsforge.map.layer.overlay.Polyline;
38
+import org.mapsforge.map.layer.renderer.TileRendererLayer;
39
+import org.mapsforge.map.model.DisplayModel;
40
+import org.mapsforge.map.model.MapViewPosition;
41
+import org.mapsforge.map.model.Model;
42
+import org.mapsforge.map.reader.MapFile;
43
+import org.mapsforge.map.rendertheme.ExternalRenderTheme;
44
+import org.mapsforge.map.rendertheme.XmlRenderTheme;
45
+
46
+public class MapViewDrawing extends MapView implements Drawing {
47
+
48
+    /**
49
+     * 
50
+     */
51
+    private static final long serialVersionUID = 8606967833704938092L;
52
+
53
+    // Default path color.
54
+    public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244);
55
+
56
+    // Graphic factory.
57
+    private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
58
+
59
+    // Default tile size.
60
+    private static final int DEFAULT_TILE_SIZE = 512;
61
+
62
+    // List of listeners.
63
+    private ArrayList<DrawingClickListener> drawingClickListeners = new ArrayList<>();
64
+
65
+    // Tile size.
66
+    int tileSize;
67
+
68
+    // Extra layers...
69
+    private static class FixedStrokeWidthLayer {
70
+        public Paint paint;
71
+        public int width;
72
+
73
+        public FixedStrokeWidthLayer(Paint paint, int width) {
74
+            this.paint = paint;
75
+            this.width = width;
76
+        }
77
+
78
+    };
79
+
80
+    ArrayList<FixedStrokeWidthLayer> extraLayers = new ArrayList<>();
81
+
82
+    public MapViewDrawing() {
83
+        getMapScaleBar().setVisible(true);
84
+        this.tileSize = DEFAULT_TILE_SIZE;
85
+        DisplayModel model = getModel().displayModel;
86
+        model.setFixedTileSize(tileSize);
87
+
88
+        addMouseWheelListener(new MouseAdapter() {
89
+
90
+            @Override
91
+            public void mouseWheelMoved(MouseWheelEvent e) {
92
+                for (FixedStrokeWidthLayer f: extraLayers) {
93
+                    f.paint.setStrokeWidth(getStrokeWidth(f.width));
94
+                }
95
+            }
96
+        });
97
+
98
+    }
99
+
100
+    /**
101
+     * @param color
102
+     * @return
103
+     */
104
+    protected int convertColor(Color color) {
105
+        return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(), color.getBlue());
106
+    }
107
+
108
+    /**
109
+     * @param width
110
+     * @return
111
+     */
112
+    private int getStrokeWidth(int width) {
113
+        byte zoomLevel = getModel().mapViewPosition.getZoomLevel();
114
+        int mul = 2;
115
+        if (zoomLevel < 8) {
116
+            mul = 1;
117
+        }
118
+        else {
119
+            mul += 2 * (zoomLevel - 8) / 3;
120
+        }
121
+        return width * mul;
122
+    }
123
+
124
+    /**
125
+     * @param width
126
+     * @param color
127
+     * @return
128
+     */
129
+    private Paint createPaintStroke(int width, Color color) {
130
+        Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint();
131
+        paintStroke.setStyle(Style.STROKE);
132
+        if (width != 0) {
133
+            paintStroke.setStrokeWidth(getStrokeWidth(width));
134
+        }
135
+        if (color != null) {
136
+            paintStroke.setColor(convertColor(color));
137
+        }
138
+        return paintStroke;
139
+    }
140
+
141
+    /**
142
+     * 
143
+     * @param color
144
+     * @return
145
+     */
146
+    private File getMapsforgeFileFromGraph(Graph graph) {
147
+        // TODO: Find a way to change this...
148
+        Map<Integer, String> idToNames = new HashMap<Integer, String>();
149
+        idToNames.put(0x100, "insa");
150
+        idToNames.put(0x110, "paris");
151
+        idToNames.put(0x200, "mayotte");
152
+        idToNames.put(0x250, "newzealand");
153
+        idToNames.put(0x300, "reunion");
154
+        idToNames.put(0x400, "midip");
155
+        idToNames.put(0x410, "morbihan");
156
+
157
+        File file = null;
158
+        if (idToNames.containsKey(graph.getMapId())) {
159
+            file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg");
160
+            if (!file.exists()) {
161
+                file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg");
162
+            }
163
+        }
164
+
165
+        if (file == null || !file.exists()) {
166
+            JFileChooser fileChooser = new JFileChooser("Maps/");
167
+            fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg"));
168
+            if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
169
+                file = fileChooser.getSelectedFile();
170
+            }
171
+        }
172
+
173
+        return file;
174
+    }
175
+
176
+    protected LatLong convertPoint(Point point) {
177
+        return new LatLong(point.getLatitude(), point.getLongitude());
178
+    }
179
+
180
+    private TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore,
181
+            MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) {
182
+        TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false,
183
+                true, false, GRAPHIC_FACTORY, hillsRenderConfig) {
184
+            @Override
185
+            public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
186
+                    org.mapsforge.core.model.Point tapXY) {
187
+                System.out.println("Tap on: " + tapLatLong);
188
+                Point pt = new Point(tapLatLong.getLongitude(), tapLatLong.getLatitude());
189
+                for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
190
+                    listener.mouseClicked(pt);
191
+                }
192
+                return true;
193
+            }
194
+        };
195
+        XmlRenderTheme renderTheme = null;
196
+        try {
197
+            renderTheme = new ExternalRenderTheme("resources/assets/custom-theme.xml");
198
+        }
199
+        catch (FileNotFoundException e) {
200
+            e.printStackTrace();
201
+        }
202
+        tileRendererLayer.setXmlRenderTheme(renderTheme);
203
+        return tileRendererLayer;
204
+    }
205
+
206
+    @Override
207
+    public void addDrawingClickListener(DrawingClickListener listener) {
208
+        this.drawingClickListeners.add(listener);
209
+    }
210
+
211
+    @Override
212
+    public void removeDrawingClickListener(DrawingClickListener listener) {
213
+        this.drawingClickListeners.remove(listener);
214
+    }
215
+
216
+    @Override
217
+    public void clear() {
218
+        getLayerManager().getLayers().clear();
219
+        extraLayers.clear();
220
+        repaint();
221
+    }
222
+
223
+    @Override
224
+    public void drawLine(Point from, Point to) {
225
+        drawLine(from, to, 0, null);
226
+    }
227
+
228
+    @Override
229
+    public void drawLine(Point from, Point to, int width) {
230
+        drawLine(from, to, width, null);
231
+    }
232
+
233
+    @Override
234
+    public void drawLine(Point from, Point to, int width, Color color) {
235
+        Paint paintStroke = createPaintStroke(width, color);
236
+        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
237
+        line.getLatLongs().add(convertPoint(from));
238
+        line.getLatLongs().add(convertPoint(to));
239
+        getLayerManager().getLayers().add(line);
240
+    }
241
+
242
+    @Override
243
+    public void drawMarker(Point point) {
244
+        drawMarker(point, Color.GREEN);
245
+    }
246
+
247
+    @Override
248
+    public void drawMarker(Point point, Color color) {
249
+        Bitmap bitmap = MarkerUtils.getMarkerForColor(color);
250
+        Marker marker = new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2);
251
+        getLayerManager().getLayers().add(marker);
252
+    }
253
+
254
+    @Override
255
+    public void drawPoint(Point point, int width, Color color) {
256
+        // TODO:
257
+    }
258
+
259
+    @Override
260
+    public void drawGraph(Graph graph, GraphPalette palette) {
261
+
262
+        File graphFile = getMapsforgeFileFromGraph(graph);
263
+
264
+        // Tile cache
265
+        TileCache tileCache = AwtUtil.createTileCache(tileSize, getModel().frameBufferModel.getOverdrawFactor(), 1024,
266
+                new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()));
267
+
268
+        // Layers
269
+        Layers layers = getLayerManager().getLayers();
270
+
271
+        MapDataStore mapDataStore = new MapFile(graphFile);
272
+        TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore,
273
+                getModel().mapViewPosition, null);
274
+        layers.add(tileRendererLayer);
275
+        BoundingBox boundingBox = mapDataStore.boundingBox();
276
+
277
+        final Model model = getModel();
278
+        if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) {
279
+            byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox,
280
+                    model.displayModel.getTileSize());
281
+            model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
282
+            model.mapViewPosition.setZoomLevelMin(zoomLevel);
283
+        }
284
+    }
285
+
286
+    @Override
287
+    public void drawGraph(Graph graph) {
288
+        drawGraph(graph, null);
289
+    }
290
+
291
+    @Override
292
+    public void drawPath(Path path, Color color, boolean markers) {
293
+        Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR);
294
+        Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE);
295
+        for (Arc arc: path.getArcs()) {
296
+            ArrayList<Point> points = arc.getPoints();
297
+            for (int i = 0; i < points.size(); ++i) {
298
+                line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude()));
299
+            }
300
+        }
301
+        getLayerManager().getLayers().add(line);
302
+        extraLayers.add(new FixedStrokeWidthLayer(paintStroke, 1));
303
+        if (markers) {
304
+            drawMarker(path.getOrigin().getPoint(), DEFAULT_PATH_COLOR);
305
+            drawMarker(path.getDestination().getPoint(), DEFAULT_PATH_COLOR);
306
+        }
307
+    }
308
+
309
+    @Override
310
+    public void drawPath(Path path, Color color) {
311
+        drawPath(path, color, true);
312
+    }
313
+
314
+    @Override
315
+    public void drawPath(Path path) {
316
+        drawPath(path, DEFAULT_PATH_COLOR, true);
317
+    }
318
+
319
+    @Override
320
+    public void drawPath(Path path, boolean markers) {
321
+        drawPath(path, DEFAULT_PATH_COLOR, markers);
322
+    }
323
+
324
+}

+ 118
- 0
src/main/org/insa/drawing/utils/MarkerUtils.java View File

@@ -0,0 +1,118 @@
1
+package org.insa.drawing.utils;
2
+
3
+import java.awt.Color;
4
+import java.awt.image.BufferedImage;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+
8
+import org.mapsforge.core.graphics.Bitmap;
9
+import org.mapsforge.map.awt.graphics.AwtBitmap;
10
+
11
+public class MarkerUtils {
12
+
13
+    // Marker
14
+    private static Map<Color, Bitmap> MARKER_BITMAPS = new HashMap<Color, Bitmap>();
15
+
16
+    /**
17
+     * Return a color mapping with the given color as the main color.
18
+     * 
19
+     * @param color
20
+     * @return
21
+     */
22
+    protected static Color[] getColorMapping(Color color) {
23
+        return new Color[] { new Color(0, 0, 0, 0), color, Color.BLACK };
24
+    }
25
+
26
+    /**
27
+     * Create a Bitmap representing a marker of the given color.
28
+     * 
29
+     * @param color
30
+     * @return
31
+     */
32
+    public static Bitmap getMarkerForColor(Color color) {
33
+        if (MARKER_BITMAPS.containsKey(color)) {
34
+            return MARKER_BITMAPS.get(color);
35
+        }
36
+
37
+        // create image
38
+        BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, BufferedImage.TYPE_INT_ARGB);
39
+
40
+        Color[] map = getColorMapping(color);
41
+        for (int i = 0; i < image.getHeight(); ++i) {
42
+            for (int j = 0; j < image.getWidth(); ++j) {
43
+                image.setRGB(j, i, map[MARKER_MASK[i][j]].getRGB());
44
+            }
45
+        }
46
+
47
+        // Create Bitmap, add it to map and return it.
48
+        Bitmap bitmap = new AwtBitmap(image);
49
+        MARKER_BITMAPS.put(color, bitmap);
50
+        return bitmap;
51
+    }
52
+
53
+    // Mask
54
+    private static byte[][] MARKER_MASK = new byte[][] {
55
+        // @formatter:off
56
+		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
57
+		{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
58
+		{0,0,0,0,0,0,0,0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