]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/viewsupport/ProjectTemplateStore.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / viewsupport / ProjectTemplateStore.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.viewsupport;
12
13import java.io.IOException;
14import java.io.Reader;
15import java.io.StringReader;
16import java.io.StringWriter;
17import java.util.HashSet;
18import java.util.Set;
19
20import org.eclipse.core.runtime.Assert;
21
22import org.eclipse.core.resources.IProject;
23import org.eclipse.core.resources.ProjectScope;
24
25import org.eclipse.jface.text.templates.Template;
26import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
27import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
28import org.eclipse.jface.text.templates.persistence.TemplateStore;
29
30import org.eclipse.ui.preferences.ScopedPreferenceStore;
31
32import org.eclipse.jdt.ui.JavaUI;
33
34import org.eclipse.jdt.internal.ui.JavaPlugin;
35
36/**
37 * @since 3.1
38 */
39public final class ProjectTemplateStore {
40
41 private static final String 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 @Override
58 public void add(TemplatePersistenceData data) {
59 internalAdd(data);
60 }
61
62 @Override
63 public void save() throws IOException {
64
65 TemplatePersistenceData[] templateData= ProjectTemplateStore.this.getTemplateData();
66 for (int i= 0; i < templateData.length; i++) {
67 if (isProjectSpecific(templateData[i].getId())) {
68 StringWriter output= new StringWriter();
69 TemplateReaderWriter writer= new TemplateReaderWriter();
70 writer.save(getTemplateData(false), output);
71
72 projectSettings.setValue(KEY, output.toString());
73 projectSettings.save();
74
75 return;
76 }
77 }
78
79 projectSettings.setToDefault(KEY);
80 projectSettings.save();
81 }
82 };
83 }
84 }
85
86 public static boolean hasProjectSpecificTempates(IProject project) {
87 String pref= new ProjectScope(project).getNode(JavaUI.ID_PLUGIN).get(KEY, null);
88 if (pref != null && pref.trim().length() > 0) {
89 Reader input= new StringReader(pref);
90 TemplateReaderWriter reader= new TemplateReaderWriter();
91 TemplatePersistenceData[] datas;
92 try {
93 datas= reader.read(input);
94 return datas.length > 0;
95 } catch (IOException e) {
96 // ignore
97 }
98 }
99 return false;
100 }
101
102
103 public TemplatePersistenceData[] getTemplateData() {
104 if (fProjectStore != null) {
105 return fProjectStore.getTemplateData(true);
106 } else {
107 return fInstanceStore.getTemplateData(true);
108 }
109 }
110
111 public Template findTemplateById(String id) {
112 Template template= null;
113 if (fProjectStore != null)
114 template= fProjectStore.findTemplateById(id);
115 if (template == null)
116 template= fInstanceStore.findTemplateById(id);
117
118 return template;
119 }
120
121 public void load() throws IOException {
122 if (fProjectStore != null) {
123 fProjectStore.load();
124
125 Set<String> datas= new HashSet<String>();
126 TemplatePersistenceData[] data= fProjectStore.getTemplateData(false);
127 for (int i= 0; i < data.length; i++) {
128 datas.add(data[i].getId());
129 }
130
131 data= fInstanceStore.getTemplateData(false);
132 for (int i= 0; i < data.length; i++) {
133 TemplatePersistenceData orig= data[i];
134 if (!datas.contains(orig.getId())) {
135 TemplatePersistenceData copy= new TemplatePersistenceData(new Template(orig.getTemplate()), orig.isEnabled(), orig.getId());
136 fProjectStore.add(copy);
137 copy.setDeleted(true);
138 }
139 }
140 }
141 }
142
143 public boolean isProjectSpecific(String id) {
144 if (id == null) {
145 return false;
146 }
147
148 if (fProjectStore == null)
149 return false;
150
151 return fProjectStore.findTemplateById(id) != null;
152 }
153
154
155 public void setProjectSpecific(String id, boolean projectSpecific) {
156 Assert.isNotNull(fProjectStore);
157
158 TemplatePersistenceData data= fProjectStore.getTemplateData(id);
159 if (data == null) {
160 return; // does not exist
161 } else {
162 data.setDeleted(!projectSpecific);
163 }
164 }
165
166 public void restoreDefaults() {
167 if (fProjectStore == null) {
168 fInstanceStore.restoreDefaults(false);
169 } else {
170 try {
171 load();
172 } catch (IOException e) {
173 JavaPlugin.log(e);
174 }
175 }
176 }
177
178 public void save() throws IOException {
179 if (fProjectStore == null) {
180 fInstanceStore.save();
181 } else {
182 fProjectStore.save();
183 }
184 }
185
186 public void revertChanges() throws IOException {
187 if (fProjectStore != null) {
188 // nothing to do
189 } else {
190 fInstanceStore.load();
191 }
192 }
193}