KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > CompatibilityTemplateStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui;
12
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
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 /**
30  * for compatibility only - don't use
31  */

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 JavaDoc 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 JavaDoc {
43         super.load();
44         
45         if (fLegacySet != null) {
46             
47             List JavaDoc legacyTemplates= new ArrayList JavaDoc(Arrays.asList(fLegacySet.getTemplates()));
48             fLegacySet.clear();
49             
50             TemplatePersistenceData[] datas= getTemplateData(true);
51             for (Iterator JavaDoc it= legacyTemplates.listIterator(); it.hasNext();) {
52                 Template t= (Template) it.next();
53                 TemplatePersistenceData orig= findSimilarTemplate(datas, t, isCodeTemplates());
54                 if (orig == null) { // no contributed match for the old template found
55
if (!isCodeTemplates())
56                         add(new TemplatePersistenceData(t, true));
57                 } else { // a contributed template seems to be the descendant of the non-id template t
58
if (!orig.getTemplate().getPattern().equals(t.getPattern()))
59                         // add as modified contributed template if changed compared to the original
60
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())); // only use description for templates (for, while...)
83
}
84     
85     private boolean isCodeTemplates() {
86         return fLegacySet instanceof org.eclipse.jdt.internal.corext.template.java.CodeTemplates;
87     }
88
89     /**
90      * Removes any duplicates from a template store. Duplicate user added templates
91      * are copied over their contributed siblings. If isCodeTemplates is true,
92      * any user added templates are then removed.
93      *
94      * @param store
95      * @param isCodeTemplates
96      */

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                 // find a contributed template that is similar and check it
103
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