TreeUi: auto-expand nodes fixed and "loading" node is back again
[fedora-idea.git] / platform-api / src / com / intellij / util / ui / AnimatedIcon.java
blob74d1bf283c8e3507bc86054b0295bfa72d2843b7
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 myAnimator.resume();
101 public void addNotify() {
102 super.addNotify();
103 if (myRunning) {
104 myAnimator.resume();
108 public void removeNotify() {
109 super.removeNotify();
110 myAnimator.suspend();
113 public void suspend() {
114 if (!myRunning && !myAnimator.isRunning()) return;
116 myRunning = false;
117 myAnimator.suspend();
118 repaint();
121 public void dispose() {
122 myAnimator.dispose();
125 public Dimension getPreferredSize() {
126 final Insets insets = getInsets();
127 return new Dimension(myPrefSize.width + insets.left + insets.right, myPrefSize.height + insets.top + insets.bottom);
130 public Dimension getMinimumSize() {
131 return getPreferredSize();
134 public Dimension getMaximumSize() {
135 return getPreferredSize();
138 protected void paintComponent(Graphics g) {
139 if (isOpaque() && (myAnimator.isRunning() || myPaintPassive || (myLastPaintWasRunning && !myAnimator.isRunning()))) {
140 g.setColor(UIUtil.getBgFillColor(this));
141 g.fillRect(0, 0, getWidth(), getHeight());
144 Icon icon;
146 if (myAnimator.isRunning()) {
147 icon = myIcons[myCurrentIconIndex];
148 } else {
149 icon = getPassiveIcon();
152 final Dimension size = getSize();
153 int x = (size.width - icon.getIconWidth()) / 2;
154 int y = (size.height - icon.getIconHeight()) / 2;
156 icon.paintIcon(this, g, x, y);
158 myLastPaintWasRunning = myAnimator.isRunning();
161 protected Icon getPassiveIcon() {
162 return myPaintPassive ? myPassiveIcon : myEmptyPassiveIcon;
166 public boolean isAnimated() {
167 return true;