1 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 ; 39 import javax.swing.text.DefaultEditorKit ; 40 import javax.swing.text.Document ; 41 import java.awt.*; 42 import java.awt.datatransfer.Clipboard ; 43 import java.awt.datatransfer.StringSelection ; 44 import java.awt.event.ActionEvent ; 45 import java.awt.event.KeyEvent ; 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 KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0); 65 inputMap.put(key, DefaultEditorKit.beginLineAction); 66 67 key = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, Event.SHIFT_MASK); 69 inputMap.put(key, DefaultEditorKit.selectionBeginLineAction); 70 71 key = KeyStroke.getKeyStroke(KeyEvent.VK_END, 0); 73 inputMap.put(key, DefaultEditorKit.endLineAction); 74 75 key = KeyStroke.getKeyStroke(KeyEvent.VK_END, Event.SHIFT_MASK); 77 inputMap.put(key, DefaultEditorKit.selectionEndLineAction); 78 79 addKeyBinding("SHIFT_DELETE", KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, Event.SHIFT_MASK), new AbstractAction() { 81 public void actionPerformed(ActionEvent 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 KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK); 93 inputMap.put(key, DefaultEditorKit.backwardAction); 94 95 key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK); 97 inputMap.put(key, DefaultEditorKit.forwardAction); 98 99 key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK); 101 inputMap.put(key, DefaultEditorKit.upAction); 102 103 key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK); 105 inputMap.put(key, DefaultEditorKit.downAction); 106 107 key = KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK); 109 inputMap.put(key, DefaultEditorKit.beginLineAction); 110 111 key = KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK); 113 inputMap.put(key, DefaultEditorKit.endLineAction); 114 115 addKeyBinding("CONTROL_D", KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK), new AbstractAction() { 117 public void actionPerformed(ActionEvent actionEvent) { 118 if(!textComponent.isWritable()) return; 119 textComponent.getActionMap().get(DefaultEditorKit.deleteNextCharAction).actionPerformed(actionEvent); 120 } 121 }); 122 123 addKeyBinding("CONTROL_K", KeyStroke.getKeyStroke(KeyEvent.VK_K, Event.CTRL_MASK), new AbstractAction() { 126 public void actionPerformed(ActionEvent e) { 127 if(!textComponent.isWritable()) return; 128 129 int start = textComponent.getCaretPosition(); 130 Document doc = textComponent.getDocument(); 131 String t; 132 try { 133 t = doc.getText(start, doc.getLength()-start); 134 } catch (BadLocationException 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 content = doc.getText(start, end); 148 doc.remove(start, end); 149 150 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); 152 cb.setContents(new StringSelection (content), null); 153 } catch (BadLocationException e1) { 154 e1.printStackTrace(); 155 } 156 } 157 }); 158 159 addKeyBinding("CONTROL_T", KeyStroke.getKeyStroke(KeyEvent.VK_T, Event.CTRL_MASK), new AbstractAction() { 162 public void actionPerformed(ActionEvent e) { 163 if(!textComponent.isWritable()) return; 164 165 int p = textComponent.getCaretPosition(); 166 Document doc = textComponent.getDocument(); 167 168 if(p < 1 || p >= doc.getLength()) 169 return; 170 171 try { 172 String 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 e1) { 177 e1.printStackTrace(); 178 } 179 } 180 }); 181 } 182 183 public void addKeyBinding(String name, KeyStroke keystroke, AbstractAction action) { 184 textComponent.getActionMap().put(name, action); 185 textComponent.getInputMap().put(keystroke, name); 186 } 187 188 } 189 | Popular Tags |