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.2 2006-10-21 23:14:29 pombredanne 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 {@link IDocument}.
24 public class SourceReference implements ISourceReference {
26 // Instance Variables ------------------------------------------------------
28 /** The associated document. */
29 private IDocument document;
31 /** The region in the document that maps to the source reference. */
32 private IRegion sourceRegion;
34 // Constructors ------------------------------------------------------------
40 * The document that contains the source reference
42 public SourceReference(IDocument document) {
50 * The document that contains the source reference
52 public SourceReference(IDocument document, int offset) {
53 this(document, offset, 0);
60 * The document that contains the source reference
62 public SourceReference(IDocument document, int offset, int length) {
63 this.document = document;
64 this.sourceRegion = new Region(offset, length);
67 // ISourceReference Implementation -----------------------------------------
70 * @see ISourceReference#getSource()
72 public String getSource() {
74 return document.get(sourceRegion.getOffset(), sourceRegion
76 } catch (BadLocationException e) {
77 throw new IllegalStateException(
78 "Model not synchronized with document"); //$NON-NLS-1$
83 * @see ISourceReference#getSourceRegion()
85 public IRegion getSourceRegion() {
89 // Public Methods ----------------------------------------------------------
92 * Sets the source region covered by the element.
95 * the offset of the region
97 * the length of the region
99 public final void setSourceRegion(int offset, int length) {
100 sourceRegion = new Region(offset, length);
104 * Sets the source region covered by the element.
107 * the source region to set
109 public final void setSourceRegion(IRegion region) {
110 setSourceRegion(region.getOffset(), region.getLength());
113 // Protected Methods -------------------------------------------------------
116 * Returns the underlying document.
118 * @return the underlying document
120 protected final IDocument getDocument() {