/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.util.ui;

import com.intellij.openapi.Disposable;

import javax.swing.*;
import java.awt.*;

public abstract class AnimatedIcon extends JComponent implements Disposable {

  private Icon[] myIcons;
  private Dimension myPrefSize = new Dimension();

  private int myCurrentIconIndex;

  private Icon myPassiveIcon;

  private Icon myEmptyPassiveIcon;
  private boolean myPaintPassive = true;

  private boolean myRunning = true;

  protected Animator myAnimator;

  private final String myName;

  private boolean myLastPaintWasRunning;
  private boolean myPaintingBgNow;

  protected AnimatedIcon(final String name) {
    myName = name;
  }

  protected final void init(Icon[] icons, Icon passiveIcon, int cycleLength, final int interCycleGap, final int maxRepeatCount) {
    myIcons = icons;
    myPassiveIcon = passiveIcon;

    myPrefSize = new Dimension();
    for (Icon each : icons) {
      myPrefSize.width = Math.max(each.getIconWidth(), myPrefSize.width);
      myPrefSize.height = Math.max(each.getIconHeight(), myPrefSize.height);
    }

    myPrefSize.width = Math.max(passiveIcon.getIconWidth(), myPrefSize.width);
    myPrefSize.height = Math.max(passiveIcon.getIconHeight(), myPrefSize.height);

    UIUtil.removeQuaquaVisualMarginsIn(this);

    myAnimator = new Animator(myName, icons.length, cycleLength, true, interCycleGap, maxRepeatCount) {
      public void paintNow(final float frame, final float totalFrames, final float cycle) {
        myCurrentIconIndex = (int)frame;
        paintImmediately(0, 0, getWidth(), getHeight());
      }

      protected void onAnimationMaxCycleReached() throws InterruptedException {
        AnimatedIcon.this.onAnimationMaxCycleReached();
      }

      public boolean isAnimated() {
        return AnimatedIcon.this.isAnimated();
      }
    };


    if (icons.length > 0) {
      myEmptyPassiveIcon = new EmptyIcon(icons[0]);
    } else {
      myEmptyPassiveIcon = new EmptyIcon(0);
    }

    setOpaque(true);
  }

  public void setPaintPassiveIcon(boolean paintPassive) {
    myPaintPassive = paintPassive;
  }

  protected void onAnimationMaxCycleReached() throws InterruptedException {

  }

  private boolean ensureAnimation(boolean running) {
    boolean changes = myAnimator.isRunning() != running;

    if (running) {
      myAnimator.resume();
    } else {
      myAnimator.suspend();
    }

    return changes;
  }

  public void addNotify() {
    super.addNotify();
    if (myRunning) {
      ensureAnimation(true);
    }
  }

  public void removeNotify() {
    super.removeNotify();
    ensureAnimation(false);
  }

  public void resume() {
    myRunning = true;
    ensureAnimation(true);
  }

  public void suspend() {
    myRunning = false;
    if (ensureAnimation(false)) {
      repaint();
    }
  }

  public void dispose() {
    myAnimator.dispose();
  }

  public Dimension getPreferredSize() {
    final Insets insets = getInsets();
    return new Dimension(myPrefSize.width + insets.left + insets.right, myPrefSize.height + insets.top + insets.bottom);
  }

  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  public Dimension getMaximumSize() {
    return getPreferredSize();
  }

  protected void paintComponent(Graphics g) {
    if (myPaintingBgNow) return;

    if ((myAnimator.isRunning() || myPaintPassive || (myLastPaintWasRunning && !myAnimator.isRunning()))) {
      Rectangle b = getBounds();

      if (!isOpaque()) {
        try {
          myPaintingBgNow = true;
          Container p = getParent();
          if (p instanceof JComponent) {
            JComponent parentComponent = (JComponent)p;
            RepaintManager.currentManager(p).addDirtyRegion(parentComponent, b.x, b.y, b.width, b.height);
          }
        }
        finally {
          myPaintingBgNow = false;
        }
      } else {
        Container parent = getParent();
        if (parent instanceof JComponent) {
          final Component opaque = UIUtil.findNearestOpaque((JComponent)parent);
          if (opaque != null) {
            g.setColor(opaque.getBackground());
            g.fillRect(b.x, b.y, b.width, b.height);
          }
        }
      }

    }

    Icon icon;

    if (myAnimator.isRunning()) {
      icon = myIcons[myCurrentIconIndex];
    } else {
      icon = getPassiveIcon();
    }

    final Dimension size = getSize();
    int x = (size.width - icon.getIconWidth()) / 2;
    int y = (size.height - icon.getIconHeight()) / 2;

    paintIcon(g, icon, x, y);

    myLastPaintWasRunning = myAnimator.isRunning();
  }

  protected void paintIcon(Graphics g, Icon icon, int x, int y) {
    icon.paintIcon(this, g, x, y);
  }

  protected Icon getPassiveIcon() {
    return myPaintPassive ? myPassiveIcon : myEmptyPassiveIcon;
  }


  public boolean isAnimated() {
    return true;
  }
}

@@ -3897,224 +3897,33 @@
 if (
-(myAnimator.isRunning() %7C%7C myPaintPassive %7C%7C (myLastPaintWasRunning && !myAnimator.isRunning()))) %7B%0A      Rectangle b = getBounds();%0A%0A      if (!isOpaque()) %7B%0A        try %7B%0A          myPaintingBgNow = true;%0A         
+isOpaque()) %7B%0A      final
  Con
@@ -3930,16 +3930,21 @@
 tainer p
+arent
  = getPa
@@ -3961,348 +3961,34 @@
     
-    if (p instanceof JComponent) %7B%0A            JComponent parentComponent = (JComponent)p;%0A            RepaintManager.currentManager(p).addDirtyRegion(parentComponent, b.x, b.y, b.width, b.height);%0A          %7D%0A        %7D%0A        finally %7B%0A          myPaintingBgNow = false;%0A        %7D%0A      %7D else %7B%0A        Container parent = getParent();%0A  
+JComponent opaque = null;%0A
     
@@ -4037,35 +4037,29 @@
     
-  final Component opaque = 
+opaque = (JComponent)
 UIUt
@@ -4106,24 +4106,35 @@
 ;%0A      
+%7D%0A
     
-if (
+  Color bg = 
 opaque !
@@ -4143,46 +4143,63 @@
 null
-) %7B%0A            g.setColor(opaque.getB
+ ? opaque.getBackground() : UIManager.getColor(%22Panel.b
 ackg
@@ -4203,18 +4203,17 @@
 ckground
-()
+%22
 );%0A     
@@ -4209,24 +4209,40 @@
 nd%22);%0A      
+g.setColor(bg);%0A
       g.fill
@@ -4250,74 +4250,46 @@
 ect(
-b.x, b.y, b.width, b.h
+0, 0, getWidth(), getH
 eight
+()
 );%0A    
-      %7D%0A        %7D%0A      %7D%0A%0A    
 %7D
+ 
 %0A%0A  
@@ -4926,11 +4926,168 @@
 rue;%0A  %7D
+%0A%0A  @Override%0A  public String toString() %7B%0A    return myName + %22 isRunning=%22 + myRunning + %22 isOpaque=%22 + isOpaque() + %22 paintPassive=%22 + myPaintPassive;%0A  %7D
 %0A%7D%0A

Revert "revert: no background progress is visible"