Index: /applications/editors/josm/plugins/slippy_map_chooser/build.xml
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/build.xml	(revision 9536)
+++ /applications/editors/josm/plugins/slippy_map_chooser/build.xml	(revision 9537)
@@ -47,5 +47,5 @@
   <target name="compile" depends="init">
     <echo message="creating ${plugin.jar}"/>
-    <javac srcdir="src" classpath="${josm}" destdir="build" />
+    <javac srcdir="src;src_jmv" classpath="${josm}" destdir="build" debug="true"/>
   </target>
   
Index: plications/editors/josm/plugins/slippy_map_chooser/src/Logger.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/Logger.java	(revision 9536)
+++ 	(revision )
@@ -1,66 +1,0 @@
-// License: GPL. Copyright 2007 by Tim Haussmann
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-
-/**
- * 
- * @author Tim Haussmann
- */
-
-public class Logger {
-
-	private static Logger iSelf;
-	
-	private File iLogFile;
-	private FileOutputStream iFout;
-	
-	private Logger(String aLogFilePath){
-		if(aLogFilePath != null && !aLogFilePath.equals(""))
-			iLogFile = new File(aLogFilePath);
-		else
-			iLogFile = new File("c:/LogFile.txt");
-		
-		try {
-			iFout = new FileOutputStream(iLogFile, false);
-		} catch (FileNotFoundException e) {
-			e.printStackTrace();
-		}
-	}
-	
-	public static void log(String aMessage){
-		if(iSelf == null)
-			iSelf = new Logger(null);
-		iSelf.logMessage(aMessage);
-	}
-	
-	public static void close(){
-		iSelf.saveLog();
-		iSelf = null;
-	}
-	
-	private void saveLog() {
-		try {
-			iFout.flush();
-			iFout.close();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
-	
-	public static void setLogFile(String aLogFilePath){
-		iSelf = new Logger(aLogFilePath);
-	}
-
-	private void logMessage(String aMessage){
-		try {
-			iFout.write((aMessage + "\n").getBytes());
-			iFout.flush();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}		
-	}
-}
Index: /applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java	(revision 9536)
+++ /applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java	(revision 9537)
@@ -2,77 +2,34 @@
 // License: GPL. Copyright 2007 by Tim Haussmann
 
-import java.awt.Cursor;
 import java.awt.Point;
-import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
-import java.awt.event.MouseWheelEvent;
-import java.awt.event.MouseWheelListener;
 
-import javax.swing.AbstractAction;
 import javax.swing.JComponent;
 import javax.swing.JPanel;
 import javax.swing.KeyStroke;
-import static org.openstreetmap.josm.tools.I18n.tr;
 
 /**
- * This class controls the user input by listening to mouse and key events. Currently implemented is:
- * - zooming in and out with scrollwheel
- * - zooming in and centering by double clicking
- * - selecting an area by clicking and dragging the mouse
+ * This class controls the user input by listening to mouse and key events.
+ * Currently implemented is: - zooming in and out with scrollwheel - zooming in
+ * and centering by double clicking - selecting an area by clicking and dragging
+ * the mouse
  * 
  * @author Tim Haussmann
  */
-public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseWheelListener {
+public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener {
 
-	
-	//positions while moving the map
-	private int iLastX = 0;
-	private int iLastY = 0;
-	
-	//start and end point of selection rectangle
+	// start and end point of selection rectangle
 	private Point iStartSelectionPoint;
 	private Point iEndSelectionPoint;
 
-	//the SlippyMapChooserComponent
+	// the SlippyMapChooserComponent
 	private final SlippyMapChooser iSlippyMapChooser;
-	
-	//The old cursor when we changed it to movement cursor.
-	private Cursor iOldCursor;
 
-	private boolean isMovementInPlace = false;
-	
 	private SizeButton iSizeButton = null;
 
-	
-	private final class ZoomerAction extends AbstractAction {
-		private final String action;
-		public ZoomerAction(String action) {
-			this.action = action;
-        }
-	    public void actionPerformed(ActionEvent e) {
-	    	if (action.equals(".") || action.equals(",")) {
-	    		Point mouse = iSlippyMapChooser.getMousePosition();
-	    		if (mouse == null)
-	    			mouse = new Point((int)iSlippyMapChooser.getBounds().getCenterX(), (int)iSlippyMapChooser.getBounds().getCenterY());
-	    		MouseWheelEvent we = new MouseWheelEvent(iSlippyMapChooser, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, action.equals(",") ? -1 : 1);
-	    		mouseWheelMoved(we);
-	    	} else {
-	    		if (action.equals(tr("left")))
-	    			iSlippyMapChooser.moveMap(-30, 0);
-	    		else if (action.equals("right"))
-	    			iSlippyMapChooser.moveMap(30, 0);
-	    		else if (action.equals("up"))
-	    			iSlippyMapChooser.moveMap(0, -30);
-	    		else if (action.equals("down"))
-	    			iSlippyMapChooser.moveMap(0, 30);
-	    	}
-	    }
-    }
-	
-	
-	
 	/**
 	 * Create a new OsmMapControl
@@ -82,142 +39,63 @@
 		iSlippyMapChooser.addMouseListener(this);
 		iSlippyMapChooser.addMouseMotionListener(this);
-		iSlippyMapChooser.addMouseWheelListener(this);
-		
-		String[] n = {",",".","up","right","down","left"};
-		int[] k = {KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT};
+
+		String[] n = { ",", ".", "up", "right", "down", "left" };
+		int[] k =
+				{ KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT,
+						KeyEvent.VK_DOWN, KeyEvent.VK_LEFT };
 
 		if (contentPane != null) {
 			for (int i = 0; i < n.length; ++i) {
-				contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), "MapMover.Zoomer."+n[i]);
-				contentPane.getActionMap().put("MapMover.Zoomer."+n[i], new ZoomerAction(n[i]));
+				contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
+						KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK),
+						"MapMover.Zoomer." + n[i]);
 			}
 		}
-		
 		iSizeButton = sizeButton;
 	}
 
 	/**
-	 * If the right mouse button is pressed and moved while holding, move the map.
-	 * If the left mouse button is pressed start the selection rectangle and stop when left mouse button is released
+	 * Start drawing the selection rectangle if it was the 1st button (left
+	 * button)
 	 */
-	public void mouseDragged(MouseEvent e) {			
-		int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
-		//Moving the map
-		if ((e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK) {
-			
-			startMovement(e);
-			
-			//calc the moved distance since the last drag event
-			int distX = e.getX()-iLastX;
-			int distY = e.getY()-iLastY;
-			
-			//change the offset for the origin of the map
-			iSlippyMapChooser.moveMap(-distX, -distY);
-			
-			//update the last position of the mouse for the next dragging event
-			iLastX = e.getX();
-			iLastY = e.getY();
-		}
-		//selecting
-		else if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){
-			iEndSelectionPoint = e.getPoint();
-			iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
-		}		
-		//end moving of the map 
-		else
-			endMovement();
-	}
-
-	/**
-	 * Start moving the map, if it was the 3rd button (right button).
-	 * Start drawing the selection rectangle if it was the 1st button (left button)
-	 */
-	@Override public void mousePressed(MouseEvent e) {
-		int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
-		if (e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0)
-			startMovement(e);
-		else if (e.getButton() == MouseEvent.BUTTON1){
-			if(!iSizeButton.hit(e.getPoint())){
+	@Override
+	public void mousePressed(MouseEvent e) {
+		if (e.getButton() == MouseEvent.BUTTON1) {
+			if (!iSizeButton.hit(e.getPoint())) {
 				iStartSelectionPoint = e.getPoint();
-				iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
+				iEndSelectionPoint = e.getPoint();
 			}
 		}
 	}
-	
-	
-	/**
-	 * When dragging the map change the cursor back to it's pre-move cursor.
-	 * If a double-click occurs center and zoom the map on the clicked location.
-	 */
-	@Override public void mouseReleased(MouseEvent e) {		
-		if (e.getButton() == MouseEvent.BUTTON3)
-			endMovement();
-		else if (e.getButton() == MouseEvent.BUTTON1){
-			if(iSizeButton.hit(e.getPoint())){
-				iSizeButton.toggle();
-				iSlippyMapChooser.resizeSlippyMap();
-			}else{
-				if(e.getClickCount() == 1){
-					iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
-					
-					//reset the selections start and end
-					iEndSelectionPoint 	 = null;
-					iStartSelectionPoint = null;
-				}
-				else if(e.getClickCount() == 2){
-					iSlippyMapChooser.zoomIn(e.getPoint());
-					iSlippyMapChooser.centerOnScreenPoint(e.getPoint());
-				}		
-			}
-		}			
-	}
 
-	/**
-	 * Start movement by setting a new cursor and remember the current mouse
-	 * position.
-	 */
-	private void startMovement(MouseEvent e) {
-		if (isMovementInPlace)
-			return;
-		isMovementInPlace = true;
-		iLastX = e.getX();
-		iLastY = e.getY();
-		iOldCursor = iSlippyMapChooser.getCursor();
-		iSlippyMapChooser.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
-	}
-
-	/**
-	 * End the movement. Setting back the cursor and clear the movement variables
-	 */
-	private void endMovement() {
-		if (!isMovementInPlace)
-			return;
-		isMovementInPlace = false;
-		if (iOldCursor != null)
-			iSlippyMapChooser.setCursor(iOldCursor);
-		else
-			iSlippyMapChooser.setCursor(Cursor.getDefaultCursor());
-		iOldCursor = null;
-	}
-
-	/**
-	 * Zoom the map in and out.
-	 * @param e The wheel event.
-	 */
-	public void mouseWheelMoved(MouseWheelEvent e) {
-		int rot = e.getWheelRotation();
-		//scroll wheel rotated away from user
-		if(rot < 0){
-			iSlippyMapChooser.zoomIn(e.getPoint());
-		}
-		//scroll wheel rotated towards the user
-		else if(rot > 0){
-			iSlippyMapChooser.zoomOut(e.getPoint());
+	@Override
+	public void mouseDragged(MouseEvent e) {
+		if (iStartSelectionPoint != null) {
+			iEndSelectionPoint = e.getPoint();
+			iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
 		}
 	}
 
 	/**
-	 * Does nothing. Only to satisfy MouseMotionListener
+	 * When dragging the map change the cursor back to it's pre-move cursor. If
+	 * a double-click occurs center and zoom the map on the clicked location.
 	 */
-	public void mouseMoved(MouseEvent e) {}
+	@Override
+	public void mouseReleased(MouseEvent e) {
+		if (e.getButton() == MouseEvent.BUTTON1) {
+			if (iSizeButton.hit(e.getPoint())) {
+				iSizeButton.toggle();
+				iSlippyMapChooser.resizeSlippyMap();
+			} else {
+				if (e.getClickCount() == 1) {
+					iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
+
+					// reset the selections start and end
+					iEndSelectionPoint = null;
+					iStartSelectionPoint = null;
+				}
+			}
+		}
+	}
+
 }
Index: plications/editors/josm/plugins/slippy_map_chooser/src/OsmMercator.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/OsmMercator.java	(revision 9536)
+++ 	(revision )
@@ -1,92 +1,0 @@
-// License: GPL. Copyright 2007 by Tim Haussmann
-
-/**
- * This class implements the Mercator Projection as it is used by Openstreetmap (and google). 
- * It provides methods to translate coordinates from 'map space' into latitude and longitude (on
- * the WGS84 ellipsoid) and vice versa. Map space is measured in pixels. The origin of the map space
- * is the top left corner. The map space origin (0,0)  has latitude ~85 and longitude -180
- * 
- * @author Tim Haussmann
- *
- */
-
-public class OsmMercator{
-
-	private static int TILE_SIZE = 256;	
-	public static final double MAX_LAT = 85.05112877980659;
-	public static final double MIN_LAT = -85.05112877980659;
-	
-	
-	public static double radius(int aZoomlevel){
-		return (TILE_SIZE * Math.pow(2, aZoomlevel))/ (2 * Math.PI);		
-	}
-	
-	/**
-	 * Returns the absolut number of pixels in y or x, defined as:
-	 * 2^Zoomlevel * TILE_WIDTH
-	 * where TILE_WIDTH is the width of a tile in pixels 
-	 * @param aZoomlevel
-	 * @return
-	 */
-	public static int getMaxPixels(int aZoomlevel){
-		return (int) (TILE_SIZE * Math.pow(2, aZoomlevel));
-	}
-		
-	public static int falseEasting(int aZoomlevel){
-		return (int)getMaxPixels(aZoomlevel)/2;
-	}
-	public static int falseNorthing(int aZoomlevel){
-		return (int) ( -1*getMaxPixels(aZoomlevel)/2);
-	}
-	
-	/**
-	 * Transform longitude to pixelspace
-	 * @param aLongitude [-180..180]
-	 * @return [0..2^Zoomlevel*TILE_SIZE[
-	 */
-	public static int LonToX(double aLongitude, int aZoomlevel){		
-		double longitude = Math.toRadians(aLongitude);
-		return (int) ((radius(aZoomlevel) * longitude) + falseEasting(aZoomlevel));
-	}
-	
-	/**
-	 * Transforms latitude to pixelspace	
-	 * @param aLat [-90...90]
-	 * @return [0..2^Zoomlevel*TILE_SIZE[
-	 */
-	public static int LatToY(double aLat, int aZoomlevel){		
-		if(aLat < MIN_LAT)
-			aLat = MIN_LAT;
-		else if(aLat > MAX_LAT)
-			aLat = MAX_LAT;
-		double latitude = Math.toRadians(aLat);
-		return (int) (-1*(radius(aZoomlevel)/2.0 * Math.log((1.0 + Math.sin(latitude))/ (1.0 - Math.sin(latitude)))) - falseNorthing(aZoomlevel));
-	}
-	
-	/**
-	 * Transforms pixel coordinate X to longitude
-	 * @param aX [0..2^Zoomlevel*TILE_WIDTH[
-	 * @return ]-180..180[
-	 */
-	public static double XToLon(int aX, int aZoomlevel){
-		aX -= falseEasting(aZoomlevel);
-		double longRadians = aX/radius(aZoomlevel);
-		double longDegrees = Math.toDegrees(longRadians);
-		double rotations = Math.floor((longDegrees + 180)/360);
-		double longitude = longDegrees - (rotations * 360);
-		return longitude;
-	}
-	
-	/**
-	 * Transforms pixel coordinate Y to latitude
-	 * @param aY [0..2^Zoomlevel*TILE_WIDTH[
-	 * @return [MIN_LAT..MAX_LAT] is about [-85..85]
-	 */
-	public static double YToLat(int aY, int aZoomlevel){
-		aY += falseNorthing(aZoomlevel);
-		double latitude =  (Math.PI/2) - (2 * Math.atan(Math.exp(-1.0 * aY /radius(aZoomlevel))));		
-		return -1*Math.toDegrees(latitude) ;
-	}
-
-	
-}
Index: plications/editors/josm/plugins/slippy_map_chooser/src/OsmTile.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/OsmTile.java	(revision 9536)
+++ 	(revision )
@@ -1,127 +1,0 @@
-// License: GPL. Copyright 2007 by Tim Haussmann
-
-import java.awt.geom.AffineTransform;
-import java.awt.image.BufferedImage;
-import java.awt.Color;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-import java.awt.RenderingHints;
-import java.awt.Shape;
-
-/**
- * @author Tim Haussmann 
- */
-
-
-public class OsmTile {
-	
-	public static final int WIDTH = 256;
-	public static final int HEIGHT = 256;
-	
-	private int iX = 0;
-	private int iY = 0;
-	
-	public static final int TileBackgroundColor = 0xe9d3b1;  
-	
-	private BufferedImage iMapImage;
-	
-	private int iZoomLevel = -1;
-	private int iIndexY = -1;
-	private int iIndexX = -1;
-	
-	//image does not exist
-	private boolean isInvalid = false;
-	
-	//for a faster equals implementation
-	private int iHash;
-	
-	public OsmTile( int aZoomLevel, int aIndexX, int aIndexY){
-			
-		iZoomLevel = aZoomLevel;
-		iIndexX = aIndexX;
-		iIndexY = aIndexY;
-		
-		iX = WIDTH * iIndexX;
-		iY = HEIGHT * iIndexY;
-		
-		iHash = toString().hashCode();
-	}
-	
-	public int getZoomlevel(){
-		return iZoomLevel;
-	}
-	
-	public void paint(Graphics g,TileDB db){
-		
-		if(iMapImage != null && ! isInvalid){
-			g.drawImage( iMapImage, iX, iY, null );
-		}
-		else if(isInvalid){
-			//draw nothing
-		}
-		else{
-			// first try to interpolate tile from parent			
-			OsmTile parent = getParent(db);
-			if (parent!=null){
-				Graphics2D g2d = (Graphics2D) g;
-				AffineTransform oldTr = g2d.getTransform();
-				Shape oldClip = g2d.getClip();
-				g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
-				
-				// since the parent will paint 4 tiles in the 
-				// current zoom level we have to clip the drawing
-				// region to the current tile
-				g2d.clipRect(iX, iY, WIDTH, HEIGHT);
-				g2d.scale(2, 2);				
-				parent.paint(g,db);
-				
-				g2d.setTransform(oldTr);
-				g2d.setClip(oldClip);
-			}
-			else{
-				// otherwise draw placeholder
-				g.setColor(Color.RED);
-				g.drawLine(iX, iY, iX+WIDTH-1, iY+HEIGHT-1);
-				g.drawLine(iX, iY+HEIGHT-1, iX+WIDTH-1, iY);
-				g.drawRect(iX, iY, WIDTH-1, HEIGHT-1);
-			}
-		}
-	} 
-	
-	public String toString() {
-		return String.valueOf(iZoomLevel) +"/" +
-		String.valueOf(iIndexX) + "/" + 
-		String.valueOf(iIndexY);
-	}
-	public static String key(int aZoomLevel, int aIndexX, int aIndexY){
-		return String.valueOf(aZoomLevel) +"/" +
-		String.valueOf(aIndexX) + "/" + 
-		String.valueOf(aIndexY);
-	}
-
-	/**
-	 * Callback for the TileDB to set the Image of this tile after loading.
-	 * @param aImage
-	 */
-	public void setImage(BufferedImage aImage) {
-		iMapImage = aImage;
-		if(iMapImage == null){
-			isInvalid = true;
-		}
-	}
-
-	/**
-	 * @return the path of this map tile on the remote server (i.e. '/1/2/12.png')
-	 */
-	public String getRemotePath() {
-		return "/"+toString()+".png";
-	}
-	
-	public OsmTile getParent(TileDB db){
-		return iZoomLevel == 0 ? null : db.get(parentKey());
-	}
-
-	public String parentKey() {
-		return key(iZoomLevel - 1,iIndexX/2,iIndexY/2);
-	}
-}
Index: /applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java	(revision 9536)
+++ /applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java	(revision 9537)
@@ -1,4 +1,6 @@
 // This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM.
 // License: GPL. Copyright 2007 by Tim Haussmann
+
+import static org.openstreetmap.josm.tools.I18n.tr;
 
 import java.awt.BorderLayout;
@@ -9,69 +11,47 @@
 import java.awt.Point;
 import java.awt.Toolkit;
-import java.util.Enumeration;
-
-import javax.swing.JComponent;
+import java.awt.geom.Point2D;
+import java.util.Vector;
+
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
-import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.gui.jmapviewer.JMapViewer;
+import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
+import org.openstreetmap.gui.jmapviewer.OsmMercator;
+import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
 import org.openstreetmap.josm.gui.download.DownloadDialog;
 import org.openstreetmap.josm.gui.download.DownloadSelection;
-import static org.openstreetmap.josm.tools.I18n.tr;
 
 /**
- * JComponent that displays the slippy map tiles 
+ * JComponent that displays the slippy map tiles
+ * 
  * @author Tim Haussmann
- *
+ * 
  */
-public class SlippyMapChooser extends JComponent implements DownloadSelection{
-	
-	private static final int MAX_ZOOMLEVEL = 20;
-	private static final int MIN_ZOOMLEVEL = 1;
-
-	private TileDB iTileDB;
-	
+public class SlippyMapChooser extends JMapViewer implements DownloadSelection {
+
 	private DownloadDialog iGui;
-	
-	//the upper left and lower right corners of the selection rectangle
+
+	// upper left and lower right corners of the selection rectangle (x/y on
+	// ZOOM_MAX)
 	Point iSelectionRectStart;
 	Point iSelectionRectEnd;
-	
-	//Offsets for x and y (i.e. to center the first tile)
-	int iOffsetX = 0;
-	int iOffsetY = 0;
-	
-	//the zoom level of the currently shown tiles
-	static int iZoomlevel = 3;
-	
-	private boolean iFirstPaint = true;
-	private LatLon  iFirstPaintCenter = new LatLon(51,7);
-	
+
 	private SizeButton iSizeButton = new SizeButton();
-	
-	//standard dimension 
+
+	// standard dimension
 	private Dimension iDownloadDialogDimension;
-	//screen size
+	// screen size
 	private Dimension iScreenSize;
-	
-	private LatLon iScreenCenterBeforeResize;
-	private LatLon iSelectionStartBeforeResize;
-	private LatLon iSelectionEndBeforeResize;
-	private boolean isJustResized = false;
-	
-	private int iVisibleTilesX = 2;
-	private int iVisibleTilesY = 3;
-	
+
 	/**
 	 * Create the chooser component.
 	 */
-	public SlippyMapChooser() {	
-				
-		//create the tile db
-		iTileDB = new TileDB(this);
-		
-		
-	
-		setMinimumSize(new Dimension(350, 350/2));
+	public SlippyMapChooser() {
+		super();
+		setZoomContolsVisible(false);
+		setMapMarkerVisible(false);
+		setMinimumSize(new Dimension(350, 350 / 2));
 	}
 
@@ -81,459 +61,153 @@
 		temp.setLayout(new BorderLayout());
 		temp.add(this, BorderLayout.CENTER);
-		temp.add(new JLabel((tr("Zoom: Mousewheel or double click.   Move map: Hold right mousebutton and move mouse.   Select: Click."))), BorderLayout.SOUTH);
+		temp.add(new JLabel((tr("Zoom: Mousewheel or double click.   "
+				+ "Move map: Hold right mousebutton and move mouse.   Select: Click."))),
+				BorderLayout.SOUTH);
 		iGui.tabpane.add(temp, tr("Slippy map"));
-		
-		new OsmMapControl(this,temp, iSizeButton);
+
+		new OsmMapControl(this, temp, iSizeButton);
+		boundingBoxChanged(gui);
+	}
+
+	protected Point getTopLeftCoordinates() {
+		return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
+	}
+
+	/**
+	 * Draw the map.
+	 */
+	@Override
+	public void paint(Graphics g) {
+		try {
+			super.paint(g);
+
+			// draw selection rectangle
+			if (iSelectionRectStart != null && iSelectionRectEnd != null) {
+
+				int zoomDiff = MAX_ZOOM - zoom;
+				Point tlc = getTopLeftCoordinates();
+				int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x;
+				int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y;
+				int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x;
+				int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y;
+
+				int w = x_max - x_min;
+				int h = y_max - y_min;
+				g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f));
+				g.fillRect(x_min, y_min, w, h);
+
+				g.setColor(Color.BLACK);
+				g.drawRect(x_min, y_min, w, h);
+
+			}
+
+			iSizeButton.paint(g);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void boundingBoxChanged(DownloadDialog gui) {
+
+		// test if a bounding box has been set set
+		if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0)
+			return;
+
+		int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM);
+		int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM);
+		int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM);
+		int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM);
+
+		iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2));
+		iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2));
+
+		// calc the screen coordinates for the new selection rectangle
+		MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon);
+		MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon);
+
+		Vector<MapMarker> marker = new Vector<MapMarker>(2);
+		marker.add(xmin_ymin);
+		marker.add(xmax_ymax);
+		setMapMarkerList(marker);
+		setDisplayToFitMapMarkers();
+		zoomOut();
+	}
+
+	/**
+	 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the
+	 * selection rectangle.
+	 * 
+	 * @param aStart
+	 * @param aEnd
+	 */
+	public void setSelection(Point aStart, Point aEnd) {
+		if (aStart == null || aEnd == null)
+			return;
+		Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y));
+		Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y));
+
+		Point tlc = getTopLeftCoordinates();
+		int zoomDiff = MAX_ZOOM - zoom;
+		Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y);
+		Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y);
+
+		pEnd.x <<= zoomDiff;
+		pEnd.y <<= zoomDiff;
+		pStart.x <<= zoomDiff;
+		pStart.y <<= zoomDiff;
+
+		iSelectionRectStart = pStart;
+		iSelectionRectEnd = pEnd;
+
+		Point2D.Double l1 = getPosition(p_max);
+		Point2D.Double l2 = getPosition(p_min);
+		iGui.minlat = Math.min(l2.x, l1.x);
+		iGui.minlon = Math.min(l1.y, l2.y);
+		iGui.maxlat = Math.max(l2.x, l1.x);
+		iGui.maxlon = Math.max(l1.y, l2.y);
+
+		iGui.boundingBoxChanged(this);
 		repaint();
