Revision 357 trunk/org.gvsig.app.document.layout2.app/org.gvsig.app.document.layout2.app.mainplugin/src/main/java/org/gvsig/app/project/documents/layout/DefaultLayoutControl.java

View differences:

DefaultLayoutControl.java
31 31
import java.awt.Toolkit;
32 32
import java.awt.geom.AffineTransform;
33 33
import java.awt.geom.Rectangle2D;
34
import java.awt.geom.Rectangle2D.Double;
34 35
import java.awt.image.BufferedImage;
35 36
import java.awt.image.MemoryImageSource;
36 37
import java.util.HashMap;
......
78 79
import org.gvsig.app.project.documents.layout.tools.listener.ILayoutGraphicListener;
79 80
import org.gvsig.fmap.dal.exception.ReadException;
80 81
import org.gvsig.tools.observer.Observable;
82
import org.slf4j.Logger;
83
import org.slf4j.LoggerFactory;
81 84

  
82 85
/**
83 86
 * Control of Layout.
......
96 99
    private Rectangle2D rectVisible;
97 100
    private BufferedImage img = null;
98 101
    private BufferedImage imgRuler = null;
99
    private boolean initial = true;
100 102
    private LayoutContext layoutContext;
101 103
    private Point origin = new Point(50, 50);
102 104
    private Point rectOrigin = new Point(origin);
105
    /**
106
     * Size and position of the layout sheet within visible area of
107
     * the layout component, measured in screen coordinates (pixels).
108
     */
103 109
    private Rectangle2D.Double rect = new Rectangle2D.Double(rectOrigin.x,
104 110
        rectOrigin.y, 400, 300);
111
    /**
112
     * Visible area of the layout document, measured on paper coordinates.
113
     */
114
    private Rectangle2D.Double viewPort = new Rectangle2D.Double();
105 115
    private FLayoutDraw layoutDraw = null;
106 116
    private FLayoutFunctions layoutFunctions = null;
107 117
    private LayoutBehavior currentLayoutTool = null;
......
114 124
    private Point position;
115 125
    private GeometryAdapter geometryAdapter = new PolyLineAdapter();
116 126

  
117
    // private String prevTool;
118 127
    private String currentTool;
119 128
    private boolean m_bCancelDrawing = false;
120 129
    private Rectangle reSel = null;
......
334 343
    }
335 344

  
336 345
    public void fullRect() {
337
        rect.setRect(origin.x, origin.y, getWidth() - (origin.x * 2),
338
            getHeight() - (origin.x * 2));
346
        Rectangle2D r = new Rectangle2D.Double(origin.x, origin.y, getWidth(),
347
            getHeight());
339 348

  
340 349
        if (layoutContext.getAttributes().isLandscape()) {
341
            rect =
342
                layoutContext.getAttributes().getRectangleLandscape(rect,
350
            r =
351
                layoutContext.getAttributes().getRectangleLandscape(r,
343 352
                    getWidth(), getHeight());
344 353
        } else {
345
            rect =
346
                layoutContext.getAttributes().getRectanglePortrait(rect,
354
            r =
355
                layoutContext.getAttributes().getRectanglePortrait(r,
347 356
                    getWidth(), getHeight());
348 357
        }
349

  
358
        setRect(r);
350 359
        refresh();
351 360
    }
352 361

  
353 362
    public Rectangle2D.Double getRect() {
354
        return rect;
363
    	return (Rectangle2D.Double) rect.clone();
355 364
    }
356 365

  
357 366
    public void setRect(Rectangle2D r) {
358 367
        rect.setRect(r);
368
        Rectangle2D bounds = getBounds();
369
        if (rect.getWidth()>0 && bounds.getWidth()>0) { // only when the component has been laid out and rect properly initialized
370
        	// ensure the AT is properly initialized
371
        	layoutDraw.initializeAffineTransform();
372
        	AffineTransform at = layoutContext.getAT();
373
        	if (at!=null) {
374
        		Rectangle2D.Double newVp = new Rectangle2D.Double(0d, 0d, bounds.getWidth(), bounds.getHeight());
375
        		viewPort.setRect(FLayoutUtilities.toSheetRect(newVp, at));
376
        	}
377
        }
359 378
    }
360 379

  
361 380
    public BufferedImage getImage() {
......
523 542
    }
524 543

  
525 544
    public void refresh() {
545
    	AffineTransform at = calculateTransformFromViewport();
546
    	if (at!=null) {
547
        	// update rect according to the viewport rectangle
548
    		Size s = getLayoutContext().getAttributes().getPaperSize();
549
    		Rectangle2D paperRect = new Rectangle2D.Double(0, 0, s.getWidth(), s.getHeight());
550
    		Rectangle2D newRect = FLayoutUtilities.fromSheetRect(paperRect, at);
551
    		setRect(newRect);
552
    	}
553
    	// repaint
526 554
        setStatus(DESACTUALIZADO);
527 555
        invalidate();
528 556
        repaint();
529 557
    }
558
    
559
    /**
560
     * Calculates the affine transform using the viewport and the control size,
561
     * instead of calculating it from {@link #rect}. as usual. This is required when
562
     * the component size changes, as {@link #rect}. has to be recalculated using
563
     * the viewport, and thus we can't calculate the affine transform using
564
     * {@link #rect}.
565
     * 
566
     * @return An matrix to transform from paper coordinates to screen coordinates
567
     */
568
    protected AffineTransform calculateTransformFromViewport() {
569
    	Rectangle2D bounds = getBounds();
570
    	if (bounds.getWidth()>0 && viewPort.getHeight()>0) {
571
    		AffineTransform at = new AffineTransform();
572
    		// paper (paper units) to screen (pixels) scale
573
        	double scale, scale1, scale2;
574
        	scale1 = bounds.getHeight() / viewPort.getHeight() * 1;
575
        	scale2 = bounds.getWidth() / viewPort.getWidth() * 1;
576
        	scale = (scale1+scale2)/2d; // average scale
577
    		AffineTransform scaleAt = new AffineTransform();
578
    		AffineTransform translationAt = new AffineTransform();
579
    		translationAt.setToTranslation(-viewPort.getMinX(), -viewPort.getMinY());
580
    		scaleAt.setToScale(scale, scale);
581
    		at.concatenate(scaleAt);
582
    		at.concatenate(translationAt);
583
    		return at;
584
    	}
585
    	return null;
586
    }
530 587

  
531 588
    public synchronized boolean isDrawingCancelled() {
532 589
        return m_bCancelDrawing;

Also available in: Unified diff