// $Id: EditableTextField.java,v 1.1 2004/03/21 03:59:35 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.gui;


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

/**
 * TextField with Cut and Paste.
 *
 * @version $Revision: 1.1 $
 * @author	Michael Meyling
 */
public class EditableTextField extends JTextField {

    private final EditableTextField outer;


    public EditableTextField() {
        outer = this;
        ClipboardListener clipboardactivator = new ClipboardListener();
        addMouseListener(clipboardactivator);
    }


    public EditableTextField(String s) {
        outer = this;
        setDragEnabled(true);
        ClipboardListener clipboardactivator = new ClipboardListener();
        addMouseListener(clipboardactivator);
        setText(s);
    }



    private class ClipboardListener
        implements MouseListener, ActionListener {

        final JPopupMenu popedit;

        public void mousePressed(MouseEvent mouseevent) {
            if (mouseevent.getModifiers() != 16) {
                popedit.show(outer, mouseevent.getX(), mouseevent.getY());
            }
        }

        public void mouseReleased(MouseEvent mouseevent) {
        }

        public void mouseClicked(MouseEvent mouseevent) {
        }

        public void mouseEntered(MouseEvent mouseevent) {
        }

        public void mouseExited(MouseEvent mouseevent) {
        }


        public void actionPerformed(ActionEvent actionevent) {
            String s = actionevent.getActionCommand();
            if (s.equals("copy")) {
                outer.copy();
            } else
            if (s.equals("cut")) {
                outer.cut();
            } else
            if (s.equals("paste")) {
                outer.paste();
            }
        }


        ClipboardListener() {
            popedit = new JPopupMenu();
            JMenuItem jmenuitem = new JMenuItem("Copy");
            jmenuitem.addActionListener(this);
            jmenuitem.setActionCommand("copy");
            JMenuItem jmenuitem1 = new JMenuItem("Cut");
            jmenuitem1.addActionListener(this);
            jmenuitem1.setActionCommand("cut");
            JMenuItem jmenuitem2 = new JMenuItem("Paste");
            jmenuitem2.addActionListener(this);
            jmenuitem2.setActionCommand("paste");
            popedit.add(jmenuitem);
            popedit.add(jmenuitem1);
            popedit.add(jmenuitem2);
        }
    }



}
