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 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: import org.eclipse.ui.internal.AnimationEngine;
028:
029: /**
030: * Creates an animation effect where the interpolated rectangles are displayed using Canvas
031: * controls that show an image of the bits that were originally occupied by the various
032: * 'start' rectangles.
033: *
034: * @since 3.3
035: *
036: */
037: public class RectangleAnimationImageFeedback extends
038: RectangleAnimationFeedbackBase {
039: private class ImageCanvas extends Canvas {
040: private Image image;
041:
042: /**
043: * @param parent
044: * @param style
045: */
046: public ImageCanvas(Composite parent, int style, Image image) {
047: super (parent, style);
048: this .image = image;
049:
050: addPaintListener(new PaintListener() {
051: public void paintControl(PaintEvent e) {
052: paintImage(e.gc);
053: }
054: });
055: }
056:
057: /**
058: * @param gc
059: */
060: protected void paintImage(GC gc) {
061: gc.drawImage(image, 0, 0, image.getBounds().width, image
062: .getBounds().height, 0, 0, getBounds().width,
063: getBounds().height);
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.swt.widgets.Widget#dispose()
068: */
069: public void dispose() {
070: super .dispose();
071: image.dispose();
072: }
073: }
074:
075: private Image backingStore;
076: private Shell theShell;
077: private Display display;
078: private List controls = new ArrayList();
079:
080: public RectangleAnimationImageFeedback(Shell parentShell,
081: Rectangle start, Rectangle end) {
082: super (parentShell, start, end);
083: }
084:
085: public void dispose() {
086: backingStore.dispose();
087: for (Iterator ctrlIter = controls.iterator(); ctrlIter
088: .hasNext();) {
089: ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
090: canvas.dispose();
091: }
092:
093: theShell.setVisible(false);
094: theShell.dispose();
095: }
096:
097: public void initialize(AnimationEngine engine) {
098: display = getAnimationShell().getDisplay();
099:
100: Rectangle psRect = getAnimationShell().getBounds();
101: theShell = new Shell(getAnimationShell(), SWT.NO_TRIM
102: | 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 ctrlIter = controls.iterator();
145: Iterator currentRects = getCurrentRects(engine.amount())
146: .iterator();
147: while (currentRects.hasNext()) {
148: ImageCanvas canvas = (ImageCanvas) ctrlIter.next();
149: canvas.setBounds((Rectangle) currentRects.next());
150: }
151: display.update();
152:
153: }
154:
155: public void changeCoordinates() {
156: Iterator startRectIter = getStartRects().iterator();
157: Iterator endRectIter = getEndRects().iterator();
158: while (startRectIter.hasNext()) {
159: Rectangle startRect = (Rectangle) startRectIter.next();
160: Rectangle mapStartRect = Geometry.toControl(theShell,
161: startRect);
162: startRect.x = mapStartRect.x;
163: startRect.y = mapStartRect.y;
164: Rectangle endRect = (Rectangle) endRectIter.next();
165: Rectangle mapEndRect = Geometry
166: .toControl(theShell, endRect);
167: endRect.x = mapEndRect.x;
168: endRect.y = mapEndRect.y;
169: }
170: }
171:
172: private void captureImages() {
173:
174: for (Iterator iterator = getStartRects().iterator(); iterator
175: .hasNext();) {
176: Rectangle rect = (Rectangle) iterator.next();
177: Image image = new Image(display, rect.width, rect.height);
178: GC gc = new GC(backingStore);
179: gc.copyArea(image, rect.x, rect.y);
180: gc.dispose();
181: ImageCanvas canvas = new ImageCanvas(theShell, SWT.BORDER
182: | SWT.NO_BACKGROUND, image);
183: controls.add(canvas);
184:
185: }
186: }
187:
188: }
|