-    }
-	
-	/**
-	 * Performs resizing of the DownloadDialog in order to enlarge or shrink the map. 
-	 */
-	public void resizeSlippyMap(){
-		if(iScreenSize == null){
-			Component c = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent();
-			//remember the initial set screen dimensions
+	}
+
+	/**
+	 * Performs resizing of the DownloadDialog in order to enlarge or shrink the
+	 * map.
+	 */
+	public void resizeSlippyMap() {
+		if (iScreenSize == null) {
+			Component c =
+					iGui.getParent().getParent().getParent().getParent().getParent().getParent()
+							.getParent().getParent().getParent();
+			// remember the initial set screen dimensions
 			iDownloadDialogDimension = c.getSize();
-			//retrive the size of the display
+			// retrive the size of the display
 			iScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
 		}
-		
-		//remember the screen center (we want to have the same center after resizing)
-		iScreenCenterBeforeResize = getLatLonOfScreenPoint(new Point(getWidth()/2, getHeight()/2));
-		
-		//remember lat/lon of the selection rectangle 
-		if(iSelectionRectEnd != null && iSelectionRectStart != null){
-			iSelectionStartBeforeResize = getLatLonOfScreenPoint(iSelectionRectStart);
-			iSelectionEndBeforeResize   = getLatLonOfScreenPoint(iSelectionRectEnd);
-		}
-		
-		//resize
-		Component co = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent();
+
+		// resize
+		Component co =
+				iGui.getParent().getParent().getParent().getParent().getParent().getParent()
+						.getParent().getParent().getParent();
 		Dimension currentDimension = co.getSize();
-		
-		//enlarge
-		if(currentDimension.equals(iDownloadDialogDimension)){
-			//make the each dimension 90% of the absolute display size and center the DownloadDialog
-			int w = iScreenSize.width*90/100;
-			int h = iScreenSize.height*90/100;
-			co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h);
-		}
-		//shrink
-		else{
-			//set the size back to the initial dimensions and center the DownloadDialog
+
+		// enlarge
+		if (currentDimension.equals(iDownloadDialogDimension)) {
+			// make the each dimension 90% of the absolute display size and
+			// center the DownloadDialog
+			int w = iScreenSize.width * 90 / 100;
+			int h = iScreenSize.height * 90 / 100;
+			co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
+		}
+		// shrink
+		else {
+			// set the size back to the initial dimensions and center the
+			// DownloadDialog
 			int w = iDownloadDialogDimension.width;
 			int h = iDownloadDialogDimension.height;
-			co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h);
-		}
-		
-		//the new dimension are 'available' after (or while) the next repaint
-		isJustResized = true;
-		
+			co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h);
+		}
 		repaint();
 	}
 
