Mercurial > projects > dwt-addons
comparison dwtx/jface/internal/text/NonDeletingPositionUpdater.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
comparison
equal
deleted
inserted
replaced
128:8df1d4193877 | 129:eb30df5ca28b |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.internal.text.NonDeletingPositionUpdater; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.jface.text.BadPositionCategoryException; | |
18 import dwtx.jface.text.DocumentEvent; | |
19 import dwtx.jface.text.IPositionUpdater; | |
20 import dwtx.jface.text.Position; | |
21 | |
22 | |
23 /** | |
24 * A position updater that never deletes a position. If the region containing | |
25 * the position is deleted, the position is moved to the beginning/end (falling | |
26 * together) of the change. If the region containing the position is replaced, | |
27 * the position is placed at the same location inside the replacement text, but | |
28 * always inside the replacement text. | |
29 * | |
30 * @since 3.1 | |
31 */ | |
32 public final class NonDeletingPositionUpdater : IPositionUpdater { | |
33 /** The position category. */ | |
34 private final String fCategory; | |
35 | |
36 /** | |
37 * Creates a new updater for the given <code>category</code>. | |
38 * | |
39 * @param category the new category. | |
40 */ | |
41 public NonDeletingPositionUpdater(String category) { | |
42 fCategory= category; | |
43 } | |
44 | |
45 /* | |
46 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent) | |
47 */ | |
48 public void update(DocumentEvent event) { | |
49 | |
50 int eventOffset= event.getOffset(); | |
51 int eventOldEndOffset= eventOffset + event.getLength(); | |
52 int eventNewLength= event.getText() is null ? 0 : event.getText().length(); | |
53 int eventNewEndOffset= eventOffset + eventNewLength; | |
54 int deltaLength= eventNewLength - event.getLength(); | |
55 | |
56 try { | |
57 Position[] positions= event.getDocument().getPositions(fCategory); | |
58 | |
59 for (int i= 0; i !is positions.length; i++) { | |
60 | |
61 Position position= positions[i]; | |
62 | |
63 if (position.isDeleted()) | |
64 continue; | |
65 | |
66 int offset= position.getOffset(); | |
67 int length= position.getLength(); | |
68 int end= offset + length; | |
69 | |
70 if (offset > eventOldEndOffset) { | |
71 // position comes way after change - shift | |
72 position.setOffset(offset + deltaLength); | |
73 } else if (end < eventOffset) { | |
74 // position comes way before change - leave alone | |
75 } else if (offset <= eventOffset && end >= eventOldEndOffset) { | |
76 // event completely internal to the position - adjust length | |
77 position.setLength(length + deltaLength); | |
78 } else if (offset < eventOffset) { | |
79 // event extends over end of position - include the | |
80 // replacement text into the position | |
81 position.setLength(eventNewEndOffset - offset); | |
82 } else if (end > eventOldEndOffset) { | |
83 // event extends from before position into it - adjust | |
84 // offset and length, including the replacement text into | |
85 // the position | |
86 position.setOffset(eventOffset); | |
87 int deleted= eventOldEndOffset - offset; | |
88 position.setLength(length - deleted + eventNewLength); | |
89 } else { | |
90 // event comprises the position - keep it at the same | |
91 // position, but always inside the replacement text | |
92 int newOffset= Math.min(offset, eventNewEndOffset); | |
93 int newEndOffset= Math.min(end, eventNewEndOffset); | |
94 position.setOffset(newOffset); | |
95 position.setLength(newEndOffset - newOffset); | |
96 } | |
97 } | |
98 } catch (BadPositionCategoryException e) { | |
99 // ignore and return | |
100 } | |
101 } | |
102 | |
103 /** | |
104 * Returns the position category. | |
105 * | |
106 * @return the position category | |
107 */ | |
108 public String getCategory() { | |
109 return fCategory; | |
110 } | |
111 } |