Skip to main content
summaryrefslogtreecommitdiffstats
blob: cd85aac9791c61db0fa2362714df4d0a0d9fa1d1 (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.INavigationLocation;
import org.eclipse.ui.NavigationLocation;


/**
 * Represents the text selection context marked for the user in the navigation history.
 *
 * @since 2.1
 */
public class TextSelectionNavigationLocation extends NavigationLocation {

	// Memento tags and values
	private static final String TAG_X= "x"; //$NON-NLS-1$
	private static final String TAG_Y= "y"; //$NON-NLS-1$
	private static final String TAG_INFO= "info"; //$NON-NLS-1$
	private static final String INFO_DELETED= "deleted"; //$NON-NLS-1$
	private static final String INFO_NOT_DELETED= "not_deleted"; //$NON-NLS-1$

	private static final String CATEGORY= "__navigation_" + TextSelectionNavigationLocation.class.hashCode(); //$NON-NLS-1$
	private static final IPositionUpdater fgPositionUpdater= new DefaultPositionUpdater(CATEGORY);


	private Position fPosition;
	private IDocument fDocument;
	private Position fSavedPosition;


	/**
	 * Creates a new text selection navigation location.
	 *
	 * @param part the text editor part
	 * @param initialize a boolean indicating whether to initialize the new instance from the current selection
	 */
	public TextSelectionNavigationLocation(ITextEditor part, boolean initialize) {
		super(part);

		if (initialize) {

			ISelection s= part.getSelectionProvider().getSelection();
			if(s == null || s.isEmpty())
				return;

			ITextSelection selection= (ITextSelection) s;

			IDocument document= getDocument(part);
			Position position= new Position(selection.getOffset(), selection.getLength());
			if (installOnDocument(document, position)) {
				fDocument= document;
				fPosition= position;
				if (!part.isDirty())
					fSavedPosition= new Position(fPosition.offset, fPosition.length);
			}
		}
	}

	/**
	 * Returns the text editor's document.
	 *
	 * @param part the text editor
	 * @return the document of the given text editor
	 */
	private IDocument getDocument(ITextEditor part) {
		IDocumentProvider provider= part.getDocumentProvider();
		return provider.getDocument(part.getEditorInput());
	}

	/**
	 * Installs the given position on the given document.
	 *
	 * @param document the document
	 * @param position the position
	 * @return <code>true</code> if the position could be installed
	 */
	private boolean  installOnDocument(IDocument document, Position position) {

		if (document != null && position != null) {

			if (!document.containsPositionCategory(CATEGORY)) {
				document.addPositionCategory(CATEGORY);
				document.addPositionUpdater(fgPositionUpdater);
			}

			try {
				document.addPosition(CATEGORY, position);
				return true;
			} catch (BadLocationException e) {
			} catch (BadPositionCategoryException e) {
			}
		}

		return false;
	}

	/**
	 * Uninstalls the given position from the given document.
	 *
	 * @param document the document
	 * @param position the position
	 * @return <code>true</code> if the position could be uninstalled
	 */
	private boolean uninstallFromDocument(IDocument document, Position position) {

		if (document != null && position != null) {
			try {

				document.removePosition(CATEGORY, position);

				Position[] category= document.getPositions(CATEGORY);
				if (category == null || category.length == 0) {
					document.removePositionCategory(CATEGORY);
					document.removePositionUpdater(fgPositionUpdater);
				}
				return true;

			} catch (BadPositionCategoryException e) {
			}
		}

		return false;
	}

	@Override
	public String toString() {
		return "Selection<" + fPosition + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Tells whether this location is equal to the current
	 * location in the given text editor.
	 *
	 * @param part the text editor
	 * @return <code>true</code> if the locations are equal
	 */
	private boolean equalsLocationOf(ITextEditor part) {

		if (fPosition == null)
			return true;

		if (fPosition.isDeleted)
			return false;

		ISelectionProvider provider= part.getSite().getSelectionProvider();
		ISelection selection= provider.getSelection();
		if (selection instanceof ITextSelection) {
			ITextSelection textSelection= (ITextSelection) selection;
			if (textSelection.getOffset() == fPosition.offset && textSelection.getLength() == fPosition.length) {
				String text= textSelection.getText();
				if (text != null) {
					try {
						return text.equals(fDocument.get(fPosition.offset, fPosition.length));
					} catch (BadLocationException e) {
					}
				}
			}
		}

		return false;
	}

	@Override
	public void dispose() {
		uninstallFromDocument(fDocument, fPosition);
		fDocument= null;
		fPosition= null;
		fSavedPosition= null;
		super.dispose();
	}

	/**
	 * Releases the state of this location.
	 */
	@Override
	public void releaseState() {
		// deactivate
		uninstallFromDocument(fDocument, fPosition);
		fDocument= null;
		fPosition= null;
		fSavedPosition= null;
		super.releaseState();
	}

	/**
	 * Merges the given location into this one.
	 *
	 * @param location the location to merge into this one
	 * @return <code>true<code> if merging was successful
	 */
	@Override
	public boolean mergeInto(INavigationLocation location) {

		if (location == null)
			return false;

		if (getClass() != location.getClass())
			return false;

		if (fPosition == null || fPosition.isDeleted)
			return true;

		TextSelectionNavigationLocation s= (TextSelectionNavigationLocation) location;
		if (s.fPosition == null || s.fPosition.isDeleted) {
			uninstallFromDocument(fDocument, fPosition);
			s.fDocument= fDocument;
			s. fPosition= fPosition;
			s.fSavedPosition= fSavedPosition;
			return true;
		}

		if (s.fDocument == fDocument)  {
			if (s.fPosition.overlapsWith(fPosition.offset, fPosition.length) || fPosition.offset + fPosition.length == s.fPosition.offset || s.fPosition.offset + s.fPosition.length == fPosition.offset)  {
				s.fPosition.offset= fPosition.offset;
				s.fPosition.length= fPosition.length;
				return true;
			}
		}

		return false;
	}

	/**
	 * Restores this location.
	 */
	@Override
	public void restoreLocation() {
		if (fPosition == null || fPosition.isDeleted)
			return;

		IEditorPart part= getEditorPart();
		if (part instanceof ITextEditor) {
			ITextEditor editor= (ITextEditor) getEditorPart();
			editor.selectAndReveal(fPosition.offset, fPosition.length);
		}
	}

	/**
	 * Restores the object state from the given memento.
	 *
	 * @param memento the memento
	 */
	@Override
	public void restoreState(IMemento memento) {

		IEditorPart part= getEditorPart();
		if (part instanceof ITextEditor) {

			// restore
			fDocument= getDocument((ITextEditor) part);

			Integer offset= memento.getInteger(TAG_X);
			Integer length= memento.getInteger(TAG_Y);
			String deleted= memento.getString(TAG_INFO);

			if (offset != null && length != null) {
				Position p= new Position(offset.intValue(), length.intValue());
				if (deleted != null)
					p.isDeleted= INFO_DELETED.equals(deleted) ? true : false;

				// activate
				if (installOnDocument(fDocument, p)) {
					fPosition= p;
					if (!part.isDirty())
						fSavedPosition= new Position(fPosition.offset, fPosition.length);
				}
			}
		}
	}

	/**
	 * Stores the object state into the given memento.
	 *
	 * @param memento the memento
	 */
	@Override
	public void saveState(IMemento memento) {
		if (fSavedPosition != null) {
			memento.putInteger(TAG_X, fSavedPosition.offset);
			memento.putInteger(TAG_Y, fSavedPosition.length);
			memento.putString(TAG_INFO, (fSavedPosition.isDeleted ? INFO_DELETED : INFO_NOT_DELETED));
		}
	}

	/**
	 * Hook method which is called when the given editor has been saved.
	 *
	 * @param part the editor part
	 */
	public void partSaved(IEditorPart part) {
		// http://dev.eclipse.org/bugs/show_bug.cgi?id=25440
		if (fPosition == null || fPosition.isDeleted())
			fSavedPosition= null;
		else
			fSavedPosition= new Position(fPosition.offset, fPosition.length);
	}

	/**
	 * Updates the this location.
	 */
	@Override
	public void update() {
		IEditorPart part= getEditorPart();
		if (part instanceof ITextEditor) {
			ITextEditor textEditor= (ITextEditor) getEditorPart();

			if(equalsLocationOf(textEditor))
				return;

			ISelection s= textEditor.getSelectionProvider().getSelection();
			if(s == null || s.isEmpty())
				return;

			ITextSelection selection= (ITextSelection) s;
			if(selection.getOffset() == 0 && selection.getLength() == 0)
				return;

			fPosition.offset= selection.getOffset();
			fPosition.length= selection.getLength();
			fPosition.isDeleted= false;

			if (!part.isDirty())
				fSavedPosition= new Position(fPosition.offset, fPosition.length);
		}
	}
}

Back to the top