001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import java.io.File;
040: import java.lang.ref.WeakReference;
041: import javax.swing.text.Position;
042:
043: /**
044: * Class for a document region that can move with changes in the document; its text, however, remains constant.
045: * @version $Id$Region
046: */
047: public class MovingDocumentRegion implements DocumentRegion {
048: protected final OpenDefinitionsDocument _doc;
049: protected final File _file;
050: protected final Position _startPosition;
051: protected final Position _endPosition;
052: protected final String _string;
053:
054: /** Create a new moving document region. */
055: public MovingDocumentRegion(OpenDefinitionsDocument doc, File file,
056: Position sp, Position ep, String s) {
057: _doc = doc;
058: _file = file;
059: _startPosition = sp;
060: _endPosition = ep;
061: _string = s;
062: }
063:
064: /** @return the document, or null if it hasn't been established yet */
065: public OpenDefinitionsDocument getDocument() {
066: return _doc;
067: }
068:
069: /** @return the file */
070: public File getFile() {
071: return _file;
072: }
073:
074: /** @return the start offset */
075: public int getStartOffset() {
076: return (_doc == null || _doc.getLength() >= _startPosition
077: .getOffset()) ? _startPosition.getOffset() : _doc
078: .getLength();
079: }
080:
081: /** @return the end offset */
082: public int getEndOffset() {
083: return (_doc == null || _doc.getLength() >= _endPosition
084: .getOffset()) ? _endPosition.getOffset() : _doc
085: .getLength();
086: }
087:
088: /** @return the string it was assigned */
089: public String getString() {
090: return _string;
091: }
092:
093: /** @return true if the specified region is equal to this one. */
094: public boolean equals(Object other) {
095: if ((other == null) || !(other instanceof MovingDocumentRegion))
096: return false;
097: MovingDocumentRegion o = (MovingDocumentRegion) other;
098: return (((_doc == null && o._doc == null) || _doc
099: .equals(o._doc))
100: && ((_file == null && o._file == null) || _file
101: .equals(o._file))
102: && _startPosition.getOffset() == o._startPosition
103: .getOffset()
104: && _endPosition.getOffset() == o._endPosition
105: .getOffset() && _string.equals(o._string));
106: }
107:
108: public String toString() {
109: return (_doc != null ? _doc.toString() : "null") + " "
110: + _startPosition.getOffset() + " .. "
111: + _endPosition.getOffset();
112: }
113: }
|