| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004, 2006 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 | package org.eclipse.ui.internal.progress; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.core.runtime.Assert; |
| 18 | import org.eclipse.core.runtime.IProgressMonitor; |
| 19 | import org.eclipse.core.runtime.jobs.Job; |
| 20 | import org.eclipse.ui.PlatformUI; |
| 21 | |
| 22 | /** |
| 23 | * The ProgressAnimationProcessor is the processor for the animation using the |
| 24 | * system progress. |
| 25 | */ |
| 26 | class ProgressAnimationProcessor implements IAnimationProcessor { |
| 27 | |
| 28 | AnimationManager manager; |
| 29 | |
| 30 | /** |
| 31 | * Create a new instance of the receiver and listen to the animation |
| 32 | * manager. |
| 33 | * |
| 34 | * @param animationManager |
| 35 | */ |
| 36 | ProgressAnimationProcessor(AnimationManager animationManager) { |
| 37 | manager = animationManager; |
| 38 | } |
| 39 | |
| 40 | List items = Collections.synchronizedList(new ArrayList()); |
| 41 | |
| 42 | /* |
| 43 | * (non-Javadoc) |
| 44 | * |
| 45 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#startAnimation(org.eclipse.core.runtime.IProgressMonitor) |
| 46 | */ |
| 47 | public void startAnimationLoop(IProgressMonitor monitor) { |
| 48 | |
| 49 | // Create an off-screen image to draw on, and a GC to draw with. |
| 50 | // Both are disposed after the animation. |
| 51 | if (items.size() == 0) { |
| 52 | return; |
| 53 | } |
| 54 | if (!PlatformUI.isWorkbenchRunning()) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | while (manager.isAnimated() && !monitor.isCanceled()) { |
| 59 | //Do nothing while animation is happening |
| 60 | } |
| 61 | |
| 62 | ProgressAnimationItem[] animationItems = getAnimationItems(); |
| 63 | for (int i = 0; i < animationItems.length; i++) { |
| 64 | animationItems[i].animationDone(); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * (non-Javadoc) |
| 71 | * |
| 72 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#addItem(org.eclipse.ui.internal.progress.AnimationItem) |
| 73 | */ |
| 74 | public void addItem(AnimationItem item) { |
| 75 | Assert.isTrue(item instanceof ProgressAnimationItem); |
| 76 | items.add(item); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * (non-Javadoc) |
| 81 | * |
| 82 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#removeItem(org.eclipse.ui.internal.progress.AnimationItem) |
| 83 | */ |
| 84 | public void removeItem(AnimationItem item) { |
| 85 | Assert.isTrue(item instanceof ProgressAnimationItem); |
| 86 | items.remove(item); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * (non-Javadoc) |
| 91 | * |
| 92 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#hasItems() |
| 93 | */ |
| 94 | public boolean hasItems() { |
| 95 | return items.size() > 0; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * (non-Javadoc) |
| 100 | * |
| 101 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#itemsInactiveRedraw() |
| 102 | */ |
| 103 | public void itemsInactiveRedraw() { |
| 104 | //Nothing to do here as SWT handles redraw |
| 105 | |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * (non-Javadoc) |
| 110 | * |
| 111 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#animationStarted(org.eclipse.core.runtime.IProgressMonitor) |
| 112 | */ |
| 113 | public void animationStarted() { |
| 114 | AnimationItem[] animationItems = getAnimationItems(); |
| 115 | for (int i = 0; i < animationItems.length; i++) { |
| 116 | animationItems[i].animationStart(); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * (non-Javadoc) |
| 123 | * |
| 124 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#getPreferredWidth() |
| 125 | */ |
| 126 | public int getPreferredWidth() { |
| 127 | return 30; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the animation items currently registered for the receiver. |
| 132 | * |
| 133 | * @return ProgressAnimationItem[] |
| 134 | */ |
| 135 | private ProgressAnimationItem[] getAnimationItems() { |
| 136 | ProgressAnimationItem[] animationItems = new ProgressAnimationItem[items |
| 137 | .size()]; |
| 138 | items.toArray(animationItems); |
| 139 | return animationItems; |
| 140 | } |
| 141 | |
| 142 | /* (non-Javadoc) |
| 143 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#animationFinished() |
| 144 | */ |
| 145 | public void animationFinished() { |
| 146 | AnimationItem[] animationItems = getAnimationItems(); |
| 147 | for (int i = 0; i < animationItems.length; i++) { |
| 148 | animationItems[i].animationDone(); |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | /* (non-Javadoc) |
| 154 | * @see org.eclipse.ui.internal.progress.IAnimationProcessor#isProcessorJob(org.eclipse.core.runtime.jobs.Job) |
| 155 | */ |
| 156 | public boolean isProcessorJob(Job job) { |
| 157 | // We have no jobs |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | } |