KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > Selection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.dom;
12
13 import org.eclipse.jdt.core.dom.ASTNode;
14
15 import org.eclipse.jface.text.IRegion;
16
17 import org.eclipse.jdt.internal.corext.Assert;
18
19 public class Selection {
20     
21     /** Flag indicating that the AST node somehow intersects with the selection. */
22     public static final int INTERSECTS= 0;
23     
24     /** Flag that indicates that an AST node appears before the selected nodes. */
25     public static final int BEFORE= 1;
26     
27     /** Flag indicating that an AST node is covered by the selection. */
28     public static final int SELECTED= 2;
29     
30     /** Flag indicating that an AST nodes appears after the selected nodes. */
31     public static final int AFTER= 3;
32     
33     private int fStart;
34     private int fLength;
35     private int fExclusiveEnd;
36     
37     protected Selection() {
38     }
39     
40     /**
41      * Creates a new selection from the given start and length.
42      *
43      * @param s the start offset of the selection (inclusive)
44      * @param l the length of the selection
45      * @return the created selection object
46      */

47     public static Selection createFromStartLength(int s, int l) {
48         Assert.isTrue(s >= 0 && l >= 0);
49         Selection result= new Selection();
50         result.fStart= s;
51         result.fLength= l;
52         result.fExclusiveEnd= s + l;
53         return result;
54     }
55     
56     /**
57      * Creates a new selection from the given start and end.
58      *
59      * @param s the start offset of the selection (inclusive)
60      * @param e the end offset of the selection (inclusive)
61      * @return the created selection object
62      */

63     public static Selection createFromStartEnd(int s, int e) {
64         Assert.isTrue(s >= 0 && e >= s);
65         Selection result= new Selection();
66         result.fStart= s;
67         result.fLength= e - s + 1;
68         result.fExclusiveEnd= result.fStart + result.fLength;
69         return result;
70     }
71     
72     public int getOffset() {
73         return fStart;
74     }
75     
76     public int getLength() {
77         return fLength;
78     }
79     
80     public int getInclusiveEnd() {
81         return fExclusiveEnd - 1;
82     }
83     
84     public int getExclusiveEnd() {
85         return fExclusiveEnd;
86     }
87     
88     /**
89      * Returns the selection mode of the given AST node regarding this selection. Possible
90      * values are <code>INTERSECTS</code>, <code>BEFORE</code>, <code>SELECTED</code>, and
91      * <code>AFTER</code>.
92      *
93      * @param node the node to return the visit mode for
94      *
95      * @return the selection mode of the given AST node regarding this selection
96      * @see #INTERSECTS
97      * @see #BEFORE
98      * @see #SELECTED
99      * @see #AFTER
100      */

101     public int getVisitSelectionMode(ASTNode node) {
102         int nodeStart= node.getStartPosition();
103         int nodeEnd= nodeStart + node.getLength();
104         if (nodeEnd <= fStart)
105             return BEFORE;
106         else if (covers(node))
107             return SELECTED;
108         else if (fExclusiveEnd <= nodeStart)
109             return AFTER;
110         return INTERSECTS;
111     }
112     
113     public int getEndVisitSelectionMode(ASTNode node) {
114         int nodeStart= node.getStartPosition();
115         int nodeEnd= nodeStart + node.getLength();
116         if (nodeEnd <= fStart)
117             return BEFORE;
118         else if (covers(node))
119             return SELECTED;
120         else if (nodeEnd >= fExclusiveEnd)
121             return AFTER;
122         return INTERSECTS;
123     }
124     
125     // enclosed* methods do an open interval check.
126

127     public boolean enclosedBy(ASTNode node) {
128         int nodeStart= node.getStartPosition();
129         return nodeStart < fStart && fExclusiveEnd < nodeStart + node.getLength();
130     }
131     
132 // public boolean enclosedBy(int nodeStart, int nodeLength) {
133
// return nodeStart < fStart && fStart + fLength < nodeStart + nodeLength;
134
// }
135

136     // cover* methods do a closed interval check.
137

138     public boolean covers(int position) {
139         return fStart <= position && position < fStart + fLength;
140     }
141     
142     public boolean covers(ASTNode node) {
143         int nodeStart= node.getStartPosition();
144         return fStart <= nodeStart && nodeStart + node.getLength() <= fExclusiveEnd;
145     }
146     
147 // public boolean covers(int nodeStart, int nodeLength) {
148
// return fStart <= nodeStart && nodeStart + nodeLength <= fExclusiveEnd;
149
// }
150

151     public boolean coveredBy(ASTNode node) {
152         int nodeStart= node.getStartPosition();
153         return nodeStart <= fStart && fExclusiveEnd <= nodeStart + node.getLength();
154     }
155     
156     public boolean coveredBy(IRegion region) {
157         int rangeStart= region.getOffset();
158         return rangeStart <= fStart && fExclusiveEnd <= rangeStart + region.getLength();
159     }
160     
161 // public boolean coveredBy(int sourceStart, int sourceEnd) {
162
// return sourceStart <= start && end <= sourceEnd;
163
// }
164
//
165
public boolean endsIn(ASTNode node) {
166         int nodeStart= node.getStartPosition();
167         return nodeStart < fExclusiveEnd && fExclusiveEnd < nodeStart + node.getLength();
168     }
169     
170     public boolean liesOutside(ASTNode node) {
171         int nodeStart= node.getStartPosition();
172         int nodeEnd= nodeStart + node.getLength();
173         boolean nodeBeforeSelection= nodeEnd < fStart;
174         boolean selectionBeforeNode= fExclusiveEnd < nodeStart;
175         return nodeBeforeSelection || selectionBeforeNode;
176     }
177     
178 // public boolean intersects(int sourceStart, int sourceEnd) {
179
// return sourceStart < start && start <= sourceEnd && sourceEnd <= end ||
180
// start <= sourceStart && sourceStart <= end && end < sourceEnd;
181
// }
182
//
183
// public boolean intersects(AstNode node) {
184
// return intersects(node.sourceStart, node.sourceEnd);
185
// }
186

187     /* non javadoc
188      * for debugging only
189      */

190     public String JavaDoc toString() {
191         return "<start == " + fStart + ", length == " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
192
}
193 }
194
Popular Tags