/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  09.06.2006
   Letzte Änderung: 12.11.2010
*/

// Beispiel mit eines Editors mit Popupmenue

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

import java.awt.event.*;

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


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


public class Popupmenue01 extends JFrame implements ClipboardOwner{

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


  JLabel jLabel1 = new JLabel();
  JTextField TName = new JTextField();

  JLabel jLabelEdit = new JLabel("Text");
  JTextArea editor = new JTextArea("Hallo\n2.Zeile");

  JProgressBar jProgressBar1 = new JProgressBar();

  JButton BnUp = new JButton();
  JButton BnDown = new JButton();
  GridBagLayout gridBagLayout1 = new GridBagLayout();

  public Popupmenue01() {
    setSize(700, 550);
    setTitle("Popupmenue01 Beispiel");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    this.getContentPane().setLayout(gridBagLayout1);

    jLabel1.setText("Name");
    TName.setText("-");


    BnUp.setText("Up");
    BnUp.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnUp_actionPerformed(e);
      }
    });
    BnDown.setText("Down");
    BnDown.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnDown_actionPerformed(e);
      }
    });

    jProgressBar1.setMaximum(100);
    jProgressBar1.setMinimum(0);


    this.getContentPane().add(jLabel1,
          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
             new Insets(17, 21, 0, 26), 0, 0));

    this.getContentPane().add(TName,
          new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(17, 25, 0, 20), 0, 0));



    this.getContentPane().add(jLabelEdit,
         new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(20, 20, 0, 50), 0, 0));


    this.getContentPane().add( new JScrollPane(editor),
        new GridBagConstraints(0, 2, 3, 1, 100.0, 100.0
            ,GridBagConstraints.EAST,
            GridBagConstraints.BOTH,
            new Insets(0,20,0,20), 0, 0));

    this.getContentPane().add(jProgressBar1,
          new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH,
             new Insets(23, 20, 0, 20), 0, 10));
            // 10 zusätzliche Höhe

    this.getContentPane().add(BnDown,
          new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(18, 20, 5, 0), 0, 0));

    this.getContentPane().add(BnUp,
           new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE,
            new Insets(18, 20, 5, 20), 0, 10));


    TName.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            Name_mouseClicked(e);
          }
        });


    editor.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            Edit_mouseClicked(e);
          }
        });


  } // setGUI

    public int getButton(MouseEvent e) {
        int mode = e.getModifiers();
        if ((mode & e.BUTTON2_MASK) > 0) {
            return 2;
        }
        if ((mode & e.BUTTON3_MASK) > 0) {
            return 3;
        }
        return 1;
    } // getButton
  
  private void Edit_mouseClicked(MouseEvent e) {
      // rechte Maustaste
      if (  getButton(e) == 3) {
         popupEditMenu( e ) ;
       }
  }

  private void Name_mouseClicked(MouseEvent e) {
  }


		// Popup-Menue fuer Kopieren Oeffnen und Loeschen
    	public void popupEditMenu( MouseEvent me ) {
      		JPopupMenu popup = new JPopupMenu();
                //Meneueintag Kopieren
      		JMenuItem lkCopy = new JMenuItem("Kopieren");
          lkCopy.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(ActionEvent e) {
               lk_copyFile();
              }
          });
          popup.add(lkCopy);

                //Menueeintag Einfügen
          JMenuItem lkPaste = new JMenuItem("Einfügen");
          lkPaste.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(ActionEvent e) {
                lk_paste();
               }
           });
          popup.add(lkPaste);

          popup.addSeparator();
                //Menueeintag Loeschen
          JMenuItem lkDelete = new JMenuItem("Löschen");
          lkDelete.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(ActionEvent e) {
                lk_delete();
              }
          });
          popup.add(lkDelete);

          popup.addSeparator();
          JMenuItem selectAll = new JMenuItem("Alles markieren");
          selectAll.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(ActionEvent e) {
                lk_selectALL();
             }
          });
          popup.add(selectAll);
          popup.show(me.getComponent(),me.getX(),me.getY());
       }//popupMenu



        // wichtig:  implements ClipboardOwner
  void lk_copyFile() {
    editor.copy();
  }

  void lk_paste() {
    editor.paste();
  }

  void lk_delete() {
    editor.cut();
  }

  void lk_selectALL() {
    editor.selectAll();
  }


  void BnDown_actionPerformed(ActionEvent e) {
    jProgressBar1.setValue( jProgressBar1.getValue()-1 );
  }

  void BnUp_actionPerformed(ActionEvent e) {
    jProgressBar1.setValue( jProgressBar1.getValue()+1 );
  }


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

}
