KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.util.Geometry;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.PaintEvent;
21 import org.eclipse.swt.events.PaintListener;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Canvas;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29
30 /**
31  * @since 3.3
32  *
33  */

34 public class ImageAnimationFeedback extends DefaultAnimationFeedback {
35     private class ImageCanvas extends Canvas {
36         private Image image;
37         /**
38          * @param parent
39          * @param style
40          */

41         public ImageCanvas(Composite parent, int style, Image image) {
42             super(parent, style);
43             this.image = image;
44             
45             addPaintListener(new PaintListener() {
46                 public void paintControl(PaintEvent e) {
47                     paintImage(e.gc);
48                 }
49             });
50         }
51         /**
52          * @param gc
53          */

54         protected void paintImage(GC gc) {
55             gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, getBounds().width, getBounds().height);
56         }
57         
58         /* (non-Javadoc)
59          * @see org.eclipse.swt.widgets.Widget#dispose()
60          */

61         public void dispose() {
62             super.dispose();
63             image.dispose();
64         }
65     }
66     
67     private Display display;
68     private Shell theShell;
69     
70     private List JavaDoc startRects = new ArrayList JavaDoc();
71     private List JavaDoc endRects = new ArrayList JavaDoc();
72     private List JavaDoc controls = new ArrayList JavaDoc();
73     
74     private Image backingStore;
75     
76     /**
77      * Creates an animation effect where the interpolated rectangles are displayed using Canvas
78      * controls that show an image of the bits that were originally occupied by the various
79      * 'start' rectangles.
80      */

81     public ImageAnimationFeedback() {}
82
83     /**
84      * @param parentShell
85      */

86     public void initialize(Shell parentShell, Rectangle startRect, Rectangle endRect) {
87         display = parentShell.getDisplay();
88
89         Rectangle psRect = parentShell.getBounds();
90         theShell = new Shell(parentShell, SWT.NO_TRIM | SWT.ON_TOP);
91         theShell.setBounds(parentShell.getBounds());
92
93         addStartRect(startRect);
94         addEndRect(endRect);
95         
96         // Capture the background image
97
backingStore = new Image(theShell.getDisplay(), psRect);
98         GC gc = new GC(display);
99         gc.copyArea(backingStore, psRect.x, psRect.y);
100         gc.dispose();
101         
102         theShell.setBackgroundImage(backingStore);
103         theShell.setVisible(true);
104         display.update();
105     }
106     
107     public void addStartRect(Rectangle rect) {
108         if (rect != null) {
109             Rectangle start = Geometry.toControl(theShell, rect);
110             startRects.add(start);
111             
112             Image image = new Image(display, rect.width, rect.height);
113             GC gc = new GC(display);
114             gc.copyArea(image, rect.x, rect.y);
115             gc.dispose();
116             
117             ImageCanvas canvas = new ImageCanvas(theShell, SWT.BORDER | SWT.NO_BACKGROUND, image);
118             controls.add(canvas);
119         }
120     }
121     
122     public void addEndRect(Rectangle rect) {
123         if (rect != null) {
124             Rectangle end = Geometry.toControl(theShell, rect);
125             endRects.add(end);
126         }
127     }
128     
129     public void renderStep(double amount) {
130         // Move the controls to the new interpolation position
131
Iterator JavaDoc startIter = startRects.iterator();
132         Iterator JavaDoc endIter = endRects.iterator();
133         Iterator JavaDoc ctrlIter = controls.iterator();
134         while (startIter.hasNext()) {
135             Rectangle start = (Rectangle) startIter.next();
136             Rectangle end = (Rectangle) endIter.next();
137             ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
138
139             // Get the bounds of the interpolated rect
140
Rectangle curRect = RectangleAnimation.interpolate(start, end, amount);
141             canvas.setBounds(curRect);
142         }
143         
144         display.update();
145     }
146
147     public void jobInit() {
148     }
149
150     /**
151      *
152      */

153     public void dispose() {
154         backingStore.dispose();
155         for (Iterator JavaDoc ctrlIter = controls.iterator(); ctrlIter.hasNext();) {
156             ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
157             canvas.dispose();
158         }
159         
160         theShell.setVisible(false);
161         theShell.dispose();
162     }
163 }
164
Popular Tags