KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > NonDeletingPositionUpdater


1 /*******************************************************************************
2  * Copyright (c) 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.ltk.internal.core.refactoring;
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 /** Copied from org.eclipse.jface.text */
19 public final class NonDeletingPositionUpdater implements IPositionUpdater {
20
21     /** The position category. */
22     private final String JavaDoc fCategory;
23
24     /**
25      * Creates a new updater for the given <code>category</code>.
26      *
27      * @param category the new category.
28      */

29     public NonDeletingPositionUpdater(String JavaDoc category) {
30         fCategory= category;
31     }
32
33     /*
34      * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
35      */

36     public void update(DocumentEvent event) {
37
38         int eventOffset= event.getOffset();
39         int eventOldEndOffset= eventOffset + event.getLength();
40         int eventNewLength= event.getText() == null ? 0 : event.getText().length();
41         int eventNewEndOffset= eventOffset + eventNewLength;
42         int deltaLength= eventNewLength - event.getLength();
43
44         try {
45             Position[] positions= event.getDocument().getPositions(fCategory);
46
47             for (int i= 0; i != positions.length; i++) {
48
49                 Position position= positions[i];
50
51                 if (position.isDeleted())
52                     continue;
53
54                 int offset= position.getOffset();
55                 int length= position.getLength();
56                 int end= offset + length;
57
58                 if (offset > eventOldEndOffset) {
59                     // position comes way after change - shift
60
position.setOffset(offset + deltaLength);
61                 } else if (end < eventOffset) {
62                     // position comes way before change - leave alone
63
} else if (offset <= eventOffset && end >= eventOldEndOffset) {
64                     // event completely internal to the position - adjust length
65
position.setLength(length + deltaLength);
66                 } else if (offset < eventOffset) {
67                     // event extends over end of position - include the
68
// replacement text into the position
69
position.setLength(eventNewEndOffset - offset);
70                 } else if (end > eventOldEndOffset) {
71                     // event extends from before position into it - adjust
72
// offset and length, including the replacement text into
73
// the position
74
position.setOffset(eventOffset);
75                     int deleted= eventOldEndOffset - offset;
76                     position.setLength(length - deleted + eventNewLength);
77                 } else {
78                     // event comprises the position - keep it at the same
79
// position, but always inside the replacement text
80
int newOffset= Math.min(offset, eventNewEndOffset);
81                     int newEndOffset= Math.min(end, eventNewEndOffset);
82                     position.setOffset(newOffset);
83                     position.setLength(newEndOffset - newOffset);
84                 }
85             }
86         } catch (BadPositionCategoryException e) {
87             // ignore and return
88
}
89     }
90
91     /**
92      * Returns the position category.
93      *
94      * @return the position category
95      */

96     public String JavaDoc getCategory() {
97         return fCategory;
98     }
99 }
100
Popular Tags