KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > macros > MacrosModel


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.modules.options.macros;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30 import javax.swing.table.DefaultTableModel JavaDoc;
31 import org.netbeans.api.editor.mimelookup.MimeLookup;
32 import org.netbeans.api.editor.mimelookup.MimePath;
33 import org.netbeans.editor.BaseKit;
34 import org.netbeans.modules.editor.options.BaseOptions;
35 import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
36 import org.netbeans.modules.options.keymap.ActionImpl;
37 import org.netbeans.modules.options.keymap.KeymapViewModel;
38 import org.openide.util.Lookup;
39 import org.openide.util.NbBundle;
40
41
42 class MacrosModel {
43     
44     private KeymapViewModel keymapModel;
45     /** Map (String (macro name) > String (macro text)).*/
46     private Map JavaDoc macroNameToText;
47     private DefaultTableModel JavaDoc tableModel;
48     private boolean changed = false;
49
50     
51     MacrosModel (Lookup lookup) {
52         keymapModel = (KeymapViewModel) lookup.lookup
53             (KeymapViewModel.class);
54         init ();
55     }
56     
57     private void init () {
58         
59         // 1) init macroNameToText
60
macroNameToText = new HashMap JavaDoc();
61         Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
62         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
63             String JavaDoc mimeType = (String JavaDoc) i.next();
64             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
65             if (baseOptions != null) {
66                 macroNameToText.putAll(baseOptions.getMacroMap());
67             }
68         }
69         macroNameToText.remove(null);
70         
71         // load shortcuts & create data for table
72
Vector JavaDoc data = new Vector JavaDoc ();
73         for(Iterator JavaDoc it = macroNameToText.keySet().iterator(); it.hasNext(); ) {
74             String JavaDoc macroName = (String JavaDoc) it.next ();
75             String JavaDoc shortcut = "";
76             ActionImpl action = keymapModel.findActionForId
77                 ("macro-" + macroName);
78             if (action == null)
79                 action = keymapModel.findActionForId (macroName);
80             if (action != null) {
81                 String JavaDoc[] shortcuts = keymapModel.getShortcuts (action);
82                 if (shortcuts.length > 0)
83                     shortcut = shortcuts [0];
84             }
85             Vector JavaDoc line = new Vector JavaDoc ();
86             line.add (macroName);
87             line.add (shortcut);
88             data.add (line);
89         }
90         Collections.sort (data, new MComparator ());
91         Vector JavaDoc columns = new Vector JavaDoc (2);
92         columns.add (loc ("Macro_Name_Title"));
93         columns.add (loc ("Macro_Code_Title"));
94         tableModel = new DefaultTableModel JavaDoc (data, columns) {
95             public boolean isCellEditable (int row, int column) {
96                 return false;
97             }
98         };
99         tableModel.getColumnName(2);
100     }
101     
102     DefaultTableModel JavaDoc getShortcutsTableModel () {
103         return tableModel;
104     }
105     
106     boolean isChanged () {
107         return changed;
108     }
109     
110     Collection JavaDoc getMacroNames () {
111         return Collections.unmodifiableCollection (macroNameToText.keySet ());
112     }
113     
114     String JavaDoc getMacroText (String JavaDoc macroName) {
115         return (String JavaDoc) macroNameToText.get (macroName);
116     }
117     
118     void addMacro (String JavaDoc macroName, String JavaDoc text) {
119         tableModel.insertRow (0, new Object JavaDoc [] {macroName, text});
120         macroNameToText.put (macroName, text);
121     }
122     
123     void removeMacro (int index) {
124         String JavaDoc macroName = (String JavaDoc) tableModel.getValueAt (index, 0);
125         macroNameToText.remove (macroName);
126         tableModel.removeRow (index);
127
128         keymapModel.refreshActions();
129         ActionImpl actionImpl = keymapModel.findActionForId(BaseKit.macroActionPrefix + macroName);
130         if (actionImpl != null) {
131             keymapModel.setShortcuts(actionImpl, Collections.emptySet());
132         }
133         changed = true;
134     }
135     
136     void setMacroText (String JavaDoc macroName, String JavaDoc text) {
137         if (macroNameToText.containsKey (macroName) &&
138             text.equals (macroNameToText.get (macroName))
139         ) return;
140         macroNameToText.put (macroName, text);
141         changed = true;
142     }
143     
144     void setShortcut (int index, String JavaDoc shortcut) {
145         tableModel.setValueAt (shortcut, index, 1);
146 // BaseOptions baseOptions = (BaseOptions) BaseOptions.findObject
147
// (BaseOptions.class, true);
148
// baseOptions.setMacroMap (new HashMap (macroNameToText));
149
// BaseKit editorKit = BaseKit.getKit (ExtKit.class);
150
// TextAction textAction = (TextAction) editorKit.getActionByName
151
// ("macro-" + tableModel.getValueAt (index, 0));
152
// System.out.println("textAction " + textAction);
153
// ActionImpl actionImpl = new EditorBridge.EditorAction (textAction);
154
saveMacros ();
155         keymapModel.refreshActions ();
156         ActionImpl actionImpl = keymapModel.findActionForId (
157             BaseKit.macroActionPrefix + tableModel.getValueAt (index, 0)
158         );
159         keymapModel.setShortcuts (actionImpl, Collections.singleton (shortcut));
160         changed = true;
161     }
162
163     void applyChanges () {
164         saveMacros ();
165         keymapModel.apply();
166         changed = false;
167     }
168     
169     void cancel () {
170         init ();
171         changed = false;
172     }
173     
174     private static String JavaDoc loc (String JavaDoc key) {
175         return NbBundle.getMessage (MacrosPanel.class, key);
176     }
177     
178     private void saveMacros () {
179         Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
180         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
181             String JavaDoc mimeType = (String JavaDoc) i.next();
182             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
183             if (baseOptions != null) {
184                 baseOptions.setMacroMap(new HashMap JavaDoc(macroNameToText));
185             }
186         }
187     }
188     
189     
190     // innerclasses ............................................................
191

192     private static class MComparator implements Comparator JavaDoc {
193         public int compare (Object JavaDoc o1, Object JavaDoc o2) {
194             String JavaDoc s1 = (String JavaDoc) ((Vector JavaDoc) o1).get (0);
195             String JavaDoc s2 = (String JavaDoc) ((Vector JavaDoc) o2).get (0);
196             return s1.compareTo (s2);
197         }
198     }
199 }
200
Popular Tags