1 11 package org.eclipse.jface.text.templates.persistence; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.eclipse.core.runtime.Assert; 22 23 import org.eclipse.jface.preference.IPersistentPreferenceStore; 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.jface.util.IPropertyChangeListener; 26 import org.eclipse.jface.util.PropertyChangeEvent; 27 28 import org.eclipse.jface.text.templates.ContextTypeRegistry; 29 import org.eclipse.jface.text.templates.Template; 30 import org.eclipse.jface.text.templates.TemplateException; 31 32 39 public class TemplateStore { 40 41 private final List fTemplates= new ArrayList (); 42 43 private IPreferenceStore fPreferenceStore; 44 48 private String fKey; 49 53 private ContextTypeRegistry fRegistry; 54 60 private boolean fIgnorePreferenceStoreChanges= false; 61 66 private IPropertyChangeListener fPropertyListener; 67 68 69 77 public TemplateStore(IPreferenceStore store, String key) { 78 Assert.isNotNull(store); 79 Assert.isNotNull(key); 80 fPreferenceStore= store; 81 fKey= key; 82 } 83 84 96 public TemplateStore(ContextTypeRegistry registry, IPreferenceStore store, String key) { 97 this(store, key); 98 fRegistry= registry; 99 } 100 101 106 public void load() throws IOException { 107 fTemplates.clear(); 108 loadContributedTemplates(); 109 loadCustomTemplates(); 110 } 111 112 120 public final void startListeningForPreferenceChanges() { 121 if (fPropertyListener == null) { 122 fPropertyListener= new IPropertyChangeListener() { 123 public void propertyChange(PropertyChangeEvent event) { 124 129 if (!fIgnorePreferenceStoreChanges && fKey.equals(event.getProperty())) 130 try { 131 load(); 132 } catch (IOException x) { 133 handleException(x); 134 } 135 } 136 }; 137 fPreferenceStore.addPropertyChangeListener(fPropertyListener); 138 } 139 140 } 141 142 148 public final void stopListeningForPreferenceChanges() { 149 if (fPropertyListener != null) { 150 fPreferenceStore.removePropertyChangeListener(fPropertyListener); 151 fPropertyListener= null; 152 } 153 } 154 155 162 protected void handleException(IOException x) { 163 x.printStackTrace(); 164 } 165 166 174 protected void loadContributedTemplates() throws IOException { 175 } 176 177 183 protected void internalAdd(TemplatePersistenceData data) { 184 if (!data.isCustom()) { 185 String id= data.getId(); 187 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 188 TemplatePersistenceData d2= (TemplatePersistenceData) it.next(); 189 if (d2.getId() != null && d2.getId().equals(id)) 190 return; 191 } 192 fTemplates.add(data); 193 } 194 } 195 196 201 public void save() throws IOException { 202 ArrayList custom= new ArrayList (); 203 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 204 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 205 if (data.isCustom() && !(data.isUserAdded() && data.isDeleted())) custom.add(data); 207 } 208 209 StringWriter output= new StringWriter (); 210 TemplateReaderWriter writer= new TemplateReaderWriter(); 211 writer.save((TemplatePersistenceData[]) custom.toArray(new TemplatePersistenceData[custom.size()]), output); 212 213 fIgnorePreferenceStoreChanges= true; 214 try { 215 fPreferenceStore.setValue(fKey, output.toString()); 216 if (fPreferenceStore instanceof IPersistentPreferenceStore) 217 ((IPersistentPreferenceStore)fPreferenceStore).save(); 218 } finally { 219 fIgnorePreferenceStoreChanges= false; 220 } 221 } 222 223 228 public void add(TemplatePersistenceData data) { 229 230 if (!validateTemplate(data.getTemplate())) 231 return; 232 233 if (data.isUserAdded()) { 234 fTemplates.add(data); 235 } else { 236 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 237 TemplatePersistenceData d2= (TemplatePersistenceData) it.next(); 238 if (d2.getId() != null && d2.getId().equals(data.getId())) { 239 d2.setTemplate(data.getTemplate()); 240 d2.setDeleted(data.isDeleted()); 241 d2.setEnabled(data.isEnabled()); 242 return; 243 } 244 } 245 246 if (data.getTemplate() != null) { 248 TemplatePersistenceData newData= new TemplatePersistenceData(data.getTemplate(), data.isEnabled()); 249 fTemplates.add(newData); 250 } 251 } 252 } 253 254 259 public void delete(TemplatePersistenceData data) { 260 if (data.isUserAdded()) 261 fTemplates.remove(data); 262 else 263 data.setDeleted(true); 264 } 265 266 269 public void restoreDeleted() { 270 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 271 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 272 if (data.isDeleted()) 273 data.setDeleted(false); 274 } 275 } 276 277 280 public void restoreDefaults() { 281 try { 282 fIgnorePreferenceStoreChanges= true; 283 fPreferenceStore.setToDefault(fKey); 284 } finally { 285 fIgnorePreferenceStoreChanges= false; 286 } 287 try { 288 load(); 289 } catch (IOException x) { 290 x.printStackTrace(); 292 } 293 } 294 295 300 public Template[] getTemplates() { 301 return getTemplates(null); 302 } 303 304 310 public Template[] getTemplates(String contextTypeId) { 311 List templates= new ArrayList (); 312 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 313 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 314 if (data.isEnabled() && !data.isDeleted() && (contextTypeId == null || contextTypeId.equals(data.getTemplate().getContextTypeId()))) 315 templates.add(data.getTemplate()); 316 } 317 318 return (Template[]) templates.toArray(new Template[templates.size()]); 319 } 320 321 327 public Template findTemplate(String name) { 328 return findTemplate(name, null); 329 } 330 331 338 public Template findTemplate(String name, String contextTypeId) { 339 Assert.isNotNull(name); 340 341 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 342 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 343 Template template= data.getTemplate(); 344 if (data.isEnabled() && !data.isDeleted() 345 && (contextTypeId == null || contextTypeId.equals(template.getContextTypeId())) 346 && name.equals(template.getName())) 347 return template; 348 } 349 350 return null; 351 } 352 353 360 public Template findTemplateById(String id) { 361 TemplatePersistenceData data= getTemplateData(id); 362 if (data != null && !data.isDeleted()) 363 return data.getTemplate(); 364 365 return null; 366 } 367 368 374 public TemplatePersistenceData[] getTemplateData(boolean includeDeleted) { 375 List datas= new ArrayList (); 376 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 377 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 378 if (includeDeleted || !data.isDeleted()) 379 datas.add(data); 380 } 381 382 return (TemplatePersistenceData[]) datas.toArray(new TemplatePersistenceData[datas.size()]); 383 } 384 385 393 public TemplatePersistenceData getTemplateData(String id) { 394 Assert.isNotNull(id); 395 for (Iterator it= fTemplates.iterator(); it.hasNext();) { 396 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 397 if (id.equals(data.getId())) 398 return data; 399 } 400 401 return null; 402 } 403 404 private void loadCustomTemplates() throws IOException { 405 String pref= fPreferenceStore.getString(fKey); 406 if (pref != null && pref.trim().length() > 0) { 407 Reader input= new StringReader (pref); 408 TemplateReaderWriter reader= new TemplateReaderWriter(); 409 TemplatePersistenceData[] datas= reader.read(input); 410 for (int i= 0; i < datas.length; i++) { 411 TemplatePersistenceData data= datas[i]; 412 add(data); 413 } 414 } 415 } 416 417 427 private boolean validateTemplate(Template template) { 428 String contextTypeId= template.getContextTypeId(); 429 if (contextExists(contextTypeId)) { 430 if (fRegistry != null) 431 try { 432 fRegistry.getContextType(contextTypeId).validate(template.getPattern()); 433 } catch (TemplateException e) { 434 return false; 435 } 436 return true; 437 } 438 439 return false; 440 } 441 442 451 private boolean contextExists(String contextTypeId) { 452 return contextTypeId != null && (fRegistry == null || fRegistry.getContextType(contextTypeId) != null); 453 } 454 455 460 protected final ContextTypeRegistry getRegistry() { 461 return fRegistry; 462 } 463 } 464 465 | Popular Tags |