intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.core / src / net / sourceforge / phpeclipse / core / model / SourceReference.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: SourceReference.java,v 1.1 2004-09-02 18:05:21 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.core.model;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.Region;
20
21 /**
22  * Default implementation of {@link ISourceReference} based on
23  * {@link IDocument}.
24  */
25 public class SourceReference implements ISourceReference {
26
27         // Instance Variables ------------------------------------------------------
28
29         /** The associated document. */
30         private IDocument document;
31
32         /** The region in the document that maps to the source reference. */
33         private IRegion sourceRegion;
34
35         // Constructors ------------------------------------------------------------
36
37         /**
38          * Constructor.
39          * 
40          * @param document The document that contains the source reference
41          */
42         public SourceReference(IDocument document) {
43                 this(document, 0, 0);
44         }
45
46         /**
47          * Constructor.
48          * 
49          * @param document The document that contains the source reference
50          */
51         public SourceReference(IDocument document, int offset) {
52                 this(document, offset, 0);
53         }
54
55         /**
56          * Constructor.
57          * 
58          * @param document The document that contains the source reference
59          */
60         public SourceReference(IDocument document, int offset, int length) {
61                 this.document = document;
62                 this.sourceRegion = new Region(offset, length);
63         }
64
65         // ISourceReference Implementation -----------------------------------------
66
67         /*
68          * @see ISourceReference#getSource()
69          */
70         public String getSource() {
71                 try {
72                         return document.get(sourceRegion.getOffset(),
73                                 sourceRegion.getLength());
74                 } catch (BadLocationException e) {
75                         throw new IllegalStateException(
76                                 "Model not synchronized with document"); //$NON-NLS-1$
77                 }
78         }
79
80         /*
81          * @see ISourceReference#getSourceRegion()
82          */
83         public IRegion getSourceRegion() {
84                 return sourceRegion;
85         }
86
87         // Public Methods ----------------------------------------------------------
88
89         /**
90          * Sets the source region covered by the element.
91          * 
92          * @param offset the offset of the region
93          * @param length the length of the region
94          */
95         public final void setSourceRegion(int offset, int length) {
96                 sourceRegion = new Region(offset, length);
97         }
98
99         /**
100          * Sets the source region covered by the element.
101          * 
102          * @param region the source region to set
103          */
104         public final void setSourceRegion(IRegion region) {
105                 setSourceRegion(region.getOffset(), region.getLength());
106         }
107
108         // Protected Methods -------------------------------------------------------
109
110         /**
111          * Returns the underlying document.
112          * 
113          * @return the underlying document
114          */
115         protected final IDocument getDocument() {
116                 return document;
117         }
118
119 }