
import java.util.Vector;

import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import java.io.*;

    // neu fuer Drag and Drop
import java.awt.dnd.*;
import java.awt.datatransfer.StringSelection;

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


// Funktioniert nur von rechts nach links !!!!!!!!

// mit "implements DropTargetListener" kann man Daten empfangen Drop

public class DragnDrop_3 extends JFrame  implements DragSourceListener, DragGestureListener {

   JList jList1;
   JList jList2;

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

    // neu fuer Drag and Drop
   DragSource ds;
   StringSelection transferable;

  //Frame konstruieren
  public DragnDrop_3() {
    setSize(620, 520);
    setLocation(300,100);
    setTitle("Liste mit Drag & Drop");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();

   
    jList2.setDragEnabled(true);
    // jList1.setDragEnabled(true);  // Damit kann man Daten verschieben

    ds = new DragSource();
    DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(jList1,
        DnDConstants.ACTION_MOVE, this);   
   // ACTION_COPY_OR_MOVE  ACTION_LINK  ACTION_COPY  ACTION_MOVE   ACTION_NONE  ACTION_REFERENCE 
  } // create


  public void dragGestureRecognized(DragGestureEvent dge) {
    System.out.println("Drag Gesture Recognized!");
    transferable = new StringSelection(jList1.getSelectedValue().toString());
    ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
  }

  public void dragEnter(DragSourceDragEvent dsde) {
    System.out.println("Drag Enter");
  }

  public void dragExit(DragSourceEvent dse) {
    System.out.println("Drag Exit");
  }

  public void dragOver(DragSourceDragEvent dsde) {
    System.out.println("Drag Over");
  }

  public void dragDropEnd(DragSourceDropEvent dsde) {
    System.out.print("Drag Drop End: ");
    if (dsde.getDropSuccess()) {
      System.out.println("Succeeded");
    } else {
      System.out.println("Failed");
    }
  }

  public void dropActionChanged(DragSourceDragEvent dsde) {
    System.out.println("Drop Action Changed");
  }

  void setGUI() {
     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;

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

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

    jList1 = new JList(liste1);
    jList1.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    scroll = new JScrollPane(jList1);
    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" );
    }

    jList2 = new JList(liste2);
    jList2.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
    scroll = new JScrollPane(jList2);

    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));

    jList1.setFont( new Font("Arial", Font.BOLD,18)  );
    jList2.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);
      }
    });


    JLabel tipp1 = new JLabel ("Funktioniert nur von rechts nach links !!!!!!!!");
    this.getContentPane().add(tipp1, new GridBagConstraints(0, 2, 3, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,20,20,0), 0, 0));
  } // setGUI


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

  }

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

  }

  void BnOK_links_click(ActionEvent e) {
     String sResult;
     Object[] elements = jList1.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) jList2.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);
  }



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





}



