async process icon - nedofiksel
[fedora-idea.git] / platform-api / src / com / intellij / util / ui / AnimatedIcon.java
blobb24c734537973f5723f4fed7393787c840884731
1 /*
2 * Copyright 2000-2007 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.util.ui;
19 import com.intellij.openapi.Disposable;
21 import javax.swing.*;
22 import java.awt.*;
24 public abstract class AnimatedIcon extends JComponent implements Disposable {
26 private Icon[] myIcons;
27 private Dimension myPrefSize = new Dimension();
29 private int myCurrentIconIndex;
31 private Icon myPassiveIcon;
33 private Icon myEmptyPassiveIcon;
34 private boolean myPaintPassive = true;
36 private boolean myRunning = true;
38 protected Animator myAnimator;
40 private final String myName;
42 private boolean myLastPaintWasRunning;
44 protected AnimatedIcon(final String name) {
45 myName = name;
48 protected final void init(Icon[] icons, Icon passiveIcon, int cycleLength, final int interCycleGap, final int maxRepeatCount) {
49 myIcons = icons;
50 myPassiveIcon = passiveIcon;
52 myPrefSize = new Dimension();
53 for (Icon each : icons) {
54 myPrefSize.width = Math.max(each.getIconWidth(), myPrefSize.width);
55 myPrefSize.height = Math.max(each.getIconHeight(), myPrefSize.height);
58 myPrefSize.width = Math.max(passiveIcon.getIconWidth(), myPrefSize.width);
59 myPrefSize.height = Math.max(passiveIcon.getIconHeight(), myPrefSize.height);
61 UIUtil.removeQuaquaVisualMarginsIn(this);
63 myAnimator = new Animator(myName, icons.length, cycleLength, true, interCycleGap, maxRepeatCount) {
64 public void paintNow(final float frame, final float totalFrames, final float cycle) {
65 myCurrentIconIndex = (int)frame;
66 paintImmediately(0, 0, getWidth(), getHeight());
69 protected void onAnimationMaxCycleReached() throws InterruptedException {
70 AnimatedIcon.this.onAnimationMaxCycleReached();
73 public boolean isAnimated() {
74 return AnimatedIcon.this.isAnimated();
79 if (icons.length > 0) {
80 myEmptyPassiveIcon = new EmptyIcon(icons[0]);
81 } else {
82 myEmptyPassiveIcon = new EmptyIcon(0);
85 setOpaque(true);
88 public void setPaintPassiveIcon(boolean paintPassive) {
89 myPaintPassive = paintPassive;
92 protected void onAnimationMaxCycleReached() throws InterruptedException {
96 private boolean ensureAnimation(boolean running) {
97 boolean changes = myAnimator.isRunning() != running;
99 if (running) {
100 setOpaque(true);
101 myAnimator.resume();
102 } else {
103 setOpaque(myPaintPassive);
104 myAnimator.suspend();
107 return changes;
110 public void addNotify() {
111 super.addNotify();
112 if (myRunning) {
113 ensureAnimation(true);
117 public void removeNotify() {
118 super.removeNotify();
119 ensureAnimation(false);
122 public void resume() {
123 myRunning = true;
124 ensureAnimation(true);
127 public void suspend() {
128 myRunning = false;
129 if (ensureAnimation(false)) {
130 repaint();
134 public void dispose() {
135 myAnimator.dispose();
138 public Dimension getPreferredSize() {
139 final Insets insets = getInsets();
140 return new Dimension(myPrefSize.width + insets.left + insets.right, myPrefSize.height + insets.top + insets.bottom);
143 public Dimension getMinimumSize() {
144 return getPreferredSize();
147 public Dimension getMaximumSize() {
148 return getPreferredSize();
151 protected void paintComponent(Graphics g) {
152 if (isOpaque() && (myAnimator.isRunning() || myPaintPassive || (myLastPaintWasRunning && !myAnimator.isRunning()))) {
153 g.setColor(UIUtil.getBgFillColor(this));
154 g.fillRect(0, 0, getWidth(), getHeight());
157 Icon icon;
159 if (myAnimator.isRunning()) {
160 icon = myIcons[myCurrentIconIndex];
161 } else {
162 icon = getPassiveIcon();
165 final Dimension size = getSize();
166 int x = (size.width - icon.getIconWidth()) / 2;
167 int y = (size.height - icon.getIconHeight()) / 2;
169 icon.paintIcon(this, g, x, y);
171 myLastPaintWasRunning = myAnimator.isRunning();
174 protected Icon getPassiveIcon() {
175 return myPaintPassive ? myPassiveIcon : myEmptyPassiveIcon;
179 public boolean isAnimated() {
180 return true;