KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > SimpleDocumentRegion


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model;
35
36 import javax.swing.text.Position JavaDoc;
37 import java.io.File JavaDoc;
38
39 /**
40  * Class for a simple document region. If a document is provided, then the region will move within the document.
41  * @version $Id$
42  */

43 public class SimpleDocumentRegion implements DocumentRegion {
44   protected final OpenDefinitionsDocument _doc;
45   protected final File JavaDoc _file;
46   protected volatile int _startOffset;
47   protected volatile int _endOffset;
48   protected volatile Position JavaDoc _startPos = null;
49   protected volatile Position JavaDoc _endPos = null;
50   
51   /** Create a new simple document region.
52     * @param doc document that contains this region, or null if we don't have a document yet
53     * @param file file that contains the region
54     * @param so start offset of the region; if doc is non-null, then a Position will be created that moves within the document
55     * @param eo end offset of the region; if doc is non-null, then a Position will be created that moves within the document
56     */

57   public SimpleDocumentRegion(OpenDefinitionsDocument doc, File JavaDoc file, int so, int eo) {
58     _doc = doc;
59     _file = file;
60     _startOffset = so;
61     _endOffset = eo;
62     if (_doc != null) {
63       try {
64         _startPos = _doc.createPosition(so);
65         _endPos = _doc.createPosition(eo);
66       }
67       catch(javax.swing.text.BadLocationException JavaDoc e) { /* ignore, offset will be static */ }
68     }
69   }
70   
71   /** @return the document, or null if it hasn't been established yet */
72   public OpenDefinitionsDocument getDocument() { return _doc; }
73
74   /** @return the file */
75   public File JavaDoc getFile() { return _file; }
76
77   /** @return the start offset */
78   public int getStartOffset() {
79     if (_startPos != null) {
80       // if we have a position that moves within the document, update the offset
81
_startOffset = _startPos.getOffset();
82     }
83     return _startOffset;
84   }
85
86   /** @return the end offset */
87   public int getEndOffset() {
88     if (_endPos != null) {
89       // if we have a position that moves within the document, update the offset
90
_endOffset = _endPos.getOffset();
91     }
92     return _endOffset;
93   }
94   
95   /** Structural equality method that copes with null! This method should be a member of class Object. */
96   public static boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
97     if (o1 == null) return o2 == null;
98     return o1.equals(o2);
99   }
100   
101   /** @return true if the specified region is equal to this one. */
102   public boolean equals(Object JavaDoc other) {
103     if (other == null || other.getClass() != getClass()) return false;
104     SimpleDocumentRegion o = (SimpleDocumentRegion) other;
105     return equals(_doc, o._doc) && equals(_file, o._file) &&
106             _startPos.getOffset() == o._startPos.getOffset() &&
107             _endPos.getOffset() == o._endPos.getOffset();
108   }
109   
110   public String JavaDoc toString() {
111     return (_doc != null ? _doc.toString() : "null") + " "+_startOffset+" .. "+_endOffset;
112   }
113 }
114
Popular Tags