dcd7b5fc31dcda995dd93bd96c3cfa90da648a7f
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / template / contentassist / InclusivePositionUpdater.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
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 change in [position.offset, position.offset +
20  * position.length] as belonging to the position.
21  * 
22  * @since 3.0
23  */
24 class InclusivePositionUpdater implements IPositionUpdater {
25
26         /** The position category. */
27         private final String fCategory;
28
29         /**
30          * Creates a new updater for the given <code>category</code>.
31          * 
32          * @param category
33          *            the new category.
34          */
35         public InclusivePositionUpdater(String category) {
36                 fCategory = category;
37         }
38
39         /*
40          * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
41          */
42         public void update(DocumentEvent event) {
43
44                 int eventOffset = event.getOffset();
45                 int eventOldLength = event.getLength();
46                 int eventNewLength = event.getText() == null ? 0 : event.getText()
47                                 .length();
48                 int deltaLength = eventNewLength - eventOldLength;
49
50                 try {
51                         Position[] positions = event.getDocument().getPositions(fCategory);
52
53                         for (int i = 0; i != positions.length; i++) {
54
55                                 Position position = positions[i];
56
57                                 if (position.isDeleted())
58                                         continue;
59
60                                 int offset = position.getOffset();
61                                 int length = position.getLength();
62                                 int end = offset + length;
63
64                                 if (offset > eventOffset + eventOldLength)
65                                         // position comes way
66                                         // after change - shift
67                                         position.setOffset(offset + deltaLength);
68                                 else if (end < eventOffset) {
69                                         // position comes way before change -
70                                         // leave alone
71                                 } else if (offset <= eventOffset
72                                                 && end >= eventOffset + eventOldLength) {
73                                         // event completely internal to the position - adjust length
74                                         position.setLength(length + deltaLength);
75                                 } else if (offset < eventOffset) {
76                                         // event extends over end of position - adjust length
77                                         int newEnd = eventOffset + eventNewLength;
78                                         position.setLength(newEnd - offset);
79                                 } else if (end > eventOffset + eventOldLength) {
80                                         // event extends from before position into it - adjust
81                                         // offset
82                                         // and length
83                                         // offset becomes end of event, length ajusted acordingly
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 getCategory() {
104                 return fCategory;
105         }
106
107 }