// display two lists for experimentingwith Dragging and Dropping
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class TestDnD extends JApplet {

    // contructor - initilaizes List1 and List2 and adds them to applet
    public TestDnD()
    {
        JComponent list1 = getListPanel("List 1", new String[] {"Circle", "Square", "Cube", "Line"});
        JComponent list2 = getListPanel("List 2", new String[] {"Lion", "Tiger", "Bear", "Horse"});
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.add(list1);
        panel.add(list2);
        add(panel);
    }

    // Program should run as Applet or Application
    public static void main(String[] args)
    {
        // create the frame with a title
        JFrame frame = new JFrame("Drag and Drop Lists");
        // exit the application when the JFrame is closed
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // add the applet to the frame
        frame.getContentPane().add(new TestDnD());
        // set the size of the frame (applet will be width by height in size)
        int height = 400;
        int width = 800;
        // pack the frame to get correct insets
        frame.pack();
        Insets fI = frame.getInsets();
        frame.setSize(width + fI.right + fI.left, height + fI.top + fI.bottom);
        // center the frame on screen
        Dimension sD = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((sD.width - width)/2, (sD.height - height)/2);
        // make the frame visible
        frame.setVisible(true);
    }

    // setup a list with some objects and a title
    private Box getListPanel(String title, Object[] listItems){
        JTextArea messageArea = new JTextArea();
        final DnDList list = new DnDList(messageArea);
        DefaultListModel listModel = new DefaultListModel();
        for (Object object: listItems) {
            listModel.addElement(object);
        }
        list.setModel(listModel);
        JScrollPane listScrollPane = new JScrollPane(list);
        listScrollPane.setBorder(BorderFactory.createTitledBorder(title));
        listScrollPane.setMinimumSize(new java.awt.Dimension(400, 150));
        listScrollPane.setMaximumSize(new java.awt.Dimension(400, 150));
        Box listBox = new Box(BoxLayout.Y_AXIS);
        listBox.add(listScrollPane);
        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event)
            {
                list.observer.setText("");
            }
        });
        listBox.add(Box.createVerticalStrut(5));
        clearButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        listBox.add(clearButton);
        listBox.add(Box.createVerticalStrut(5));
        JScrollPane observerScrollPane = new JScrollPane(list.observer);
        listBox.add(observerScrollPane);
        listBox.setBorder(LineBorder.createBlackLineBorder());
        return listBox;
    }

}
