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
;
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
) {
48 protected final void init(Icon
[] icons
, Icon passiveIcon
, int cycleLength
, final int interCycleGap
, final int maxRepeatCount
) {
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]);
82 myEmptyPassiveIcon
= new EmptyIcon(0);
88 public void setPaintPassiveIcon(boolean paintPassive
) {
89 myPaintPassive
= paintPassive
;
92 protected void onAnimationMaxCycleReached() throws InterruptedException
{
96 public void resume() {
102 public void addNotify() {
107 public void removeNotify() {
108 super.removeNotify();
112 public void suspend() {
118 private boolean _suspend() {
119 setOpaque(myPaintPassive
);
121 if (myRunning
|| myAnimator
.isRunning()) {
122 myAnimator
.suspend();
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());
155 if (myAnimator
.isRunning()) {
156 icon
= myIcons
[myCurrentIconIndex
];
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() {