KickJava   Java API By Example, From Geeks To Geeks.

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


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.widgets.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.events.*;
17
18 /**
19  * A Canvas which reduces flicker by drawing in an off screen buffer.
20  */

21 public abstract class BufferedCanvas extends Canvas {
22
23     //private static final boolean USE_DOUBLE_BUFFER= !"carbon".equals(SWT.getPlatform()); //$NON-NLS-1$
24
private static final boolean USE_DOUBLE_BUFFER= true;
25     
26     /** The drawable for double buffering */
27     Image fBuffer;
28
29     public BufferedCanvas(Composite parent, int flags) {
30         super(parent, flags | SWT.NO_BACKGROUND);
31
32         addPaintListener(
33             new PaintListener() {
34                 public void paintControl(PaintEvent event) {
35                     doubleBufferPaint(event.gc);
36                 }
37             }
38         );
39
40         addDisposeListener(
41             new DisposeListener() {
42                 public void widgetDisposed(DisposeEvent e) {
43                     if (fBuffer != null) {
44                         fBuffer.dispose();
45                         fBuffer= null;
46                     }
47                 }
48             }
49         );
50     }
51
52     public void repaint() {
53         if (!isDisposed()) {
54             GC gc= new GC(this);
55             doubleBufferPaint(gc);
56             gc.dispose();
57         }
58     }
59
60     /*
61      * Double buffer drawing.
62      */

63     void doubleBufferPaint(GC dest) {
64         
65         if (!USE_DOUBLE_BUFFER) {
66             doPaint(dest);
67             return;
68         }
69
70         Point size= getSize();
71
72         if (size.x <= 1 || size.y <= 1) // we test for <= 1 because on X11 controls have initial size 1,1
73
return;
74
75         if (fBuffer != null) {
76             Rectangle r= fBuffer.getBounds();
77             if (r.width != size.x || r.height != size.y) {
78                 fBuffer.dispose();
79                 fBuffer= null;
80             }
81         }
82         if (fBuffer == null)
83             fBuffer= new Image(getDisplay(), size.x, size.y);
84
85         GC gc= new GC(fBuffer);
86         try {
87             gc.setBackground(getBackground());
88             gc.fillRectangle(0, 0, size.x, size.y);
89             doPaint(gc);
90         } finally {
91             gc.dispose();
92         }
93
94         dest.drawImage(fBuffer, 0, 0);
95     }
96
97     abstract public void doPaint(GC gc);
98 }
99
Popular Tags