

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

import java.util.Vector;



import java.awt.dnd.*;
import java.awt.datatransfer.*;


// Beispiel mit zwei Listen
// Listen durchs Arrays
// Drag & Drop


public class GUI_09 extends JFrame {

   JList2 jListe1;
   JList2 jListe2;

   Vector liste1= new Vector();
   Vector liste2= new Vector();

  //Frame konstruieren
  public GUI_09() {
     JButton BnOk1;  // Aktionsschalter Links
     JButton BnOk2;  // Aktionsschalter Rechts
     JButton BnESC;  // Aktionsschalter Abbruch

     JButton BnLeft = new JButton("Links");
     JButton BnRight = new JButton("Rechts");

     JPanel panel;
     JScrollPane scroll;
     String[] items1;
     String[] items2;

    setSize(620, 520);
    setTitle("Liste mit Drag & Drop");

    this.getContentPane().setLayout ( new GridBagLayout() );

    for (int i=0; i<10; i++) {
      liste1.add(  Integer.toString(i+1)+" Auswahl" );
    }

    jListe1 = new JList2(liste1);
    jListe1.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    scroll = new JScrollPane(jListe1);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,20,0,20), 0, 0));


    for (int i=0; i<5; i++) {
      liste2.add ( Integer.toString(i+1)+" Fachrichtung" );
    }

    jListe2 = new JList2(liste2);
    jListe2.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
    scroll = new JScrollPane(jListe2);

    this.getContentPane().add(scroll,
          new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(20,30,0,20), 0, 0));

    jListe1.setFont( new Font("Arial", Font.BOLD,18)  );
    jListe2.setFont( new Font("Arial", Font.BOLD,18)  );

    /*
    SINGLE_SELECTION
    SINGLE_INTERVAL_SELECTION
    MULTIPLE_INTERVAL_SELECTION
    */

    JPanel pnMitte = new JPanel();
    pnMitte.setLayout( new GridBagLayout() );
    BnLeft.setFont( new Font("Arial", Font.BOLD,18)  );
    BnRight.setFont( new Font("Arial", Font.BOLD,18)  );

    pnMitte.add(BnRight,
          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.NONE,
            new Insets(0,0,0,0), 0, 0));
    pnMitte.add(BnLeft,
          new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTH, GridBagConstraints.NONE,
            new Insets(30,0,0,0), 0, 0));



    this.getContentPane().add(pnMitte,
          new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.VERTICAL,
            new Insets(20,20,0,20), 0, 0));



     // ----------
   BnLeft.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnLeft_click();
      }
    });

   BnRight.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnRight_click();
      }
    });


    panel = new JPanel();
    panel.setLayout ( new FlowLayout() );

    BnOk1 = new JButton("Ok (links)");  // Label erzeugen
    panel.add(BnOk1);

    BnOk2 = new JButton("Ok (rechts)");  // Label erzeugen
    panel.add(BnOk2);

    BnESC = new JButton("Abbruch");  // Label erzeugen
    panel.add(BnESC);

    BnOk1.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOk2.setFont( new Font("Arial", Font.BOLD,18)  );
    BnESC.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().add(panel, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(30,0,30,0), 0, 0));


    BnOk1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnOK_links_click(e);
      }
    });

    BnOk2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnOK_rechts_click(e);
      }
    });

    BnESC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnESC_actionPerformed(e);
      }
    });



    jListe1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

      public void mouseDragged(MouseEvent e) {
        jListe1_mouseDragged(e);
      }
    });
    jListe1.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseReleased(MouseEvent e) {
        jListe1_mouseReleased(e);
      }
    });


    jListe2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

      public void mouseDragged(MouseEvent e) {
        jListe2_mouseDragged(e);
      }
    });
    jListe2.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseReleased(MouseEvent e) {
        jListe2_mouseReleased(e);
      }
    });

  } // create


   void jListe1_mouseDragged(MouseEvent e) {
     // System.out.println("liste1 drag "+ e.getSource() );
  }

  void jListe1_mouseReleased(MouseEvent e) {
     if (e.getSource()== jListe2) {
       System.out.println("liste1 mouse Release "+ e.getSource());
     }
  }


   void jListe2_mouseDragged(MouseEvent e) {
     //System.out.println("liste2 drag "+ e.getSource());
  }

  void jListe2_mouseReleased(MouseEvent e) {
     if (e.getSource()== jListe1) {
       System.out.println("liste2 mouse Release "+ e.getSource());
     }
  }


  void BnLeft_click() {
    int i = jListe2.getSelectedIndex();
    if (i>=0) {
      String s = (String) liste2.elementAt(i);
      liste2.remove(i);
      liste1.add( s );
      jListe1.setListData( liste1 );
      jListe2.setListData( liste2 );
    }

  }

  void BnRight_click() {
    int i = jListe1.getSelectedIndex();
    if (i>=0) {
      String s = (String) liste1.elementAt(i);
      liste1.remove(i);
      liste2.add( s );
      jListe1.setListData( liste1 );
      jListe2.setListData( liste2 );
    }

  }

  void BnOK_links_click(ActionEvent e) {
     String sResult;
     Object[] elements = jListe1.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
         sResult = (String) elements[i];  // Typecasting
         System.out.println(sResult);
       }
       System.out.println("");
       System.out.println("");
     }
  }


  void BnOK_rechts_click(ActionEvent e) {
     String sResult;
     sResult = (String) jListe2.getSelectedValue();
     if (sResult==null) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       System.out.println(sResult);
       System.out.println(" ");
       System.out.println(" ");
     }
  }

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


  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


  public static void main(String[] args) {
    GUI_09 frame = new GUI_09();
    frame.setVisible(true);
  }


}


class JList2 extends JList {

  private DragSource dragSource;
  private DragGestureListener dgListener;
  private DragSourceListener dsListener;
  public JList2(  ) {
    super(  );
  }

  public JList2( Vector listData ) {
    super( listData );
    this.dragSource = DragSource.getDefaultDragSource();
    this.dgListener = new DGListener();
    this.dsListener = new DSListener();
  this.dragSource.createDefaultDragGestureRecognizer(
      this, DnDConstants.ACTION_COPY_OR_MOVE, this.dgListener );
  }

  public JList2( ListModel dataModel ) {
    super( dataModel );
  }

  public JList2( Object[] listData ) {
    super( listData );
  }




}
