KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > link > InclusivePositionUpdater


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.jface.text.link;
12
13 import org.eclipse.jface.text.BadPositionCategoryException;
14 import org.eclipse.jface.text.DocumentEvent;
15 import org.eclipse.jface.text.IPositionUpdater;
16 import org.eclipse.jface.text.Position;
17
18 /**
19  * Position updater that considers any change in
20  * <code>[p.offset,&nbsp;p.offset&nbsp;+&nbsp;p.length]</code> of a {@link Position}
21  * <code>p</code> as belonging to the position.
22  * <p>
23  * Internal class. Do not use. Public for testing purposes only.
24  * </p>
25  *
26  * @since 3.0
27  */

28 public class InclusivePositionUpdater implements IPositionUpdater {
29
30     /** The position category. */
31     private final String JavaDoc fCategory;
32
33     /**
34      * Creates a new updater for the given <code>category</code>.
35      *
36      * @param category the new category.
37      */

38     public InclusivePositionUpdater(String JavaDoc category) {
39         fCategory= category;
40     }
41
42     /*
43      * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
44      */

45     public void update(DocumentEvent event) {
46
47         int eventOffset= event.getOffset();
48         int eventOldLength= event.getLength();
49         int eventNewLength= event.getText() == null ? 0 : event.getText().length();
50         int deltaLength= eventNewLength - eventOldLength;
51
52         try {
53             Position[] positions= event.getDocument().getPositions(fCategory);
54
55             for (int i= 0; i != positions.length; i++) {
56
57                 Position position= positions[i];
58
59                 if (position.isDeleted())
60                     continue;
61
62                 int offset= position.getOffset();
63                 int length= position.getLength();
64                 int end= offset + length;
65
66                 if (offset > eventOffset + eventOldLength)
67                     // position comes way
68
// after change - shift
69
position.setOffset(offset + deltaLength);
70                 else if (end < eventOffset) {
71                     // position comes way before change -
72
// leave alone
73
} else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
74                     // event completely internal to the position - adjust length
75
position.setLength(length + deltaLength);
76                 } else if (offset < eventOffset) {
77                     // event extends over end of position - adjust length
78
int newEnd= eventOffset + eventNewLength;
79                     position.setLength(newEnd - offset);
80                 } else if (end > eventOffset + eventOldLength) {
81                     // event extends from before position into it - adjust offset
82
// and length
83
// offset becomes end of event, length adjusted accordingly
84
// we want to recycle the overlapping part
85
position.setOffset(eventOffset);
86                     int deleted= eventOffset + eventOldLength - offset;
87                     position.setLength(length - deleted + eventNewLength);
88                 } else {
89                     // event consumes the position - delete it
90
position.delete();
91                 }
92             }
93         } catch (BadPositionCategoryException e) {
94             // ignore and return
95
}
96     }
97
98     /**
99      * Returns the position category.
100      *
101      * @return the position category
102      */

103     public String JavaDoc getCategory() {
104         return fCategory;
105     }
106
107 }
108
Popular Tags