KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > TemplateContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.text.BadLocationException;
17
18 /**
19  * Provides the context for a <code>Template</code> being resolved. Keeps track
20  * of resolved variables.
21  * <p>
22  * Clients may extend this class.
23  * </p>
24  *
25  * @since 3.0
26  */

27 public abstract class TemplateContext {
28
29     /** The context type of this context */
30     private final TemplateContextType fContextType;
31     /** Additional variables. */
32     private final Map JavaDoc fVariables= new HashMap JavaDoc();
33     /** A flag to indicate that the context should not be modified. */
34     private boolean fReadOnly;
35
36     /**
37      * Creates a template context of a particular context type.
38      *
39      * @param contextType the context type of this context
40      */

41     protected TemplateContext(TemplateContextType contextType) {
42         fContextType= contextType;
43         fReadOnly= true;
44     }
45
46     /**
47      * Returns the context type of this context.
48      *
49      * @return the context type of this context
50      */

51     public TemplateContextType getContextType() {
52         return fContextType;
53     }
54
55     /**
56      * Sets or clears the read-only flag.
57      *
58      * @param readOnly the new read-only state
59      */

60     public void setReadOnly(boolean readOnly) {
61         fReadOnly= readOnly;
62     }
63
64     /**
65      * Returns <code>true</code> if the receiver is read-only, <code>false</code> otherwise.
66      *
67      * @return <code>true</code> if the receiver is read-only, <code>false</code> otherwise
68      */

69     public boolean isReadOnly() {
70         return fReadOnly;
71     }
72
73     /**
74      * Defines the value of a variable.
75      *
76      * @param name the name of the variable
77      * @param value the value of the variable, <code>null</code> to un-define a variable
78      */

79     public void setVariable(String JavaDoc name, String JavaDoc value) {
80         fVariables.put(name, value);
81     }
82
83     /**
84      * Returns the value of a defined variable.
85      *
86      * @param name the name of the variable
87      * @return returns the value of the variable, <code>null</code> if the variable was not defined
88      */

89     public String JavaDoc getVariable(String JavaDoc name) {
90         return (String JavaDoc) fVariables.get(name);
91     }
92
93     /**
94      * Evaluates the template in this context and returns a template buffer.
95      * <p>
96      * Evaluation means translating the template into a <code>TemplateBuffer</code>,
97      * resolving the defined variables in this context and possibly formatting
98      * the resolved buffer.</p>
99      *
100      * @param template the template to evaluate
101      * @return returns the buffer with the evaluated template or <code>null</code> if the buffer could not be created
102      * @throws BadLocationException if evaluation fails due to concurrently changed documents etc.
103      * @throws TemplateException if the template specification is not valid
104      */

105     public abstract TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException;
106
107     /**
108      * Tests if the specified template can be evaluated in this context.
109      * <p>Examples are templates defined for a different context (e.g. a javadoc
110      * template cannot be evaluated in Java context).</p>
111      *
112      * @param template the <code>Template</code> to check
113      * @return <code>true</code> if <code>template</code> can be evaluated
114      * in this context, <code>false</code> otherwise
115      */

116     public abstract boolean canEvaluate(Template template);
117
118 }
119
Popular Tags