Lines 41-46
Link Here
|
41 |
|
41 |
|
42 |
import java.lang.ref.Reference; |
42 |
import java.lang.ref.Reference; |
43 |
import java.lang.ref.WeakReference; |
43 |
import java.lang.ref.WeakReference; |
|
|
44 |
import java.util.HashMap; |
44 |
import java.util.Map; |
45 |
import java.util.Map; |
45 |
import java.util.WeakHashMap; |
46 |
import java.util.WeakHashMap; |
46 |
import java.util.logging.Logger; |
47 |
import java.util.logging.Logger; |
Lines 99-108
Link Here
|
99 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
100 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
100 |
*/ |
101 |
*/ |
101 |
public static CodeStylePreferences get(Document doc) { |
102 |
public static CodeStylePreferences get(Document doc) { |
|
|
103 |
return get(doc, doc != null ? (String) doc.getProperty("mimeType") : null); //NOI18N |
104 |
} |
105 |
|
106 |
/** |
107 |
* Gets <code>CodeStylePreferences</code> for a document and an embedding mimeType. |
108 |
* This is the prefered method to use. Whenever you have both <code>Document</code> and its |
109 |
* <code>FileObject</code> always use this method and provide the <code>Document</code> |
110 |
* instance. |
111 |
* |
112 |
* @param doc The document to get <code>CodeStylePreferences</code> for, |
113 |
* can be <code>null</code>. If <code>null</code>, the method will return |
114 |
* global preferences for 'all languages'. |
115 |
* |
116 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
117 |
*/ |
118 |
public static CodeStylePreferences get(Document doc, String mimeType) { |
102 |
if (doc != null) { |
119 |
if (doc != null) { |
103 |
return get(doc, (String) doc.getProperty("mimeType")); //NOI18N |
120 |
return getPreferences(doc, mimeType); |
104 |
} else { |
121 |
} else { |
105 |
return get(null, null); |
122 |
return getPreferences(null, null); |
106 |
} |
123 |
} |
107 |
} |
124 |
} |
108 |
|
125 |
|
Lines 118-130
Link Here
|
118 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
135 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
119 |
*/ |
136 |
*/ |
120 |
public static CodeStylePreferences get(FileObject file) { |
137 |
public static CodeStylePreferences get(FileObject file) { |
|
|
138 |
return get(file, file != null ? file.getMIMEType() : null); |
139 |
} |
140 |
|
141 |
/** |
142 |
* Gets <code>CodeStylePreferences</code> for a file. If you also have a |
143 |
* <code>Document</code> instance you should use the {@link #get(javax.swing.text.Document)} |
144 |
* method. |
145 |
* |
146 |
* @param file The file to get <code>CodeStylePreferences</code> for, |
147 |
* can be <code>null</code>. If <code>null</code>, the method will return |
148 |
* global preferences for 'all languages'. |
149 |
* |
150 |
* @return The <code>CodeStylePreferences</code>, never <code>null</code>. |
151 |
*/ |
152 |
public static CodeStylePreferences get(FileObject file, String mimeType) { |
121 |
if (file != null) { |
153 |
if (file != null) { |
122 |
return get(file, file.getMIMEType()); //NOI18N |
154 |
return getPreferences(file, mimeType); //NOI18N |
123 |
} else { |
155 |
} else { |
124 |
return get(null, null); |
156 |
return getPreferences(null, null); |
125 |
} |
157 |
} |
126 |
} |
158 |
} |
127 |
|
159 |
|
128 |
public Preferences getPreferences() { |
160 |
public Preferences getPreferences() { |
129 |
synchronized (this) { |
161 |
synchronized (this) { |
130 |
// This is here solely for the purpose of previewing changes in formatting settings |
162 |
// This is here solely for the purpose of previewing changes in formatting settings |
Lines 153-159
Link Here
|
153 |
private static final String DEFAULT_PROFILE = "default"; // NOI18N |
185 |
private static final String DEFAULT_PROFILE = "default"; // NOI18N |
154 |
private static final String PROJECT_PROFILE = "project"; // NOI18N |
186 |
private static final String PROJECT_PROFILE = "project"; // NOI18N |
155 |
|
187 |
|
156 |
private static final Map<Object, CodeStylePreferences> cache = new WeakHashMap<Object, CodeStylePreferences>(); |
188 |
private static final Map<Object, Map<String, CodeStylePreferences>> cache = new WeakHashMap<Object, Map<String, CodeStylePreferences>>(); |
157 |
|
189 |
|
158 |
private final String mimeType; |
190 |
private final String mimeType; |
159 |
private final Reference<Document> refDoc; |
191 |
private final Reference<Document> refDoc; |
Lines 175-183
Link Here
|
175 |
} |
207 |
} |
176 |
}; |
208 |
}; |
177 |
|
209 |
|
178 |
private static CodeStylePreferences get(Object obj, String mimeType) { |
210 |
private static CodeStylePreferences getPreferences(Object obj, String mimeType) { |
179 |
synchronized (cache) { |
211 |
synchronized (cache) { |
180 |
CodeStylePreferences csp = cache.get(obj); |
212 |
Map<String, CodeStylePreferences> csps = cache.get(obj); |
|
|
213 |
CodeStylePreferences csp = csps != null ? csps.get(mimeType) : null; |
181 |
if (csp == null) { |
214 |
if (csp == null) { |
182 |
Document doc; |
215 |
Document doc; |
183 |
FileObject file; |
216 |
FileObject file; |
Lines 195-201
Link Here
|
195 |
mimeType, |
228 |
mimeType, |
196 |
doc == null ? null : new WeakReference<Document>(doc), |
229 |
doc == null ? null : new WeakReference<Document>(doc), |
197 |
file == null ? "no file" : file.getPath()); //NOI18N |
230 |
file == null ? "no file" : file.getPath()); //NOI18N |
198 |
cache.put(obj, csp); |
231 |
if (csps == null) { |
|
|
232 |
csps = new HashMap<String, CodeStylePreferences>(); |
233 |
cache.put(obj, csps); |
234 |
} |
235 |
csps.put(mimeType, csp); |
199 |
} |
236 |
} |
200 |
|
237 |
|
201 |
return csp; |
238 |
return csp; |