package org.jmeterplugins.repository.plugins;

import org.apache.jmeter.gui.action.ActionNames;
import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jorphan.gui.ComponentUtil;
import org.jmeterplugins.repository.GenericCallback;
import org.jmeterplugins.repository.Plugin;
import org.jmeterplugins.repository.PluginManager;
import org.jmeterplugins.repository.PluginManagerMenuItem;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import static org.jmeterplugins.repository.PluginManagerDialog.SPACING;

public class SuggestDialog extends JDialog {

    private final PluginManager manager;
    private JLabel titleLabel = new JLabel("");
    private JLabel statusLabel = new JLabel("");

    public SuggestDialog(Frame parent, PluginManager manager, Set<Plugin> plugins) {
        super(parent, "JMeter Plugins Manager", true);
        setLocationRelativeTo(parent);
        this.manager = manager;
        init(plugins);
    }

    private void init(Set<Plugin> plugins) {
        setLayout(new BorderLayout());
        Dimension size = new Dimension(800, 600);
        setSize(size);
        setPreferredSize(size);
        setResizable(false);
        setIconImage(PluginManagerMenuItem.getPluginsIcon().getImage());
        ComponentUtil.centerComponentInWindow(this);

        JPanel mainPanel = new JPanel(new BorderLayout(0,0));
        mainPanel.setBorder(SPACING);

        final StringBuilder message = new StringBuilder("<html><p>Your test plan requires following plugins:</p><ul>");
        for (Plugin plugin : plugins) {
            message.append("<li>").append(plugin.getName()).append("</li>");
        }
        message.append("</ul>");
        message.append("<p></p>");
        message.append("<p>Plugins Manager can install it automatically. Following changes will be applied:</p>");
        message.append("</html>");
        titleLabel.setText(message.toString());
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        mainPanel.add(getDetailsPanel(), BorderLayout.CENTER);

        mainPanel.add(getButtonsPanel(), BorderLayout.SOUTH);

        add(mainPanel, BorderLayout.CENTER);
        //pack();
    }

    private JPanel getButtonsPanel() {
        JButton btnYes = new JButton("Yes, install it");
        btnYes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(final String s) {
                        SwingUtilities.invokeLater(
                                new Runnable() {

                                    @Override
                                    public void run() {
                                        statusLabel.setText(s);
                                        repaint();
                                    }
                                });
                    }
                });
                ActionRouter.getInstance().actionPerformed(new ActionEvent(this, 0, ActionNames.EXIT));
            }
        });

        JButton btnNo = new JButton("Cancel");
        btnNo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        JPanel buttons = new JPanel(new FlowLayout());
        buttons.add(btnYes);
        buttons.add(btnNo);
        JPanel btnPanel = new JPanel(new BorderLayout());
        btnPanel.setBorder(SPACING);
        btnPanel.add(buttons, BorderLayout.EAST);
        btnPanel.add(statusLabel, BorderLayout.WEST);
        return btnPanel;
    }

    private JPanel getDetailsPanel() {
        JTextArea messageLabel = new JTextArea(manager.getChangesAsText());
        messageLabel.setEditable(false);

        JPanel messagePanel = new JPanel(new BorderLayout());
        messagePanel.setBorder(SPACING);
        messagePanel.add(new JScrollPane(messageLabel));

        return messagePanel;
    }

}
@@ -162,35 +162,31 @@
 he.j
-meter.gui.util.VerticalPane
+orphan.gui.ComponentUti
 l;%0Ai
@@ -210,33 +210,68 @@
 jorphan.
-gui.ComponentUtil
+logging.LoggingManager;%0Aimport org.apache.log.Logger
 ;%0Aimport
@@ -498,47 +498,8 @@
 .*;%0A
-import javax.swing.border.EmptyBorder;%0A
 impo
@@ -725,18 +725,126 @@
 JDialog 
-%7B%0A
+implements GenericCallback%3CString%3E %7B%0A    private static final Logger log = LoggingManager.getLoggerForClass();
 %0A    pri
@@ -1618,16 +1618,17 @@
 ayout(0,
+ 
 0));%0A   
@@ -2468,32 +2468,38 @@
 nel() %7B%0A        
+final 
 JButton btnYes =
@@ -2527,24 +2527,120 @@
 stall it%22);%0A
+        final JButton btnNo = new JButton(%22Cancel%22);%0A        final SuggestDialog dialog = this;%0A
         btnY
@@ -2760,259 +2760,107 @@
     
-manager.applyChanges(new GenericCallback%3CString%3E() %7B%0A                    @Override%0A                    public void notify(final String s) %7B%0A                        SwingUtilities.invokeLater(%0A                                new Runnable() %7B%0A%0A         
+btnYes.setEnabled(false);%0A                btnNo.setEnabled(false);%0A%0A                new Thread() %7B%0A
     
@@ -2875,23 +2875,16 @@
         
-       
 @Overrid
@@ -2905,32 +2905,16 @@
         
-            
-    
 public v
@@ -2953,215 +2953,87 @@
     
-                statusLabel.setText(s);%0A                                        repaint();%0A                                    %7D%0A                                %7D);%0A                    %7D%0A                %7D);%0A
+manager.applyChanges(dialog);%0A                        dispose();%0A              
     
@@ -3038,22 +3038,16 @@
         
-      
 ActionRo
@@ -3130,32 +3130,40 @@
 ));%0A            
+        
 %7D%0A        %7D);%0A%0A 
@@ -3156,29 +3156,43 @@
         
+ 
-%7D
+       %7D.start(
 );%0A
-%0A
         
 JButton 
@@ -3187,47 +3187,27 @@
     
-JButton btnNo = new JButton(%22Cancel%22
+    %7D%0A        %7D
 );%0A
+%0A
     
@@ -4065,9 +4065,336 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void notify(final String s) %7B%0A        SwingUtilities.invokeLater(%0A                new Runnable() %7B%0A%0A                    @Override%0A                    public void run() %7B%0A                        statusLabel.setText(s);%0A                        repaint();%0A                    %7D%0A                %7D);%0A%0A    %7D%0A
 %7D

Fix UI deadlock