
// letzte Aenderung: 23.10.2010

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


// Allgemeine Funktionen fuer die Zwischenablage Clipboard
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;



// hier werden nicht die implementierten Funktionen
// des JTextField benutzt
// Also ist diese Loesung allgemeiner

public class Clipboard02 extends JFrame implements ClipboardOwner {
  JToolBar toolBar = new JToolBar();
  JButton BnCopy = new JButton("Copy to Clipboard");
  JButton BnCut = new JButton("Cut to Clipboard");
  JButton BnPaste = new JButton("Paste from Clipboard");

  JTextArea editor = new JTextArea("Dies ist eine Zeile\r\nDies ist die 2. Zeile");

  public Clipboard02() {
    this.setTitle("Beispiel über die Zwischenablage");
    this.setLocation(100,100);
    this.setSize(600,300);
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  public void setGUI() {

    BnCopy.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnCopy_click();
      }
    });
    toolBar.add(BnCopy);
    toolBar.addSeparator( new Dimension(20,5) );

    BnCut.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnCut_click();
      }
    });
    toolBar.add(BnCut);

    toolBar.addSeparator( new Dimension(20,5) );

    BnPaste.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        BnPaste_click();
      }
    });
    toolBar.add(BnPaste);


    this.getContentPane().add(toolBar, BorderLayout.NORTH);

    JScrollPane sc = new JScrollPane(editor);
    this.getContentPane().add(sc, BorderLayout.CENTER);
    editor.setFont( new Font("Arial", Font.BOLD,18)  );

  }


  private void BnCopy_click() {
    String myString = editor.getSelectedText();
    StringSelection stringSelection = new StringSelection( myString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
    // statt editor.copy();
  }

  private void BnCut_click() {
    String myString = editor.getSelectedText();
    // manuell ausschneiden
    String sEd = editor.getText();
    int iStart = editor.getSelectionStart();
    int iEnd = editor.getSelectionEnd();
    //String sEdBew =
    System.out.println(iStart+"   "+iEnd);

      // Hier Clipboard vom String zur stringSelection
    StringSelection stringSelection = new StringSelection( myString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
    // editor.cut();
  }

  private void BnPaste_click() {
    String sText = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) &&
      contents.isDataFlavorSupported(DataFlavor.stringFlavor)
    ;
    if ( hasTransferableText ) {
      try {
        sText = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (UnsupportedFlavorException ex){
        //highly unlikely since we are using a standard DataFlavor
        System.out.println(ex);
        ex.printStackTrace();
      }
      catch (IOException ex) {
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    String sEd = editor.getText();
    int iStart = editor.getSelectionStart();
    int iEnd = editor.getSelectionEnd();
    System.out.println(iStart+"   "+iEnd);
    if ( (iStart==0) && (iEnd==0) ) {
      editor.setText(sText);
    }
    else {
      // substring zählt das letzte Zeichnen nicht mit !!!
      String sEd1 = sEd.substring(0, iStart);
      String sEd2 = sEd.substring(iEnd+1, sEd.length() );
      editor.setText(  sEd1 + sText + sEd2);
    }
    // wenn man die Selektion nicht wieder setzt,
    // erfolgt eine Exception
    editor.setSelectionStart(iStart);
    editor.setSelectionEnd(iEnd);
    // statt editor.paste();
  }

  // New
     /**
   * Empty implementation of the ClipboardOwner interface.
   */
   public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
     //do nothing
   }




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

} // Clipboard02


