KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > ProjectTemplateStore


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.viewsupport;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.ProjectScope;
24
25 import org.eclipse.jface.text.templates.Template;
26 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
27 import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
28 import org.eclipse.jface.text.templates.persistence.TemplateStore;
29
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;
31
32 import org.eclipse.jdt.ui.JavaUI;
33
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35
36 /**
37  * @since 3.1
38  */

39 public final class ProjectTemplateStore {
40     
41     private static final String JavaDoc KEY= "org.eclipse.jdt.ui.text.custom_code_templates"; //$NON-NLS-1$
42

43     private final TemplateStore fInstanceStore;
44     private final TemplateStore fProjectStore;
45     
46     public ProjectTemplateStore(IProject project) {
47         fInstanceStore= JavaPlugin.getDefault().getCodeTemplateStore();
48         if (project == null) {
49             fProjectStore= null;
50         } else {
51             final ScopedPreferenceStore projectSettings= new ScopedPreferenceStore(new ProjectScope(project), JavaUI.ID_PLUGIN);
52             fProjectStore= new TemplateStore(projectSettings, KEY) {
53                 /*
54                  * Make sure we keep the id of added code templates - add removes
55                  * it in the usual add() method
56                  */

57                 public void add(TemplatePersistenceData data) {
58                     internalAdd(data);
59                 }
60                 
61                 public void save() throws IOException JavaDoc {
62                     
63                     StringWriter JavaDoc output= new StringWriter JavaDoc();
64                     TemplateReaderWriter writer= new TemplateReaderWriter();
65                     writer.save(getTemplateData(false), output);
66                     
67                     projectSettings.setValue(KEY, output.toString());
68                     projectSettings.save();
69                 }
70             };
71         }
72     }
73     
74     public static boolean hasProjectSpecificTempates(IProject project) {
75         String JavaDoc pref= new ProjectScope(project).getNode(JavaUI.ID_PLUGIN).get(KEY, null);
76         if (pref != null && pref.trim().length() > 0) {
77             Reader JavaDoc input= new StringReader JavaDoc(pref);
78             TemplateReaderWriter reader= new TemplateReaderWriter();
79             TemplatePersistenceData[] datas;
80             try {
81                 datas= reader.read(input);
82                 return datas.length > 0;
83             } catch (IOException JavaDoc e) {
84                 // ignore
85
}
86         }
87         return false;
88     }
89     
90     
91     public TemplatePersistenceData[] getTemplateData() {
92         if (fProjectStore != null) {
93             return fProjectStore.getTemplateData(true);
94         } else {
95             return fInstanceStore.getTemplateData(true);
96         }
97     }
98     
99     public Template findTemplateById(String JavaDoc id) {
100         Template template= null;
101         if (fProjectStore != null)
102             template= fProjectStore.findTemplateById(id);
103         if (template == null)
104             template= fInstanceStore.findTemplateById(id);
105         
106         return template;
107     }
108     
109     public void load() throws IOException JavaDoc {
110         if (fProjectStore != null) {
111             fProjectStore.load();
112             
113             Set JavaDoc datas= new HashSet JavaDoc();
114             TemplatePersistenceData[] data= fProjectStore.getTemplateData(false);
115             for (int i= 0; i < data.length; i++) {
116                 datas.add(data[i].getId());
117             }
118             
119             data= fInstanceStore.getTemplateData(false);
120             for (int i= 0; i < data.length; i++) {
121                 TemplatePersistenceData orig= data[i];
122                 if (!datas.contains(orig.getId())) {
123                     TemplatePersistenceData copy= new TemplatePersistenceData(new Template(orig.getTemplate()), orig.isEnabled(), orig.getId());
124                     fProjectStore.add(copy);
125                     copy.setDeleted(true);
126                 }
127             }
128         }
129     }
130     
131     public boolean isProjectSpecific(String JavaDoc id) {
132         if (id == null) {
133             return false;
134         }
135         
136         if (fProjectStore == null)
137             return false;
138         
139         return fProjectStore.findTemplateById(id) != null;
140     }
141     
142     
143     public void setProjectSpecific(String JavaDoc id, boolean projectSpecific) {
144         Assert.isNotNull(fProjectStore);
145         
146         TemplatePersistenceData data= fProjectStore.getTemplateData(id);
147         if (data == null) {
148             return; // does not exist
149
} else {
150             data.setDeleted(!projectSpecific);
151         }
152     }
153
154     public void restoreDefaults() {
155         if (fProjectStore == null) {
156             fInstanceStore.restoreDefaults();
157         } else {
158             fProjectStore.restoreDefaults();
159         }
160     }
161     
162     public void save() throws IOException JavaDoc {
163         if (fProjectStore == null) {
164             fInstanceStore.save();
165         } else {
166             fProjectStore.save();
167         }
168     }
169     
170     public void revertChanges() throws IOException JavaDoc {
171         if (fProjectStore != null) {
172             // nothing to do
173
} else {
174             fInstanceStore.load();
175         }
176     }
177 }
178
Popular Tags