-	/**
-	 * Draw the map.
-	 */
-	@Override public void paint(Graphics g) {
-		
-		if(iFirstPaint){
-			//calculate numbers of visible tiles 			
-			calcVisibleTiles();	
-			
-			//save selection
-			LatLon selStart = null;
-			LatLon selEnd   = null;
-			if(iSelectionRectEnd != null && iSelectionRectStart != null){
-				selStart = getLatLonOfScreenPoint(iSelectionRectStart);
-				selEnd   = getLatLonOfScreenPoint(iSelectionRectEnd);
-			}			
-			centerOnLatLon(iFirstPaintCenter);	
-			//restore selection 
-			if(selStart != null && selEnd != null){
-				iSelectionRectStart = getScreenPointForLatLon(selStart);
-				iSelectionRectEnd   = getScreenPointForLatLon(selEnd);
-			}
-			
-			loadVisibleTiles();
-			iFirstPaint = false;
-			repaint();
-		}
-		
-		if(isJustResized){
-			centerOnLatLon(iScreenCenterBeforeResize);
-			//restore selection 
-			if(iSelectionEndBeforeResize != null && iSelectionStartBeforeResize != null){
-				iSelectionRectStart = getScreenPointForLatLon(iSelectionStartBeforeResize);
-				iSelectionRectEnd   = getScreenPointForLatLon(iSelectionEndBeforeResize);
-			}
-			
-			//calculate numbers of visible tiles 			
-			calcVisibleTiles();			
-			
-			loadVisibleTiles();
-			isJustResized = false;
-		}
-			
-		//translate origin to draw map tiles in 'map space'
-		g.translate(iOffsetX, iOffsetY);
-		
-		//draw tiles of the current zoomlevel
-		for(int y=0; y<iVisibleTilesY; y++){
-			for(int x=0; x<iVisibleTilesX; x++){					
-				OsmTile t = iTileDB.get(OsmTile.key(iZoomlevel,  (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH, ((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT)));
-				if(t != null){
-					t.paint(g,iTileDB);						
-				}
-			} 
-		}		
-		
-		//translate origin back
-		g.translate(-iOffsetX, -iOffsetY);
-		
-		//draw selection rectangle
-		if(iSelectionRectStart != null && iSelectionRectEnd != null){
-			
-			g.setColor(Color.black);
-			g.drawRect(iSelectionRectStart.x, iSelectionRectStart.y, iSelectionRectEnd.x -iSelectionRectStart.x, iSelectionRectEnd.y -iSelectionRectStart.y);
-		
-			g.setColor(new Color(0.9f,0.7f,0.7f,0.6f));
-			g.fillRect(iSelectionRectStart.x+1, iSelectionRectStart.y+1, iSelectionRectEnd.x -iSelectionRectStart.x-1, iSelectionRectEnd.y -iSelectionRectStart.y-1);
-		}
-		
-		iSizeButton.paint(g);
-		
-		
-		if(SlippyMapChooserPlugin.DEBUG_MODE){
-			g.setColor(Color.black);
-			g.drawString("Free Memory: " + Runtime.getRuntime().freeMemory()/1024 + "/" + Runtime.getRuntime().totalMemory()/1024 + "kB" , 5, 50);
-			g.drawString("Tiles in DB: " + iTileDB.getCachedTilesSize(), 5, 65);
-			g.drawString("Loading Queue Size: " + iTileDB.getLoadingQueueSize(), 5, 80);
-			
-		}
-	}
-
-	
-	public void boundingBoxChanged(DownloadDialog gui) {
-		
-		//calc the screen coordinates for the new selection rectangle
-		int x1 = OsmMercator.LonToX(gui.minlon, iZoomlevel);
-		int x2 = OsmMercator.LonToX(gui.maxlon, iZoomlevel);
-		int y1 = OsmMercator.LatToY(gui.minlat, iZoomlevel);
-		int y2 = OsmMercator.LatToY(gui.maxlat, iZoomlevel);
-		
-		iSelectionRectStart = new Point(Math.min(x1, x2)+iOffsetX, Math.min(y1, y2)+iOffsetY);
-		iSelectionRectEnd   = new Point(Math.max(x1, x2)+iOffsetX, Math.max(y1, y2)+iOffsetY);
-		
-		//calc zoom level 	
-		double dLat = Math.abs(gui.maxlat - gui.minlat);
-		double dLon = Math.abs(gui.maxlon - gui.minlon);		
-		int zoomLat = (int) (Math.log(90 / dLat)/Math.log(2));
-		int zoomLon = (int) (Math.log(90 / dLon)/Math.log(2));		
-		iZoomlevel = Math.max(zoomLat, zoomLon);
-		
-		//center on the rectangle
-		if(gui.minlat != 0 && gui.maxlat != 0 && gui.minlon != 0 && gui.maxlat != 0){
-			iFirstPaintCenter = new LatLon((gui.minlat + gui.maxlat)/2, (gui.minlon + gui.maxlon)/2);
-			iFirstPaint = true;
-		}
-		repaint();
-	}
-	
-	/**
-	 * Loads all tiles that are visible
-	 */
-	void loadVisibleTiles(){	
-		for(int y=iVisibleTilesY-1; y>=0; y--){
-			for(int x=0; x<iVisibleTilesX; x++){					
-				if(y > 0 && y < iVisibleTilesX-2){
-					iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_HIGH );
-				}else{
-					iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_LOW );
-				}
-			} 
-		}		
-	}
-
-	/**
-	 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the selection rectangle.
-	 * @param aStart  
-	 * @param aEnd
-	 */
-	void setSelection(Point aStart, Point aEnd){
-		if(aStart == null || aEnd == null)
-			return;
-		iSelectionRectEnd = new Point(Math.max(aEnd.x , aStart.x ) ,Math.max(aEnd.y, aStart.y ));
-		iSelectionRectStart = new Point(Math.min(aEnd.x , aStart.x ) ,Math.min(aEnd.y , aStart.y ));
-		
-		LatLon l1 = getLatLonOfScreenPoint(aStart);	
-		LatLon l2 = getLatLonOfScreenPoint(aEnd);
-		
-		iGui.minlat = Math.min(l1.lat(), l2.lat());
-		iGui.minlon = Math.min(l1.lon(), l2.lon());
-		iGui.maxlat = Math.max(l1.lat(), l2.lat());
-		iGui.maxlon = Math.max(l1.lon(), l2.lon());
-		
-		iGui.boundingBoxChanged(this);
-		
-		repaint();
-	}
-	
-	/**
-	 * Callback for OsmMapControll. Moves the map and the selection rectangle.
-	 * @param x number of pixels to move along the x-axis (longitude)
-	 * @param y number of pixels to move along the y axis (latitude)
-	 */
-	void moveMap(int x, int y) {
-		int moveX = x;
-		int moveY = y;
-		
-		int tempOffsetX = iOffsetX - x;
-		int tempOffsetY = iOffsetY - y;
-		
-		
-		int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);
-		
-		if(moveX != 0){
-			//deactivate dragging if the map is smaller than the DownloadDialog
-			if(maxPixels < getWidth()){
-				//center map horizontally
-				iOffsetX = (getWidth()-maxPixels)/2;	
-				moveX = 0;
-			}else{
-				//don't allow the map to hide outside the JComponent drawing area
-				if(tempOffsetX > 30){
-					if(moveX < 0){
-						moveX = 0;
-						iOffsetX = 30;
-					}
-				}else if(-tempOffsetX > maxPixels + 30 - getWidth()){
-					if(moveX > 0){
-						moveX = 0;
-						iOffsetX = -(maxPixels + 30 - getWidth());
-					}
-				}
-			}
-		}
-		
-		if(moveY != 0){
-			//deactivate dragging if the map is smaller than the DownloadDialog
-			if(maxPixels < getHeight()){
-				//center map vertically
-				iOffsetY = (getHeight()-maxPixels)/2;
-				moveY = 0;
-			}else{
-				//don't allow the map to hide outside the JComponent drawing area
-				if(tempOffsetY > 30){
-					if(moveY < 0){
-						moveY = 0;
-						iOffsetY = 30;
-					}
-				}else if(-tempOffsetY > maxPixels + 30 - getHeight()){
-					if(moveY > 0){
-						moveY = 0;
-						iOffsetY = -(maxPixels + 30 - getHeight());
-					}
-				}
-			}
-		}
-		
-		
-		
-		
-		
-		//execute the movement
-		iOffsetX -= moveX;
-		iOffsetY -= moveY;
-		
-		//also move the selection rect
-		if(iSelectionRectEnd != null && iSelectionRectStart != null){
-			iSelectionRectEnd.x   -= moveX;
-			iSelectionRectEnd.y   -= moveY;
-			iSelectionRectStart.x -= moveX;
-			iSelectionRectStart.y -= moveY;
-		}
-		
-		loadVisibleTiles();
-		
-		repaint();
-	}
-	
-	/**
-	 * Zoom in one level	
-	 * Callback for OsmMapControl. Zoom out one level		 
-	 */
-	void zoomIn(Point curPos){	
-		
-		//cache center of screen and the selection rectangle
-		LatLon l = getLatLonOfScreenPoint(curPos);
-		LatLon selStart = null;
-		LatLon selEnd   = null;
-		if(iSelectionRectEnd != null && iSelectionRectStart != null){
-			selStart = getLatLonOfScreenPoint(iSelectionRectStart);
-			selEnd   = getLatLonOfScreenPoint(iSelectionRectEnd);
-		}
-		
-		//increment zoom level
-		iZoomlevel += 1;
-		if(iZoomlevel > MAX_ZOOMLEVEL){
-			iZoomlevel = MAX_ZOOMLEVEL;
-			return;
-		}
-					
-		setLatLonAtPoint(l, curPos);
-		
-		//restore selection 
-		if(selStart != null && selEnd != null){
-			iSelectionRectStart = getScreenPointForLatLon(selStart);
-			iSelectionRectEnd   = getScreenPointForLatLon(selEnd);
-		}
-		
-		loadVisibleTiles();
-		
-		centerMap();
-		
-		repaint();
-	}
-	
-	/**
-	 * Zoom out one level.
-	 * Callback for OsmMapControl. 
-	 */
-	void zoomOut(Point curPos){
-		//cache center of screen and the selction rect
-		LatLon l = getLatLonOfScreenPoint(curPos);
-		LatLon selStart = null;
-		LatLon selEnd   = null;
-		if(iSelectionRectEnd != null && iSelectionRectStart != null){
-			selStart = getLatLonOfScreenPoint(iSelectionRectStart);
-			selEnd   = getLatLonOfScreenPoint(iSelectionRectEnd);
-		}
-		
-		//decrement zoom level
-		iZoomlevel -= 1;
-		if(iZoomlevel < MIN_ZOOMLEVEL){
-			iZoomlevel = MIN_ZOOMLEVEL;
-			return;
-		}
-		
-		setLatLonAtPoint(l, curPos);
-		
-		//restore selection 
-		if(selStart != null && selEnd != null){
-			iSelectionRectStart = getScreenPointForLatLon(selStart);
-			iSelectionRectEnd   = getScreenPointForLatLon(selEnd);
-		}
-		
-		loadVisibleTiles();
-		
-		centerMap();
-		
-		repaint();
-	}
-	
-	/**
-	 * Calculates the latitude and longitude of a Point given in map space
-	 * @param aPoint in pixels on the map
-	 * @return the LatLon of the given Point
-	 */
-	private LatLon getLatLonOfPoint(Point aPoint){	
-		return new LatLon(OsmMercator.YToLat(aPoint.y, iZoomlevel),OsmMercator.XToLon(aPoint.x,	iZoomlevel));
-	}
-	
-	/**
-	 * Returns the map coordinates for a LatLon
-	 * @param aLatLon 
-	 * @return
-	 */
-	private Point getPointForLatLon(LatLon aLatLon){
-		Point p = new Point();
-		p.y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);
-		p.x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);
-		return p;
-	}
-	
-	/**
-	 * Calculates the latitude and longitude of a Point given in screen space (in the JComponent)
-	 * @param aPoint in Pixels on the screen (the JComponent)
-	 * @return the LatLon of the given Point
-	 */
-	LatLon getLatLonOfScreenPoint(Point aPoint){
-		Point mapCoordinates = new Point(aPoint);		
-		mapCoordinates.x -= iOffsetX;
-		mapCoordinates.y -= iOffsetY;			
-		return getLatLonOfPoint(mapCoordinates);
-	}
-	
-	/**
-	 * Calculates the screen coordinates of a LatLon.
-	 * @param aLatLon 
-	 * @return the coordinates as Point in the JComponent 
-	 */
-	Point getScreenPointForLatLon(LatLon aLatLon){
-		Point p = getPointForLatLon(aLatLon);
-		p.x += iOffsetX;
-		p.y += iOffsetY;
-		return p;
-	}
-	
-	
-	/**
-	 * Centers the map on a Point 
-	 * @param aPoint in screen coordinates (JComponent)
-	 */
-	void centerOnScreenPoint(Point aPoint){
-		moveMap(aPoint.x - getWidth()/2, aPoint.y-getHeight()/2);
-	}
-	
-	/**
-	 * Centers the map on the location given by LatLon
-	 * @param aLatLon the location to center on
-	 */
-	private void centerOnLatLon(LatLon aLatLon){
-		setLatLonAtPoint(aLatLon, new Point(getWidth()/2,getHeight()/2));
-	}
-
-	/**
-	 * Moves the map that the specified latLon is shown at the point on screen
-	 * given
-	 * @param aLatLon a position
-	 * @param p a point on the screen
-	 */
-	private void setLatLonAtPoint(LatLon aLatLon, Point p){
-		int x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);
-		int y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);
-		iOffsetX = - x + p.x;
-		iOffsetY = - y + p.y;
-		repaint();
-	}
-	
-	/**
-	 * Caclulates the visible tiles for each dimension
-	 */
-	private void calcVisibleTiles(){
-		if(iGui != null){
-			iVisibleTilesX = iGui.getWidth()/256 + 2;
-			iVisibleTilesY = iGui.getHeight()/256 + 2;
-		}
-	}
-	
-	/**
-	 * Centers the map after zooming. I.e. when the map is smaller than the
-	 * component it is shown in.
-	 */
-	private void centerMap(){
-		
-		int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);		
-	
-		if(maxPixels < getWidth()){
-			//center map horizontally
-			iOffsetX = (getWidth()-maxPixels)/2;
-		}
-		if(maxPixels < getHeight()){
-			//center map vertically
-			iOffsetY = (getHeight()-maxPixels)/2;
-		}
-	}
 }
