Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 645ba51dd724d69e2d34f9c51868f082761bb327 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
import org.eclipse.jface.text.templates.persistence.TemplateStore;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
 * @since 5.0
 */
public final class ProjectTemplateStore {
	private static final String KEY = "org.eclipse.cdt.ui.text.custom_code_templates"; //$NON-NLS-1$

	private final TemplateStore fInstanceStore;
	private final TemplateStore fProjectStore;

	public ProjectTemplateStore(IProject project) {
		fInstanceStore = CUIPlugin.getDefault().getCodeTemplateStore();
		if (project == null) {
			fProjectStore = null;
		} else {
			final ScopedPreferenceStore projectSettings = new ScopedPreferenceStore(new ProjectScope(project),
					CUIPlugin.PLUGIN_ID);
			fProjectStore = new TemplateStore(projectSettings, KEY) {
				/*
				 * Make sure we keep the id of added code templates - add removes
				 * it in the usual add() method
				 */
				@Override
				public void add(TemplatePersistenceData data) {
					if (data.isUserAdded()) {
						super.add(data);
					} else {
						internalAdd(data);
					}
				}

				@Override
				public void save() throws IOException {
					StringWriter output = new StringWriter();
					TemplateReaderWriter writer = new TemplateReaderWriter();
					writer.save(getTemplateData(false), output);

					projectSettings.setValue(KEY, output.toString());
					projectSettings.save();
				}
			};
		}
	}

	public static boolean hasProjectSpecificTempates(IProject project) {
		String pref = new ProjectScope(project).getNode(CUIPlugin.PLUGIN_ID).get(KEY, null);
		if (pref != null && pref.trim().length() > 0) {
			Reader input = new StringReader(pref);
			TemplateReaderWriter reader = new TemplateReaderWriter();
			TemplatePersistenceData[] datas;
			try {
				datas = reader.read(input);
				return datas.length > 0;
			} catch (IOException e) {
				// ignore
			}
		}
		return false;
	}

	public TemplatePersistenceData[] getTemplateData() {
		if (fProjectStore != null) {
			return fProjectStore.getTemplateData(true);
		}
		return fInstanceStore.getTemplateData(true);
	}

	public Template findTemplateById(String id) {
		Template template = null;
		if (fProjectStore != null)
			template = fProjectStore.findTemplateById(id);
		if (template == null)
			template = fInstanceStore.findTemplateById(id);

		return template;
	}

	public void load() throws IOException {
		if (fProjectStore != null) {
			fProjectStore.load();

			Set<String> datas = new HashSet<>();
			TemplatePersistenceData[] data = fProjectStore.getTemplateData(false);
			for (TemplatePersistenceData element : data) {
				String id = element.getId();
				if (id == null) {
					id = element.getTemplate().getName();
				}
				datas.add(id);
			}

			data = fInstanceStore.getTemplateData(false);
			for (TemplatePersistenceData orig : data) {
				String origId = orig.getId();
				if (origId == null) {
					origId = orig.getTemplate().getName();
				}
				if (!datas.contains(orig.getId())) {
					TemplatePersistenceData copy = new TemplatePersistenceData(new Template(orig.getTemplate()),
							orig.isEnabled(), orig.getId());
					fProjectStore.add(copy);
					copy.setDeleted(true);
				}
			}
		}
	}

	public boolean isProjectSpecific(String id) {
		if (id == null) {
			return false;
		}

		if (fProjectStore == null)
			return false;

		return fProjectStore.findTemplateById(id) != null;
	}

	public void setProjectSpecific(String id, boolean projectSpecific) {
		Assert.isNotNull(fProjectStore);

		TemplatePersistenceData data = fProjectStore.getTemplateData(id);
		if (data == null) {
			return; // does not exist
		}
		data.setDeleted(!projectSpecific);
	}

	public void restoreDefaults() {
		if (fProjectStore == null) {
			fInstanceStore.restoreDefaults();
		} else {
			fProjectStore.restoreDefaults();
		}
	}

	public void save() throws IOException {
		if (fProjectStore == null) {
			fInstanceStore.save();
		} else {
			fProjectStore.save();
		}
	}

	public void revertChanges() throws IOException {
		if (fProjectStore != null) {
			// nothing to do
		} else {
			fInstanceStore.load();
		}
	}

	public void addTemplateData(TemplatePersistenceData data) {
		if (fProjectStore != null) {
			fProjectStore.add(data);
		} else {
			fInstanceStore.add(data);
		}
	}

	public void delete(TemplatePersistenceData data) {
		if (fProjectStore != null) {
			fProjectStore.delete(data);
		} else {
			fInstanceStore.delete(data);
		}
	}
}

Back to the top