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 remains static all the time and does not respond to changes in the document.
045: * @version $Id$Region
046: */
047: public class StaticDocumentRegion implements DocumentRegion {
048: protected final OpenDefinitionsDocument _doc;
049: protected final File _file;
050: protected final int _startOffset;
051: protected final int _endOffset;
052: protected final String _string;
053:
054: /** Create a new static document region. Precondition: s != null. */
055: public StaticDocumentRegion(OpenDefinitionsDocument doc, File file,
056: int so, int eo, String s) {
057: assert s != null;
058: _doc = doc;
059: _file = file;
060: _startOffset = so;
061: _endOffset = eo;
062: _string = s;
063: }
064:
065: /** @return the document, or null if it hasn't been established yet */
066: public OpenDefinitionsDocument getDocument() {
067: return _doc;
068: }
069:
070: /** @return the file */
071: public File getFile() {
072: return _file;
073: }
074:
075: /** @return the start offset */
076: public int getStartOffset() {
077: return (_doc == null || _doc.getLength() >= _startOffset) ? _startOffset
078: : _doc.getLength();
079: }
080:
081: /** @return the end offset */
082: public int getEndOffset() {
083: return (_doc == null || _doc.getLength() >= _endOffset) ? _endOffset
084: : _doc.getLength();
085: }
086:
087: /** @return the string it was assigned */
088: public String getString() {
089: return _string;
090: }
091:
092: private static boolean equals(OpenDefinitionsDocument doc1,
093: OpenDefinitionsDocument doc2) {
094: if (doc1 == null)
095: return (doc2 == null);
096: if (doc2 == null)
097: return false;
098: return doc1.equals(doc2);
099: }
100:
101: private static boolean equals(File f1, File f2) {
102: if (f1 == null)
103: return (f2 == null);
104: if (f2 == null)
105: return false;
106: return f1.equals(f2);
107: }
108:
109: /** @return true if the specified region is equal to this one. */
110: public boolean equals(Object other) {
111: if (other == null || !(other instanceof StaticDocumentRegion))
112: return false;
113: StaticDocumentRegion o = (StaticDocumentRegion) other;
114: return (equals(_doc, o._doc) && equals(_file, o._file)
115: && _startOffset == o._startOffset
116: && _endOffset == o._endOffset && _string
117: .equals(o._string));
118: }
119:
120: public String toString() {
121: return (_doc != null ? _doc.toString() : "null") + " "
122: + _startOffset + " .. " + _endOffset;
123: }
124: }
|