/*
  This class demos the use of jlist for moving things about
  This class demos the use of JSplitframe
  This demo includes class Q_Button for handling quiting
*/

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

class List_Frame extends JFrame
{
    DefaultListModel t_model, s_model;
    JList sourcelist, targetlist;
    JButton move;

    public List_Frame ( Object []Things_to_list )
    {
	int LinesVisible = 6; // number of lines visiable

	Container cp = getContentPane();
	cp.add( new JLabel ( "List of Words", JLabel.CENTER ), "North");

	addWindowListener( new WindowHandler() );
	move = new JButton ( "Move" );
	cp.add ( move, "Center" );
	move.addActionListener( new Button_Handler() );


	s_model = new DefaultListModel();
	for ( int i=0; i< Things_to_list.length; i++)
	    s_model.addElement( Things_to_list[i] );
	
	sourcelist = new JList( s_model );
	
	sourcelist.setVisibleRowCount( LinesVisible );
	
	sourcelist.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

	JScrollPane source = new JScrollPane(sourcelist);
	JPanel w_panel = new JPanel();
	w_panel.setLayout( new BorderLayout() );
	w_panel.add( source );
	w_panel.add( new JLabel ( "Source ==>" ), "North" );

	t_model = new DefaultListModel();
	targetlist = new JList ( t_model );
	JScrollPane target = new JScrollPane(targetlist);
	JPanel e_panel = new JPanel();
	e_panel.setLayout( new BorderLayout() );
	e_panel.add( target );
	e_panel.add( new JLabel ( "==> Target" ), "North" );

	cp.add ( w_panel, "West" );
	cp.add ( e_panel, "East" );
	cp.add ( new Q_Button(), "South" );
	
    }
    class Button_Handler implements ActionListener
    {
	public void actionPerformed( ActionEvent evt )
	{
	    Object ob = evt.getSource();
	    if ( ob == move ) {
		Object temp[] = sourcelist.getSelectedValues();
		int index[] = sourcelist.getSelectedIndices();
		for ( int i=0; i< temp.length; i++ )
		    t_model.addElement( temp[i] );
		for ( int i=index.length-1; i > -1;  i-- )
		    s_model.remove( index[i] );
	    }
	}
    }
}

class Q_Button extends JButton implements ActionListener
{
    public Q_Button( String s )
    {
	super( s );
	addActionListener( this );
    }
    
    public Q_Button ()
    {
	this("Quit");
    }

    public void actionPerformed( ActionEvent e ) 
    {
	System.exit(0);
    }
}

class WindowHandler extends WindowAdapter
{
    public void windowClosing( WindowEvent e )
    { System.exit(0); }
}

public class JList_Test_1
{
    public static void main( String []args)
    {
	String []list = new String[10];
	list[0] = "Alpha";
	list[1] = "Bravo";
	list[2] = "Charlie";
	list[3] = "Delta";
	list[4] = "Echo";
	list[5] = "Foxtrot";
	list[6] = "Golf";
	list[7] = "Hotel";
	list[8] = "India";
	list[9] = "Julet";

	JFrame jf = new List_Frame( list );
	jf.setSize(500,150);
	jf.setVisible(true);
    }
}


