package loggerplusplus.userinterface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * Created by corey on 24/08/17.
 */
public class PopOutPanel extends JPanel {
    private final Component component;
    private final JLabel placeholder;
    private String title;
    private boolean isPoppedOut;
    private JFrame popoutFrame;

    public PopOutPanel(Component component, String title){
        this.component = component;
        this.title = title;
        this.placeholder = new JLabel(title + " is popped out.");
        this.placeholder.setHorizontalAlignment(SwingConstants.CENTER);
        this.setLayout(new BorderLayout());
        this.add(component, BorderLayout.CENTER);
    }

    public void toggle(){
        if (this.isPoppedOut) popIn();
        else popOut();
    }

    public void popIn(){
        this.remove(placeholder);
        this.add(component, BorderLayout.CENTER);
        this.revalidate();
        this.repaint();
        this.isPoppedOut = false;
        //TODO Set button popout text
    }

    public void popOut(){
        //TODO Set button popin text
        this.popoutFrame = new JFrame();
        final PopOutPanel _this = this;
        popoutFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        popoutFrame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent windowEvent) {
                popoutFrame.add(component);
                isPoppedOut = true;
                _this.add(placeholder, BorderLayout.CENTER);
                _this.revalidate();
                _this.repaint();
                popoutFrame.pack();
            }

            @Override
            public void windowClosing(WindowEvent windowEvent) {
                popIn();
            }

            @Override
            public void windowClosed(WindowEvent windowEvent) {}

            @Override
            public void windowIconified(WindowEvent windowEvent) {}

            @Override
            public void windowDeiconified(WindowEvent windowEvent) {}

            @Override
            public void windowActivated(WindowEvent windowEvent) {}

            @Override
            public void windowDeactivated(WindowEvent windowEvent) {}
        });

        popoutFrame.setVisible(true);
    }
}

@@ -1062,37 +1062,35 @@
     
-//TODO Set button popout text
+this.popoutFrame.dispose();
 %0A   
@@ -1123,45 +1123,8 @@
 ()%7B%0A
-        //TODO Set button popin text%0A
     
@@ -1164,48 +1164,8 @@
 ();%0A
-        final PopOutPanel _this = this;%0A
     
@@ -1470,25 +1470,36 @@
             
-_
+PopOutPanel.
 this.add(pla
@@ -1542,25 +1542,36 @@
             
-_
+PopOutPanel.
 this.revalid
@@ -1593,17 +1593,28 @@
         
-_
+PopOutPanel.
 this.rep
@@ -1751,32 +1751,32 @@
  windowEvent) %7B%0A
-
                 
@@ -1770,24 +1770,40 @@
             
+ if(isPoppedOut)
  popIn();%0A  

Fix popout panel not closing when popin is done via menu