KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > CodeTemplateOverrideAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.codetemplates;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.ActionMap JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28 import javax.swing.text.DefaultEditorKit JavaDoc;
29 import javax.swing.text.TextAction JavaDoc;
30 import org.netbeans.editor.BaseKit;
31
32 /**
33  * Code template allows the client to paste itself into the given
34  * text component.
35  *
36  * @author Miloslav Metelka
37  */

38 final class CodeTemplateOverrideAction extends TextAction JavaDoc {
39
40     /**
41      * Action property that gets filled by the original action in the action map
42      * before the custom actions get installed.
43      */

44     public static final String JavaDoc ORIGINAL_ACTION_PROPERTY = "original-action"; // NOI18N
45

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 JavaDoc installOverrideActionMap(JComponent JavaDoc component,
54     CodeTemplateInsertHandler handler) {
55
56         ActionMap JavaDoc origActionMap = component.getActionMap();
57         ActionMap JavaDoc actionMap = new ActionMap JavaDoc() {
58             public Action JavaDoc get(Object JavaDoc key) {
59
60                 Action JavaDoc 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 // new CodeTemplateOverrideAction(handler, UNDO),
73
// new CodeTemplateOverrideAction(handler, REDO),
74
};
75         
76         // Install the actions into new action map
77
for (int i = actions.length - 1; i >= 0; i--) {
78             CodeTemplateOverrideAction action = actions[i];
79             Object JavaDoc actionKey = (String JavaDoc)action.getValue(Action.NAME);
80             assert (actionKey != null);
81             // Translate to the real key in the action map
82
actionKey = action.findActionKey(component);
83             if (actionKey != null) { // == null may happen during unit tests
84
Action JavaDoc 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         // Install the new action map and return the original action map
93
component.setActionMap(actionMap);
94         return origActionMap;
95     }
96     
97     private static String JavaDoc 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 JavaDoc();
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 JavaDoc getOrigAction() {
128         return (Action JavaDoc)getValue(ORIGINAL_ACTION_PROPERTY);
129     }
130     
131     public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc findActionKey(JComponent JavaDoc component) {
155         KeyStroke JavaDoc 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 JavaDoc();
177         }
178         // Assume the 'a' character will trigger defaultKeyTypedAction
179
Object JavaDoc key = component.getInputMap().get(keyStroke);
180         return key;
181     }
182
183 }
184
Popular Tags