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: d1771aad778439f95304f23329d5a008ab897950 (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
/*******************************************************************************
 * 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;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wst.sse.ui.internal.provisional.extensions.ISourceEditingTextTools;

/**
 */
public abstract class AbstractDropAction implements IDropAction {

	/*
	 * Replaces targetEditor's current selection by "text"
	 */
	protected boolean insert(String text, IEditorPart targetEditor) {
		if (text == null || text.length() == 0) {
			return true;
		}

		ITextSelection textSelection = null;
		IDocument doc = null;
		ISelection selection = null;

		ISourceEditingTextTools tools = (ISourceEditingTextTools) targetEditor.getAdapter(ISourceEditingTextTools.class);
		if (tools != null) {
			doc = tools.getDocument();
			selection = tools.getSelection();
		}

		ITextEditor textEditor = null;
		if (targetEditor instanceof ITextEditor) {
			textEditor = (ITextEditor) targetEditor;
		}
		if (textEditor == null) {
			textEditor = (ITextEditor) ((IAdaptable) targetEditor).getAdapter(ITextEditor.class);
		}
		if (textEditor == null && tools != null && tools.getEditorPart() instanceof ITextEditor) {
			textEditor = (ITextEditor) tools.getEditorPart();
		}
		if (textEditor == null && tools != null && tools.getEditorPart() != null) {
			textEditor = (ITextEditor) tools.getEditorPart().getAdapter(ITextEditor.class);
		}

		if (selection == null && textEditor != null) {
			selection = textEditor.getSelectionProvider().getSelection();
		}
		if (doc == null && textEditor != null) {
			doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
		}

		if (selection instanceof ITextSelection) {
			textSelection = (ITextSelection) selection;
			try {
				doc.replace(textSelection.getOffset(), textSelection.getLength(), text);
			}
			catch (BadLocationException e) {
				return false;
			}
		}
		if (textEditor != null && textSelection != null) {
			ISelectionProvider sp = textEditor.getSelectionProvider();
			ITextSelection sel = new TextSelection(textSelection.getOffset(), text.length());
			sp.setSelection(sel);
			textEditor.selectAndReveal(sel.getOffset(), sel.getLength());
		}

		return true;
	}

	public boolean isSupportedData(Object data) {
		return true;
	}

	public abstract boolean run(DropTargetEvent event, IEditorPart targetEditor);
}

Back to the top