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 javax.swing.text.Position;
040: import java.io.File;
041:
042: /**
043: * Class for a simple document region. If a document is provided, then the region will move within the document.
044: * @version $Id$
045: */
046: public class SimpleDocumentRegion implements DocumentRegion {
047: protected final OpenDefinitionsDocument _doc;
048: protected final File _file;
049: protected volatile int _startOffset;
050: protected volatile int _endOffset;
051: protected volatile Position _startPos = null;
052: protected volatile Position _endPos = null;
053:
054: /** Create a new simple document region.
055: * @param doc document that contains this region, or null if we don't have a document yet
056: * @param file file that contains the region
057: * @param so start offset of the region; if doc is non-null, then a Position will be created that moves within the document
058: * @param eo end offset of the region; if doc is non-null, then a Position will be created that moves within the document
059: */
060: public SimpleDocumentRegion(OpenDefinitionsDocument doc, File file,
061: int so, int eo) {
062: _doc = doc;
063: _file = file;
064: _startOffset = so;
065: _endOffset = eo;
066: if (_doc != null) {
067: try {
068: _startPos = _doc.createPosition(so);
069: _endPos = _doc.createPosition(eo);
070: } catch (javax.swing.text.BadLocationException e) { /* ignore, offset will be static */
071: }
072: }
073: }
074:
075: /** @return the document, or null if it hasn't been established yet */
076: public OpenDefinitionsDocument getDocument() {
077: return _doc;
078: }
079:
080: /** @return the file */
081: public File getFile() {
082: return _file;
083: }
084:
085: /** @return the start offset */
086: public int getStartOffset() {
087: if (_startPos != null) {
088: // if we have a position that moves within the document, update the offset
089: _startOffset = _startPos.getOffset();
090: }
091: return _startOffset;
092: }
093:
094: /** @return the end offset */
095: public int getEndOffset() {
096: if (_endPos != null) {
097: // if we have a position that moves within the document, update the offset
098: _endOffset = _endPos.getOffset();
099: }
100: return _endOffset;
101: }
102:
103: /** Structural equality method that copes with null! This method should be a member of class Object. */
104: public static boolean equals(Object o1, Object o2) {
105: if (o1 == null)
106: return o2 == null;
107: return o1.equals(o2);
108: }
109:
110: /** @return true if the specified region is equal to this one. */
111: public boolean equals(Object other) {
112: if (other == null || other.getClass() != getClass())
113: return false;
114: SimpleDocumentRegion o = (SimpleDocumentRegion) other;
115: return equals(_doc, o._doc) && equals(_file, o._file)
116: && _startPos.getOffset() == o._startPos.getOffset()
117: && _endPos.getOffset() == o._endPos.getOffset();
118: }
119:
120: public String toString() {
121: return (_doc != null ? _doc.toString() : "null") + " "
122: + _startOffset + " .. " + _endOffset;
123: }
124: }
|