KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28 import org.netbeans.api.editor.mimelookup.MimeLookup;
29 import org.netbeans.api.editor.settings.CodeTemplateSettings;
30 import org.netbeans.api.editor.settings.KeyBindingSettings;
31 import org.netbeans.api.editor.settings.MultiKeyBinding;
32 import org.netbeans.editor.BaseKit;
33 import org.openide.util.Lookup;
34 import org.openide.util.LookupEvent;
35 import org.openide.util.LookupListener;
36 import org.openide.util.WeakListeners;
37
38 /**
39  * Abbreviation expansion settings for a particular mimetype.
40  *
41  * @author Miloslav Metelka
42  * @version 1.00
43  */

44
45 final class AbbrevSettings implements LookupListener {
46     
47     private static final KeyStroke JavaDoc DEFAULT_EXPANSION_KEYSTROKE = KeyStroke.getKeyStroke(' ');
48     
49     private static final Map JavaDoc mimeType2settings = new HashMap JavaDoc();
50     
51     public static synchronized AbbrevSettings get(String JavaDoc mimeType) {
52         AbbrevSettings settings = (AbbrevSettings)mimeType2settings.get(mimeType);
53         if (settings == null) {
54             settings = new AbbrevSettings(mimeType);
55             mimeType2settings.put(mimeType, settings);
56         }
57         return settings;
58     }
59     
60     public static KeyStroke JavaDoc getDefaultExpansionKeyStroke() {
61         return DEFAULT_EXPANSION_KEYSTROKE;
62     }
63     
64     /** MimeLookup result of code templates */
65     private Lookup.Result codeTemplateSettingsLookupResult;
66     
67 // private Lookup.Result keyBindingSettingsLookupResult;
68

69     private KeyStroke JavaDoc expandKeyStroke;
70     
71
72     AbbrevSettings(String JavaDoc mimeType) {
73         MimeLookup mimeLookup = MimeLookup.getMimeLookup(mimeType);
74
75         codeTemplateSettingsLookupResult = mimeLookup.lookup(
76                 new Lookup.Template(CodeTemplateSettings.class));
77         codeTemplateSettingsLookupResult.addLookupListener(
78                 (LookupListener)WeakListeners.create(LookupListener.class, this, mimeLookup));
79
80 /*
81         keyBindingSettingsLookupResult = mimeLookup.lookup(
82                 new Lookup.Template(KeyBindingSettings.class));
83         keyBindingSettingsLookupResult.addLookupListener(
84                 (LookupListener)WeakListeners.create(LookupListener.class, this, mimeLookup));
85  */

86
87         resultChanged(null); // populate the cached value
88
}
89     
90     public KeyStroke JavaDoc getExpandKeyStroke() {
91     // #68528 - Temporary solution is to take the expansion keystroke
92
KeyStroke JavaDoc eks = org.netbeans.modules.editor.options.BaseOptions.getCodeTemplateExpandKey();
93     if (eks == null) {
94         eks = getDefaultExpansionKeyStroke();
95     }
96     eks = patchExpandKeystroke(eks);
97         return eks;
98     }
99
100     public String JavaDoc getExpandKeyStrokeText() {
101         KeyStroke JavaDoc expandKeyStroke = getExpandKeyStroke();
102         String JavaDoc expandKeyStrokeText;
103         if (expandKeyStroke.equals(KeyStroke.getKeyStroke(' '))) {
104             expandKeyStrokeText = "SPACE"; // NOI18N
105
} else if (expandKeyStroke.equals(KeyStroke.getKeyStroke(new Character JavaDoc(' '), InputEvent.SHIFT_MASK))) {
106             expandKeyStrokeText = "Shift-SPACE"; // NOI18N
107
} else if (expandKeyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))) {
108             expandKeyStrokeText = "TAB"; // NOI18N
109
} else if (expandKeyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))) {
110             expandKeyStrokeText = "ENTER"; // NOI18N
111
} else {
112             expandKeyStrokeText = expandKeyStroke.toString();
113         }
114         return expandKeyStrokeText;
115     }
116
117     public void resultChanged(LookupEvent ev) {
118     // #68528 - Temporary solution is to take the expansion keystroke
119
// from BaseOptions every time when asked so the following code is inactive
120

121     /* The following way of getting the result is not implemented yet.
122     KeyStroke eks = null;
123         Iterator it = codeTemplateSettingsLookupResult.allInstances().iterator();
124         if (it.hasNext()) {
125         CodeTemplateSettings cts = (CodeTemplateSettings)it.next();
126             eks = cts.getExpandKey();
127         }
128      */

129
130     /*
131     if (eks == null) {
132             eks = getDefaultExpansionKeyStroke();
133         }
134      */

135     }
136
137     private KeyStroke JavaDoc patchExpandKeystroke(KeyStroke JavaDoc eks) {
138     // Patch the keyPressed => keyTyped to prevent insertion of expand chars into editor
139
if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0))) {
140             eks = KeyStroke.getKeyStroke(' ');
141         } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_MASK))) {
142             eks = KeyStroke.getKeyStroke(new Character JavaDoc(' '), InputEvent.SHIFT_MASK);
143         } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))) {
144         } else if (eks.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))) {
145         }
146     return eks;
147     }
148     
149     
150
151 }
152
Popular Tags