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
9 * Christopher Lenz - initial API and implementation
11 * $Id: SourceReference.java,v 1.1 2004-09-02 18:05:21 jsurfer Exp $
14 package net.sourceforge.phpeclipse.core.model;
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;
22 * Default implementation of {@link ISourceReference} based on
25 public class SourceReference implements ISourceReference {
27 // Instance Variables ------------------------------------------------------
29 /** The associated document. */
30 private IDocument document;
32 /** The region in the document that maps to the source reference. */
33 private IRegion sourceRegion;
35 // Constructors ------------------------------------------------------------
40 * @param document The document that contains the source reference
42 public SourceReference(IDocument document) {
49 * @param document The document that contains the source reference
51 public SourceReference(IDocument document, int offset) {
52 this(document, offset, 0);
58 * @param document The document that contains the source reference
60 public SourceReference(IDocument document, int offset, int length) {
61 this.document = document;
62 this.sourceRegion = new Region(offset, length);
65 // ISourceReference Implementation -----------------------------------------
68 * @see ISourceReference#getSource()
70 public String getSource() {
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$
81 * @see ISourceReference#getSourceRegion()
83 public IRegion getSourceRegion() {
87 // Public Methods ----------------------------------------------------------
90 * Sets the source region covered by the element.
92 * @param offset the offset of the region
93 * @param length the length of the region
95 public final void setSourceRegion(int offset, int length) {
96 sourceRegion = new Region(offset, length);
100 * Sets the source region covered by the element.
102 * @param region the source region to set
104 public final void setSourceRegion(IRegion region) {
105 setSourceRegion(region.getOffset(), region.getLength());
108 // Protected Methods -------------------------------------------------------
111 * Returns the underlying document.
113 * @return the underlying document
115 protected final IDocument getDocument() {