ClipboardListener.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  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 
060         final JMenuItem copy1 = new JMenuItem("Copy");
061         copy1.addActionListener(this);
062         copy1.setActionCommand("copy");
063         popEdit.add(copy1);
064 
065         final JMenuItem find1 = new JMenuItem("Find");
066 // FIXME 20130201 m31: didn't work :-(
067 //        find1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));
068         find1.addActionListener(this);
069         find1.setActionCommand("find");
070         popEdit.add(find1);
071 
072         final JMenuItem jmenuitem2 = new JMenuItem("Cut");
073         jmenuitem2.addActionListener(this);
074         jmenuitem2.setActionCommand("cut");
075         popEdit.add(jmenuitem2);
076 
077         final JMenuItem jmenuitem3 = new JMenuItem("Paste");
078         jmenuitem3.addActionListener(this);
079         jmenuitem3.setActionCommand("paste");
080         popEdit.add(jmenuitem3);
081 
082         popNoEdit = new JPopupMenu();
083 
084         final JMenuItem selectAll2 = new JMenuItem("Select All");
085         selectAll2.addActionListener(this);
086         selectAll2.setActionCommand("selectAll");
087         popEdit.add(selectAll2);
088 
089         final JMenuItem copy2 = new JMenuItem("Copy");
090         copy2.addActionListener(this);
091         copy2.setActionCommand("copy");
092         popEdit.add(copy2);
093 
094         final JMenuItem find2 = new JMenuItem("Find");
095         find2.addActionListener(this);
096         find2.setActionCommand("find");
097         popEdit.add(find2);
098 
099         popNoEdit.add(selectAll2);
100 
101         popNoEdit.add(copy2);
102 
103         popNoEdit.add(find2);
104 
105     }
106 
107     /**
108      * Add context menu item.
109      *
110      @param   item    Add this menu entry.
111      */
112     public void addMenuItem(final JMenuItem item) {
113         popEdit.add(item);
114         popNoEdit.add(item);
115     }
116 
117     public void mousePressed(final MouseEvent mouseevent) {
118         if (mouseevent.getModifiers() != 16) {
119             if (outer.isEditable()) {
120                 popEdit.show(outer, mouseevent.getX(), mouseevent.getY());
121             else {
122                 popNoEdit.show(outer, mouseevent.getX(), mouseevent.getY());
123             }
124         }
125     }
126 
127     public void actionPerformed(final ActionEvent actionevent) {
128         final String s = actionevent.getActionCommand();
129         if (s.equals("copy")) {
130             outer.copy();
131         else if (s.equals("cut")) {
132             outer.cut();
133         else if (s.equals("paste")) {
134             outer.paste();
135         else if (s.equals("selectAll")) {
136             outer.selectAll();
137         else if (s.equals("find")) {
138             find();
139         }
140     }
141 
142     private void find()  {
143         FindDialog finder = new FindDialog(outer);
144         finder.show();
145     }
146 
147 }