KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > ui > text > Match


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.ui.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15 /**
16  * A textual match in a given object. This class may be instantiated and also subclassed (to add
17  * additional match state like accuracy, etc). The element a match is reported
18  * against is assumed to contain the match, and the UI will group matches
19  * against the same element together. A match has an offset and a length which
20  * may be specified in characters or in lines.
21  *
22  * @since 3.0
23  */

24 public class Match {
25     
26     /**
27      * A constant expressing that offset and length of this match are specified
28      * in lines
29      */

30     public static final int UNIT_LINE= 1;
31     
32     /**
33      * A constant expressing that offset and length of this match are specified
34      * in characters
35      */

36     public static final int UNIT_CHARACTER= 2;
37
38     private static final int IS_FILTERED= 1 << 2;
39     
40     private Object JavaDoc fElement;
41     private int fOffset;
42     private int fLength;
43     private int fFlags;
44
45     /**
46      * Constructs a new Match object.
47      *
48      * @param element
49      * the element that contains the match
50      * @param unit
51      * the unit offset and length are based on
52      * @param offset
53      * the offset the match starts at
54      * @param length
55      * the length of the match
56      */

57     public Match(Object JavaDoc element, int unit, int offset, int length) {
58         Assert.isTrue(unit == UNIT_CHARACTER || unit == UNIT_LINE);
59         fElement= element;
60         fOffset= offset;
61         fLength= length;
62         fFlags= unit;
63     }
64
65     /**
66      * Constructs a new Match object. The offset and length will be based on
67      * characters.
68      *
69      * @param element
70      * the element that contains the match
71      * @param offset
72      * the offset the match starts at
73      * @param length
74      * the length of the match
75      */

76     public Match(Object JavaDoc element, int offset, int length) {
77         this(element, UNIT_CHARACTER, offset, length);
78     }
79
80     /**
81      * Returns the offset of this match.
82      *
83      * @return the offset
84      */

85     public int getOffset() {
86         return fOffset;
87     }
88
89     /**
90      * Sets the offset of this match.
91      *
92      * @param offset
93      * the offset to set
94      */

95     public void setOffset(int offset) {
96         fOffset= offset;
97     }
98
99     /**
100      * Returns the length of this match.
101      *
102      * @return the length
103      */

104     public int getLength() {
105         return fLength;
106     }
107
108     /**
109      * Sets the length.
110      *
111      * @param length
112      * the length to set
113      */

114     public void setLength(int length) {
115         fLength= length;
116     }
117
118     /**
119      * Returns the element that contains this match. The element is used to group the match.
120      *
121      * @return the element that contains this match
122      */

123     public Object JavaDoc getElement() {
124         return fElement;
125     }
126
127     /**
128      * Returns whether match length and offset are expressed in lines or
129      * characters.
130      *
131      * @return either UNIT_LINE or UNIT_CHARACTER;
132      */

133     public int getBaseUnit() {
134         if ((fFlags & UNIT_LINE) != 0)
135             return UNIT_LINE;
136         return UNIT_CHARACTER;
137     }
138     
139     /**
140      * Marks this match as filtered or not.
141      *
142      * @param value <code>true</code> if the match is filtered;
143      * otherwise <code>false</code>
144      *
145      * @since 3.1
146      */

147     public void setFiltered(boolean value) {
148         if (value) {
149             fFlags |= IS_FILTERED;
150         } else {
151             fFlags &= (~IS_FILTERED);
152         }
153     }
154     
155     /**
156      * Returns whether this match is filtered or not.
157      *
158      * @return <code>true<code> if the match is filtered;
159      * otherwise <code>false</code>
160      *
161      * @since 3.1
162      */

163     public boolean isFiltered() {
164         return (fFlags & IS_FILTERED) != 0;
165     }
166 }
167
Popular Tags