Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60dfc06f2a094bad0faaad9b4a497709005e4dd4 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.templates;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.templates.Template;


/**
 * TemplatePersistenceData stores information about a template. It uniquely
 * references contributed templates via their id. Contributed templates may be
 * deleted or modified. All template may be enabled or not.
 * <p>
 * Clients may use this class, although this is not usually needed except when
 * implementing a custom template preference page or template store. This class
 * is not intended to be subclassed.
 * </p>
 *
 * @since 3.7
 * @noextend This class is not intended to be subclassed by clients.
 */
public class TemplatePersistenceData {
	private final Template fOriginalTemplate;
	private final String fId;
	private final boolean fOriginalIsEnabled;

	private Template fCustomTemplate= null;
	private boolean fIsDeleted= false;
	private boolean fCustomIsEnabled= true;

	/**
	 * Creates a new, user-added instance that is not linked to a contributed
	 * template.
	 *
	 * @param template the template which is stored by the new instance
	 * @param enabled whether the template is enabled
	 */
	public TemplatePersistenceData(Template template, boolean enabled) {
		this(template, enabled, null);
	}

	/**
	 * Creates a new instance. If <code>id</code> is not <code>null</code>,
	 * the instance is represents a template that is contributed and can be
	 * identified via its id.
	 *
	 * @param template the template which is stored by the new instance
	 * @param enabled whether the template is enabled
	 * @param id the id of the template, or <code>null</code> if a user-added
	 *        instance should be created
	 */
	public TemplatePersistenceData(Template template, boolean enabled, String id) {
		Assert.isNotNull(template);
		fOriginalTemplate= template;
		fCustomTemplate= template;
		fOriginalIsEnabled= enabled;
		fCustomIsEnabled= enabled;
		fId= id;
	}

	/**
	 * Returns the id of this template store, or <code>null</code> if there is none.
	 *
	 * @return the id of this template store
	 */
	public String getId() {
		return fId;
	}

	/**
	 * Returns the deletion state of the stored template. This is only relevant
	 * of contributed templates.
	 *
	 * @return the deletion state of the stored template
	 */
	public boolean isDeleted() {
		return fIsDeleted;
	}

	/**
	 * Sets the deletion state of the stored template.
	 *
	 * @param isDeleted the deletion state of the stored template
	 */
	public void setDeleted(boolean isDeleted) {
		fIsDeleted= isDeleted;
	}

	/**
	 * Returns the template encapsulated by the receiver.
	 *
	 * @return the template encapsulated by the receiver
	 */
	public Template getTemplate() {
		return fCustomTemplate;
	}


	/**
	 * Sets the template encapsulated by the receiver.
	 *
	 * @param template the new template
	 */
	public void setTemplate(Template template) {
		fCustomTemplate= template;
	}

	/**
	 * Returns whether the receiver represents a custom template, i.e. is either
	 * a user-added template or a contributed template that has been modified.
	 *
	 * @return <code>true</code> if the contained template is a custom
	 *         template and cannot be reconstructed from the contributed
	 *         templates
	 */
	public boolean isCustom() {
		return fId == null
				|| fIsDeleted
				|| fOriginalIsEnabled != fCustomIsEnabled
				|| !fOriginalTemplate.equals(fCustomTemplate);
	}

	/**
	 * Returns whether the receiver represents a modified template, i.e. a
	 * contributed template that has been changed.
	 *
	 * @return <code>true</code> if the contained template is contributed but has been modified, <code>false</code> otherwise
	 */
	public boolean isModified() {
		return isCustom() && !isUserAdded();
	}

	/**
	 * Returns <code>true</code> if the contained template was added by a
	 * user, i.e. does not reference a contributed template.
	 *
	 * @return <code>true</code> if the contained template was added by a user, <code>false</code> otherwise
	 */
	public boolean isUserAdded() {
		return fId == null;
	}


	/**
	 * Reverts the template to its original setting.
	 */
	public void revert() {
		fCustomTemplate= fOriginalTemplate;
		fCustomIsEnabled= fOriginalIsEnabled;
		fIsDeleted= false;
	}


	/**
	 * Returns the enablement state of the contained template.
	 *
	 * @return the enablement state of the contained template
	 */
	public boolean isEnabled() {
		return fCustomIsEnabled;
	}

	/**
	 * Sets the enablement state of the contained template.
	 *
	 * @param isEnabled the new enablement state of the contained template
	 */
	public void setEnabled(boolean isEnabled) {
		fCustomIsEnabled= isEnabled;
	}
}

Back to the top