Skip to main content
summaryrefslogtreecommitdiffstats
blob: 37a3bec9ece311ca4a2376af135d79fd1a4e1544 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.dnd.internal;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.dnd.TemplateTransfer;
import org.eclipse.jst.pagedesigner.commands.PaletteDropInsertCommand;
import org.eclipse.jst.pagedesigner.editors.pagedesigner.PageDesignerResources;
import org.eclipse.jst.pagedesigner.editors.palette.TagToolPaletteEntry;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.texteditor.ITextEditorDropTargetListener;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.sse.ui.internal.ExtendedEditorDropTargetAdapter;

/**
 * @author mengbo
 */
public class DesignerSourceDropTargetListener extends
		ExtendedEditorDropTargetAdapter implements
		ITextEditorDropTargetListener {
	private int _location;

	private StructuredTextEditor _textEditor;

	public DesignerSourceDropTargetListener(StructuredTextEditor textEditor) {
		super(false);
		_textEditor = textEditor;
		setTextViewer(_textEditor.getTextViewer());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.ITextEditorDropTargetListener#getTransfers()
	 */
	public Transfer[] getTransfers() {
		return new Transfer[] { TemplateTransfer.getInstance(),
				TextTransfer.getInstance() };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent)
	 */
	public void dragOperationChanged(DropTargetEvent event) {
		super.dragOperationChanged(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.dnd.DropTargetListener#dragOver(org.eclipse.swt.dnd.DropTargetEvent)
	 */
	public void dragOver(DropTargetEvent event) {
		StyledText text = null;
		if (_textEditor.getTextViewer() != null) {
			text = _textEditor.getTextViewer().getTextWidget();
			if (TemplateTransfer.getInstance().isSupportedType(
					event.currentDataType)) {
				if (_textEditor.getTextViewer() != null) {
					Point p = new Point(event.x, event.y);
					SourceViewerDragDropHelper.getInstance().updateCaret(
							_textEditor, p);
					_location = text.getCaretOffset();
					if (TemplateTransfer.getInstance().isSupportedType(
							event.currentDataType)) {
						_location = SourceViewerDragDropHelper.getInstance()
								.getValidLocation(_textEditor, _location);
					}
					SourceViewerDragDropHelper.getInstance().showCaret(
							_textEditor, _location);
				}
			} else if (TextTransfer.getInstance().isSupportedType(
					event.currentDataType)) {
				super.dragOver(event);
				_location = text.getCaretOffset();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.dnd.DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent)
	 */
	public void drop(DropTargetEvent event) {
		StyledText text = null;
		if (_textEditor.getTextViewer() != null) {
			text = _textEditor.getTextViewer().getTextWidget();
		}
		text.setCaretOffset(_location);
		Command command = getCommand(event);
		if (command == null) {
			return;
		}
		command.execute();
	}

	private Command getCommand(DropTargetEvent event) {
		if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
			Object data = event.data;
			if (data instanceof String) {
				SourceViewLocalDropCommand command = new SourceViewLocalDropCommand(
						_textEditor, data, _location);
				return command;
			}
		} else if (TemplateTransfer.getInstance().isSupportedType(
				event.currentDataType)) {
			Object data = event.data;
			PaletteDropInsertCommand command = null;
			if (data instanceof TagToolPaletteEntry) {
				TagToolPaletteEntry tagItem = (TagToolPaletteEntry) data;
				// "Create new item"
				command = new PaletteDropInsertCommand(
						PageDesignerResources
								.getInstance()
								.getString(
										"DesignerSourceDropTargetListener.InserCommandLabel"), _textEditor, tagItem, _location); //$NON-NLS-1$
			}
			return command;
		}
		return null;
	}
}

Back to the top