A massive organize imports and formatting of the sources using default Eclipse code...
[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.2 2006-10-21 23:14:29 pombredanne 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 {@link IDocument}.
23  */
24 public class SourceReference implements ISourceReference {
25
26         // Instance Variables ------------------------------------------------------
27
28         /** The associated document. */
29         private IDocument document;
30
31         /** The region in the document that maps to the source reference. */
32         private IRegion sourceRegion;
33
34         // Constructors ------------------------------------------------------------
35
36         /**
37          * Constructor.
38          * 
39          * @param document
40          *            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
50          *            The document that contains the source reference
51          */
52         public SourceReference(IDocument document, int offset) {
53                 this(document, offset, 0);
54         }
55
56         /**
57          * Constructor.
58          * 
59          * @param document
60          *            The document that contains the source reference
61          */
62         public SourceReference(IDocument document, int offset, int length) {
63                 this.document = document;
64                 this.sourceRegion = new Region(offset, length);
65         }
66
67         // ISourceReference Implementation -----------------------------------------
68
69         /*
70          * @see ISourceReference#getSource()
71          */
72         public String getSource() {
73                 try {
74                         return document.get(sourceRegion.getOffset(), sourceRegion
75                                         .getLength());
76                 } catch (BadLocationException e) {
77                         throw new IllegalStateException(
78                                         "Model not synchronized with document"); //$NON-NLS-1$
79                 }
80         }
81
82         /*
83          * @see ISourceReference#getSourceRegion()
84          */
85         public IRegion getSourceRegion() {
86                 return sourceRegion;
87         }
88
89         // Public Methods ----------------------------------------------------------
90
91         /**
92          * Sets the source region covered by the element.
93          * 
94          * @param offset
95          *            the offset of the region
96          * @param length
97          *            the length of the region
98          */
99         public final void setSourceRegion(int offset, int length) {
100                 sourceRegion = new Region(offset, length);
101         }
102
103         /**
104          * Sets the source region covered by the element.
105          * 
106          * @param region
107          *            the source region to set
108          */
109         public final void setSourceRegion(IRegion region) {
110                 setSourceRegion(region.getOffset(), region.getLength());
111         }
112
113         // Protected Methods -------------------------------------------------------
114
115         /**
116          * Returns the underlying document.
117          * 
118          * @return the underlying document
119          */
120         protected final IDocument getDocument() {
121                 return document;
122         }
123
124 }