1 19 20 package org.netbeans.lib.editor.codetemplates; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.KeyEvent ; 24 import javax.swing.Action ; 25 import javax.swing.ActionMap ; 26 import javax.swing.JComponent ; 27 import javax.swing.KeyStroke ; 28 import javax.swing.text.DefaultEditorKit ; 29 import javax.swing.text.TextAction ; 30 import org.netbeans.editor.BaseKit; 31 32 38 final class CodeTemplateOverrideAction extends TextAction { 39 40 44 public static final String ORIGINAL_ACTION_PROPERTY = "original-action"; 46 private static final int DEFAULT_KEY_TYPED = 0; 47 private static final int TAB = 1; 48 private static final int SHIFT_TAB = 2; 49 private static final int ENTER = 3; 50 private static final int UNDO = 4; 51 private static final int REDO = 5; 52 53 public static ActionMap installOverrideActionMap(JComponent component, 54 CodeTemplateInsertHandler handler) { 55 56 ActionMap origActionMap = component.getActionMap(); 57 ActionMap actionMap = new ActionMap () { 58 public Action get(Object key) { 59 60 Action retValue; 61 62 retValue = super.get(key); 63 return retValue; 64 } 65 66 }; 67 CodeTemplateOverrideAction[] actions = new CodeTemplateOverrideAction[] { 68 new CodeTemplateOverrideAction(handler, DEFAULT_KEY_TYPED), 69 new CodeTemplateOverrideAction(handler, TAB), 70 new CodeTemplateOverrideAction(handler, SHIFT_TAB), 71 new CodeTemplateOverrideAction(handler, ENTER), 72 }; 75 76 for (int i = actions.length - 1; i >= 0; i--) { 78 CodeTemplateOverrideAction action = actions[i]; 79 Object actionKey = (String )action.getValue(Action.NAME); 80 assert (actionKey != null); 81 actionKey = action.findActionKey(component); 83 if (actionKey != null) { Action origAction = origActionMap.get(actionKey); 85 action.putValue(ORIGINAL_ACTION_PROPERTY, origAction); 86 actionMap.put(actionKey, action); 87 } 88 } 89 90 actionMap.setParent(origActionMap); 91 92 component.setActionMap(actionMap); 94 return origActionMap; 95 } 96 97 private static String actionType2Name(int actionType) { 98 switch (actionType) { 99 case DEFAULT_KEY_TYPED: 100 return DefaultEditorKit.defaultKeyTypedAction; 101 case TAB: 102 return BaseKit.insertTabAction; 103 case SHIFT_TAB: 104 return BaseKit.removeTabAction; 105 case ENTER: 106 return DefaultEditorKit.insertBreakAction; 107 case UNDO: 108 return BaseKit.undoAction; 109 case REDO: 110 return BaseKit.redoAction; 111 default: 112 throw new IllegalArgumentException (); 113 } 114 } 115 116 117 private final CodeTemplateInsertHandler handler; 118 119 private final int actionType; 120 121 private CodeTemplateOverrideAction(CodeTemplateInsertHandler handler, int actionType) { 122 super(actionType2Name(actionType)); 123 this.handler = handler; 124 this.actionType = actionType; 125 } 126 127 private Action getOrigAction() { 128 return (Action )getValue(ORIGINAL_ACTION_PROPERTY); 129 } 130 131 public void actionPerformed(ActionEvent evt) { 132 switch (actionType) { 133 case DEFAULT_KEY_TYPED: 134 handler.defaultKeyTypedAction(evt, getOrigAction()); 135 break; 136 case TAB: 137 handler.tabAction(evt, getOrigAction()); 138 break; 139 case SHIFT_TAB: 140 handler.shiftTabAction(evt); 141 break; 142 case ENTER: 143 handler.enterAction(evt); 144 break; 145 case UNDO: 146 handler.undoAction(evt); 147 break; 148 case REDO: 149 handler.redoAction(evt); 150 break; 151 } 152 } 153 154 Object findActionKey(JComponent component) { 155 KeyStroke keyStroke; 156 switch (actionType) { 157 case DEFAULT_KEY_TYPED: 158 keyStroke = KeyStroke.getKeyStroke('a'); 159 break; 160 case TAB: 161 keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); 162 break; 163 case SHIFT_TAB: 164 keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK); 165 break; 166 case ENTER: 167 keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 168 break; 169 case UNDO: 170 keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK); 171 break; 172 case REDO: 173 keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK); 174 break; 175 default: 176 throw new IllegalArgumentException (); 177 } 178 Object key = component.getInputMap().get(keyStroke); 180 return key; 181 } 182 183 } 184 | Popular Tags |