001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.swt.SWTException;
014: import org.eclipse.swt.graphics.Color;
015: import org.eclipse.swt.graphics.GC;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.graphics.ImageData;
018: import org.eclipse.swt.widgets.Display;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.internal.misc.StatusUtil;
021: import org.eclipse.ui.statushandlers.StatusManager;
022:
023: /**
024: * Base class for Cyclic animations.
025: *
026: * @since 3.3
027: *
028: */
029: public abstract class ImageCycleFeedbackBase extends
030: AnimationFeedbackBase {
031: protected Image[] images;
032: protected Image stoppedImage;
033: private Image offScreenImage;
034: private GC offScreenImageGC;
035: private int imageDataIndex;
036: private Image image;
037: private ImageData imageData;
038: protected Display display;
039: protected Color background;
040:
041: /**
042: * @param parentShell
043: */
044: public ImageCycleFeedbackBase(Shell parentShell) {
045: super (parentShell);
046: // TODO Auto-generated constructor stub
047: }
048:
049: /**
050: *
051: * @param parentShell
052: * @param images :
053: * an array of images
054: */
055: public ImageCycleFeedbackBase(Shell parentShell, Image[] images) {
056: super (parentShell);
057: this .images = images;
058: }
059:
060: /**
061: * Set the image during progress without caching.
062: *
063: * @param image
064: */
065: public abstract void showImage(Image image);
066:
067: /**
068: * Save initial Image which would be stoppedImage
069: *
070: */
071: public abstract void saveStoppedImage();
072:
073: /**
074: * Set the stopped Image upon animation completion
075: *
076: * @param image
077: */
078: public abstract void setStoppedImage(Image image);
079:
080: public void dispose() {
081: // TODO Auto-generated method stub
082: if (stoppedImage == null || stoppedImage.isDisposed())
083: return;
084: setStoppedImage(stoppedImage);
085:
086: if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
087: offScreenImageGC.dispose();
088:
089: if (offScreenImage != null && !offScreenImage.isDisposed())
090: offScreenImage.dispose();
091: }
092:
093: public boolean jobInit(AnimationEngine engine) {
094: return super .jobInit(engine);
095: }
096:
097: public void renderStep(AnimationEngine engine) {
098: // TODO Auto-generated method stub
099: if (offScreenImage == null) {
100: offScreenImage = getOffscreenImage();
101: }
102:
103: try {
104: imageDataIndex = (imageDataIndex + 1) % images.length;
105: image = images[imageDataIndex];
106: imageData = image.getImageData();
107:
108: offScreenImageGC.drawImage(image, 0, 0, imageData.width,
109: imageData.height, imageData.x, imageData.y,
110: imageData.width, imageData.height);
111:
112: final Image finalImage = image;
113:
114: display.syncExec(new Runnable() {
115: /*
116: * (non-Javadoc)
117: *
118: * @see java.lang.Runnable#run()
119: */
120: public void run() {
121: showImage(finalImage);
122:
123: }
124: });
125:
126: /*
127: * Sleep for the specified delay time (adding commonly-used
128: * slow-down fudge factors).
129: */
130: // try {
131: // Thread.sleep(30);
132: // } catch (InterruptedException e) {
133: // }
134: if (images == null)
135: return;
136: } catch (SWTException ex) {
137: IStatus status = StatusUtil.newStatus(
138: WorkbenchPlugin.PI_WORKBENCH, ex);
139: StatusManager.getManager().handle(status);
140: }
141: }
142:
143: private Image getOffscreenImage() {
144: saveStoppedImage();
145: imageDataIndex = 0;
146: image = images[imageDataIndex];
147: imageData = image.getImageData();
148: /*
149: * Create an off-screen image to draw on, and fill it with the shell
150: * background.
151: */
152: offScreenImage = new Image(display, imageData.width,
153: imageData.height);
154:
155: offScreenImageGC = new GC(offScreenImage);
156: offScreenImageGC.setBackground(background);
157: offScreenImageGC.fillRectangle(0, 0, imageData.width,
158: imageData.height);
159:
160: /*
161: * Create the first image and draw it on the off-screen image.
162: */
163:
164: offScreenImageGC.drawImage(image, 0, 0, imageData.width,
165: imageData.height, imageData.x, imageData.y,
166: imageData.width, imageData.height);
167:
168: return offScreenImage;
169: }
170:
171: }
|