KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > XTextField


1 /*
2  * SSHTools - Java SSH2 API
3  *
4  * Copyright (C) 2002 Lee David Painter.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * You may also distribute it and/or modify it under the terms of the
12  * Apache style J2SSH Software License. A copy of which should have
13  * been provided with the distribution.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * License document supplied with your distribution for more details.
19  *
20  */

21
22 package com.sshtools.ui.swing;
23
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.datatransfer.Clipboard JavaDoc;
26 import java.awt.datatransfer.ClipboardOwner JavaDoc;
27 import java.awt.datatransfer.DataFlavor JavaDoc;
28 import java.awt.datatransfer.StringSelection JavaDoc;
29 import java.awt.datatransfer.Transferable JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import java.awt.event.MouseAdapter JavaDoc;
35 import java.awt.event.MouseEvent JavaDoc;
36
37 import javax.swing.AbstractAction JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.JPopupMenu JavaDoc;
40 import javax.swing.JTextField JavaDoc;
41 import javax.swing.KeyStroke JavaDoc;
42 import javax.swing.SwingUtilities JavaDoc;
43 import javax.swing.text.Document JavaDoc;
44
45 /**
46  *
47  *
48  * @author $author$
49  */

50 public class XTextField extends JTextField JavaDoc implements ClipboardOwner JavaDoc {
51     private JPopupMenu JavaDoc popup;
52     private Action JavaDoc cutAction;
53     private Action JavaDoc copyAction;
54     private Action JavaDoc pasteAction;
55     private Action JavaDoc deleteAction;
56     private Action JavaDoc selectAllAction;
57
58     /**
59      * Creates a new XTextField object.
60      */

61     public XTextField() {
62         this(null, null, 0);
63     }
64
65     /**
66      * Creates a new XTextField object.
67      *
68      * @param text
69      */

70     public XTextField(String JavaDoc text) {
71         this(null, text, 0);
72     }
73
74     /**
75      * Creates a new XTextField object.
76      *
77      * @param columns
78      */

79     public XTextField(int columns) {
80         this(null, null, columns);
81     }
82
83     /**
84      * Creates a new XTextField object.
85      *
86      * @param text
87      * @param columns
88      */

89     public XTextField(String JavaDoc text, int columns) {
90         this(null, text, columns);
91     }
92
93     /**
94      * Creates a new XTextField object.
95      *
96      * @param doc
97      * @param text
98      * @param columns
99      */

100     public XTextField(Document JavaDoc doc, String JavaDoc text, int columns) {
101         super(doc, text, columns);
102         initXtensions();
103     }
104
105     /**
106      *
107      *
108      * @param clipboard
109      * @param contents
110      */

111     public void lostOwnership(Clipboard JavaDoc clipboard, Transferable JavaDoc contents) {
112     }
113
114     private void showPopup(int x, int y) {
115         // Grab the focus, this should deselect any other selected fields.
116
requestFocus();
117
118         // If the popup has never been show before - then build it
119
if (popup == null) {
120             popup = new JPopupMenu JavaDoc("Clipboard");
121             popup.add(cutAction = new CutAction());
122             popup.add(copyAction = new CopyAction());
123             popup.add(pasteAction = new PasteAction());
124             popup.add(deleteAction = new DeleteAction());
125             popup.addSeparator();
126             popup.add(selectAllAction = new SelectAllAction());
127         }
128
129         // Enabled the actions based on the field contents
130
cutAction.setEnabled(isEnabled() && (getSelectedText() != null));
131         copyAction.setEnabled(isEnabled() && (getSelectedText() != null));
132         deleteAction.setEnabled(isEnabled() && (getSelectedText() != null));
133         pasteAction.setEnabled(isEnabled() && Toolkit.getDefaultToolkit()
134                         .getSystemClipboard()
135                         .getContents(this)
136                         .isDataFlavorSupported(DataFlavor.stringFlavor));
137         selectAllAction.setEnabled(isEnabled());
138
139         // Make the popup visible
140
popup.show(this, x, y);
141     }
142
143     private void initXtensions() {
144         addMouseListener(new MouseAdapter JavaDoc() {
145             public void mouseClicked(MouseEvent JavaDoc evt) {
146                 if (SwingUtilities.isRightMouseButton(evt)) {
147                     showPopup(evt.getX(), evt.getY());
148                 }
149             }
150         });
151         addFocusListener(new FocusListener JavaDoc() {
152             public void focusGained(FocusEvent JavaDoc evt) {
153                 XTextField.this.selectAll();
154             }
155
156             public void focusLost(FocusEvent JavaDoc evt) {
157                 // if(popup.isVisible())
158
// popup.setVisible(false);
159
}
160         });
161     }
162
163     // Supporting actions
164
class CopyAction extends AbstractCopyAction {
165         public void actionPerformed(ActionEvent JavaDoc evt) {
166             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection JavaDoc(getText()), XTextField.this);
167         }
168     }
169
170     class CutAction extends AbstractAction JavaDoc {
171         public CutAction() {
172             putValue(Action.NAME, "Cut");
173             putValue(Action.SMALL_ICON, new ResourceIcon(XTextField.class, "/images/actions/cut-16x16.png"));
174             putValue(Action.SHORT_DESCRIPTION, "Cut selection");
175             putValue(Action.LONG_DESCRIPTION, "Cut the selection from the text and place it in the clipboard");
176             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('u'));
177             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
178         }
179
180         public void actionPerformed(ActionEvent JavaDoc evt) {
181             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection JavaDoc(getText()), XTextField.this);
182             setText("");
183         }
184     }
185
186     class PasteAction extends AbstractAction JavaDoc {
187         public PasteAction() {
188             putValue(Action.NAME, "Paste");
189             putValue(Action.SMALL_ICON, new ResourceIcon(XTextField.class, "/images/actions/paste-16x16.png"));
190             putValue(Action.SHORT_DESCRIPTION, "Paste clipboard content");
191             putValue(Action.LONG_DESCRIPTION, "Paste the clipboard contents to the current care position or replace the selection");
192             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('p'));
193             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
194         }
195
196         public void actionPerformed(ActionEvent JavaDoc evt) {
197             Transferable JavaDoc t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this);
198
199             if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
200                 try {
201                     setText(t.getTransferData(DataFlavor.stringFlavor).toString());
202                 } catch (Exception JavaDoc e) {
203                     // Dont care
204
}
205             }
206         }
207     }
208
209     class DeleteAction extends AbstractAction JavaDoc {
210         public DeleteAction() {
211             putValue(Action.NAME, "Delete");
212             putValue(Action.SMALL_ICON, new ResourceIcon(XTextField.class, "/images/actions/delete-16x16.png"));
213             putValue(Action.SHORT_DESCRIPTION, "Delete selection");
214             putValue(Action.LONG_DESCRIPTION, "Delete the selection from the text");
215             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('d'));
216             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
217         }
218
219         public void actionPerformed(ActionEvent JavaDoc evt) {
220             setText("");
221         }
222     }
223
224     class SelectAllAction extends AbstractSelectAllAction {
225         public void actionPerformed(ActionEvent JavaDoc evt) {
226             selectAll();
227         }
228     }
229 }
230
Popular Tags