001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import java.awt.*;
029: import java.awt.datatransfer.*;
030: import java.awt.event.*;
031:
032: import javax.swing.*;
033: import javax.swing.text.*;
034:
035: /**
036: *
037: *
038: * @author $author$
039: * @version $Revision: 1.14 $
040: */
041: public class XTextField extends JTextField implements ClipboardOwner {
042: private JPopupMenu popup;
043: private Action cutAction;
044: private Action copyAction;
045: private Action pasteAction;
046: private Action deleteAction;
047: private Action selectAllAction;
048:
049: /**
050: * Creates a new XTextField object.
051: */
052: public XTextField() {
053: this (null, null, 0);
054: }
055:
056: /**
057: * Creates a new XTextField object.
058: *
059: * @param text
060: */
061: public XTextField(String text) {
062: this (null, text, 0);
063: }
064:
065: /**
066: * Creates a new XTextField object.
067: *
068: * @param columns
069: */
070: public XTextField(int columns) {
071: this (null, null, columns);
072: }
073:
074: /**
075: * Creates a new XTextField object.
076: *
077: * @param text
078: * @param columns
079: */
080: public XTextField(String text, int columns) {
081: this (null, text, columns);
082: }
083:
084: /**
085: * Creates a new XTextField object.
086: *
087: * @param doc
088: * @param text
089: * @param columns
090: */
091: public XTextField(Document doc, String text, int columns) {
092: super (doc, text, columns);
093: initXtensions();
094: }
095:
096: /**
097: *
098: *
099: * @param clipboard
100: * @param contents
101: */
102: public void lostOwnership(Clipboard clipboard, Transferable contents) {
103: }
104:
105: private void showPopup(int x, int y) {
106: // Grab the focus, this should deselect any other selected fields.
107: requestFocus();
108:
109: // If the popup has never been show before - then build it
110: if (popup == null) {
111: popup = new JPopupMenu("Clipboard");
112: popup.add(cutAction = new CutAction());
113: popup.add(copyAction = new CopyAction());
114: popup.add(pasteAction = new PasteAction());
115: popup.add(deleteAction = new DeleteAction());
116: popup.addSeparator();
117: popup.add(selectAllAction = new SelectAllAction());
118: }
119:
120: // Enabled the actions based on the field contents
121: cutAction
122: .setEnabled(isEnabled() && (getSelectedText() != null));
123: copyAction.setEnabled(isEnabled()
124: && (getSelectedText() != null));
125: deleteAction.setEnabled(isEnabled()
126: && (getSelectedText() != null));
127: pasteAction.setEnabled(isEnabled()
128: && Toolkit.getDefaultToolkit().getSystemClipboard()
129: .getContents(this ).isDataFlavorSupported(
130: DataFlavor.stringFlavor));
131: selectAllAction.setEnabled(isEnabled());
132:
133: // Make the popup visible
134: popup.show(this , x, y);
135: }
136:
137: private void initXtensions() {
138: addMouseListener(new MouseAdapter() {
139: public void mouseClicked(MouseEvent evt) {
140: if (SwingUtilities.isRightMouseButton(evt)) {
141: showPopup(evt.getX(), evt.getY());
142: }
143: }
144: });
145: addFocusListener(new FocusListener() {
146: public void focusGained(FocusEvent evt) {
147: XTextField.this .selectAll();
148: }
149:
150: public void focusLost(FocusEvent evt) {
151: // if(popup.isVisible())
152: // popup.setVisible(false);
153: }
154: });
155: }
156:
157: // Supporting actions
158: class CopyAction extends AbstractAction {
159: public CopyAction() {
160: putValue(Action.NAME, "Copy");
161: putValue(Action.SMALL_ICON, new ResourceIcon(
162: XTextField.class, "copy.png"));
163: putValue(Action.SHORT_DESCRIPTION, "Copy");
164: putValue(Action.LONG_DESCRIPTION,
165: "Copy the selection from the text and place it in the clipboard");
166: putValue(Action.MNEMONIC_KEY, new Integer('c'));
167: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
168: KeyEvent.VK_C, KeyEvent.CTRL_MASK));
169: }
170:
171: public void actionPerformed(ActionEvent evt) {
172: Toolkit.getDefaultToolkit().getSystemClipboard()
173: .setContents(new StringSelection(getText()),
174: XTextField.this );
175: }
176: }
177:
178: class CutAction extends AbstractAction {
179: public CutAction() {
180: putValue(Action.NAME, "Cut");
181: putValue(Action.SMALL_ICON, new ResourceIcon(
182: XTextField.class, "cut.png"));
183: putValue(Action.SHORT_DESCRIPTION, "Cut selection");
184: putValue(Action.LONG_DESCRIPTION,
185: "Cut the selection from the text and place it in the clipboard");
186: putValue(Action.MNEMONIC_KEY, new Integer('u'));
187: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
188: KeyEvent.VK_X, KeyEvent.CTRL_MASK));
189: }
190:
191: public void actionPerformed(ActionEvent evt) {
192: Toolkit.getDefaultToolkit().getSystemClipboard()
193: .setContents(new StringSelection(getText()),
194: XTextField.this );
195: setText("");
196: }
197: }
198:
199: class PasteAction extends AbstractAction {
200: public PasteAction() {
201: putValue(Action.NAME, "Paste");
202: putValue(Action.SMALL_ICON, new ResourceIcon(
203: XTextField.class, "paste.png"));
204: putValue(Action.SHORT_DESCRIPTION,
205: "Paste clipboard content");
206: putValue(
207: Action.LONG_DESCRIPTION,
208: "Paste the clipboard contents to the current care position or replace the selection");
209: putValue(Action.MNEMONIC_KEY, new Integer('p'));
210: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
211: KeyEvent.VK_X, KeyEvent.CTRL_MASK));
212: }
213:
214: public void actionPerformed(ActionEvent evt) {
215: Transferable t = Toolkit.getDefaultToolkit()
216: .getSystemClipboard().getContents(this );
217:
218: if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
219: try {
220: setText(t.getTransferData(DataFlavor.stringFlavor)
221: .toString());
222: } catch (Exception e) {
223: // Dont care
224: }
225: }
226: }
227: }
228:
229: class DeleteAction extends AbstractAction {
230: public DeleteAction() {
231: putValue(Action.NAME, "Delete");
232: putValue(Action.SMALL_ICON, new ResourceIcon(
233: XTextField.class, "delete.png"));
234: putValue(Action.SHORT_DESCRIPTION, "Delete selection");
235: putValue(Action.LONG_DESCRIPTION,
236: "Delete the selection from the text");
237: putValue(Action.MNEMONIC_KEY, new Integer('d'));
238: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
239: KeyEvent.VK_X, KeyEvent.CTRL_MASK));
240: }
241:
242: public void actionPerformed(ActionEvent evt) {
243: setText("");
244: }
245: }
246:
247: class SelectAllAction extends AbstractAction {
248: SelectAllAction() {
249: putValue(Action.SMALL_ICON, new EmptyIcon(16, 16));
250: putValue(Action.NAME, "Select All");
251: putValue(Action.SHORT_DESCRIPTION, "Select All");
252: putValue(Action.LONG_DESCRIPTION,
253: "Select all items in the context");
254: putValue(Action.MNEMONIC_KEY, new Integer('a'));
255: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
256: KeyEvent.VK_A, KeyEvent.CTRL_MASK));
257: }
258:
259: public void actionPerformed(ActionEvent evt) {
260: selectAll();
261: }
262: }
263: }
|