package org.perfcake.pc4idea.editor.gui;

import com.intellij.openapi.project.Project;
import org.perfcake.model.Scenario;
import org.perfcake.pc4idea.editor.components.EnabledIndicator;
import org.perfcake.pc4idea.editor.wizard.DestinationEditor;

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

/**
 * Created with IntelliJ IDEA.
 * User: Stanislav Kaleta
 * Date: 15.10.2014
 */
public class DestinationComponent extends AbstractPanel {
    private final String TITLE ="Destination Editor";
    private final Color componentColor;

    private JPanel panelEditor;
    private Scenario.Reporting.Reporter.Destination destination;

    private JLabel destinationAttr;
    private EnabledIndicator destinationEnabled;

    public DestinationComponent(Project project, Color componentColor){
        super(project);
        this.componentColor = componentColor;

        initComponents();
    }

    private void initComponents(){
        destinationAttr = new JLabel("DestinationClass");
        destinationAttr.setFont(new Font(destinationAttr.getFont().getName(), 0, 15));
        destinationAttr.setForeground(componentColor);
        destinationEnabled = new EnabledIndicator(componentColor);

        SpringLayout layout = new SpringLayout();
        this.setLayout(layout);
        this.add(destinationAttr);
        this.add(destinationEnabled);

        layout.putConstraint(SpringLayout.NORTH, destinationEnabled,
                10,
                SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, destinationEnabled,
                10,
                SpringLayout.WEST, this);

        layout.putConstraint(SpringLayout.NORTH, destinationAttr,
                10,
                SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST,destinationAttr,
                5,
                SpringLayout.EAST,destinationEnabled);
    }
    private void setComponentSize(){
        FontMetrics fontMetrics = destinationAttr.getFontMetrics(destinationAttr.getFont());
        int width = fontMetrics.stringWidth(destinationAttr.getText()) + 25 + 20;

        this.setMinimumSize(new Dimension(width,40));
        this.setPreferredSize(new Dimension(width,40));
        this.setMaximumSize(new Dimension(width,40));
    }

    @Override
    protected Color getColor() {
        return componentColor;
    }

    @Override
    protected String getEditorTitle() {
        return TITLE;
    }

    @Override
    protected JPanel getEditorPanel() {
        panelEditor = new DestinationEditor();
        panelEditor.setDestination(destination);
        return panelEditor;
    }

    @Override
    protected void applyChanges(){
        this.setComponent(panelEditor.getDestination());
    }

    @Override
    public void setComponent(Object component) {
        destination = (Scenario.Reporting.Reporter.Destination)component;
        destinationAttr.setText(destination.getClazz());
        destinationEnabled.setState(destination.isEnabled());

        setComponentSize();
    }

    @Override
    public Object getComponent() {
        /*TODO maybe not needed*/
        return destination;
    }

}

@@ -183,69 +183,8 @@
 tor;
-%0Aimport org.perfcake.pc4idea.editor.wizard.DestinationEditor;
 %0A%0Aim
@@ -420,16 +420,51 @@
 ditor%22;%0A
+    private final int HEIGHT = 40;%0A
     priv
@@ -490,24 +490,59 @@
 ponentColor;
+%0A    private final Project project;
 %0A%0A    privat
@@ -629,16 +629,39 @@
 ation;%0A%0A
+    private int width;%0A
     priv
@@ -830,24 +830,56 @@
 r(project);%0A
+        this.project = project;%0A
         this
@@ -929,60 +929,487 @@
 init
-Components();%0A    %7D%0A%0A    private void initComponents
+();%0A%0A        panelEditor = initPanelEditor();%0A    %7D%0A%0A    @Override%0A    protected void paintComponent(Graphics g) %7B%0A        super.paintComponent(g);%0A        g.setFont(destinationAttr.getFont());%0A        width = g.getFontMetrics().stringWidth(destinationAttr.getText()) + 25 + 20;%0A        setComponentSize();%0A%0A        Graphics2D g2D = (Graphics2D) g;%0A        g2D.setColor(componentColor);%0A        g2D.drawRoundRect(4, 4, width - 8, HEIGHT - 8, 20, 20);%0A    %7D%0A%0A    private void init
 ()%7B%0A
@@ -2362,16 +2362,18 @@
 abled);%0A
+%0A%0A
     %7D%0A  
@@ -2411,184 +2411,8 @@
 ()%7B%0A
-        FontMetrics fontMetrics = destinationAttr.getFontMetrics(destinationAttr.getFont());%0A        int width = fontMetrics.stringWidth(destinationAttr.getText()) + 25 + 20;%0A%0A
     
@@ -2447,34 +2447,38 @@
 Dimension(width,
-40
+HEIGHT
 ));%0A        this
@@ -2507,34 +2507,38 @@
 Dimension(width,
-40
+HEIGHT
 ));%0A        this
@@ -2577,12 +2577,142 @@
 dth,
-40))
+HEIGHT));%0A    %7D%0A%0A    private JPanel initPanelEditor() %7B%0A        JPanel panel = new JPanel();%0A            /*TODO*/%0A        return panel
 ;%0A  
@@ -2942,104 +2942,8 @@
 ) %7B%0A
-        panelEditor = new DestinationEditor();%0A        panelEditor.setDestination(destination);%0A
     
@@ -3034,56 +3034,16 @@
     
-this.setComponent(panelEditor.getDestination());
+/*TODO*/
 %0A   
@@ -3305,37 +3305,8 @@
 ());
-%0A%0A        setComponentSize();
 %0A   
@@ -3370,60 +3370,27 @@
     
-/*TODO maybe not needed*/%0A        return destination
+return null/*TODO*/
 ;%0A  

Revert "implemented editor"