KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.swt.SWTException;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.ImageData;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.internal.misc.StatusUtil;
23 import org.eclipse.ui.statushandlers.StatusManager;
24
25 /**
26  * Base class for Cyclic animations.
27  *
28  * @since 3.3
29  *
30  */

31 public abstract class ImageCycleFeedbackBase extends AnimationFeedbackBase {
32     protected Image[] images;
33     protected Image stoppedImage;
34     private Image offScreenImage;
35     private GC offScreenImageGC;
36     private int imageDataIndex;
37     private Image image;
38     private ImageData imageData;
39     protected Display display;
40     protected Color background;
41
42     /**
43      * @param parentShell
44      */

45     public ImageCycleFeedbackBase(Shell parentShell) {
46         super(parentShell);
47         // TODO Auto-generated constructor stub
48
}
49
50     /**
51      *
52      * @param parentShell
53      * @param images :
54      * an array of images
55      */

56     public ImageCycleFeedbackBase(Shell parentShell, Image[] images) {
57         super(parentShell);
58         this.images = images;
59     }
60
61     /**
62      * Set the image during progress without caching.
63      *
64      * @param image
65      */

66     public abstract void showImage(Image image);
67
68     /**
69      * Save initial Image which would be stoppedImage
70      *
71      */

72     public abstract void saveStoppedImage();
73
74     /**
75      * Set the stopped Image upon animation completion
76      *
77      * @param image
78      */

79     public abstract void setStoppedImage(Image image);
80
81     public void dispose() {
82         // TODO Auto-generated method stub
83
if (stoppedImage == null || stoppedImage.isDisposed())
84             return;
85         setStoppedImage(stoppedImage);
86
87         if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
88             offScreenImageGC.dispose();
89
90         if (offScreenImage != null && !offScreenImage.isDisposed())
91             offScreenImage.dispose();
92     }
93
94     public boolean jobInit(AnimationEngine engine) {
95         return super.jobInit(engine);
96     }
97
98     public void renderStep(AnimationEngine engine) {
99         // TODO Auto-generated method stub
100
if (offScreenImage == null) {
101             offScreenImage = getOffscreenImage();
102         }
103
104         try {
105             imageDataIndex = (imageDataIndex + 1) % images.length;
106             image = images[imageDataIndex];
107             imageData = image.getImageData();
108
109             offScreenImageGC.drawImage(image, 0, 0, imageData.width,
110                     imageData.height, imageData.x, imageData.y,
111                     imageData.width, imageData.height);
112
113             final Image finalImage = image;
114
115             display.syncExec(new Runnable JavaDoc() {
116                 /*
117                  * (non-Javadoc)
118                  *
119                  * @see java.lang.Runnable#run()
120                  */

121                 public void run() {
122                     showImage(finalImage);
123
124                 }
125             });
126
127             /*
128              * Sleep for the specified delay time (adding commonly-used
129              * slow-down fudge factors).
130              */

131             // try {
132
// Thread.sleep(30);
133
// } catch (InterruptedException e) {
134
// }
135
if (images == null)
136                 return;
137         } catch (SWTException ex) {
138             IStatus status = StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH,
139                     ex);
140             StatusManager.getManager().handle(status);
141         }
142     }
143
144     private Image getOffscreenImage() {
145         saveStoppedImage();
146         imageDataIndex = 0;
147         image = images[imageDataIndex];
148         imageData = image.getImageData();
149         /*
150          * Create an off-screen image to draw on, and fill it with the shell
151          * background.
152          */

153         offScreenImage = new Image(display, imageData.width, imageData.height);
154
155         offScreenImageGC = new GC(offScreenImage);
156         offScreenImageGC.setBackground(background);
157         offScreenImageGC.fillRectangle(0, 0, imageData.width, imageData.height);
158
159         /*
160          * Create the first image and draw it on the off-screen image.
161          */

162
163         offScreenImageGC.drawImage(image, 0, 0, imageData.width,
164                 imageData.height, imageData.x, imageData.y, imageData.width,
165                 imageData.height);
166
167         return offScreenImage;
168     }
169
170 }
171
Popular Tags