Index: FFrameGrid.java
===================================================================
--- FFrameGrid.java	(revision 343)
+++ FFrameGrid.java	(revision 344)
@@ -22,6 +22,7 @@
 package org.gvsig.app.project.documents.layout.fframes;
 
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics2D;
 import java.awt.font.TextLayout;
@@ -128,246 +129,295 @@
     private IMarkerSymbol symbolPoint = (IMarkerSymbol) symbolManager
         .createSymbol(IMarkerSymbol.SYMBOL_NAME);
     private Font font = new Font("Arial", Font.PLAIN, sizeFont);
-
-    private AffineTransform emptyTransform = new AffineTransform();
     
     public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
         BufferedImage imgBase) {
-        ViewPort vp = fframeViewDependence.getMapContext().getViewPort();        
-       
-        Rectangle2D.Double r = getBoundingBox(at);
-        Rectangle2D rView = fframeViewDependence.getBoundingBox(at);
-        if (r==null || rView==null) {
-        	//extent may be null, for instance if no layer has been loaded on the view
-        	return;
-        }      
-        Envelope envelope = vp.getAdjustedEnvelope();
-        updateIntervals(envelope);
-        double intervalX = getIntervalX();
-        double intervalY = getIntervalY();
-        
-        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y + (r.height / 2));
-        
-        double minX = envelope.getMinimum(0);
-        double minY = envelope.getMinimum(1);
-    	
-        // init point, in map coordinates
-        double initPointGridWCX = Math.floor(minX / intervalX)*intervalX;
-        double initPointGridWCY = Math.floor(minY / intervalY)*intervalY;
-        
-        // shift from the beginning of the map till the first point of the grid
-        double shiftXMap = initPointGridWCX-minX;
-        double shiftYMap = initPointGridWCY-minY;
-        
-        // init point, in screen coordinates (pixels)
-        double initPointGridPxX = rView.getMinX() + (rView.getWidth() * shiftXMap) /  envelope.getLength(0);
-        double initPointGridPxY = rView.getMinY() + (rView.getHeight() * shiftYMap) /  envelope.getLength(1);
-        double intervalPxWidth = (rView.getWidth() * intervalX) /  envelope.getLength(0);
-        double intervalPxHeight = (rView.getHeight() * intervalY) /  envelope.getLength(1);
-        
-        ISymbol symbolForMargins = null;
-        if (isLine){
-            symbolForMargins = symbolLine;
-        }else{
-            try {
-                symbolForMargins = (ISymbol)symbolLine.clone();
-                symbolForMargins.setColor(symbolPoint.getColor());
-            } catch (CloneNotSupportedException e) {
-                LOG.error("Symbol desn't support the clone method", e);
-            }
-        }
-       
-        //Draw the box
-        drawBox(rView, g, symbolForMargins);
+    	try {
+    		Rectangle2D.Double r = getBoundingBox(at);
+    		Rectangle2D rView = fframeViewDependence.getBoundingBox(at);
+    		if (r==null || rView==null) {
+    			//extent may be null, for instance if no layer has been loaded on the view
+    			return;
+    		}
+    		ViewPort vp = (ViewPort) fframeViewDependence.getMapContext().getViewPort().clone();
 
-        //Draw the margins
-        double myScale = at.getScaleX() * 0.0234; 
-        int scaledFontSize = (int) (myScale * sizeFont);
-        Font font = new Font(
-        		this.font.getFamily(), 
-        		this.font.getStyle(),
-                scaledFontSize);
-        
-        drawMargins(rView,
-        		initPointGridWCX,
-        		initPointGridWCY,
-        		intervalX,
-        		intervalY,
-        		initPointGridPxX, 
-        		initPointGridPxY, 
-        		intervalPxWidth,
-        		intervalPxHeight,
-        		g, 
-        		font, 
-        		symbolForMargins);
-      
-        //Draw the lines or the points
-        if (isLine) { 
-            drawLines(rView, 
-            		initPointGridPxX, 
-            		initPointGridPxY, 
-            		intervalPxWidth,
-            		intervalPxHeight, 
-            		vp, g);
-        } else {
-            drawPoints(rView, 
-            		initPointGridPxX, 
-            		initPointGridPxY, 
-            		intervalPxWidth,
-            		intervalPxHeight, 
-            		vp, g);
-        }
-        
-        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
-            + (r.height / 2));
+    		vp.setOffset(new Point2D.Double(rView.getMinX(), rView.getMinY()));
+    		vp.setImageSize(new Dimension((int)Math.round(rView.getWidth()), (int)Math.round(rView.getHeight())));
+    		vp.refreshExtent();
+    		
+    		Envelope envelope = vp.getAdjustedEnvelope();
+    		AffineTransform mapTransform = vp.getAffineTransform();
+    		updateIntervals(envelope);
+    		double intervalX = getIntervalX();
+    		double intervalY = getIntervalY();
+
+    		g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y + (r.height / 2));
+
+    		double minX = envelope.getMinimum(0);
+    		double minY = envelope.getMinimum(1);
+    		double maxX = envelope.getMaximum(0);
+    		double maxY = envelope.getMaximum(1);
+
+    		// init point, in map coordinates
+    		double initPointGridWCX = Math.ceil(minX / intervalX)*intervalX;
+    		double initPointGridWCY = Math.ceil(minY / intervalY)*intervalY;
+
+    		ISymbol symbolForMargins = null;
+    		if (isLine){
+    			symbolForMargins = symbolLine;
+    		}else{
+    			try {
+    				symbolForMargins = (ISymbol)symbolLine.clone();
+    				symbolForMargins.setColor(symbolPoint.getColor());
+    			} catch (CloneNotSupportedException e) {
+    				LOG.error("Symbol desn't support the clone method", e);
+    			}
+    		}
+
+    		//Draw the box
+    		drawBox(rView, g, symbolForMargins);
+
+    		//Draw the margins
+    		double myScale = at.getScaleX() * 0.0234; // FIXME: 0.0234?? Should this depend on output resolution?
+    		int scaledFontSize = (int) (myScale * sizeFont);
+    		Font font = new Font(
+    				this.font.getFamily(), 
+    				this.font.getStyle(),
+    				scaledFontSize);
+
+    		drawLabels(rView,
+    				initPointGridWCX,
+    				initPointGridWCY,
+    				maxX,
+    				maxY,
+    				intervalX,
+    				intervalY,
+    				mapTransform,
+    				g, 
+    				font, 
+    				symbolForMargins);
+
+    		//Draw the lines or the points
+    		if (isLine) { 
+    			drawLines(rView,
+        				initPointGridWCX,
+        				initPointGridWCY,
+        				maxX,
+        				maxY,
+        				intervalX,
+        				intervalY,
+        				mapTransform,
+        				g);
+    		} else {
+    			drawPoints(initPointGridWCX,
+				initPointGridWCY,
+				maxX,
+				maxY,
+				intervalX,
+				intervalY,
+				mapTransform,
+				g);
+    		}
+
+    		g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
+    				+ (r.height / 2));
+    	} catch (CloneNotSupportedException e1) {}
     } 
