package org.perfcake.pc4idea.editor.gui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Stanislav Kaleta
 * Date: 28.9.2014
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractPanel extends JPanel {
    private final Project project;

    public AbstractPanel(Project project) {
        super();
        this.project = project;
        this.setOpaque(false);
        //this.setSize(new Dimension(100,100));
        //this.setVisible(true);
        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                if (evt.getButton() == MouseEvent.BUTTON1) {
                    if (evt.getClickCount() == 2) {
                        ComponentEditor editor = new ComponentEditor(getEditorTitle(), getEditorPanel());
                        editor.show();
                        if (editor.getExitCode() == 0) {
                            applyChanges();
                        }
                    }
                } /*TODO right click -> popup menu*/
            }
        });

    }

    protected abstract Color getColor();
    protected abstract String getEditorTitle();
    protected abstract JPanel getEditorPanel();
    protected abstract void applyChanges();
    public abstract void setComponent(Object component);
    public abstract Object getComponent();

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

        Graphics2D g2D = (Graphics2D) g;
        g2D.setColor(getColor());
        g2D.drawRoundRect(4, 4, this.getWidth() - 8, this.getHeight() - 8, 20, 20);
    }


    private class ComponentEditor extends DialogWrapper {
        private JPanel centerPanel;

        public ComponentEditor(String title, JPanel centerPanel){
            super(project, false); /*TODO can be parent?*/
            setTitle(title);
            this.centerPanel = centerPanel;
            this.setResizable(true);
            init();
        }
        @Nullable
        @Override
        protected JComponent createCenterPanel() {
            return centerPanel;
        }
    }
}

@@ -91,88 +91,62 @@
 ort 
-com.intellij.openapi.ui.DialogWrapper;%0Aimport org.jetbrains.annotations.Nullable
+org.perfcake.pc4idea.editor.components.ComponentEditor
 ;%0A%0Ai
@@ -340,73 +340,8 @@
 014%0A
- * To change this template use File %7C Settings %7C File Templates.%0A
  */%0A
@@ -557,89 +557,9 @@
 e);%0A
-        //this.setSize(new Dimension(100,100));%0A        //this.setVisible(true);
 %0A
+
     
@@ -1084,16 +1084,98 @@
        %7D
+%0A                if (evt.getButton() == MouseEvent.BUTTON3) %7B%0A                    
  /*TODO 
@@ -1202,16 +1202,34 @@
  menu*/%0A
+                %7D%0A
         
@@ -1703,24 +1703,24 @@
 etColor());%0A
+
         g2D.
@@ -1801,503 +1801,6 @@
   %7D%0A
-%0A%0A    private class ComponentEditor extends DialogWrapper %7B%0A        private JPanel centerPanel;%0A%0A        public ComponentEditor(String title, JPanel centerPanel)%7B%0A            super(project, false); /*TODO can be parent?*/%0A            setTitle(title);%0A            this.centerPanel = centerPanel;%0A            this.setResizable(true);%0A            init();%0A        %7D%0A        @Nullable%0A        @Override%0A        protected JComponent createCenterPanel() %7B%0A            return centerPanel;%0A        %7D%0A    %7D%0A
 %7D%0A

modif. dialog