KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > swing > ATEKeyBindings


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.ate.swing;
33
34 import org.antlr.works.ate.ATETextPane;
35 import org.antlr.xjlib.foundation.XJSystem;
36
37 import javax.swing.*;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.DefaultEditorKit JavaDoc;
40 import javax.swing.text.Document JavaDoc;
41 import java.awt.*;
42 import java.awt.datatransfer.Clipboard JavaDoc;
43 import java.awt.datatransfer.StringSelection JavaDoc;
44 import java.awt.event.ActionEvent JavaDoc;
45 import java.awt.event.KeyEvent JavaDoc;
46
47 public class ATEKeyBindings {
48
49     private ATETextPane textComponent = null;
50
51     public ATEKeyBindings(ATETextPane textComponent) {
52         this.textComponent = textComponent;
53         if(XJSystem.isMacOS()) {
54             addEmacsKeyBindings();
55         }
56
57         addStandardKeyBindings();
58     }
59
60     public void addStandardKeyBindings() {
61         InputMap inputMap = textComponent.getInputMap();
62
63         // HOME to move cursor to begin of line
64
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0);
65         inputMap.put(key, DefaultEditorKit.beginLineAction);
66
67         // SHIFT-HOME to move cursor to begin of line and select
68
key = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, Event.SHIFT_MASK);
69         inputMap.put(key, DefaultEditorKit.selectionBeginLineAction);
70
71         // END to move cursor to end of line
72
key = KeyStroke.getKeyStroke(KeyEvent.VK_END, 0);
73         inputMap.put(key, DefaultEditorKit.endLineAction);
74
75         // SHIFT-END to move cursor to end of line and select
76
key = KeyStroke.getKeyStroke(KeyEvent.VK_END, Event.SHIFT_MASK);
77         inputMap.put(key, DefaultEditorKit.selectionEndLineAction);
78
79         // Add shift-delete to act as the standard delete key
80
addKeyBinding("SHIFT_DELETE", KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, Event.SHIFT_MASK), new AbstractAction() {
81             public void actionPerformed(ActionEvent JavaDoc actionEvent) {
82                 if(!textComponent.isWritable()) return;
83                 textComponent.getActionMap().get(DefaultEditorKit.deleteNextCharAction).actionPerformed(actionEvent);
84             }
85         });
86     }
87
88     public void addEmacsKeyBindings() {
89         InputMap inputMap = textComponent.getInputMap();
90
91         // Ctrl-b to go backward one character
92
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
93         inputMap.put(key, DefaultEditorKit.backwardAction);
94
95         // Ctrl-f to go forward one character
96
key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
97         inputMap.put(key, DefaultEditorKit.forwardAction);
98
99         // Ctrl-p to go up one line
100
key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
101         inputMap.put(key, DefaultEditorKit.upAction);
102
103         // Ctrl-n to go down one line
104
key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK);
105         inputMap.put(key, DefaultEditorKit.downAction);
106
107         // Ctrl-a to move cursor to begin of line
108
key = KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK);
109         inputMap.put(key, DefaultEditorKit.beginLineAction);
110
111         // Ctrl-e to move cursor to end of line
112
key = KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK);
113         inputMap.put(key, DefaultEditorKit.endLineAction);
114
115         // Ctrl-d to delete the character under the cursor
116
addKeyBinding("CONTROL_D", KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK), new AbstractAction() {
117             public void actionPerformed(ActionEvent JavaDoc actionEvent) {
118                 if(!textComponent.isWritable()) return;
119                 textComponent.getActionMap().get(DefaultEditorKit.deleteNextCharAction).actionPerformed(actionEvent);
120             }
121         });
122
123         // Ctrl-k to delete the characters from the current position to the end of the line
124
// Has to create a custom action to handle this one.
125
addKeyBinding("CONTROL_K", KeyStroke.getKeyStroke(KeyEvent.VK_K, Event.CTRL_MASK), new AbstractAction() {
126             public void actionPerformed(ActionEvent JavaDoc e) {
127                 if(!textComponent.isWritable()) return;
128
129                 int start = textComponent.getCaretPosition();
130                 Document JavaDoc doc = textComponent.getDocument();
131                 String JavaDoc t;
132                 try {
133                     t = doc.getText(start, doc.getLength()-start);
134                 } catch (BadLocationException JavaDoc e1) {
135                     e1.printStackTrace();
136                     return;
137                 }
138
139                 int end = 0;
140                 while(end<t.length() && t.charAt(end) != '\n' && t.charAt(end) != '\r') {
141                     end++;
142                 }
143
144                 try {
145                     end = Math.max(1, end);
146
147                     String JavaDoc content = doc.getText(start, end);
148                     doc.remove(start, end);
149
150                     // Copy the deleted portion of text into the system clipboard
151
Clipboard JavaDoc cb = Toolkit.getDefaultToolkit().getSystemClipboard();
152                     cb.setContents(new StringSelection JavaDoc(content), null);
153                 } catch (BadLocationException JavaDoc e1) {
154                     e1.printStackTrace();
155                 }
156             }
157         });
158
159         // Ctrl-t swap the two characters before and after the current position
160
// Has to create a custom action to handle this one.
161
addKeyBinding("CONTROL_T", KeyStroke.getKeyStroke(KeyEvent.VK_T, Event.CTRL_MASK), new AbstractAction() {
162             public void actionPerformed(ActionEvent JavaDoc e) {
163                 if(!textComponent.isWritable()) return;
164
165                 int p = textComponent.getCaretPosition();
166                 Document JavaDoc doc = textComponent.getDocument();
167
168                 if(p < 1 || p >= doc.getLength())
169                     return;
170
171                 try {
172                     String JavaDoc before = doc.getText(p-1, 1);
173                     doc.remove(p-1, 1);
174                     doc.insertString(p, before, null);
175                     textComponent.setCaretPosition(p);
176                 } catch (BadLocationException JavaDoc e1) {
177                     e1.printStackTrace();
178                 }
179             }
180         });
181     }
182
183     public void addKeyBinding(String JavaDoc name, KeyStroke keystroke, AbstractAction action) {
184         textComponent.getActionMap().put(name, action);
185         textComponent.getInputMap().put(keystroke, name);
186     }
187
188 }
189
Popular Tags