1 19 20 package org.netbeans.modules.options.macros; 21 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 import javax.swing.table.DefaultTableModel ; 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 46 private Map macroNameToText; 47 private DefaultTableModel 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 macroNameToText = new HashMap (); 61 Set mimeTypes = EditorSettings.getDefault().getMimeTypes(); 62 for(Iterator i = mimeTypes.iterator(); i.hasNext(); ) { 63 String mimeType = (String ) 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 Vector data = new Vector (); 73 for(Iterator it = macroNameToText.keySet().iterator(); it.hasNext(); ) { 74 String macroName = (String ) it.next (); 75 String shortcut = ""; 76 ActionImpl action = keymapModel.findActionForId 77 ("macro-" + macroName); 78 if (action == null) 79 action = keymapModel.findActionForId (macroName); 80 if (action != null) { 81 String [] shortcuts = keymapModel.getShortcuts (action); 82 if (shortcuts.length > 0) 83 shortcut = shortcuts [0]; 84 } 85 Vector line = new Vector (); 86 line.add (macroName); 87 line.add (shortcut); 88 data.add (line); 89 } 90 Collections.sort (data, new MComparator ()); 91 Vector columns = new Vector (2); 92 columns.add (loc ("Macro_Name_Title")); 93 columns.add (loc ("Macro_Code_Title")); 94 tableModel = new DefaultTableModel (data, columns) { 95 public boolean isCellEditable (int row, int column) { 96 return false; 97 } 98 }; 99 tableModel.getColumnName(2); 100 } 101 102 DefaultTableModel getShortcutsTableModel () { 103 return tableModel; 104 } 105 106 boolean isChanged () { 107 return changed; 108 } 109 110 Collection getMacroNames () { 111 return Collections.unmodifiableCollection (macroNameToText.keySet ()); 112 } 113 114 String getMacroText (String macroName) { 115 return (String ) macroNameToText.get (macroName); 116 } 117 118 void addMacro (String macroName, String text) { 119 tableModel.insertRow (0, new Object [] {macroName, text}); 120 macroNameToText.put (macroName, text); 121 } 122 123 void removeMacro (int index) { 124 String macroName = (String ) 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 macroName, String 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 shortcut) { 145 tableModel.setValueAt (shortcut, index, 1); 146 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 loc (String key) { 175 return NbBundle.getMessage (MacrosPanel.class, key); 176 } 177 178 private void saveMacros () { 179 Set mimeTypes = EditorSettings.getDefault().getMimeTypes(); 180 for(Iterator i = mimeTypes.iterator(); i.hasNext(); ) { 181 String mimeType = (String ) i.next(); 182 BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class); 183 if (baseOptions != null) { 184 baseOptions.setMacroMap(new HashMap (macroNameToText)); 185 } 186 } 187 } 188 189 190 192 private static class MComparator implements Comparator { 193 public int compare (Object o1, Object o2) { 194 String s1 = (String ) ((Vector ) o1).get (0); 195 String s2 = (String ) ((Vector ) o2).get (0); 196 return s1.compareTo (s2); 197 } 198 } 199 } 200 | Popular Tags |