package com.bmesta.intellij;

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

import com.intellij.openapi.editor.Editor;
import com.intellij.ui.JBColor;

/**
 * @author Baptiste Mesta
 */
public class ParticleContainer extends JComponent {


    public static final int SIZE = 100;
    private final JComponent parent;
    private boolean dir;

    public ParticleContainer(Editor editor) {
        parent = editor.getContentComponent();
        parent.add(this);
        setVisible(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                    updateParticles();

                    try {
                        Thread.sleep(1000 / 60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        }).start();
    }

    private void shakeEditor(JComponent parent, int dx, int dy, boolean dir) {
        final Rectangle bounds = parent.getBounds();
        parent.setBounds(bounds.x + (dir ? -dx : dx), bounds.y + (dir ? -dy : dy), bounds.width, bounds.height);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        renderParticles(g);
    }

    private void updateParticles() {
        for (int i = 0; i <= particles.size() - 1; i++) {
            if (particles.get(i).update())
                particles.remove(i);
        }
        this.repaint();

    }

    private ArrayList<Particle> particles = new ArrayList<Particle>(500);

    public void addParticle(int x, int y) {
        int dx, dy;
        dx = (int) (Math.random() * 3 + 1) * (Math.random()>0.5?-1:1) ;
        dy = (int) (Math.random() * -3 + 1);

        int size = (int) (Math.random() * 4);
        int life = (int) (Math.random() * (120)) + 380;
        final Particle e = new Particle(x, y, dx, dy, size, life, JBColor.darkGray);
        particles.add(e);
    }

    public void renderParticles(Graphics g2d) {
        for (int i = 0; i <= particles.size() - 1; i++) {
            particles.get(i).render(g2d);
        }
    }


    public void update(Point point) {
        final int midX = SIZE / 2;
        final int midY = SIZE / 2;
        this.setBounds(point.x - midX, point.y - midY, SIZE, SIZE);
        addParticle(midX, midY);
        addParticle(midX, midY);
        addParticle(midX, midY);
        addParticle(midX, midY);
        addParticle(midX, midY);
        addParticle(midX, midY);
        shakeEditor(parent, 5, 5, dir);
        dir = !dir;
        this.repaint();
    }
}

@@ -42,16 +42,95 @@
 .awt.*;%0A
+import java.awt.event.ComponentEvent;%0Aimport java.awt.event.ComponentListener;%0A
 import j
@@ -340,48 +340,8 @@
 %7B%0A%0A%0A
-    public static final int SIZE = 100;%0A
     
@@ -530,23 +530,759 @@
     
-setVisible(true
+this.setBounds(parent.getBounds());%0A        this.setBorder(BorderFactory.createLineBorder(JBColor.RED));%0A        setVisible(true);%0A        parent.addComponentListener(new ComponentListener() %7B%0A            @Override%0A            public void componentResized(ComponentEvent e) %7B%0A                ParticleContainer.this.setBounds(parent.getBounds());%0A            %7D%0A%0A            @Override%0A            public void componentMoved(ComponentEvent e) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void componentShown(ComponentEvent e) %7B%0A                System.out.println(%22shown%22);%0A%0A            %7D%0A%0A            @Override%0A            public void componentHidden(ComponentEvent e) %7B%0A                System.out.println(%22hidden%22);%0A            %7D%0A        %7D
 );%0A 
@@ -2474,21 +2474,18 @@
 dom() * 
-3 + 1
+4 
 ) * (Mat
@@ -2546,17 +2546,17 @@
 () * -3 
-+
+-
  1);%0A%0A  
@@ -2595,17 +2595,20 @@
 dom() * 
-4
+3 +1
 );%0A     
@@ -2625,43 +2625,10 @@
 e = 
-(int) (Math.random() * (120)) + 380
+15
 ;%0A  
@@ -2947,32 +2947,34 @@
 oint) %7B%0A        
+//
 final int midX =
@@ -2988,24 +2988,26 @@
  2;%0A        
+//
 final int mi
@@ -3021,32 +3021,34 @@
 ZE / 2;%0A        
+//
 this.setBounds(p
@@ -3107,34 +3107,40 @@
 addParticle(
-midX, midY
+point.x, point.y
 );%0A        a
@@ -3146,34 +3146,79 @@
 addParticle(
-midX, midY
+point.x, point.y);%0A        addParticle(point.x, point.y
 );%0A        a
@@ -3224,34 +3224,40 @@
 addParticle(
-midX, midY
+point.x, point.y
 );%0A        a
@@ -3263,34 +3263,40 @@
 addParticle(
-midX, midY
+point.x, point.y
 );%0A        a
@@ -3302,34 +3302,40 @@
 addParticle(
-midX, midY
+point.x, point.y
 );%0A        a
@@ -3349,18 +3349,24 @@
 cle(
-midX, midY
+point.x, point.y
 );%0A 

change particle area and do not move container