
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.*;

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


public class GUI_09 extends JFrame
    implements DragGestureListener, DragSourceListener, DropTargetListener
{

  // Neu für Drag & Drop
    DropTarget mytarget;
    DragSource mysource;
    DragGestureRecognizer dgr;

   JList jListe1;
   JList 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);
    setLocation(700,100);
    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 JList(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 JList(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);
      }
    });

    setDrag_n_Drop();

  } // create




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


  // Drag & Drop
  // Drag & Drop
  // Drag & Drop
  // Drag & Drop
  // Drag & Drop
  // Drag & Drop


  void setDrag_n_Drop() {

     mysource = new DragSource();

     mytarget = new DropTarget(jListe1,
        DnDConstants.ACTION_COPY_OR_MOVE, this);

      dgr = mysource.createDefaultDragGestureRecognizer(jListe2,
        DnDConstants.ACTION_COPY_OR_MOVE, this);

  }


    public void dragGestureRecognized(DragGestureEvent e) {
      int i = jListe2.getSelectedIndex();
      System.out.println("dragGestureRecognized");
      if (i>=0) {
        //String sel = (String) liste2.elementAt(i);
        String sel =  Integer.toString(i);
        System.out.println("String drag "+sel);
    	  e.startDrag(DragSource.DefaultCopyDrop,
		        new StringSelection(sel), this);
      }

    }

    public void dragEnter(DropTargetDragEvent e) {
    	System.out.println("dragEnter");
    	e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
    }

    public void drop(DropTargetDropEvent e) {
    	System.out.println("drop");
      // hier der Auffangs-code
      try {
          if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            Transferable tr = e.getTransferable();
            e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            // String s = (String)tr.getTransferData(DataFlavor.stringFlavor);
            // Nummer, Index als string übergeben
            String sel = (String)tr.getTransferData(DataFlavor.stringFlavor);
            int i = IVal(sel);
            System.out.println("drop  true: "+sel);
            if (i>=0) {
              String s = (String) liste2.elementAt(i);
              liste2.remove(i);
              liste1.addElement(s);
              jListe1.setListData( liste1 );
              jListe2.setListData( liste2 );
            }
            e.dropComplete(true);
         }
         else {
            e.rejectDrop();
          System.out.println("drop  false");
         }
      }
      catch (Exception ex) {
          System.out.println("Data transfer exception: " + ex);
      }

    }


   public int IVal(String sText) {
        // erläuterung von iIVal = Integer.valueOf(szText).intValue();
        int result = -1;
        String s;
        s = new String(sText); // neues Objekt
        s.trim();
        if (s.length() == 0) {
            result = -1;
        }
        else {
            try {
                result = Integer.valueOf(sText).intValue();
            }
            catch (NumberFormatException e) {
                result = -1;
            }
        }
        return result;
    } // IVal



    public void dragExit(DropTargetEvent e) {
    	System.out.println("dragExit");
    }
    public void dragDropEnd(DragSourceDropEvent e) {
    	System.out.println("dragDropEnd");
    }


    public void dragOver(DropTargetDragEvent e) {
    	System.out.println("dragOver (drop)");
    }
    public void dropActionChanged(DropTargetDragEvent e) {
    	System.out.println("dropActionChanged");
    }

    public void dragEnter(DragSourceDragEvent e) {
    	System.out.println("dragEnter");
    }
    public void dragExit(DragSourceEvent e) {
    	System.out.println("dragExit");
    }
    public void dragOver(DragSourceDragEvent e) {
    	System.out.println("dragOver (drag)");
    }
    public void dropActionChanged(DragSourceDragEvent e) {
    	System.out.println("dragActionChanged (drag)");
    }



}



