comparison dwtx/jface/text/templates/TemplateContext.d @ 129:eb30df5ca28b

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