001: /*******************************************************************************
002: * Copyright (c) 2006, 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.graphics.Color;
019: import org.eclipse.swt.graphics.Rectangle;
020: import org.eclipse.swt.graphics.Region;
021: import org.eclipse.swt.widgets.Display;
022: import org.eclipse.swt.widgets.Shell;
023:
024: /**
025: * @since 3.3
026: *
027: */
028: public class DefaultAnimationFeedback {
029: private static final int LINE_WIDTH = 1;
030:
031: private Display display;
032: private Shell theShell;
033: private Region shellRegion;
034:
035: private List startRects = new ArrayList();
036: private List endRects = new ArrayList();
037:
038: public DefaultAnimationFeedback() {
039: }
040:
041: /**
042: * @param parentShell
043: */
044: public void initialize(Shell parentShell, Rectangle startRect,
045: Rectangle endRect) {
046: addStartRect(startRect);
047: addEndRect(endRect);
048:
049: theShell = new Shell(parentShell, SWT.NO_TRIM | SWT.ON_TOP);
050: display = theShell.getDisplay();
051: Color color = display
052: .getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);
053: theShell.setBackground(color);
054:
055: // Ensure that the background won't show on the initial display
056: shellRegion = new Region(display);
057: theShell.setRegion(shellRegion);
058: }
059:
060: public void addStartRect(Rectangle rect) {
061: if (rect != null) {
062: startRects.add(rect);
063: }
064: }
065:
066: public void addEndRect(Rectangle rect) {
067: if (rect != null) {
068: endRects.add(rect);
069: }
070: }
071:
072: public List getStartRects() {
073: return startRects;
074: }
075:
076: public List getEndRects() {
077: return endRects;
078: }
079:
080: public void renderStep(double amount) {
081: if (shellRegion != null) {
082: shellRegion.dispose();
083: shellRegion = new Region(display);
084: }
085:
086: // Iterate across the set of start/end rects
087: Iterator startIter = startRects.iterator();
088: Iterator endIter = endRects.iterator();
089: while (startIter.hasNext()) {
090: Rectangle start = (Rectangle) startIter.next();
091: Rectangle end = (Rectangle) endIter.next();
092:
093: // Get the bounds of the interpolated rect
094: Rectangle curRect = RectangleAnimation.interpolate(start,
095: end, amount);
096:
097: Rectangle rect = Geometry.toControl(theShell, curRect);
098: shellRegion.add(rect);
099: rect.x += LINE_WIDTH;
100: rect.y += LINE_WIDTH;
101: rect.width = Math.max(0, rect.width - 2 * LINE_WIDTH);
102: rect.height = Math.max(0, rect.height - 2 * LINE_WIDTH);
103:
104: shellRegion.subtract(rect);
105: }
106:
107: theShell.setRegion(shellRegion);
108:
109: display.update();
110: }
111:
112: /**
113: *
114: */
115: public void dispose() {
116: theShell.setVisible(false);
117: theShell.dispose();
118: shellRegion.dispose();
119: }
120:
121: /**
122: * Perform any initialization you want to have happen -before- the
123: * amination starts
124: */
125: public void jobInit() {
126: // Compute the shell's bounds
127: Rectangle shellBounds = Geometry.copy((Rectangle) startRects
128: .get(0));
129: Iterator startIter = startRects.iterator();
130: Iterator endIter = endRects.iterator();
131: while (startIter.hasNext()) {
132: shellBounds.add((Rectangle) startIter.next());
133: shellBounds.add((Rectangle) endIter.next());
134: }
135: theShell.setBounds(shellBounds);
136:
137: // Making the shell visible will be slow on old video cards, so only start
138: // the timer once it is visible.
139: theShell.setVisible(true);
140: }
141: }
|