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: 1c39cbb4f0c0ff530b58edb8fcd18c197a623645 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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.ui.internal.selection;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.util.Assert;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
import org.w3c.dom.Node;

/**
 * @deprecated use StructuredSelectActionDelegate instead
 */

public abstract class StructureSelectAction extends Action {
	protected StructuredTextEditor fEditor = null;
	protected SelectionHistory fHistory;
	protected IStructuredModel fModel = null;
	protected StructuredTextViewer fViewer = null;

	public StructureSelectAction(StructuredTextEditor editor) {
		super();

		Assert.isNotNull(editor);
		fEditor = editor;
		fHistory = (SelectionHistory) editor.getAdapter(SelectionHistory.class);
		fViewer = editor.getTextViewer();
		fModel = editor.getModel();
		Assert.isNotNull(fViewer);
	}

	abstract protected IndexedRegion getCursorIndexedRegion();

	protected IndexedRegion getIndexedRegion(int offset) {
		IndexedRegion indexedRegion = null;

		int lastOffset = offset;
		IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
		IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
		if (model != null) {
			try {
				indexedRegion = model.getIndexedRegion(lastOffset);
				while (indexedRegion == null && lastOffset >= 0) {
					lastOffset--;
					indexedRegion = model.getIndexedRegion(lastOffset);
				}
			}
			finally {
				model.releaseFromRead();
			}
		}

		return indexedRegion;
	}

	abstract protected Region getNewSelectionRegion(Node node, Region region);

	public void run() {
		Region currentRegion = new Region(fViewer.getSelectedRange().x, fViewer.getSelectedRange().y);
		if (currentRegion.getLength() == fViewer.getDocument().getLength())
			return;

		IndexedRegion cursorIndexedRegion = getCursorIndexedRegion();
		if (cursorIndexedRegion instanceof Node) {
			Node cursorNode = (Node) cursorIndexedRegion;

			// use parent node for empty text node
			if (cursorNode.getNodeType() == Node.TEXT_NODE && cursorNode.getNodeValue().trim().length() == 0) {
				cursorNode = cursorNode.getParentNode();

				if (cursorNode instanceof IndexedRegion)
					cursorIndexedRegion = (IndexedRegion) cursorNode;
			}

			Region cursorNodeRegion = new Region(cursorIndexedRegion.getStartOffset(), cursorIndexedRegion.getEndOffset() - cursorIndexedRegion.getStartOffset());

			Region newRegion = null;
			if (cursorNodeRegion.getOffset() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() <= currentRegion.getOffset() + currentRegion.getLength() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentRegion.getOffset() + currentRegion.getLength())
				newRegion = getNewSelectionRegion(cursorNode, currentRegion);
			else
				newRegion = cursorNodeRegion;

			if (newRegion != null) {
				fHistory.remember(currentRegion);
				try {
					fHistory.ignoreSelectionChanges();
					fEditor.selectAndReveal(newRegion.getOffset(), newRegion.getLength());
				}
				finally {
					fHistory.listenToSelectionChanges();
				}
			}
		}
	}
}

Back to the top