001 /*
002 * Copyright (C) 2003 Adam Olsen
003 *
004 * This program is free software; you can redistribute it and/or modify it under
005 * the terms of the GNU General Public License as published by the Free Software
006 * Foundation; either version 1, or (at your option) any later version.
007 *
008 * This program is distributed in the hope that it will be useful, but WITHOUT
009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
011 * details.
012 *
013 * You should have received a copy of the GNU General Public License along with
014 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
015 * Ave, Cambridge, MA 02139, USA.
016 */
017
018 package com.valhalla.gui;
019
020 import java.awt.Toolkit;
021 import java.awt.datatransfer.Clipboard;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.ActionListener;
024 import java.awt.event.MouseAdapter;
025 import java.awt.event.MouseEvent;
026 import java.util.Hashtable;
027
028 import javax.swing.JMenuItem;
029 import javax.swing.JPopupMenu;
030 import javax.swing.text.JTextComponent;
031
032 public class CopyPasteContextMenu extends JPopupMenu {
033 private JMenuItem cut = new JMenuItem("Cut");
034
035 private JMenuItem copy = new JMenuItem("Copy");
036
037 private JMenuItem paste = new JMenuItem("Paste");
038
039 private JMenuItem delete = new JMenuItem("Delete");
040
041 private static CopyPasteContextMenu m;
042
043 private static JTextComponent current = null;
044
045 private CMouseListener mouseListener = new CMouseListener();
046
047 private Clipboard board = Toolkit.getDefaultToolkit().getSystemClipboard();
048
049 private CopyPasteContextMenu() {
050 add(cut);
051 add(copy);
052 add(paste);
053 add(delete);
054
055 ItemListener l = new ItemListener();
056 cut.addActionListener(l);
057 copy.addActionListener(l);
058 paste.addActionListener(l);
059 delete.addActionListener(l);
060 }
061
062 public static CopyPasteContextMenu newInstance() {
063 m = new CopyPasteContextMenu();
064 return m;
065 }
066
067 private class ItemListener implements ActionListener {
068 public void actionPerformed(ActionEvent e) {
069 if (e.getSource() == cut)
070 current.cut();
071 else if (e.getSource() == copy)
072 current.copy();
073 else if (e.getSource() == paste)
074 current.paste();
075 else if (e.getSource() == delete)
076 current.replaceSelection("");
077 }
078 }
079
080 private void disableEditItems() {
081 cut.setEnabled(false);
082 paste.setEnabled(false);
083 delete.setEnabled(false);
084 }
085
086 private void enableEditItems() {
087 cut.setEnabled(true);
088 paste.setEnabled(true);
089 delete.setEnabled(true);
090 }
091
092 public static void registerComponent(JTextComponent comp) {
093 if (m == null)
094 m = new CopyPasteContextMenu();
095 comp.addMouseListener(m.mouseListener);
096 }
097
098 private class CMouseListener extends MouseAdapter {
099 public void mousePressed(MouseEvent e) {
100 checkPop(e);
101 }
102
103 public void mouseClicked(MouseEvent e) {
104 checkPop(e);
105 }
106
107 public void mouseReleased(MouseEvent e) {
108 checkPop(e);
109 }
110
111 private void checkPop(MouseEvent e) {
112 if (e.isPopupTrigger()) {
113 current = (JTextComponent) e.getSource();
114 if (!current.isEditable())
115 m.disableEditItems();
116 else
117 m.enableEditItems();
118
119 if (m.board.getContents(current) == null)
120 m.paste.setEnabled(false);
121 else if (current.isEditable())
122 m.paste.setEnabled(true);
123
124 String text = current.getSelectedText();
125
126 if (text == null) {
127 m.copy.setEnabled(false);
128 m.cut.setEnabled(false);
129 } else {
130 m.copy.setEnabled(true);
131 if (current.isEditable())
132 m.cut.setEnabled(true);
133 }
134
135 if (text != null) {
136 e.consume();
137 m.show(current, e.getX(), e.getY());
138 }
139 }
140 }
141 }
142 }