1 19 20 package org.netbeans.modules.options.codetemplates; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.Vector ; 32 import javax.swing.KeyStroke ; 33 import javax.swing.event.TableModelEvent ; 34 import javax.swing.event.TableModelListener ; 35 import javax.swing.table.DefaultTableModel ; 36 import org.netbeans.api.editor.mimelookup.MimeLookup; 37 import org.netbeans.api.editor.mimelookup.MimePath; 38 import org.netbeans.modules.editor.options.BaseOptions; 39 import org.netbeans.modules.editor.settings.storage.api.EditorSettings; 40 import org.openide.util.NbBundle; 41 42 43 class CodeTemplatesModel implements TableModelListener { 44 45 48 private List languages = new ArrayList (); 49 private Map languageToMimeType = new HashMap (); 50 private Set modifiedLanguages = new HashSet (); 51 private Map languageToModel = new HashMap (); 52 private Map modelToLanguage = new HashMap (); 53 54 55 CodeTemplatesModel () { 56 57 Vector columns = new Vector (); 58 columns.add (loc ("Abbreviation_Title")); 59 columns.add (loc ("Expanded_Text_Title")); 60 61 Set mimeTypes = EditorSettings.getDefault().getAllMimeTypes(); 62 for(Iterator i = mimeTypes.iterator(); i.hasNext(); ) { 63 String mimeType = (String ) i.next(); 64 65 if (isCompoundMimeType(mimeType)) { continue; 69 } 70 71 BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class); 73 if (baseOptions == null) { 74 continue; 75 } 76 77 String language = EditorSettings.getDefault().getLanguageName(mimeType); 79 languages.add(language); 80 Collections.sort(languages); 81 languageToMimeType.put(language, mimeType); 82 83 Map abbreviationsMap = baseOptions.getAbbrevMap (); 85 86 List table = new ArrayList (); 88 for(Iterator it2 = abbreviationsMap.keySet().iterator(); it2.hasNext(); ) { 89 String abbreviation = (String ) it2.next(); 90 Vector line = new Vector (2); 91 line.add(abbreviation); 92 line.add(abbreviationsMap.get(abbreviation)); 93 table.add(line); 94 } 95 Collections.sort(table, new MComparator()); 96 97 DefaultTableModel tableModel = new DefaultTableModel (new Vector (table), columns) { 99 public boolean isCellEditable(int row, int column) { 100 return false; 101 } 102 }; 103 104 tableModel.addTableModelListener(this); 105 modelToLanguage.put(tableModel, language); 106 languageToModel.put(language, tableModel); 107 } 108 109 expander = BaseOptions.getCodeTemplateExpandKey (); 110 } 111 112 private boolean isCompoundMimeType(String mimeType) { 113 int idx = mimeType.lastIndexOf('+'); 114 return idx != -1 && idx < mimeType.length() - 1; 115 } 116 117 List getLanguages () { 118 return Collections.unmodifiableList(languages); 119 } 120 121 String getMimeType (String language) { 122 return (String ) languageToMimeType.get (language); 123 } 124 125 DefaultTableModel getTableModel (String language) { 126 return (DefaultTableModel ) languageToModel.get (language); 127 } 128 129 void addRow (String language, String abbreviation, String text) { 130 DefaultTableModel tableModel = getTableModel (language); 131 Vector newLine = new Vector (); 132 newLine.add (abbreviation); 133 newLine.add (text); 134 tableModel.insertRow (0, newLine); 135 modifiedLanguages.add (language); 136 } 137 138 void removeRow (String language, int index) { 139 DefaultTableModel tableModel = getTableModel (language); 140 tableModel.removeRow (index); 141 modifiedLanguages.add (language); 142 } 143 144 void saveChanges () { 145 for(Iterator i = modifiedLanguages.iterator(); i.hasNext(); ) { 147 String language = (String ) i.next(); 148 String mimeType = (String ) languageToMimeType.get(language); 149 150 BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class); 152 153 DefaultTableModel tableModel = getTableModel(language); 155 Map abbreviationsMap = new HashMap (); 156 for (int idx = 0; idx < tableModel.getRowCount(); idx++) { 157 abbreviationsMap.put( 158 tableModel.getValueAt(idx, 0), 159 tableModel.getValueAt(idx, 1) 160 ); 161 } 162 163 baseOptions.setAbbrevMap(abbreviationsMap); 165 } 166 167 if (expander != null && !BaseOptions.getCodeTemplateExpandKey().equals(expander)) { 169 BaseOptions.setCodeTemplateExpandKey(expander); 170 } 171 } 172 173 boolean isChanged () { 174 return !modifiedLanguages.isEmpty () || 175 !BaseOptions.getCodeTemplateExpandKey ().equals (expander); 176 } 177 178 public void tableChanged (TableModelEvent e) { 179 DefaultTableModel tableModel = (DefaultTableModel ) e.getSource (); 180 String language = (String ) modelToLanguage.get (tableModel); 181 modifiedLanguages.add (language); 182 } 183 184 private static String loc (String key) { 185 return NbBundle.getMessage (CodeTemplatesModel.class, key); 186 } 187 188 KeyStroke getExpander () { 189 return expander; 190 } 191 192 private KeyStroke expander; 193 void setExpander (KeyStroke expander) { 194 this.expander = expander; 195 } 196 197 private static class MComparator implements Comparator { 198 public int compare (Object o1, Object o2) { 199 String s1 = (String ) ((Vector ) o1).get (0); 200 String s2 = (String ) ((Vector ) o2).get (0); 201 return s1.compareTo (s2); 202 } 203 } 204 } 205 206 207 | Popular Tags |