Skip to main content
summaryrefslogtreecommitdiffstats
blob: 230633689780efc7975a0c233bf6cbb5208a31f5 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.internal.tasklist.ui.views;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.mylar.core.util.MylarStatusHandler;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.internal.tasklist.Task;
import org.eclipse.mylar.internal.tasklist.TaskCategory;
import org.eclipse.mylar.internal.tasklist.ui.ITaskListElement;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.TransferData;

/**
 * @author Mik Kersten
 * @author Robert Elves (added URL based task creation support)
 */
public class TaskListDropAdapter extends ViewerDropAdapter {

	private Task newTask = null;

	public TaskListDropAdapter(Viewer viewer) {
		super(viewer);
		setFeedbackEnabled(true);
	}

	@Override
	public boolean performDrop(Object data) {

		Object currentTarget = getCurrentTarget();
		List<ITask> tasksToMove = new ArrayList<ITask>();

		if (isUrl(data) && createTaskFromUrl(data)) {
			tasksToMove.add(newTask);
		} else {
			ISelection selection = ((TreeViewer) getViewer()).getSelection();
			for (Object selectedObject : ((IStructuredSelection) selection).toList()) {
				ITask toMove = null;
				if (selectedObject instanceof ITask) {
					toMove = (ITask) selectedObject;
				} else if (selectedObject instanceof IQueryHit) {
					toMove = ((IQueryHit) selectedObject).getOrCreateCorrespondingTask();
				}
				if (toMove != null) {
					tasksToMove.add(toMove);
				}
			}
		}

		for (ITask task : tasksToMove) {
			if (currentTarget instanceof TaskCategory) {
				MylarTaskListPlugin.getTaskListManager().moveToCategory((TaskCategory) currentTarget, task);
			} else if (currentTarget instanceof ITask) {
				ITask targetTask = (ITask) currentTarget;
				if (targetTask.getCategory() == null) {
					MylarTaskListPlugin.getTaskListManager().moveToRoot(task);
				} else {
					MylarTaskListPlugin.getTaskListManager().moveToCategory((TaskCategory) targetTask.getCategory(),
							task);
				}
			} else if (currentTarget == null) {
				MylarTaskListPlugin.getTaskListManager().moveToRoot(newTask);
			}
		}

		// Make new task the current selection in the view
		if (newTask != null) {
			StructuredSelection ss = new StructuredSelection(newTask);
			getViewer().setSelection(ss);
			getViewer().refresh();
		}

		return true;

	}

	/**
	 * @return true if string is a http(s) url
	 */
	public boolean isUrl(Object data) {
		String uri = "";
		if (data instanceof String) {
			uri = (String) data;
			if ((uri.startsWith("http://") || uri.startsWith("https://"))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param data
	 *            string containing url and title separated by <quote>\n</quote>
	 * @return true if task succesfully created, false otherwise
	 */
	public boolean createTaskFromUrl(Object data) {

		if (!(data instanceof String))
			return false;

		String[] urlTransfer = ((String) data).split("\n");

		String url = "";
		String urlTitle = "<retrieving from URL>";

		if (urlTransfer.length > 0) {
			url = urlTransfer[0];
		} else {
			return false;
		}

		// Removed in order to default to retrieving title from url rather than
		// accepting what was sent by the brower's DnD code. (see bug 114401)
		// If a Title is provided, use it.
		// if (urlTransfer.length > 1) {
		// urlTitle = urlTransfer[1];
		// }
		// if (urlTransfer.length < 2) { // no title provided
		// retrieveTaskDescription(url);
		// }
		retrieveTaskDescription(url);

		newTask = new Task(MylarTaskListPlugin.getTaskListManager().genUniqueTaskHandle(), urlTitle, true);

		if (newTask == null) {
			return false;
		}

		newTask.setPriority(MylarTaskListPlugin.PriorityLevel.P3.toString());
		newTask.setUrl(url);
		newTask.openTaskInEditor(true);

		return true;

	}

	@Override
	public boolean validateDrop(Object targetObject, int operation, TransferData transferType) {
		Object selectedObject = ((IStructuredSelection) ((TreeViewer) getViewer()).getSelection()).getFirstElement();
		if (selectedObject instanceof ITaskListElement && ((ITaskListElement) selectedObject).isDragAndDropEnabled()) {
			if (getCurrentTarget() instanceof TaskCategory) {
				return true;
			} else if (getCurrentTarget() instanceof ITaskListElement
					&& (getCurrentLocation() == ViewerDropAdapter.LOCATION_AFTER || getCurrentLocation() == ViewerDropAdapter.LOCATION_BEFORE)) {
				return true;
			} else {
				return false;
			}
		}

		return TextTransfer.getInstance().isSupportedType(transferType);
	}

	/**
	 * Attempts to set the task pageTitle to the title from the specified url
	 */
	protected void retrieveTaskDescription(final String url) {

		try {
			RetrieveTitleFromUrlJob job = new RetrieveTitleFromUrlJob(url) {
				@Override
				protected void setTitle(final String pageTitle) {
					newTask.setDescription(pageTitle);
					MylarTaskListPlugin.getTaskListManager().notifyTaskChanged(newTask);
				}
			};
			job.schedule();
		} catch (RuntimeException e) {
			MylarStatusHandler.fail(e, "could not open task web page", false);
		}
	}
}

Back to the top