KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > template > java > ExclusivePositionUpdater


1 /*******************************************************************************
2  * Copyright (c) 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.jdt.internal.corext.template.java;
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 takes any changes at the borders of a position to not belong to the position.
20  *
21  * @since 3.2
22  */

23 final class ExclusivePositionUpdater implements IPositionUpdater {
24
25     /** The position category. */
26     private final String JavaDoc fCategory;
27
28     /**
29      * Creates a new updater for the given <code>category</code>.
30      *
31      * @param category the new category.
32      */

33     public ExclusivePositionUpdater(String JavaDoc category) {
34         fCategory= category;
35     }
36
37     /*
38      * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
39      */

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

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