KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > RectangleAnimationImageFeedback


1 /*******************************************************************************
2  * Copyright (c) 2007 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.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.util.Geometry;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.PaintEvent;
20 import org.eclipse.swt.events.PaintListener;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.widgets.Canvas;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.internal.AnimationEngine;
29
30 /**
31  * Creates an animation effect where the interpolated rectangles are displayed using Canvas
32  * controls that show an image of the bits that were originally occupied by the various
33  * 'start' rectangles.
34  *
35  * @since 3.3
36  *
37  */

38 public class RectangleAnimationImageFeedback extends
39         RectangleAnimationFeedbackBase {
40     private class ImageCanvas extends Canvas {
41         private Image image;
42
43         /**
44          * @param parent
45          * @param style
46          */

47         public ImageCanvas(Composite parent, int style, Image image) {
48             super(parent, style);
49             this.image = image;
50
51             addPaintListener(new PaintListener() {
52                 public void paintControl(PaintEvent e) {
53                     paintImage(e.gc);
54                 }
55             });
56         }
57
58         /**
59          * @param gc
60          */

61         protected void paintImage(GC gc) {
62             gc.drawImage(image, 0, 0, image.getBounds().width, image
63                     .getBounds().height, 0, 0, getBounds().width,
64                     getBounds().height);
65         }
66
67         /* (non-Javadoc)
68          * @see org.eclipse.swt.widgets.Widget#dispose()
69          */

70         public void dispose() {
71             super.dispose();
72             image.dispose();
73         }
74     }
75
76     private Image backingStore;
77     private Shell theShell;
78     private Display display;
79     private List JavaDoc controls = new ArrayList JavaDoc();
80
81
82     public RectangleAnimationImageFeedback(Shell parentShell, Rectangle start,
83             Rectangle end) {
84         super(parentShell, start, end);
85     }
86
87     public void dispose() {
88         backingStore.dispose();
89         for (Iterator JavaDoc ctrlIter = controls.iterator(); ctrlIter.hasNext();) {
90             ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
91             canvas.dispose();
92         }
93
94         theShell.setVisible(false);
95         theShell.dispose();
96     }
97
98     public void initialize(AnimationEngine engine) {
99         display = getAnimationShell().getDisplay();
100
101         Rectangle psRect = getAnimationShell().getBounds();
102         theShell = new Shell(getAnimationShell(), SWT.NO_TRIM | SWT.ON_TOP);
103         theShell.setBounds(getAnimationShell().getBounds());
104
105         // Capture the background image
106
backingStore = new Image(theShell.getDisplay(), psRect);
107         GC gc = new GC(display);
108         gc.copyArea(backingStore, psRect.x, psRect.y);
109         gc.dispose();
110 // changeCoordinates();
111
// captureImages();
112
theShell.setBackgroundImage(backingStore);
113         theShell.setVisible(true);
114         display.update();
115
116     }
117         
118     /* (non-Javadoc)
119      * @see org.eclipse.ui.internal.RectangleAnimationFeedbackBase#jobInit(org.eclipse.ui.internal.AnimationEngine)
120      */

121     public boolean jobInit(AnimationEngine engine) {
122         changeCoordinates();
123         captureImages();
124         return super.jobInit(engine);
125     }
126     
127     public void addStartRect(Rectangle rect) {
128         if (rect == null)
129             return;
130
131         // Rectangle start = Geometry.toControl(getAnimationShell(), rect);
132
super.addStartRect(rect);
133
134     }
135
136     public void addEndRect(Rectangle rect) {
137         if (rect != null) {
138             // Rectangle end = Geometry.toControl(getAnimationShell(), rect);
139
super.addEndRect(rect);
140         }
141     }
142
143     public void renderStep(AnimationEngine engine) {
144         Iterator JavaDoc ctrlIter = controls.iterator();
145         Iterator JavaDoc currentRects = getCurrentRects(engine.amount()).iterator();
146         while (currentRects.hasNext()) {
147             ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
148             canvas.setBounds((Rectangle) currentRects.next());
149         }
150         display.update();
151
152     }
153
154     public void changeCoordinates() {
155         Iterator JavaDoc startRectIter = getStartRects().iterator();
156         Iterator JavaDoc endRectIter = getEndRects().iterator();
157         while (startRectIter.hasNext()) {
158             Rectangle startRect = (Rectangle) startRectIter.next();
159             Rectangle mapStartRect = Geometry.toControl(theShell, startRect);
160             startRect.x = mapStartRect.x;
161             startRect.y = mapStartRect.y;
162             Rectangle endRect = (Rectangle) endRectIter.next();
163             Rectangle mapEndRect = Geometry.toControl(theShell, endRect);
164             endRect.x = mapEndRect.x;
165             endRect.y = mapEndRect.y;
166         }
167     }
168
169     private void captureImages() {
170
171         for (Iterator JavaDoc iterator = getStartRects().iterator(); iterator.hasNext();) {
172             Rectangle rect = (Rectangle) iterator.next();
173             Image image = new Image(display, rect.width, rect.height);
174             GC gc = new GC(backingStore);
175             gc.copyArea(image, rect.x, rect.y);
176             gc.dispose();
177             ImageCanvas canvas = new ImageCanvas(theShell, SWT.BORDER
178                     | SWT.NO_BACKGROUND, image);
179             controls.add(canvas);
180
181         }
182     }
183
184 }
185
Popular Tags