Mercurial > projects > dwt-addons
comparison dwtx/jface/text/link/InclusivePositionUpdater.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) 2000, 2008 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.text.link.InclusivePositionUpdater; | |
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 * Position updater that considers any change in | |
25 * <code>[p.offset, p.offset + p.length]</code> of a {@link Position} | |
26 * <code>p</code> as belonging to the position. | |
27 * <p> | |
28 * Internal class. Do not use. Public for testing purposes only. | |
29 * </p> | |
30 * | |
31 * @since 3.0 | |
32 * @noinstantiate This class is not intended to be instantiated by clients. | |
33 * @noextend This class is not intended to be subclassed by clients. | |
34 */ | |
35 public class InclusivePositionUpdater : IPositionUpdater { | |
36 | |
37 /** The position category. */ | |
38 private final String fCategory; | |
39 | |
40 /** | |
41 * Creates a new updater for the given <code>category</code>. | |
42 * | |
43 * @param category the new category. | |
44 */ | |
45 public InclusivePositionUpdater(String category) { | |
46 fCategory= category; | |
47 } | |
48 | |
49 /* | |
50 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent) | |
51 */ | |
52 public void update(DocumentEvent event) { | |
53 | |
54 int eventOffset= event.getOffset(); | |
55 int eventOldLength= event.getLength(); | |
56 int eventNewLength= event.getText() is null ? 0 : event.getText().length(); | |
57 int deltaLength= eventNewLength - eventOldLength; | |
58 | |
59 try { | |
60 Position[] positions= event.getDocument().getPositions(fCategory); | |
61 | |
62 for (int i= 0; i !is positions.length; i++) { | |
63 | |
64 Position position= positions[i]; | |
65 | |
66 if (position.isDeleted()) | |
67 continue; | |
68 | |
69 int offset= position.getOffset(); | |
70 int length= position.getLength(); | |
71 int end= offset + length; | |
72 | |
73 if (offset > eventOffset + eventOldLength) | |
74 // position comes way | |
75 // after change - shift | |
76 position.setOffset(offset + deltaLength); | |
77 else if (end < eventOffset) { | |
78 // position comes way before change - | |
79 // leave alone | |
80 } else if (offset <= eventOffset && end >= eventOffset + eventOldLength) { | |
81 // event completely internal to the position - adjust length | |
82 position.setLength(length + deltaLength); | |
83 } else if (offset < eventOffset) { | |
84 // event extends over end of position - adjust length | |
85 int newEnd= eventOffset + eventNewLength; | |
86 position.setLength(newEnd - offset); | |
87 } else if (end > eventOffset + eventOldLength) { | |
88 // event extends from before position into it - adjust offset | |
89 // and length | |
90 // offset becomes end of event, length adjusted accordingly | |
91 // we want to recycle the overlapping part | |
92 position.setOffset(eventOffset); | |
93 int deleted= eventOffset + eventOldLength - offset; | |
94 position.setLength(length - deleted + eventNewLength); | |
95 } else { | |
96 // event consumes the position - delete it | |
97 position.delete(); | |
98 } | |
99 } | |
100 } catch (BadPositionCategoryException e) { | |
101 // ignore and return | |
102 } | |
103 } | |
104 | |
105 /** | |
106 * Returns the position category. | |
107 * | |
108 * @return the position category | |
109 */ | |
110 public String getCategory() { | |
111 return fCategory; | |
112 } | |
113 | |
114 } |