Index: plications/editors/josm/plugins/slippy_map_chooser/src/TileDB.java
===================================================================
--- /applications/editors/josm/plugins/slippy_map_chooser/src/TileDB.java	(revision 9536)
+++ 	(revision )
@@ -1,250 +1,0 @@
-// License: GPL. Copyright 2007 by Tim Haussmann
-
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
-
-import javax.imageio.ImageIO;
-import javax.swing.JComponent;
-
-
-
-/**
- *  The TileDB is responsible for fetching and storing all needed map tiles. The tiles
- *  are stored in memory and are fetched one by one in background.
- *  Tiles are stored in a HashTable. The path of the tile on the tile server (i.e. '/12/3/5.png') 
- *  acts as key and the OsmTile as value.
- *  @author Tim Haussmann
- *
- */
-public class TileDB{
-
-	public static final int PRIO_HIGH = 1;
-	public static final int PRIO_LOW = 2;
-	
-	//Osm tile server address
-	private static final String OSM_TILE_SERVER = "http://tile.openstreetmap.org";
-	
-	//Queue for loading tiles
-	private Vector<OsmTile> iTileQueue = new Vector<OsmTile>();	
-	
-	//DB for holding the downloaded tiles
-	private Hashtable<String,OsmTile> iHashTable = new Hashtable<String,OsmTile>(SlippyMapChooserPlugin.MAX_TILES_IN_DB + 50);
-	
-	//the order in that the tiles have been downloaded, needed to delete old ones
-	private Vector<String> iTileOrder = new Vector<String>(SlippyMapChooserPlugin.MAX_TILES_IN_DB + 50);
-	
-	//to update the map after a tile has been loaded
-	private JComponent iSlippyMapChooser;
-	
-	private int[] iMaxLatValues;
-	private int[] iMaxLonValues;
-	
-	/**
-	 * Creates the TileDB.
-	 * @param aWorldChooser
-	 */
-	public TileDB(JComponent aWorldChooser){
-		iSlippyMapChooser = aWorldChooser;
-		
-		iMaxLatValues = new int[20];
-		iMaxLonValues = new int[20];
-		for(int i=0; i<20; i++){
-			iMaxLatValues[i] = OsmMercator.LatToY(OsmMercator.MIN_LAT, i)/OsmTile.HEIGHT;
-			iMaxLonValues[i] = OsmMercator.LonToX(180, i)/OsmTile.WIDTH;
-		}
-		
-		new TileFetcherThread(this);
-		new TileFetcherThread(this);
-		new TileFetcherThread(this);
-		new TileFetcherThread(this);
-	}
-	
-	/**
-	 * Creates a OsmTile if it's not in the Hashtable. New created tiles are added to the
-	 * end of the download queue (Vector) and the DB (Hashtable). After that the fetching thread is 
-	 * notified.
-	 * @param aZoomLevel the zoomlevel for the tile to create
-	 * @param aIndexX the X-index of the tile to create. Must be within [0..2^zoomlevel[
-	 * @param aIndexY the Y-index of the tile to create. Must be within [0..2^zoomlevel[
-	 * @param aPriority PRIO_HIGH adds this tile at the waiting queue start  PRIO_LOW at the end
-	 */
-	public synchronized void loadTile(int aZoomLevel, int aIndexX, int aIndexY, int aPriority)
-	{		
-		
-		if(aZoomLevel <0 || aIndexX <0 || aIndexY <0){
-			return;
-		}else if(iMaxLonValues[aZoomLevel] <= aIndexX ||
-				iMaxLatValues[aZoomLevel] <= aIndexY ){
-			return;
-		}
-		String key = OsmTile.key(aZoomLevel, aIndexX, aIndexY);
-		OsmTile t = iHashTable.get(key);
-		if(t != null){
-			//if a tile is already in the DB and still in the laoding queue
-			//move it to the beginning of the queue in order to make the currently
-			//visible tiles load first
-			if(iTileQueue.remove(t)){				
-				iTileQueue.add(0,t);
-			}
-			return;
-		}
-		
-		t = new OsmTile(aZoomLevel, aIndexX, aIndexY);		
-		iHashTable.put(t.toString(),t);	
-		iTileOrder.add(0,key);
-		
-		if(aPriority == PRIO_LOW){
-			iTileQueue.addElement(t);
-		}else if(aPriority == PRIO_HIGH){
-			iTileQueue.add(0,t);
-		}else{
-			iTileQueue.addElement(t);
-		}
-		
-		//check if old tiles need to be deleted
-		if(iTileOrder.size() > SlippyMapChooserPlugin.MAX_TILES_IN_DB){
-			for(int i=0; i<SlippyMapChooserPlugin.MAX_TILES_REDUCE_BY; i++){
-				String removedKey = iTileOrder.remove(iTileOrder.size()-1);
-				iHashTable.remove(removedKey);
-			}
-		}
-		
-		//Wake up the fetching threads
-		synchronized (this) {
-			notifyAll();
-		}
-	}
-	
-	/**
-	 * Returns the next tile that needs to be loaded. This method is called by the threads
-	 * that fetch the tiles. If the download queue is empty the calling threads are waiting.
-	 * @return the first tile in the download queue with the same zoom level that is currently displayed or just the first. 
-	 */
-	private OsmTile getNextTile(){
-		if(iTileQueue.size() > 0){
-			OsmTile t;
-			Enumeration<OsmTile> e = iTileQueue.elements();
-			while(e.hasMoreElements()){
-				t = e.nextElement();
-				if(t.getZoomlevel() == SlippyMapChooser.iZoomlevel){
-					return t;
-				}
-			}
-			return iTileQueue.firstElement();	
-		}
-		else{			
-			synchronized (this) {
-				try {
-					wait();
-				} catch (InterruptedException e) {
-					e.printStackTrace();
-				}				
-			}
-			return iTileQueue.firstElement();	
-		}
-	}
-	
-	/**
-	 * Access tiles in the TileDB.
-	 * @return an Enumeration that holds all OsmTiles currently stored in the TileDB (Hashtable)
-	 */
-	public Enumeration<OsmTile> elements() {	
-		return iHashTable.elements();
-	}
-	
-	/**
-	 * Returns a OsmTile by key
-	 * @param aKey 
-	 * @return
-	 */
-	public OsmTile get(String aKey){
-		return iHashTable.get(aKey);
-	}
-	
-	/**
-	 * Delete OsmTiles from the TileDB.
-	 * @param aTile that is to be deleted.
-	 */
-	public void remove(OsmTile aTile){
-		iHashTable.remove(aTile.toString());
-		iTileQueue.removeElement(aTile);
-	}
-
-	/**
-	 * Returns the number of OsmTiles waiting to be fetched from the server.
-	 * @return 
-	 */
-	public int getLoadingQueueSize() {
-		return iTileQueue.size();
-	}
-
-	/**
-	 * Returns the number of tiles stored in the TileDB
-	 * @return
-	 */
-	public int getCachedTilesSize() {		
-		return iHashTable.size();
-	}
-	
-	
-	/***************************************************
-	 * Private inner class to fetch the tiles over http
-	 * *************************************************
-	 */
-	
-	class TileFetcherThread implements Runnable{
-		
-		private TileDB iTileDB;
-		private Thread iThread;
-		
-		public TileFetcherThread(TileDB aTileDB){
-			iTileDB = aTileDB;
-			iThread = new Thread(this);
-			iThread.start();
-		}
-		
-		public void run(){
-			
-			while(true){
-				
-				//get the next tile to load
-				OsmTile t = iTileDB.getNextTile();
-				
-				URL tileUrl = null;
-				try {
-					
-					//build the url to the tile
-					tileUrl = new URL(OSM_TILE_SERVER + t.getRemotePath());
-										
-					//get the tile
-					InputStream in = tileUrl.openStream();
-				
-					//the tile needs the image...					
-					t.setImage(ImageIO.read(in));
-					
-				} catch (MalformedURLException e) {
-//					System.out.println("Catched: " + e.getMessage());
-//					e.printStackTrace();				
-					t.setImage(null);
-				} catch (IOException e) {
-//					System.out.println("Catched: " + e.getMessage());
-//					e.printStackTrace();	
-					t.setImage(null);
-				} catch (Exception e){
-//					System.out.println("Catched: " + e.getMessage());
-//					e.printStackTrace();
-					t.setImage(null);
-				}
-				
-				iTileQueue.removeElement(t);
-				iSlippyMapChooser.repaint();	
-			}
-		}		
-	}
-}
