KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > 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.jface.internal.text;
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 /**
20  * A position updater that never deletes a position. If the region containing
21  * the position is deleted, the position is moved to the beginning/end (falling
22  * together) of the change. If the region containing the position is replaced,
23  * the position is placed at the same location inside the replacement text, but
24  * always inside the replacement text.
25  *
26  * @since 3.1
27  */

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

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

44     public void update(DocumentEvent event) {
45
46         int eventOffset= event.getOffset();
47         int eventOldEndOffset= eventOffset + event.getLength();
48         int eventNewLength= event.getText() == null ? 0 : event.getText().length();
49         int eventNewEndOffset= eventOffset + eventNewLength;
50         int deltaLength= eventNewLength - event.getLength();
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 > eventOldEndOffset) {
67                     // position comes way after change - shift
68
position.setOffset(offset + deltaLength);
69                 } else if (end < eventOffset) {
70                     // position comes way before change - leave alone
71
} else if (offset <= eventOffset && end >= eventOldEndOffset) {
72                     // event completely internal to the position - adjust length
73
position.setLength(length + deltaLength);
74                 } else if (offset < eventOffset) {
75                     // event extends over end of position - include the
76
// replacement text into the position
77
position.setLength(eventNewEndOffset - offset);
78                 } else if (end > eventOldEndOffset) {
79                     // event extends from before position into it - adjust
80
// offset and length, including the replacement text into
81
// the position
82
position.setOffset(eventOffset);
83                     int deleted= eventOldEndOffset - offset;
84                     position.setLength(length - deleted + eventNewLength);
85                 } else {
86                     // event comprises the position - keep it at the same
87
// position, but always inside the replacement text
88
int newOffset= Math.min(offset, eventNewEndOffset);
89                     int newEndOffset= Math.min(end, eventNewEndOffset);
90                     position.setOffset(newOffset);
91                     position.setLength(newEndOffset - newOffset);
92                 }
93             }
94         } catch (BadPositionCategoryException e) {
95             // ignore and return
96
}
97     }
98
99     /**
100      * Returns the position category.
101      *
102      * @return the position category
103      */

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