Skip to main content
summaryrefslogtreecommitdiffstats
blob: e59a1857b96bacd184e4a2e38ec9989d4c08bb1a (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar committers 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
 *******************************************************************************/

package org.eclipse.mylyn.tasks.ui.editors;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylyn.internal.tasks.ui.wizards.NewAttachmentWizard;
import org.eclipse.mylyn.internal.tasks.ui.wizards.NewAttachmentWizardDialog;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.widgets.Control;

/**
 * Not API.
 * 
 * @author Mik Kersten
 */
class RepositoryTaskEditorDropListener implements DropTargetListener {

	private final AbstractRepositoryTaskEditor AbstractTaskEditor;

	private final FileTransfer fileTransfer;

	private final TextTransfer textTransfer;

	private final Control control;

	public RepositoryTaskEditorDropListener(AbstractRepositoryTaskEditor AbstractTaskEditor,
			FileTransfer fileTransfer, TextTransfer textTransfer, Control control) {
		this.AbstractTaskEditor = AbstractTaskEditor;
		this.fileTransfer = fileTransfer;
		this.textTransfer = textTransfer;
		this.control = control;
	}

	public void dragEnter(DropTargetEvent event) {
		if (event.detail == DND.DROP_DEFAULT) {
			if ((event.operations & DND.DROP_COPY) != 0) {
				event.detail = DND.DROP_COPY;
			} else {
				event.detail = DND.DROP_NONE;
			}
		}
		// will accept text but prefer to have files dropped
		for (int i = 0; i < event.dataTypes.length; i++) {
			if (fileTransfer.isSupportedType(event.dataTypes[i])) {
				event.currentDataType = event.dataTypes[i];
				// files should only be copied
				if (event.detail != DND.DROP_COPY) {
					event.detail = DND.DROP_NONE;
				}
				break;
			}
		}
	}

	public void dragOver(DropTargetEvent event) {
		event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
		// if (textTransfer.isSupportedType(event.currentDataType)) {
		// // NOTE: on unsupported platforms this will return null
		// Object o = textTransfer.nativeToJava(event.currentDataType);
		// String t = (String)o;
		// if (t != null) System.out.println(t);
		// }
	}

	public void dragOperationChanged(DropTargetEvent event) {
		if ((event.detail == DND.DROP_DEFAULT) || (event.operations & DND.DROP_COPY) != 0) {

			event.detail = DND.DROP_COPY;
		} else {
			event.detail = DND.DROP_NONE;
		}

		// allow text to be moved but files should only be copied
		if (fileTransfer.isSupportedType(event.currentDataType)) {
			if (event.detail != DND.DROP_COPY) {
				event.detail = DND.DROP_NONE;
			}
		}
	}

	public void dragLeave(DropTargetEvent event) {
	}

	public void dropAccept(DropTargetEvent event) {
	}

	public void drop(DropTargetEvent event) {
		if (textTransfer.isSupportedType(event.currentDataType)) {
			String text = (String) event.data;
			AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask(
					this.AbstractTaskEditor.repository.getUrl(),
					this.AbstractTaskEditor.taskData.getId());
			if (!(task instanceof AbstractTask)) {
				// Should not happen
				return;
			}

			AbstractTaskEditor.setGlobalBusy(true);
			NewAttachmentWizard naw = new NewAttachmentWizard(this.AbstractTaskEditor.repository,
					(AbstractTask) task, text);
			openDialog(naw);
		}
		if (fileTransfer.isSupportedType(event.currentDataType)) {
			String[] files = (String[]) event.data;
			if (files.length > 0) {
				AbstractTask task = TasksUiPlugin.getTaskListManager().getTaskList().getTask(
						this.AbstractTaskEditor.repository.getUrl(),
						this.AbstractTaskEditor.taskData.getId());
				if (!(task instanceof AbstractTask)) {
					// Should not happen
					return;
				}

				NewAttachmentWizard naw = new NewAttachmentWizard(this.AbstractTaskEditor.repository,
						(AbstractTask) task, new File(files[0]));
				openDialog(naw);

			}
		}
	}

	private void openDialog(NewAttachmentWizard naw) {
		AbstractTaskEditor.setGlobalBusy(true);
		NewAttachmentWizardDialog dialog = new NewAttachmentWizardDialog(control.getShell(), naw);
		naw.setDialog(dialog);
		dialog.create();
		int result = dialog.open();
		if (result != MessageDialog.OK) {
			AbstractTaskEditor.setGlobalBusy(false);
		}
	}
}

Back to the top