added TemplatePreferencePage
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / HTMLUnitContext.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template.php;
6
7 import net.sourceforge.phpdt.internal.corext.template.ContextType;
8 import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.Template;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
11 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15
16 /**
17  * A compilation unit context.
18  */
19 public class HTMLUnitContext extends DocumentTemplateContext {
20
21   /** The platform default line delimiter. */
22   private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
23
24   private static final String specialChars = "&<#";
25   /** The compilation unit, may be <code>null</code>. */
26   //    private final ICompilationUnit fCompilationUnit;
27
28   /**
29    * Creates a compilation unit context.
30    * 
31    * @param type   the context type.
32    * @param document the document.
33    * @param completionPosition the completion position within the document.
34    * @param compilationUnit the compilation unit (may be <code>null</code>).
35    */
36   protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
37   //,ICompilationUnit compilationUnit)
38   {
39     super(type, document, completionPosition);
40     //  fCompilationUnit= compilationUnit;
41   }
42
43   /*
44   * @see TemplateContext#canEvaluate(Template templates)
45   */
46   public boolean canEvaluate(Template template) {
47     // return fForceEvaluation || 
48     return template.matches(getKey(), getContextType().getName());
49   }
50
51   /**
52    * Returns <code>true</code> if template matches the prefix and context,
53    * <code>false</code> otherwise.
54    */
55   public boolean canEvaluate(String identifier) {
56     String prefix = getKey();
57     return
58     //      fEnabled &&
59     //      fContextTypeName.equals(contextTypeName) &&
60      (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
61   }
62
63   /*
64   * @see TemplateContext#evaluate(Template template)
65   */
66   public TemplateBuffer evaluate(Template template) throws CoreException {
67     if (!canEvaluate(template))
68       return null;
69
70     TemplateTranslator translator = new TemplateTranslator();
71     TemplateBuffer buffer = translator.translate(template.getPattern());
72
73     getContextType().edit(buffer, this);
74
75     String lineDelimiter = null;
76     try {
77       lineDelimiter = getDocument().getLineDelimiter(0);
78     } catch (BadLocationException e) {
79     }
80
81     if (lineDelimiter == null)
82       lineDelimiter = PLATFORM_LINE_DELIMITER;
83
84     //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
85     //    formatter.edit(buffer, this);
86
87     return buffer;
88   }
89
90   /*
91    * @see DocumentTemplateContext#getCompletionPosition();
92    */
93   public int getStart() {
94     IDocument document = getDocument();
95     try {
96       int start = getCompletionPosition();
97
98       while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
99         || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
100         start--;
101       }
102
103       if (((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
104         || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
105         start--;
106       }
107
108       return start;
109
110     } catch (BadLocationException e) {
111       return getCompletionPosition();
112     }
113   }
114
115   /**
116    * Returns the character before start position of completion.
117    */
118   public char getCharacterBeforeStart() {
119     int start = getStart();
120
121     try {
122       return start == 0 ? ' ' : getDocument().getChar(start - 1);
123
124     } catch (BadLocationException e) {
125       return ' ';
126     }
127   }
128   /**
129    * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
130    */
131   //    public final ICompilationUnit getCompilationUnit() {
132   //            return fCompilationUnit;
133   //    }
134
135   /**
136    * Returns the enclosing element of a particular element type, <code>null</code>
137    * if no enclosing element of that type exists.
138    */
139   //    public IJavaElement findEnclosingElement(int elementType) {
140   //            if (fCompilationUnit == null)
141   //                    return null;
142   //
143   //            try {
144   //                    IJavaElement element= fCompilationUnit.getElementAt(getStart());
145   //                    while (element != null && element.getElementType() != elementType)
146   //                            element= element.getParent();
147   //                    
148   //                    return element;
149   //
150   //            } catch (JavaModelException e) {
151   //                    return null;
152   //            }       
153   //    }
154
155 }