KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > reconciler > DirtyRegion


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.jface.text.reconciler;
12
13 import org.eclipse.jface.text.ITypedRegion;
14
15
16 /**
17  * A dirty region describes a document range which has been changed.
18  */

19 public class DirtyRegion implements ITypedRegion {
20
21     /**
22      * Identifies an insert operation.
23      */

24     final static public String JavaDoc INSERT= "__insert"; //$NON-NLS-1$
25
/**
26      * Identifies a remove operation.
27      */

28     final static public String JavaDoc REMOVE= "__remove"; //$NON-NLS-1$
29

30     /** The region's offset. */
31     private int fOffset;
32     /** The region's length. */
33     private int fLength;
34     /** Indicates the type of the applied change. */
35     private String JavaDoc fType;
36     /** The text which has been inserted. */
37     private String JavaDoc fText;
38
39     /**
40      * Creates a new dirty region.
41      *
42      * @param offset the offset within the document where the change occurred
43      * @param length the length of the text within the document that changed
44      * @param type the type of change that this region represents: {@link #INSERT} {@link #REMOVE}
45      * @param text the substitution text
46      */

47     public DirtyRegion(int offset, int length, String JavaDoc type, String JavaDoc text) {
48         fOffset= offset;
49         fLength= length;
50         fType= normalizeTypeValue(type);
51         fText= text;
52     }
53
54     /**
55      * Computes the normalized type value to ensure that the implementation can use object identity rather
56      * than equality.
57      *
58      * @param type the type value
59      * @return the normalized type value or <code>null</code>
60      * @since 3.1
61      */

62     private String JavaDoc normalizeTypeValue(String JavaDoc type) {
63         if (INSERT.equals(type))
64             return INSERT;
65         if (REMOVE.equals(type))
66             return REMOVE;
67         return null;
68     }
69
70     /*
71      * @see ITypedRegion#getOffset()
72      */

73     public int getOffset() {
74         return fOffset;
75     }
76
77     /*
78      * @see ITypedRegion#getLength()
79      */

80     public int getLength() {
81         return fLength;
82     }
83
84     /*
85      * @see ITypedRegion#getType
86      */

87     public String JavaDoc getType() {
88         return fType;
89     }
90
91     /**
92      * Returns the text that changed as part of the region change.
93      *
94      * @return the changed text
95      */

96     public String JavaDoc getText() {
97         return fText;
98     }
99
100     /**
101      * Modify the receiver so that it encompasses the region specified by the dirty region.
102      *
103      * @param dr the dirty region with which to merge
104      */

105     void mergeWith(DirtyRegion dr) {
106         int start= Math.min(fOffset, dr.fOffset);
107         int end= Math.max(fOffset + fLength, dr.fOffset + dr.fLength);
108         fOffset= start;
109         fLength= end - start;
110         fText= (dr.fText == null ? fText : (fText == null) ? dr.fText : fText + dr.fText);
111     }
112 }
113
Popular Tags