129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.templates.TemplateContext;
|
|
14
|
131
|
15 import dwtx.jface.text.templates.SimpleTemplateVariableResolver; // packageimport
|
|
16 import dwtx.jface.text.templates.TemplateBuffer; // packageimport
|
|
17 import dwtx.jface.text.templates.TemplateContextType; // packageimport
|
|
18 import dwtx.jface.text.templates.Template; // packageimport
|
|
19 import dwtx.jface.text.templates.TemplateVariable; // packageimport
|
|
20 import dwtx.jface.text.templates.PositionBasedCompletionProposal; // packageimport
|
|
21 import dwtx.jface.text.templates.TemplateException; // packageimport
|
|
22 import dwtx.jface.text.templates.TemplateTranslator; // packageimport
|
|
23 import dwtx.jface.text.templates.DocumentTemplateContext; // packageimport
|
|
24 import dwtx.jface.text.templates.GlobalTemplateVariables; // packageimport
|
|
25 import dwtx.jface.text.templates.InclusivePositionUpdater; // packageimport
|
|
26 import dwtx.jface.text.templates.TemplateProposal; // packageimport
|
|
27 import dwtx.jface.text.templates.ContextTypeRegistry; // packageimport
|
|
28 import dwtx.jface.text.templates.JFaceTextTemplateMessages; // packageimport
|
|
29 import dwtx.jface.text.templates.TemplateCompletionProcessor; // packageimport
|
|
30 import dwtx.jface.text.templates.TextTemplateMessages; // packageimport
|
|
31 import dwtx.jface.text.templates.TemplateVariableType; // packageimport
|
|
32 import dwtx.jface.text.templates.TemplateVariableResolver; // packageimport
|
|
33
|
|
34
|
129
|
35 import dwt.dwthelper.utils;
|
|
36
|
|
37 import java.util.HashMap;
|
|
38 import java.util.Map;
|
|
39
|
|
40 import dwtx.jface.text.BadLocationException;
|
|
41
|
|
42 /**
|
|
43 * Provides the context for a <code>Template</code> being resolved. Keeps track
|
|
44 * of resolved variables.
|
|
45 * <p>
|
|
46 * Clients may extend this class.
|
|
47 * </p>
|
|
48 *
|
|
49 * @since 3.0
|
|
50 */
|
|
51 public abstract class TemplateContext {
|
|
52
|
|
53 /** The context type of this context */
|
|
54 private final TemplateContextType fContextType;
|
|
55 /** Additional variables. */
|
|
56 private final Map fVariables= new HashMap();
|
|
57 /** A flag to indicate that the context should not be modified. */
|
|
58 private bool fReadOnly;
|
|
59
|
|
60 /**
|
|
61 * Creates a template context of a particular context type.
|
|
62 *
|
|
63 * @param contextType the context type of this context
|
|
64 */
|
|
65 protected TemplateContext(TemplateContextType contextType) {
|
|
66 fContextType= contextType;
|
|
67 fReadOnly= true;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Returns the context type of this context.
|
|
72 *
|
|
73 * @return the context type of this context
|
|
74 */
|
|
75 public TemplateContextType getContextType() {
|
|
76 return fContextType;
|
|
77 }
|
|
78
|
|
79 /**
|
|
80 * Sets or clears the read-only flag.
|
|
81 *
|
|
82 * @param readOnly the new read-only state
|
|
83 */
|
|
84 public void setReadOnly(bool readOnly) {
|
|
85 fReadOnly= readOnly;
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * Returns <code>true</code> if the receiver is read-only, <code>false</code> otherwise.
|
|
90 *
|
|
91 * @return <code>true</code> if the receiver is read-only, <code>false</code> otherwise
|
|
92 */
|
|
93 public bool isReadOnly() {
|
|
94 return fReadOnly;
|
|
95 }
|
|
96
|
|
97 /**
|
|
98 * Defines the value of a variable.
|
|
99 *
|
|
100 * @param name the name of the variable
|
|
101 * @param value the value of the variable, <code>null</code> to un-define a variable
|
|
102 */
|
|
103 public void setVariable(String name, String value) {
|
|
104 fVariables.put(name, value);
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Returns the value of a defined variable.
|
|
109 *
|
|
110 * @param name the name of the variable
|
|
111 * @return returns the value of the variable, <code>null</code> if the variable was not defined
|
|
112 */
|
|
113 public String getVariable(String name) {
|
|
114 return (String) fVariables.get(name);
|
|
115 }
|
|
116
|
|
117 /**
|
|
118 * Evaluates the template in this context and returns a template buffer.
|
|
119 * <p>
|
|
120 * Evaluation means translating the template into a <code>TemplateBuffer</code>,
|
|
121 * resolving the defined variables in this context and possibly formatting
|
|
122 * the resolved buffer.</p>
|
|
123 *
|
|
124 * @param template the template to evaluate
|
|
125 * @return returns the buffer with the evaluated template or <code>null</code> if the buffer could not be created
|
|
126 * @throws BadLocationException if evaluation fails due to concurrently changed documents etc.
|
|
127 * @throws TemplateException if the template specification is not valid
|
|
128 */
|
|
129 public abstract TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException;
|
|
130
|
|
131 /**
|
|
132 * Tests if the specified template can be evaluated in this context.
|
|
133 * <p>Examples are templates defined for a different context (e.g. a javadoc
|
|
134 * template cannot be evaluated in Java context).</p>
|
|
135 *
|
|
136 * @param template the <code>Template</code> to check
|
|
137 * @return <code>true</code> if <code>template</code> can be evaluated
|
|
138 * in this context, <code>false</code> otherwise
|
|
139 */
|
|
140 public abstract bool canEvaluate(Template template);
|
|
141
|
|
142 }
|