Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 0a422a3835e53cc600fbc4b61d7efb190e05b59b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
 * Copyright (c) 2001, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/

package org.eclipse.wst.sse.core.internal.text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.Position;
import org.eclipse.wst.sse.core.internal.util.Assert;

/**
 * Based on the Position management methods from
 * org.eclipse.jface.text.AbstractDocument
 */

public class GenericPositionManager {
	private CharSequence fCharSequence;



	private Map fPositions;
	/** All registered document position updaters */
	private List fPositionUpdaters;

	/**
	 * don't allow instantiation with out document pointer
	 * 
	 */
	private GenericPositionManager() {
		super();
	}

	/**
	 * 
	 */
	public GenericPositionManager(CharSequence charSequence) {
		this();
		// we only use charSequence for "length", to
		// made more generic than "document" even "text store"
		fCharSequence = charSequence;
		completeInitialization();
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#addPosition(org.eclipse.jface.text.Position)
	 */
	public void addPosition(Position position) throws BadLocationException {
		try {
			addPosition(IDocument.DEFAULT_CATEGORY, position);
		}
		catch (BadPositionCategoryException e) {
		}
	}



	/*
	 * @see org.eclipse.jface.text.IDocument#addPosition(java.lang.String,
	 *      org.eclipse.jface.text.Position)
	 */
	public synchronized void addPosition(String category, Position position) throws BadLocationException, BadPositionCategoryException {

		if ((0 > position.offset) || (0 > position.length) || (position.offset + position.length > getDocumentLength()))
			throw new BadLocationException();

		if (category == null)
			throw new BadPositionCategoryException();

		List list = (List) fPositions.get(category);
		if (list == null)
			throw new BadPositionCategoryException();

		list.add(computeIndexInPositionList(list, position.offset), position);
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#addPositionCategory(java.lang.String)
	 */
	public void addPositionCategory(String category) {

		if (category == null)
			return;

		if (!containsPositionCategory(category))
			fPositions.put(category, new ArrayList());
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#addPositionUpdater(org.eclipse.jface.text.IPositionUpdater)
	 */
	public void addPositionUpdater(IPositionUpdater updater) {
		insertPositionUpdater(updater, fPositionUpdaters.size());
	}


	/**
	 * Initializes document listeners, positions, and position updaters. Must
	 * be called inside the constructor after the implementation plug-ins have
	 * been set.
	 */
	protected void completeInitialization() {

		fPositions = new HashMap();
		fPositionUpdaters = new ArrayList();

		addPositionCategory(IDocument.DEFAULT_CATEGORY);
		addPositionUpdater(new DefaultPositionUpdater(IDocument.DEFAULT_CATEGORY));
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#computeIndexInCategory(java.lang.String,
	 *      int)
	 */
	public int computeIndexInCategory(String category, int offset) throws BadLocationException, BadPositionCategoryException {

		if (0 > offset || offset > getDocumentLength())
			throw new BadLocationException();

		List c = (List) fPositions.get(category);
		if (c == null)
			throw new BadPositionCategoryException();

		return computeIndexInPositionList(c, offset);
	}


	/**
	 * Computes the index in the list of positions at which a position with
	 * the given offset would be inserted. The position is supposed to become
	 * the first in this list of all positions with the same offset.
	 * 
	 * @param positions
	 *            the list in which the index is computed
	 * @param offset
	 *            the offset for which the index is computed
	 * @return the computed index
	 * 
	 * @see IDocument#computeIndexInCategory(String, int)
	 */
	protected synchronized int computeIndexInPositionList(List positions, int offset) {

		if (positions.size() == 0)
			return 0;

		int left = 0;
		int right = positions.size() - 1;
		int mid = 0;
		Position p = null;

		while (left < right) {

			mid = (left + right) / 2;

			p = (Position) positions.get(mid);
			if (offset < p.getOffset()) {
				if (left == mid)
					right = left;
				else
					right = mid - 1;
			}
			else if (offset > p.getOffset()) {
				if (right == mid)
					left = right;
				else
					left = mid + 1;
			}
			else if (offset == p.getOffset()) {
				left = right = mid;
			}

		}

		int pos = left;
		p = (Position) positions.get(pos);
		if (offset > p.getOffset()) {
			// append to the end
			pos++;
		}
		else {
			// entry will became the first of all entries with the same
			// offset
			do {
				--pos;
				if (pos < 0)
					break;
				p = (Position) positions.get(pos);
			}
			while (offset == p.getOffset());
			++pos;
		}

		Assert.isTrue(0 <= pos && pos <= positions.size());

		return pos;
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#containsPosition(java.lang.String,
	 *      int, int)
	 */
	public boolean containsPosition(String category, int offset, int length) {

		if (category == null)
			return false;

		List list = (List) fPositions.get(category);
		if (list == null)
			return false;

		int size = list.size();
		if (size == 0)
			return false;

		int index = computeIndexInPositionList(list, offset);
		if (index < size) {
			Position p = (Position) list.get(index);
			while (p != null && p.offset == offset) {
				if (p.length == length)
					return true;
				++index;
				p = (index < size) ? (Position) list.get(index) : null;
			}
		}

		return false;
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#containsPositionCategory(java.lang.String)
	 */
	public boolean containsPositionCategory(String category) {
		if (category != null)
			return fPositions.containsKey(category);
		return false;
	}



	public int getDocumentLength() {
		return fCharSequence.length();
	}

	/**
	 * Returns all positions managed by the document grouped by category.
	 * 
	 * @return the document's positions
	 */
	protected Map getDocumentManagedPositions() {
		return fPositions;
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#getPositionCategories()
	 */
	public String[] getPositionCategories() {
		String[] categories = new String[fPositions.size()];
		Iterator keys = fPositions.keySet().iterator();
		for (int i = 0; i < categories.length; i++)
			categories[i] = (String) keys.next();
		return categories;
	}


	public Position[] getPositions(String category) throws BadPositionCategoryException {

		if (category == null)
			throw new BadPositionCategoryException();

		List c = (List) fPositions.get(category);
		if (c == null)
			throw new BadPositionCategoryException();

		Position[] positions = new Position[c.size()];
		c.toArray(positions);
		return positions;
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#getPositionUpdaters()
	 */
	public IPositionUpdater[] getPositionUpdaters() {
		IPositionUpdater[] updaters = new IPositionUpdater[fPositionUpdaters.size()];
		fPositionUpdaters.toArray(updaters);
		return updaters;
	}



	/*
	 * @see org.eclipse.jface.text.IDocument#insertPositionUpdater(org.eclipse.jface.text.IPositionUpdater,
	 *      int)
	 */
	public synchronized void insertPositionUpdater(IPositionUpdater updater, int index) {

		for (int i = fPositionUpdaters.size() - 1; i >= 0; i--) {
			if (fPositionUpdaters.get(i) == updater)
				return;
		}

		if (index == fPositionUpdaters.size())
			fPositionUpdaters.add(updater);
		else
			fPositionUpdaters.add(index, updater);
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#removePosition(org.eclipse.jface.text.Position)
	 */
	public void removePosition(Position position) {
		try {
			removePosition(IDocument.DEFAULT_CATEGORY, position);
		}
		catch (BadPositionCategoryException e) {
		}
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#removePosition(java.lang.String,
	 *      org.eclipse.jface.text.Position)
	 */
	public synchronized void removePosition(String category, Position position) throws BadPositionCategoryException {

		if (position == null)
			return;

		if (category == null)
			throw new BadPositionCategoryException();

		List c = (List) fPositions.get(category);
		if (c == null)
			throw new BadPositionCategoryException();

		// remove based on identity not equality
		int size = c.size();
		for (int i = 0; i < size; i++) {
			if (position == c.get(i)) {
				c.remove(i);
				return;
			}
		}
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#removePositionCategory(java.lang.String)
	 */
	public void removePositionCategory(String category) throws BadPositionCategoryException {

		if (category == null)
			return;

		if (!containsPositionCategory(category))
			throw new BadPositionCategoryException();

		fPositions.remove(category);
	}

	/*
	 * @see org.eclipse.jface.text.IDocument#removePositionUpdater(org.eclipse.jface.text.IPositionUpdater)
	 */
	public synchronized void removePositionUpdater(IPositionUpdater updater) {
		for (int i = fPositionUpdaters.size() - 1; i >= 0; i--) {
			if (fPositionUpdaters.get(i) == updater) {
				fPositionUpdaters.remove(i);
				return;
			}
		}
	}


	/**
	 * Updates all positions of all categories to the change described by the
	 * document event. All registered document updaters are called in the
	 * sequence they have been arranged. Uses a robust iterator.
	 * 
	 * @param event
	 *            the document event describing the change to which to adapt
	 *            the positions
	 */
	protected synchronized void updatePositions(DocumentEvent event) {
		List list = new ArrayList(fPositionUpdaters);
		Iterator e = list.iterator();
		while (e.hasNext()) {
			IPositionUpdater u = (IPositionUpdater) e.next();
			u.update(event);
		}
	}


}

Back to the top