KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16
17 /**
18  * Queue used by {@link org.eclipse.jface.text.reconciler.Reconciler} to manage
19  * dirty regions. When a dirty region is inserted into the queue, the queue tries
20  * to fold it into the neighboring dirty region.
21  *
22  * @see org.eclipse.jface.text.reconciler.Reconciler
23  * @see org.eclipse.jface.text.reconciler.DirtyRegion
24  */

25 class DirtyRegionQueue {
26
27     /** The list of dirty regions. */
28     private List JavaDoc fDirtyRegions= new ArrayList JavaDoc();
29
30     /**
31      * Creates a new empty dirty region.
32      */

33     public DirtyRegionQueue() {
34         super();
35     }
36
37     /**
38      * Adds a dirty region to the end of the dirty-region queue.
39      *
40      * @param dr the dirty region to add
41      */

42     public void addDirtyRegion(DirtyRegion dr) {
43         // If the dirty region being added is directly after the last dirty
44
// region on the queue then merge the two dirty regions together.
45
DirtyRegion lastDR= getLastDirtyRegion();
46         boolean wasMerged= false;
47         if (lastDR != null)
48             if (lastDR.getType() == dr.getType())
49                 if (lastDR.getType() == DirtyRegion.INSERT) {
50                     if (lastDR.getOffset() + lastDR.getLength() == dr.getOffset()) {
51                         lastDR.mergeWith(dr);
52                         wasMerged= true;
53                     }
54                 } else if (lastDR.getType() == DirtyRegion.REMOVE) {
55                     if (dr.getOffset() + dr.getLength() == lastDR.getOffset()) {
56                         lastDR.mergeWith(dr);
57                         wasMerged= true;
58                     }
59                 }
60
61         if (!wasMerged)
62             // Don't merge- just add the new one onto the queue.
63
fDirtyRegions.add(dr);
64     }
65
66     /**
67      * Returns the last dirty region that was added to the queue.
68      *
69      * @return the last DirtyRegion on the queue
70      */

71     private DirtyRegion getLastDirtyRegion() {
72         int size= fDirtyRegions.size();
73         return (size == 0 ? null : (DirtyRegion) fDirtyRegions.get(size - 1));
74     }
75
76     /**
77      * Returns the number of regions in the queue.
78      *
79      * @return the dirty-region queue-size
80      */

81     public int getSize() {
82         return fDirtyRegions.size();
83     }
84
85     /**
86      * Throws away all entries in the queue.
87      */

88     public void purgeQueue() {
89         fDirtyRegions.clear();
90     }
91
92     /**
93      * Removes and returns the first dirty region in the queue
94      *
95      * @return the next dirty region on the queue
96      */

97     public DirtyRegion removeNextDirtyRegion() {
98         if (fDirtyRegions.size() == 0)
99             return null;
100         DirtyRegion dr= (DirtyRegion) fDirtyRegions.get(0);
101         fDirtyRegions.remove(0);
102         return dr;
103     }
104 }
105
Popular Tags