KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ImageCanvas


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16
17 /**
18  * A <code>Canvas</code> showing a single centered SWT <code>Image</code>.
19  * If the <code>Image</code> is larger than the <code>Canvas<code>,
20  * <code>Scrollbars</code> will appear.
21  */

22 class ImageCanvas extends Canvas {
23
24     private Image fImage;
25
26     /*
27      * Create a new ImageCanvas with the given SWT stylebits.
28      * (SWT.H_SCROLL and SWT.V_SCROLL are automtically added).
29      */

30     public ImageCanvas(Composite parent, int style) {
31         super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL);
32
33         ScrollBar sb= getHorizontalBar();
34         sb.setIncrement(20);
35         sb.addListener(SWT.Selection, new Listener() {
36             public void handleEvent(Event e) {
37                 repaint();
38             }
39         });
40
41         sb= getVerticalBar();
42         sb.setIncrement(20);
43         sb.addListener(SWT.Selection, new Listener() {
44             public void handleEvent(Event e) {
45                 repaint();
46             }
47         });
48
49         addListener(SWT.Resize, new Listener() {
50             public void handleEvent(Event e) {
51                 updateScrollbars();
52             }
53         });
54
55         addListener(SWT.Paint, new Listener() {
56             public void handleEvent(Event event) {
57                 paint(event.gc);
58             }
59         });
60     }
61
62     /*
63      * Set the SWT Image to use as the ImageCanvas contents.
64      */

65     public void setImage(Image img) {
66         fImage= img;
67
68         if (!isDisposed()) {
69             getHorizontalBar().setSelection(0);
70             getVerticalBar().setSelection(0);
71             updateScrollbars();
72             getParent().layout();
73             redraw();
74         }
75     }
76
77     public void repaint() {
78         if (!isDisposed()) {
79             GC gc= new GC(this);
80             paint(gc);
81             gc.dispose();
82         }
83     }
84
85     void paint(GC gc) {
86         if (fImage != null) {
87             Rectangle bounds= fImage.getBounds();
88             Rectangle clientArea= getClientArea();
89
90             int x;
91             if (bounds.width < clientArea.width)
92                 x= (clientArea.width - bounds.width) / 2;
93             else
94                 x= -getHorizontalBar().getSelection();
95
96             int y;
97             if (bounds.height < clientArea.height)
98                 y= (clientArea.height - bounds.height) / 2;
99             else
100                 y= -getVerticalBar().getSelection();
101
102             gc.drawImage(fImage, x, y);
103         }
104     }
105
106     /**
107      * @private
108      */

109     void updateScrollbars() {
110         Rectangle bounds= fImage != null ? fImage.getBounds() : new Rectangle(0, 0, 0, 0);
111         Point size= getSize();
112         Rectangle clientArea= getClientArea();
113
114         ScrollBar horizontal= getHorizontalBar();
115         if (bounds.width <= clientArea.width) {
116             horizontal.setVisible(false);
117             horizontal.setSelection(0);
118         } else {
119             horizontal.setPageIncrement(clientArea.width - horizontal.getIncrement());
120             int max= bounds.width + (size.x - clientArea.width);
121             horizontal.setMaximum(max);
122             horizontal.setThumb(size.x > max ? max : size.x);
123             horizontal.setVisible(true);
124         }
125
126         ScrollBar vertical= getVerticalBar();
127         if (bounds.height <= clientArea.height) {
128             vertical.setVisible(false);
129             vertical.setSelection(0);
130         } else {
131             vertical.setPageIncrement(clientArea.height - vertical.getIncrement());
132             int max= bounds.height + (size.y - clientArea.height);
133             vertical.setMaximum(max);
134             vertical.setThumb(size.y > max ? max : size.y);
135             vertical.setVisible(true);
136         }
137     }
138
139 }
140
Popular Tags