Tree: progress icon artfact fixed
[fedora-idea.git] / platform-api / src / com / intellij / util / ui / AnimatedIcon.java
bloba1af11134bef66ba490b11a8f621d6d765339d98
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 public void resume() {
97 myRunning = true;
98 setOpaque(true);
99 myAnimator.resume();
102 public void addNotify() {
103 super.addNotify();
104 resume();
107 public void removeNotify() {
108 super.removeNotify();
109 _suspend();
112 public void suspend() {
113 if (_suspend()) {
114 repaint();
118 private boolean _suspend() {
119 setOpaque(myPaintPassive);
121 if (myRunning || myAnimator.isRunning()) {
122 myAnimator.suspend();
123 myRunning = false;
124 return true;
125 } else {
126 return false;
130 public void dispose() {
131 myAnimator.dispose();
134 public Dimension getPreferredSize() {
135 final Insets insets = getInsets();
136 return new Dimension(myPrefSize.width + insets.left + insets.right, myPrefSize.height + insets.top + insets.bottom);
139 public Dimension getMinimumSize() {
140 return getPreferredSize();
143 public Dimension getMaximumSize() {
144 return getPreferredSize();
147 protected void paintComponent(Graphics g) {
148 if (isOpaque() && (myAnimator.isRunning() || myPaintPassive || (myLastPaintWasRunning && !myAnimator.isRunning()))) {
149 g.setColor(UIUtil.getBgFillColor(this));
150 g.fillRect(0, 0, getWidth(), getHeight());
153 Icon icon;
155 if (myAnimator.isRunning()) {
156 icon = myIcons[myCurrentIconIndex];
157 } else {
158 icon = getPassiveIcon();
161 final Dimension size = getSize();
162 int x = (size.width - icon.getIconWidth()) / 2;
163 int y = (size.height - icon.getIconHeight()) / 2;
165 icon.paintIcon(this, g, x, y);
167 myLastPaintWasRunning = myAnimator.isRunning();
170 protected Icon getPassiveIcon() {
171 return myPaintPassive ? myPassiveIcon : myEmptyPassiveIcon;
175 public boolean isAnimated() {
176 return true;