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