
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 einer Liste und einem Editor
// Liste durchs Arrays
// Drag & Drop


// Funktioniert nur von links nach Rechts
//   Liste in den Editor

/*
Drag GUI Elemente
  ColorChooser
  JEditorPane
  JFileChooser
  JFormattedTextField
  JList
  JTable
  JTextArea
  JTextField
  JTextPane
  JTree 
*/

/* 
automatische Drop Elemente
  EditorPane
  JFormattedTextField
  JPasswordField
  JTextArea
  JTextField
  JTextPane
  JColorChooser 
*/

/*
zusaetzlicher Code fuer Drop
 JList
 JTable
 JTree 

    jList2.setDropMode(DropMode.USE_SELECTION);
    jList2.setDragEnabled(true);
    //DropMode.INSERT
      //DropMode.INSERT_COLS
    // DropMode.INSERT_ROWS
    //DropMode.ON
    // DropMode.ON_OR_INSERT
    // DropMode.ON_OR_INSERT_COLS
    // DropMode.ON_OR_INSERT_ROWS
    //DropMode.USE_SELECTION

*/

public class DragnDrop_3 extends JFrame  implements DragSourceListener, DragGestureListener {

   JList jList1;
   JTextArea editor;

   Vector liste1= 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();

   
    jList1.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 BnESC;  // Aktionsschalter Abbruch

     JButton BnRight = new JButton("Rechts");

     JPanel panel;
     JScrollPane scroll;
     String[] items1;

    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, 3.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
             new Insets(20,20,0,20), 0, 0));


    editor = new JTextArea("abc\ndef\nghi\n");
    scroll = new JScrollPane(editor);

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

    jList1.setFont( new Font("Arial", Font.BOLD,18)  );
    editor.setFont( new Font("Arial", Font.BOLD,18)  );

    /*
    SINGLE_SELECTION
    SINGLE_INTERVAL_SELECTION
    MULTIPLE_INTERVAL_SELECTION
    */

    JPanel pnMitte = new JPanel();
    pnMitte.setLayout( new GridBagLayout() );
    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));


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



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


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

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

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



   
    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 BnRight_click() {
    int i = jList1.getSelectedIndex();
    if (i>=0) {
      String s = (String) liste1.elementAt(i);
      liste1.remove(i);
      editor.append( s );
      jList1.setListData( liste1 );
    }

  }


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



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





}



