/*
 * Copyright (c) 2006-2013 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.ui;

import com.dmdirc.Main;
import com.dmdirc.plugins.Service;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Simple dialog to pick and load a UI in an existing client.
 */
public class UIAttachDialog extends JDialog implements ActionListener,
        ListSelectionListener {

    /** Java Serialisation version ID. */
    private static final long serialVersionUID = 1;
    /** Sensible component gap. */
    private static final int GAP = 5;
    /** Services list. */
    private final JList<Service> list;
    /** Main instance. */
    private final Main main;

    /** Creates a new dialog allowing the user to select and load a UI. */
    public UIAttachDialog(final Main main) {
        super();
        this.main = main;
        list = initList();
        setLayout(new BorderLayout());
        add(list, BorderLayout.CENTER);
        add(initButtonPanel(), BorderLayout.PAGE_END);
    }

    /** Initialises the model with a list of UIs. */
    private JList<Service> initList() {
        final DefaultListModel<Service> model = new DefaultListModel<Service>();
        final JList<Service> newList = new JList<Service>(model);
        newList.setCellRenderer(new ServiceRenderer());
        newList.addListSelectionListener(this);
        final List<Service> services = main.getPluginManager()
                .getServicesByType("ui");
        for (Service service : services) {
            model.addElement(service);
        }
        newList.setSelectedIndex(0);
        return newList;
    }

    /** Initialises the OK and and cancel button panel. */
    private JPanel initButtonPanel() {
        final JPanel panel = new JPanel();
        final BoxLayout box = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        final JButton ok = new JButton("OK");
        final JButton cancel = new JButton("Cancel");

        ok.setActionCommand("OK");
        ok.addActionListener(this);
        cancel.setActionCommand("Cancel");
        cancel.addActionListener(this);

        panel.setLayout(box);
        panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        panel.add(Box.createHorizontalGlue());
        if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
            panel.add(ok);
            panel.add(Box.createHorizontalStrut(GAP));
            panel.add(cancel);
        } else {
            panel.add(cancel);
            panel.add(Box.createHorizontalStrut(GAP));
            panel.add(ok);
        }

        return panel;
    }

    /** Displays this dialog. */
    public void display() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }

    /** {@inheritDoc} */
    @Override
    public void actionPerformed(final ActionEvent e) {
        if ("OK".equals(e.getActionCommand())) {
            if (list.getSelectedValue() == null) {
                return;
            }
            ((Service) list.getSelectedValue()).activate();
        }
        dispose();
    }

    /** {@inheritDoc} */
    @Override
    public void valueChanged(final ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            list.setSelectedIndex(e.getLastIndex());
        }
    }

    /**
     * Renders plugin services properly.
     */
    private static class ServiceRenderer extends DefaultListCellRenderer {

        /** Java Serialisation version ID. */
        private static final long serialVersionUID = 1;

        /** {@inheritDoc} */
        @Override
        public Component getListCellRendererComponent(final JList<?> list,
                final Object value, final int index, final boolean isSelected,
                final boolean cellHasFocus) {
            final Component label = super.getListCellRendererComponent(list,
                    value, index, isSelected, cellHasFocus);

            if (value instanceof Service) {
                setText(((Service) value).getName());
            }

            return label;
        }
    }
}

@@ -1163,20 +1163,31 @@
 .dmdirc.
-Main
+plugins.Service
 ;%0Aimport
@@ -1213,16 +1213,23 @@
 .Service
+Manager
 ;%0A%0Aimpor
@@ -2194,21 +2194,50 @@
 /** 
-Main instan
+Service manager to use to find UI servi
 ce
+s
 . */
@@ -2255,25 +2255,45 @@
 e final 
-Main main
+ServiceManager serviceManager
 ;%0A%0A    /
@@ -2294,16 +2294,23 @@
 %0A    /**
+%0A     *
  Creates
@@ -2369,98 +2369,206 @@
  UI.
- */%0A    public UIAttachDialog(final Main main) %7B%0A        super();%0A        this.main = main
+%0A     *%0A     * @param serviceManager Service manager to use to find UI services.%0A     */%0A    public UIAttachDialog(final ServiceManager serviceManager) %7B%0A        this.serviceManager = serviceManager
 ;%0A  
@@ -2895,23 +2895,16 @@
 stModel%3C
-Service
 %3E();%0A   
@@ -2949,23 +2949,16 @@
 w JList%3C
-Service
 %3E(model)
@@ -3106,48 +3106,22 @@
 s = 
-main.getPluginManager()%0A                
+serviceManager
 .get

Remove usage of Main.