KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > persistence > TemplatePersistenceData


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.jface.text.templates.persistence;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.text.templates.Template;
16
17
18 /**
19  * TemplatePersistenceData stores information about a template. It uniquely
20  * references contributed templates via their id. Contributed templates may be
21  * deleted or modified. All template may be enabled or not.
22  * <p>
23  * Clients may use this class, although this is not usually needed except when
24  * implementing a custom template preference page or template store. This class
25  * is not intended to be subclassed.
26  * </p>
27  *
28  * @since 3.0
29  */

30 public class TemplatePersistenceData {
31     private final Template fOriginalTemplate;
32     private final String JavaDoc fId;
33     private final boolean fOriginalIsEnabled;
34
35     private Template fCustomTemplate= null;
36     private boolean fIsDeleted= false;
37     private boolean fCustomIsEnabled= true;
38
39     /**
40      * Creates a new, user-added instance that is not linked to a contributed
41      * template.
42      *
43      * @param template the template which is stored by the new instance
44      * @param enabled whether the template is enabled
45      */

46     public TemplatePersistenceData(Template template, boolean enabled) {
47         this(template, enabled, null);
48     }
49
50     /**
51      * Creates a new instance. If <code>id</code> is not <code>null</code>,
52      * the instance is represents a template that is contributed and can be
53      * identified via its id.
54      *
55      * @param template the template which is stored by the new instance
56      * @param enabled whether the template is enabled
57      * @param id the id of the template, or <code>null</code> if a user-added
58      * instance should be created
59      */

60     public TemplatePersistenceData(Template template, boolean enabled, String JavaDoc id) {
61         Assert.isNotNull(template);
62         fOriginalTemplate= template;
63         fCustomTemplate= template;
64         fOriginalIsEnabled= enabled;
65         fCustomIsEnabled= enabled;
66         fId= id;
67     }
68
69     /**
70      * Returns the id of this template store, or <code>null</code> if there is none.
71      *
72      * @return the id of this template store
73      */

74     public String JavaDoc getId() {
75         return fId;
76     }
77
78     /**
79      * Returns the deletion state of the stored template. This is only relevant
80      * of contributed templates.
81      *
82      * @return the deletion state of the stored template
83      */

84     public boolean isDeleted() {
85         return fIsDeleted;
86     }
87
88     /**
89      * Sets the deletion state of the stored template.
90      *
91      * @param isDeleted the deletion state of the stored template
92      */

93     public void setDeleted(boolean isDeleted) {
94         fIsDeleted= isDeleted;
95     }
96
97     /**
98      * Returns the template encapsulated by the receiver.
99      *
100      * @return the template encapsulated by the receiver
101      */

102     public Template getTemplate() {
103         return fCustomTemplate;
104     }
105
106
107     /**
108      * Sets the template encapsulated by the receiver.
109      *
110      * @param template the new template
111      */

112     public void setTemplate(Template template) {
113         fCustomTemplate= template;
114     }
115
116     /**
117      * Returns whether the receiver represents a custom template, i.e. is either
118      * a user-added template or a contributed template that has been modified.
119      *
120      * @return <code>true</code> if the contained template is a custom
121      * template and cannot be reconstructed from the contributed
122      * templates
123      */

124     public boolean isCustom() {
125         return fId == null
126                 || fIsDeleted
127                 || fOriginalIsEnabled != fCustomIsEnabled
128                 || !fOriginalTemplate.equals(fCustomTemplate);
129     }
130
131     /**
132      * Returns whether the receiver represents a modified template, i.e. a
133      * contributed template that has been changed.
134      *
135      * @return <code>true</code> if the contained template is contributed but has been modified, <code>false</code> otherwise
136      */

137     public boolean isModified() {
138         return isCustom() && !isUserAdded();
139     }
140
141     /**
142      * Returns <code>true</code> if the contained template was added by a
143      * user, i.e. does not reference a contributed template.
144      *
145      * @return <code>true</code> if the contained template was added by a user, <code>false</code> otherwise
146      */

147     public boolean isUserAdded() {
148         return fId == null;
149     }
150
151
152     /**
153      * Reverts the template to its original setting.
154      */

155     public void revert() {
156         fCustomTemplate= fOriginalTemplate;
157         fCustomIsEnabled= fOriginalIsEnabled;
158         fIsDeleted= false;
159     }
160
161
162     /**
163      * Returns the enablement state of the contained template.
164      *
165      * @return the enablement state of the contained template
166      */

167     public boolean isEnabled() {
168         return fCustomIsEnabled;
169     }
170
171     /**
172      * Sets the enablement state of the contained template.
173      *
174      * @param isEnabled the new enablement state of the contained template
175      */

176     public void setEnabled(boolean isEnabled) {
177         fCustomIsEnabled= isEnabled;
178     }
179 }
180
Popular Tags