Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/draw2d/imageviewer/views/ImageViewport.java


1   package com.port80.draw2d.imageviewer.views;
2   
3   import org.eclipse.draw2d.Figure;
4   import org.eclipse.draw2d.Graphics;
5   import org.eclipse.draw2d.geometry.Rectangle;
6   import org.eclipse.swt.graphics.Image;
7   import org.eclipse.swt.graphics.ImageData;
8   import org.eclipse.swt.graphics.Point;
9   import org.eclipse.swt.widgets.Display;
10  
11  import com.port80.eclipse.util.ImageFactory;
12  import com.port80.util.Msg;
13  
14  /**
15   * A viewport on an image that construct the visible area of the image on the fly.
16   * 
17   * @author chrisl
18   */
19  public class ImageViewport extends Figure {
20  
21    ////////////////////////////////////////////////////////////////////////
22  
23    private static final String NAME = "ImageViewport";
24    private static final boolean DEBUG = false;
25  
26    ////////////////////////////////////////////////////////////////////////
27  
28    private ImageData fData;
29    // The coordinate on the image that is displayed at the top-left corner of the viewport.
30    private Point fViewOffset;
31  
32    ////////////////////////////////////////////////////////////////////////
33  
34    public ImageViewport() {
35    }
36  
37    ////////////////////////////////////////////////////////////////////////
38  
39    public void setViewOffset(int x, int y, boolean repaint) {
40      fViewOffset.x = x;
41      fViewOffset.y = y;
42      if (repaint)
43        repaint();
44    }
45  
46    /** @return The view offset, not copied, do not modify. */
47    public Point getViewOffset() {
48      return fViewOffset;
49    }
50  
51    public void setImageData(ImageData data) {
52      fData = data;
53      setBounds(new Rectangle(0, 0, fData.width, fData.height));
54    }
55  
56    ////////////////////////////////////////////////////////////////////////
57  
58    /**
59     * @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
60     */
61    protected void paintFigure(Graphics graphics) {
62      if (fData == null)
63        return;
64      Rectangle clip = new Rectangle();
65      graphics.getClip(clip);
66      if (DEBUG)
67        Msg.println(NAME + ".paintFigure(): clip=" + clip);
68      clip.intersect(getBounds());
69      if (clip.width == 0 && clip.height == 0)
70        return;
71      Image image = createImage(clip);
72      graphics.pushState();
73      graphics.drawImage(image, clip.x, clip.y);
74      image.dispose();
75      graphics.popState();
76    }
77  
78    private Image createImage(Rectangle clip) {
79      ImageData data = ImageFactory.getDefault().deriveImageData(fData, clip.x, clip.y, clip.width, clip.height, 0xf8f8f8);
80      Image ret = new Image(Display.getDefault(), data);
81      return ret;
82    }
83  
84    ////////////////////////////////////////////////////////////////////////
85  
86  }