-    
-    private void drawPoints(Rectangle2D rView, 
-    		double initPointGridPxX, 
-    		double initPointGridPxY, 
-    		double intervalPxX, 
-    		double intervalPxY, 
-    		ViewPort viewPort, 
+
+    /**
+     * Draw the grid as point symbols
+     * 
+     * @param minGridX The starting grid point on the x axis, in map coordinates
+     * @param minGridY The starting grid point on the y axis, in map coordinates
+     * @param maxGridX The maximum map point on the x axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param maxGridY The maximum map point on the y axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param intervalGridX The grid interval on the x axis, in map coordinates
+     * @param intervalGridY The grid interval on the y axis, in map coordinates
+     * @param mapTransform Transforms map coordinates to screen coordinates
+     * @param g The graphics object to draw on
+     */
+    private void drawPoints(
+    		double minGridX,
+    		double minGridY,
+    		double maxGridX,
+    		double maxGridY,
+    		double intervalGridX,
+    		double intervalGridY,
+			AffineTransform mapTransform, 
     		Graphics2D g) {
-        double minX = rView.getMinX();
-        double maxX = rView.getMaxX();
-        double minY = rView.getMinY();
-        double maxY = rView.getMaxY();
-        double x = initPointGridPxX;
-        double y = initPointGridPxY;
-        
-        while (x <= maxX) {
-            if (x > minX) {
-                while (y <= maxY) {
-                    if (y > minY) {
-                        Point2D p = new Point2D.Double(x, y);
-                        drawPoint(p, g);
-                    }
-                    y = y + intervalPxY;
-                }
-                y = initPointGridPxY;
-            }
-            x = x + intervalPxX;
+    	
+        Point2D currentPoint = new Point2D.Double(minGridX, minGridY);
+        Point2D screenPoint = new Point2D.Double();
+        while (currentPoint.getX() < maxGridX) {
+        	while (currentPoint.getY() < maxGridY) {
+        		mapTransform.transform(currentPoint, screenPoint);
+                drawPoint(g, screenPoint);
+            	currentPoint.setLocation(currentPoint.getX(), currentPoint.getY()+intervalGridY);
+        	}
+        	currentPoint.setLocation(currentPoint.getX()+intervalGridX, minGridY);
         }
     }
-    
-    private void drawLines(Rectangle2D rView, 
-    		double initPointGridPxX, 
-    		double initPointGridPxY, 
-    		double intervalPxX, 
-    		double intervalPxY, 
-    		ViewPort viewPort, 
+
+    /**
+     * Draw the grid as line symbols
+     * 
+     * @param rView The rectangle of the view, on screen coordinates
+     * @param minGridX The starting grid point on the x axis, in map coordinates
+     * @param minGridY The starting grid point on the y axis, in map coordinates
+     * @param maxGridX The maximum map point on the x axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param maxGridY The maximum map point on the y axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param intervalGridX The grid interval on the x axis, in map coordinates
+     * @param intervalGridY The grid interval on the y axis, in map coordinates
+     * @param mapTransform Transforms map coordinates to screen coordinates
+     * @param g The graphics object to draw on
+     */
+    private void drawLines(Rectangle2D rView,
+    		double minMapX,
+    		double minMapY,
+    		double maxMapX,
+    		double maxMapY,
+    		double intervalMapX,
+    		double intervalMapY,
+			AffineTransform mapTransform, 
     		Graphics2D g) {
         double minX = rView.getMinX();
         double maxX = rView.getMaxX();
         double minY = rView.getMinY();
         double maxY = rView.getMaxY();
-        double x = initPointGridPxX;
-        double y = initPointGridPxY;
-        
+        double xMap = minMapX;
+        double yMap = minMapY;
+
+        // transform to screen coordinates
+        Point2D currentPoint = new Point2D.Double(minMapX, minMapY);
+    	// transform from map coordinates to screen (pixels) coordinates
+        mapTransform.transform(currentPoint, currentPoint);
+               
         //Draw the vertical lines
-        while (x < maxX) {
-        	if (x>minX) {
-        		Point2D antPoint = new Point2D.Double(x, minY - 5);
-        		Point2D p = new Point2D.Double(x, maxY + 5);
-        		drawLine(antPoint, p, g, symbolLine);                    
-        		antPoint = (Point2D) p.clone();               
-        	}
-            x = x + intervalPxX;
+        while (xMap < maxMapX) {
+                drawLine(currentPoint.getX(), minY, currentPoint.getX(), maxY, g, symbolLine);
+        	xMap = xMap + intervalMapX;
+        	currentPoint.setLocation(xMap, minMapY);
+        	// from map coordinates to screen (pixels)
+        	mapTransform.transform(currentPoint, currentPoint);
         }
         //Draw the horizontal lines
-        while (y <= maxY) {
-        	if (y > minY) {
-        		Point2D antPoint = new Point2D.Double(minX, y);
-        		Point2D p = new Point2D.Double(rView.getMaxX(), y);
-        		drawLine(antPoint, p, g, symbolLine);              
-        		antPoint = (Point2D) p.clone();               
-        	}
-            y = y + intervalPxY;
+        while (yMap < maxMapY) {
+        	drawLine(minX, currentPoint.getY(), maxX , currentPoint.getY(), g, symbolLine);
+        	yMap = yMap + intervalMapY;
+        	currentPoint.setLocation(minMapX, yMap);
+        	// from map coordinates to screen (pixels)
+        	mapTransform.transform(currentPoint, currentPoint);
         }
     }
-    
-    private void drawMargins(Rectangle2D rView,
-    		double initPointGridMapX,
-    		double initPointGridMapY,
-    		double intervalMapX,
-    		double intervalMapY,
-    		double initPointGridPxX, 
-    		double initPointGridPxY, 
-    		double intervalPxX, 
-    		double intervalPxY, 
+
+    /**
+     * Draws the labels and the tick marks of labels
+     * 
+     * @param rView The rectangle of the view, on screen coordinates
+     * @param minGridX The starting grid point on the x axis, in map coordinates
+     * @param minGridY The starting grid point on the y axis, in map coordinates
+     * @param maxGridX The maximum map point on the x axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param maxGridY The maximum map point on the y axis, in map coordinates.
+     * It may be bigger than the maximum grid point
+     * @param intervalGridX The grid interval on the x axis, in map coordinates
+     * @param intervalGridY The grid interval on the y axis, in map coordinates
+     * @param mapTransform Transforms map coordinates to screen coordinates
+     * @param g The graphics object to draw on
+     * @param font
+     * @param symbolForMargins
+     */
+    private void drawLabels(Rectangle2D rView,
+    		double minGridX,
+    		double minGridY,
+    		double maxGridX,
+    		double maxGridY,
+    		double intervalGridX,
+    		double intervalGridY,
+    		AffineTransform mapTransform,
     		Graphics2D g,  
     		Font font,
     		ISymbol 
     		symbolForMargins) {
     	AffineTransform defaultAt = g.getTransform();
     	
-        double minX = rView.getMinX();
-        double maxX = rView.getMaxX();
-        double minY = rView.getMinY();
-        double maxY = rView.getMaxY();
-        double xPx = initPointGridPxX;
-        double xMap = initPointGridMapX;
-        
-        double angle = RotatedTextUtils.normalizeAngle(Math.toRadians(getVertLabelRotation()));
-        int anchor = getLabelAnchor();
-        // Draw vertical ticks and labels
-        while (xPx < maxX) {
-            if (xPx > minX) {    
-                // bottom tick
-                drawLine(xPx, maxY + OFFSET_IN_PIXELS, xPx , maxY, g, symbolForMargins);
-                // top tick
-                drawLine(xPx, minY, xPx, minY - OFFSET_IN_PIXELS, g, symbolForMargins);
-                
-                g.rotate(angle);
-                g.setColor(textColor);
-                
-                // bottom label
-                Point2D p1 = new Point2D.Double(xPx, maxY+OFFSET_IN_PIXELS); // bottom
-                TextLayout textaux = new TextLayout(this.labelFormat.format(xMap), font, g.getFontRenderContext());
-                Point2D rotatedP1 = RotatedTextUtils.getPosition(p1, textaux, angle, RotatedTextUtils.PLACEMENT_BOTTOM, anchor);
-                textaux.draw(g, (int) (rotatedP1.getX()), (int) rotatedP1.getY());
+    	double minX = rView.getMinX();
+    	double maxX = rView.getMaxX();
+    	double minY = rView.getMinY();
+    	double maxY = rView.getMaxY();
+    	double xMap = minGridX;
+    	double yMap = minGridY;
 
-                // top label
-                Point2D p2 = new Point2D.Double(xPx, minY-OFFSET_IN_PIXELS);
-                p2.setLocation(p2.getX(), p2.getY()-OFFSET_IN_PIXELS);
-                Point2D rotatedP2 = RotatedTextUtils.getPosition(p2, textaux, angle, RotatedTextUtils.PLACEMENT_TOP, anchor);
-                textaux.draw(g, (int) (rotatedP2.getX()),(int) rotatedP2.getY());
-                g.setTransform(defaultAt);
+    	// transform to screen coordinates
+    	Point2D initGridPx = new Point2D.Double(minGridX, minGridY);
+    	mapTransform.transform(initGridPx, initGridPx);
 
-            }
-            xMap = xMap + intervalMapX;
-            xPx = xPx + intervalPxX;
-        }
-        double yPx = initPointGridPxY;
-        double yMap = initPointGridMapY;
+    	double xPx = initGridPx.getX();
 
-        angle = RotatedTextUtils.normalizeAngle(Math.toRadians(getHorizLabelRotation()));
-        // horizontal ticks and labels
-        while (yPx < maxY) {
-            if (yPx > minY) {
-               
-                // left tick
-                drawLine(minX - OFFSET_IN_PIXELS, yPx, minX , yPx, g, symbolForMargins);
-                // right tick
-                drawLine(maxX, yPx, maxX + OFFSET_IN_PIXELS, yPx, g, symbolForMargins);
-                 
-                g.rotate(angle);
-                g.setColor(textColor);
-                TextLayout textaux = new TextLayout(this.labelFormat.format(yMap), font, g.getFontRenderContext());
-                // left label
-                Point2D p1 = new Point2D.Double(minX-OFFSET_IN_PIXELS, yPx);
-                Point2D rotatedP1 = RotatedTextUtils.getPosition(p1, textaux, angle, RotatedTextUtils.PLACEMENT_LEFT, anchor);
-                textaux.draw(g, (int) (rotatedP1.getX()), (int) rotatedP1.getY());
-                // right label
-                Point2D p2 = new Point2D.Double(maxX+OFFSET_IN_PIXELS, yPx);
-                Point2D rotatedP2 = RotatedTextUtils.getPosition(p2, textaux, angle, RotatedTextUtils.PLACEMENT_RIGHT, anchor);
-                textaux.draw(g, (int) (rotatedP2.getX()),(int) rotatedP2.getY());
-                g.setTransform(defaultAt);
-            }
-            yMap = yMap + intervalMapY;
-            yPx = yPx + intervalPxY;
-        }
+    	double angle = RotatedTextUtils.normalizeAngle(Math.toRadians(getVertLabelRotation()));
+    	int anchor = getLabelAnchor();
+    	// Draw vertical ticks and labels
+    	while (xMap < maxGridX) {   
+    		// bottom tick
+    		drawLine(xPx, maxY + OFFSET_IN_PIXELS, xPx , maxY, g, symbolForMargins);
+    		// top tick
+    		drawLine(xPx, minY, xPx, minY - OFFSET_IN_PIXELS, g, symbolForMargins);
+
+    		g.rotate(angle);
+    		g.setColor(textColor);
+
+    		// bottom label
+    		Point2D p1 = new Point2D.Double(xPx, maxY+OFFSET_IN_PIXELS); // bottom
+    		TextLayout textaux = new TextLayout(this.labelFormat.format(xMap), font, g.getFontRenderContext());
+    		Point2D rotatedP1 = RotatedTextUtils.getPosition(p1, textaux, angle, RotatedTextUtils.PLACEMENT_BOTTOM, anchor);
+    		textaux.draw(g, (int) (rotatedP1.getX()), (int) rotatedP1.getY());
+
+    		// top label
+    		Point2D p2 = new Point2D.Double(xPx, minY-OFFSET_IN_PIXELS);
+    		Point2D rotatedP2 = RotatedTextUtils.getPosition(p2, textaux, angle, RotatedTextUtils.PLACEMENT_TOP, anchor);
+    		textaux.draw(g, (int) (rotatedP2.getX()),(int) rotatedP2.getY());
+    		g.setTransform(defaultAt);
+
+
+    		xMap = xMap + intervalGridX;
+
+    		initGridPx.setLocation(xMap, minGridY);
+    		mapTransform.transform(initGridPx, initGridPx);
+    		xPx = initGridPx.getX();
+    	}
+    	double yPx = initGridPx.getY();
+    	angle = RotatedTextUtils.normalizeAngle(Math.toRadians(getHorizLabelRotation()));
+    	// horizontal ticks and labels
+    	while (yMap < maxGridY) {
+
+    		// left tick
+    		drawLine(minX - OFFSET_IN_PIXELS, yPx, minX , yPx, g, symbolForMargins);
+    		// right tick
+    		drawLine(maxX, yPx, maxX + OFFSET_IN_PIXELS, yPx, g, symbolForMargins);
+
+    		g.rotate(angle);
+    		g.setColor(textColor);
+    		TextLayout textaux = new TextLayout(this.labelFormat.format(yMap), font, g.getFontRenderContext());
+
+    		// left label
+    		Point2D p1 = new Point2D.Double(minX-OFFSET_IN_PIXELS, yPx);
+    		Point2D rotatedP1 = RotatedTextUtils.getPosition(p1, textaux, angle, RotatedTextUtils.PLACEMENT_LEFT, anchor);
+    		textaux.draw(g, (int) (rotatedP1.getX()), (int) rotatedP1.getY());
+    		// right label
+    		Point2D p2 = new Point2D.Double(maxX+OFFSET_IN_PIXELS, yPx);
+    		Point2D rotatedP2 = RotatedTextUtils.getPosition(p2, textaux, angle, RotatedTextUtils.PLACEMENT_RIGHT, anchor);
+    		textaux.draw(g, (int) (rotatedP2.getX()),(int) rotatedP2.getY());
+    		g.setTransform(defaultAt);
+    		yMap = yMap + intervalGridY;
+
+    		initGridPx.setLocation(minGridX, yMap);
+    		mapTransform.transform(initGridPx, initGridPx);
+    		yPx = initGridPx.getY();
+    	}
     }
     
     private void drawBox(Rectangle2D mapRectangle, Graphics2D g, ISymbol symbol){        
@@ -388,11 +438,18 @@
         } 
     }
     
-    private void drawPoint(Point2D point, Graphics2D g){
+    /**
+     * Draws a point using the configured symbol
+     * 
+     * @param g The graphics object to draw on
+     * @param point Point to draw, in map coordinates
+     * @param mapTransform Transforms from map coordinates to screen coordinates
+     */
+    private void drawPoint(Graphics2D g, Point2D point){
         try {
             Geometry geom =
                 createPoint((int) point.getX(), (int) point.getY());
-            symbolPoint.draw(g, emptyTransform, geom, null, null);           
+            symbolPoint.draw(g, null, geom, null, null);           
         }  catch (LocatorException e) {
             LOG.error("Error drawing the grid", e);
         } catch (CreateGeometryException e) {
@@ -411,10 +468,6 @@
             LOG.error("Error drawing the grid", e);
         }
     }
-    
-    private void drawLine(Point2D startPoint, Point2D endPoint, Graphics2D g,  ISymbol symbolLine){
-        drawLine(startPoint.getX(), startPoint.getY(), endPoint.getX(), endPoint.getY(), g, symbolLine);
-    }
 
     private Geometry createLine(int i, int j, int k, int l)
         throws LocatorException, CreateGeometryException {
@@ -617,7 +670,7 @@
      * calculate them.
      * 
      * @return  One of {@link #FIXED_DISTANCE_MODE},
-     * {@link #NUM_DIVISIONS_HORIZ_MODE} or {@link #NUM_DIVISIONS_HORIZ_MODE}.
+     * {@link #NUM_DIVISIONS_HORIZ_MODE} or {@link #NUM_DIVISIONS_VERT_MODE}.
      */
     public int getUseNumDivisions() {
     	return this.useNumDivisions;
@@ -631,7 +684,7 @@
     	this.numDivVert = ny;
     }
     
-    public int getNumDivisionHoriz() {
+    public int getNumDivisionsHoriz() {
     	return this.numDivHoriz;
     }
     
