KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > codetemplates > CodeTemplatesModel


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.codetemplates;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Vector JavaDoc;
32 import javax.swing.KeyStroke JavaDoc;
33 import javax.swing.event.TableModelEvent JavaDoc;
34 import javax.swing.event.TableModelListener JavaDoc;
35 import javax.swing.table.DefaultTableModel JavaDoc;
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 JavaDoc {
44
45     // A mime type is something like text/x-java and a language is a localized name of
46
// the programming language denoted by a mime type (e.g. Java).
47

48     private List JavaDoc/*<String>*/ languages = new ArrayList JavaDoc();
49     private Map JavaDoc/*<String, String>*/ languageToMimeType = new HashMap JavaDoc();
50     private Set JavaDoc/*<String>*/ modifiedLanguages = new HashSet JavaDoc();
51     private Map JavaDoc/*<String, DefaultTableModel>*/ languageToModel = new HashMap JavaDoc();
52     private Map JavaDoc/*<DefaultTableModel, String>*/ modelToLanguage = new HashMap JavaDoc();
53     
54     
55     CodeTemplatesModel () {
56         
57         Vector JavaDoc columns = new Vector JavaDoc ();
58         columns.add (loc ("Abbreviation_Title"));
59         columns.add (loc ("Expanded_Text_Title"));
60
61         Set JavaDoc mimeTypes = EditorSettings.getDefault().getAllMimeTypes();
62         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
63             String JavaDoc mimeType = (String JavaDoc) i.next();
64             
65             // Skip compound mime types (e.g. text/x-ant+xml), they inherit
66
// code templates from their base mime type
67
if (isCompoundMimeType(mimeType)) { //NOI18N
68
continue;
69             }
70             
71             // Find BaseOptions
72
BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
73             if (baseOptions == null) {
74                 continue;
75             }
76             
77             // Add the language and its mime type to the map
78
String JavaDoc language = EditorSettings.getDefault().getLanguageName(mimeType);
79             languages.add(language);
80             Collections.sort(languages);
81             languageToMimeType.put(language, mimeType);
82             
83             // Load the code templates
84
Map JavaDoc abbreviationsMap = baseOptions.getAbbrevMap ();
85
86             // Load the table
87
List JavaDoc table = new ArrayList JavaDoc();
88             for(Iterator JavaDoc it2 = abbreviationsMap.keySet().iterator(); it2.hasNext(); ) {
89                 String JavaDoc abbreviation = (String JavaDoc) it2.next();
90                 Vector JavaDoc line = new Vector JavaDoc(2);
91                 line.add(abbreviation);
92                 line.add(abbreviationsMap.get(abbreviation));
93                 table.add(line);
94             }
95             Collections.sort(table, new MComparator());
96             
97             // Create the code templates table model for this language
98
DefaultTableModel JavaDoc tableModel = new DefaultTableModel JavaDoc(new Vector JavaDoc(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 JavaDoc mimeType) {
113         int idx = mimeType.lastIndexOf('+');
114         return idx != -1 && idx < mimeType.length() - 1;
115     }
116     
117     List JavaDoc getLanguages () {
118         return Collections.unmodifiableList(languages);
119     }
120     
121     String JavaDoc getMimeType (String JavaDoc language) {
122         return (String JavaDoc) languageToMimeType.get (language);
123     }
124     
125     DefaultTableModel JavaDoc getTableModel (String JavaDoc language) {
126         return (DefaultTableModel JavaDoc) languageToModel.get (language);
127     }
128     
129     void addRow (String JavaDoc language, String JavaDoc abbreviation, String JavaDoc text) {
130         DefaultTableModel JavaDoc tableModel = getTableModel (language);
131         Vector JavaDoc newLine = new Vector JavaDoc ();
132         newLine.add (abbreviation);
133         newLine.add (text);
134         tableModel.insertRow (0, newLine);
135         modifiedLanguages.add (language);
136     }
137     
138     void removeRow (String JavaDoc language, int index) {
139         DefaultTableModel JavaDoc tableModel = getTableModel (language);
140         tableModel.removeRow (index);
141         modifiedLanguages.add (language);
142     }
143     
144     void saveChanges () {
145         // Save modified code templates
146
for(Iterator JavaDoc i = modifiedLanguages.iterator(); i.hasNext(); ) {
147             String JavaDoc language = (String JavaDoc) i.next();
148             String JavaDoc mimeType = (String JavaDoc) languageToMimeType.get(language);
149             
150             // Find BaseOptions
151
BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
152             
153             // Get the code templates from the model
154
DefaultTableModel JavaDoc tableModel = getTableModel(language);
155             Map JavaDoc abbreviationsMap = new HashMap JavaDoc();
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             // Save the code templates
164
baseOptions.setAbbrevMap(abbreviationsMap);
165         }
166
167         // Save modified expander key
168
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 JavaDoc e) {
179         DefaultTableModel JavaDoc tableModel = (DefaultTableModel JavaDoc) e.getSource ();
180         String JavaDoc language = (String JavaDoc) modelToLanguage.get (tableModel);
181         modifiedLanguages.add (language);
182     }
183     
184     private static String JavaDoc loc (String JavaDoc key) {
185         return NbBundle.getMessage (CodeTemplatesModel.class, key);
186     }
187     
188     KeyStroke JavaDoc getExpander () {
189         return expander;
190     }
191     
192     private KeyStroke JavaDoc expander;
193     void setExpander (KeyStroke JavaDoc expander) {
194         this.expander = expander;
195     }
196     
197     private static class MComparator implements Comparator JavaDoc {
198         public int compare (Object JavaDoc o1, Object JavaDoc o2) {
199             String JavaDoc s1 = (String JavaDoc) ((Vector JavaDoc) o1).get (0);
200             String JavaDoc s2 = (String JavaDoc) ((Vector JavaDoc) o2).get (0);
201             return s1.compareTo (s2);
202         }
203     }
204 }
205
206
207
Popular Tags