1 11 package org.eclipse.jdt.internal.ui; 12 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.jface.preference.IPreferenceStore; 20 21 import org.eclipse.jface.text.templates.ContextTypeRegistry; 22 import org.eclipse.jface.text.templates.Template; 23 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; 24 import org.eclipse.jface.text.templates.persistence.TemplateStore; 25 26 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore; 27 28 29 32 public final class CompatibilityTemplateStore extends ContributionTemplateStore { 33 34 35 private org.eclipse.jdt.internal.corext.template.java.TemplateSet fLegacySet; 36 37 public CompatibilityTemplateStore(ContextTypeRegistry registry, IPreferenceStore store, String key, org.eclipse.jdt.internal.corext.template.java.TemplateSet legacySet) { 38 super(registry, store, key); 39 fLegacySet= legacySet; 40 } 41 42 public void load() throws IOException { 43 super.load(); 44 45 if (fLegacySet != null) { 46 47 List legacyTemplates= new ArrayList (Arrays.asList(fLegacySet.getTemplates())); 48 fLegacySet.clear(); 49 50 TemplatePersistenceData[] datas= getTemplateData(true); 51 for (Iterator it= legacyTemplates.listIterator(); it.hasNext();) { 52 Template t= (Template) it.next(); 53 TemplatePersistenceData orig= findSimilarTemplate(datas, t, isCodeTemplates()); 54 if (orig == null) { if (!isCodeTemplates()) 56 add(new TemplatePersistenceData(t, true)); 57 } else { if (!orig.getTemplate().getPattern().equals(t.getPattern())) 59 orig.setTemplate(t); 61 } 62 } 63 64 save(); 65 fLegacySet= null; 66 } 67 } 68 69 private static TemplatePersistenceData findSimilarTemplate(TemplatePersistenceData[] datas, Template template, boolean isCodeTemplates) { 70 for (int i= 0; i < datas.length; i++) { 71 TemplatePersistenceData data= datas[i]; 72 Template orig= data.getTemplate(); 73 if (isSimilar(template, orig, isCodeTemplates)) 74 return data; 75 } 76 77 return null; 78 } 79 80 private static boolean isSimilar(Template t, Template orig, boolean isCodeTemplates) { 81 return orig.getName().equals(t.getName()) && orig.getContextTypeId().equals(t.getContextTypeId()) 82 && (isCodeTemplates || orig.getDescription().equals(t.getDescription())); } 84 85 private boolean isCodeTemplates() { 86 return fLegacySet instanceof org.eclipse.jdt.internal.corext.template.java.CodeTemplates; 87 } 88 89 97 public static void pruneDuplicates(TemplateStore store, boolean isCodeTemplates) { 98 TemplatePersistenceData[] datas= store.getTemplateData(true); 99 for (int i= datas.length - 1; i >= 0; i--) { 100 TemplatePersistenceData data= datas[i]; 101 if (data.isUserAdded()) { 102 TemplatePersistenceData similar= findSimilarTemplate(datas, data.getTemplate(), isCodeTemplates); 104 if (similar != data && !similar.isUserAdded()) { 105 similar.setTemplate(data.getTemplate()); 106 store.delete(data); 107 } 108 } 109 } 110 111 if (isCodeTemplates) { 112 datas= store.getTemplateData(true); 113 for (int i= datas.length - 1; i >= 0; i--) { 114 if (datas[i].isUserAdded()) 115 store.delete(datas[i]); 116 } 117 } 118 } 119 } 120 | Popular Tags |