KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > templates > AntContext


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.ant.internal.ui.editor.templates;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
16 import org.eclipse.ant.internal.ui.editor.formatter.XmlFormatter;
17 import org.eclipse.ant.internal.ui.model.IAntModel;
18 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.Position;
22 import org.eclipse.jface.text.templates.DocumentTemplateContext;
23 import org.eclipse.jface.text.templates.Template;
24 import org.eclipse.jface.text.templates.TemplateBuffer;
25 import org.eclipse.jface.text.templates.TemplateContextType;
26 import org.eclipse.jface.text.templates.TemplateException;
27
28 public class AntContext extends DocumentTemplateContext {
29     
30     private IAntModel fAntModel;
31     
32     public AntContext(TemplateContextType type, IDocument document, IAntModel model, Position position) {
33         super(type, document, position);
34         fAntModel= model;
35     }
36     
37     /* (non-Javadoc)
38      * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
39      */

40     public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
41
42         TemplateBuffer templateBuffer= super.evaluate(template);
43         
44         if (templateBuffer == null) {
45             return null;
46         }
47         //TODO Not enabled see bug 55356
48
if (false && AntUIPlugin.getDefault().getPreferenceStore().getBoolean(AntEditorPreferenceConstants.TEMPLATES_USE_CODEFORMATTER)) {
49             FormattingPreferences prefs = new FormattingPreferences();
50             XmlFormatter.format(templateBuffer, this, prefs);
51         }
52         return templateBuffer;
53     }
54     
55     /**
56      * @return Returns the AntModel.
57      */

58     public IAntModel getAntModel() {
59         return fAntModel;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.jface.text.templates.DocumentTemplateContext#getEnd()
64      */

65     public int getEnd() {
66         int replacementOffset = getCompletionOffset();
67         int replacementLength = getCompletionLength();
68         if (replacementOffset > 0 && getDocument().get().charAt(replacementOffset - 1) == '<' && getDocument().getLength() > 1) {
69             replacementLength++;
70         }
71         return replacementLength + replacementOffset;
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.jface.text.templates.DocumentTemplateContext#getStart()
76      */

77     public int getStart() {
78         int replacementOffset= getCompletionOffset();
79         if (replacementOffset > 0 && getDocument().get().charAt(replacementOffset - 1) == '<') {
80             replacementOffset--;
81         }
82         return replacementOffset;
83     }
84 }
85
Popular Tags