ClipboardListener.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.gui.se.element;
017 
018 import java.awt.event.ActionEvent;
019 import java.awt.event.ActionListener;
020 import java.awt.event.MouseAdapter;
021 import java.awt.event.MouseEvent;
022 
023 import javax.swing.JMenuItem;
024 import javax.swing.JPopupMenu;
025 import javax.swing.text.JTextComponent;
026 
027 /**
028  * Responsible for text field context menus.
029  *
030  @author  Michael Meyling.
031  *
032  */
033 class ClipboardListener extends MouseAdapter implements ActionListener {
034 
035     /** Popup menu for editable fields. */
036     private final JPopupMenu popEdit;
037 
038     /** Popup menu for non editable fields. */
039     private final JPopupMenu popNoEdit;
040 
041     /** Reference to text field. */
042     private final JTextComponent outer;
043 
044 
045     /**
046      * Constructor.
047      *
048      @param   outer   Work with this field.
049      */
050     ClipboardListener(final JTextComponent outer) {
051         this.outer = outer;
052 
053         popEdit = new JPopupMenu();
054 
055         final JMenuItem selectAll1 = new JMenuItem("Select All");
056         selectAll1.addActionListener(this);
057         selectAll1.setActionCommand("selectAll");
058         popEdit.add(selectAll1);
059         final JMenuItem copy1 = new JMenuItem("Copy");
060         copy1.addActionListener(this);
061         copy1.setActionCommand("copy");
062         popEdit.add(copy1);
063 
064         popNoEdit = new JPopupMenu();
065         final JMenuItem selectAll2 = new JMenuItem("Select All");
066         selectAll2.addActionListener(this);
067         selectAll2.setActionCommand("selectAll");
068         popNoEdit.add(selectAll2);
069         final JMenuItem jmenuitem1b = new JMenuItem("Copy");
070         jmenuitem1b.addActionListener(this);
071         jmenuitem1b.setActionCommand("copy");
072         popNoEdit.add(jmenuitem1b);
073         final JMenuItem jmenuitem2 = new JMenuItem("Cut");
074         jmenuitem2.addActionListener(this);
075         jmenuitem2.setActionCommand("cut");
076         popEdit.add(jmenuitem2);
077         final JMenuItem jmenuitem3 = new JMenuItem("Paste");
078         jmenuitem3.addActionListener(this);
079         jmenuitem3.setActionCommand("paste");
080         popEdit.add(jmenuitem3);
081     }
082 
083     /**
084      * Add context menu item.
085      *
086      @param   item    Add this menu entry.
087      */
088     public void addMenuItem(final JMenuItem item) {
089         popEdit.add(item);
090         popNoEdit.add(item);
091     }
092 
093     public void mousePressed(final MouseEvent mouseevent) {
094         if (mouseevent.getModifiers() != 16) {
095             if (outer.isEditable()) {
096                 popEdit.show(outer, mouseevent.getX(), mouseevent.getY());
097             else {
098                 popNoEdit.show(outer, mouseevent.getX(), mouseevent.getY());
099             }
100         }
101     }
102 
103     public void actionPerformed(final ActionEvent actionevent) {
104         final String s = actionevent.getActionCommand();
105         if (s.equals("copy")) {
106             outer.copy();
107         else if (s.equals("cut")) {
108             outer.cut();
109         else if (s.equals("paste")) {
110             outer.paste();
111         else if (s.equals("selectAll")) {
112             outer.selectAll();
113         }
114     }
115 
116 }