001: /*******************************************************************************
002: * Copyright (c) 2006 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 java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.util.Geometry;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.PaintEvent;
019: import org.eclipse.swt.events.PaintListener;
020: import org.eclipse.swt.graphics.GC;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.swt.graphics.Rectangle;
023: import org.eclipse.swt.widgets.Canvas;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Display;
026: import org.eclipse.swt.widgets.Shell;
027:
028: /**
029: * @since 3.3
030: *
031: */
032: public class ImageAnimationFeedback extends DefaultAnimationFeedback {
033: private class ImageCanvas extends Canvas {
034: private Image image;
035:
036: /**
037: * @param parent
038: * @param style
039: */
040: public ImageCanvas(Composite parent, int style, Image image) {
041: super (parent, style);
042: this .image = image;
043:
044: addPaintListener(new PaintListener() {
045: public void paintControl(PaintEvent e) {
046: paintImage(e.gc);
047: }
048: });
049: }
050:
051: /**
052: * @param gc
053: */
054: protected void paintImage(GC gc) {
055: gc.drawImage(image, 0, 0, image.getBounds().width, image
056: .getBounds().height, 0, 0, getBounds().width,
057: getBounds().height);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.swt.widgets.Widget#dispose()
062: */
063: public void dispose() {
064: super .dispose();
065: image.dispose();
066: }
067: }
068:
069: private Display display;
070: private Shell theShell;
071:
072: private List startRects = new ArrayList();
073: private List endRects = new ArrayList();
074: private List controls = new ArrayList();
075:
076: private Image backingStore;
077:
078: /**
079: * Creates an animation effect where the interpolated rectangles are displayed using Canvas
080: * controls that show an image of the bits that were originally occupied by the various
081: * 'start' rectangles.
082: */
083: public ImageAnimationFeedback() {
084: }
085:
086: /**
087: * @param parentShell
088: */
089: public void initialize(Shell parentShell, Rectangle startRect,
090: Rectangle endRect) {
091: display = parentShell.getDisplay();
092:
093: Rectangle psRect = parentShell.getBounds();
094: theShell = new Shell(parentShell, SWT.NO_TRIM | SWT.ON_TOP);
095: theShell.setBounds(parentShell.getBounds());
096:
097: addStartRect(startRect);
098: addEndRect(endRect);
099:
100: // Capture the background image
101: backingStore = new Image(theShell.getDisplay(), psRect);
102: GC gc = new GC(display);
103: gc.copyArea(backingStore, psRect.x, psRect.y);
104: gc.dispose();
105:
106: theShell.setBackgroundImage(backingStore);
107: theShell.setVisible(true);
108: display.update();
109: }
110:
111: public void addStartRect(Rectangle rect) {
112: if (rect != null) {
113: Rectangle start = Geometry.toControl(theShell, rect);
114: startRects.add(start);
115:
116: Image image = new Image(display, rect.width, rect.height);
117: GC gc = new GC(display);
118: gc.copyArea(image, rect.x, rect.y);
119: gc.dispose();
120:
121: ImageCanvas canvas = new ImageCanvas(theShell, SWT.BORDER
122: | SWT.NO_BACKGROUND, image);
123: controls.add(canvas);
124: }
125: }
126:
127: public void addEndRect(Rectangle rect) {
128: if (rect != null) {
129: Rectangle end = Geometry.toControl(theShell, rect);
130: endRects.add(end);
131: }
132: }
133:
134: public void renderStep(double amount) {
135: // Move the controls to the new interpolation position
136: Iterator startIter = startRects.iterator();
137: Iterator endIter = endRects.iterator();
138: Iterator ctrlIter = controls.iterator();
139: while (startIter.hasNext()) {
140: Rectangle start = (Rectangle) startIter.next();
141: Rectangle end = (Rectangle) endIter.next();
142: ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
143:
144: // Get the bounds of the interpolated rect
145: Rectangle curRect = RectangleAnimation.interpolate(start,
146: end, amount);
147: canvas.setBounds(curRect);
148: }
149:
150: display.update();
151: }
152:
153: public void jobInit() {
154: }
155:
156: /**
157: *
158: */
159: public void dispose() {
160: backingStore.dispose();
161: for (Iterator ctrlIter = controls.iterator(); ctrlIter
162: .hasNext();) {
163: ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
164: canvas.dispose();
165: }
166:
167: theShell.setVisible(false);
168: theShell.dispose();
169: }
170: }
|