e89d632263d05b71d09cb5534b222632f910d391
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / DocumentTemplateContext.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template;
6
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.IDocument;
9
10 //import org.eclipse.jdt.internal.corext.Assert;
11
12 /**
13  * A typical text based document template context.
14  */
15 public abstract class DocumentTemplateContext extends TemplateContext {
16
17         /** The text of the document. */
18         private final IDocument fDocument;
19         /** The completion position. */
20         private final int fCompletionPosition;
21
22         /**
23          * Creates a document template context.
24          */
25         protected DocumentTemplateContext(ContextType type, IDocument document, int completionPosition) {
26                 super(type);
27                 
28 //              Assert.isNotNull(document);
29 //              Assert.isTrue(completionPosition >= 0 && completionPosition <= document.getLength());
30                 
31                 fDocument= document;
32                 fCompletionPosition= completionPosition;
33         }
34         
35         public IDocument getDocument() {
36                 return fDocument;       
37         }
38         
39         /**
40          * Returns the string of the context.
41          */
42 //      public String getString() {
43 //              return fDocument.get();
44 //      }
45         
46         /**
47          * Returns the completion position within the string of the context.
48          */
49         public int getCompletionPosition() {
50                 return fCompletionPosition;     
51         }
52         
53         /**
54          * Returns the keyword which triggered template insertion.
55          */
56         public String getKey() {
57                 int offset= getStart();
58                 int length= getEnd() - offset;
59                 try {
60                         return fDocument.get(offset, length);
61                 } catch (BadLocationException e) {
62                         return ""; //$NON-NLS-1$        
63                 }
64         }
65
66         /**
67          * Returns the beginning offset of the keyword.
68          */
69         public int getStart() {
70                 return fCompletionPosition;             
71         }
72         
73         /**
74          * Returns the end offset of the keyword.
75          */
76         public int getEnd() {
77                 return fCompletionPosition;
78         }
79                 
80 }