/*******************************************************************************
 * 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.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylar.internal.tasklist.ui.TaskListUiUtil;
import org.eclipse.mylar.internal.tasklist.ui.views.TaskListView;
import org.eclipse.mylar.provisional.core.MylarPlugin;
import org.eclipse.mylar.provisional.tasklist.AbstractQueryHit;
import org.eclipse.mylar.provisional.tasklist.AbstractRepositoryQuery;
import org.eclipse.mylar.provisional.tasklist.ITask;
import org.eclipse.mylar.provisional.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.provisional.tasklist.TaskCategory;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchImages;

/**
 * @author Mik Kersten and Ken Sueda
 */
public class DeleteAction extends Action {

	public static final String ID = "org.eclipse.mylar.tasklist.actions.delete";

	public DeleteAction() {
		setText("Delete");
		setId(ID);
		setImageDescriptor(WorkbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
	}

	@Override
	public void run() {
		ISelection selection = TaskListView.getDefault().getViewer().getSelection();
		for (Object selectedObject : ((IStructuredSelection) selection).toList()) {
			if (selectedObject instanceof ITask || selectedObject instanceof AbstractQueryHit) {
				ITask task = null;
				if (selectedObject instanceof AbstractQueryHit) {
					task = ((AbstractQueryHit) selectedObject).getCorrespondingTask();
				} else {
					task = (ITask) selectedObject;
				}
				if (task == null) {
					MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
							"Mylar Tasks", "No task data to delte.");
					return;
				}
				if (task.isActive()) {
					MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
							"Mylar Tasks", "Task must be deactivated in order to delete.");
					return;
				}

				String message = genDeleteConfirmationMessage(task);
				boolean deleteConfirmed = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
						.getShell(), "Confirm Delete", message);
				if (!deleteConfirmed) {
					return;
				}

				MylarTaskListPlugin.getTaskListManager().deleteTask(task);
				MylarPlugin.getContextManager().contextDeleted(task.getHandleIdentifier());
				TaskListUiUtil.closeEditorInActivePage(task);
			} else if (selectedObject instanceof AbstractRepositoryQuery) {
				boolean deleteConfirmed = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
						.getShell(), "Confirm delete", "Delete the selected query? Task data will not be deleted.");
				if (deleteConfirmed) {
					MylarTaskListPlugin.getTaskListManager().deleteQuery((AbstractRepositoryQuery) selectedObject);
				}
			} else if (selectedObject instanceof TaskCategory) {
				boolean deleteConfirmed = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
						.getShell(), "Confirm Delete", "Delete the selected category and all contained tasks?");
				if (!deleteConfirmed)
					return;

				TaskCategory cat = (TaskCategory) selectedObject;
				for (ITask task : cat.getChildren()) {
					MylarPlugin.getContextManager().contextDeleted(task.getHandleIdentifier());
					TaskListUiUtil.closeEditorInActivePage(task);
				}
				MylarTaskListPlugin.getTaskListManager().deleteCategory(cat);
			} else {
				MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Delete failed",
						"Nothing selected.");
				return;
			}
		}
	}

	public static String genDeleteConfirmationMessage(ITask task) {
		return "Delete the selected task and discard task context?\n\n" + task.getDescription();
	}
}

@@ -2390,23 +2390,24 @@
 a to del
+e
 te.%22);%0A
-
 %09%09%09%09%09ret
@@ -2409,32 +2409,33 @@
 %09%09return;%0A%09%09%09%09%7D%0A
+%0A
 %09%09%09%09if (task.isA
@@ -2455,179 +2455,175 @@
 %09%09%09M
-essageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+ylarTaskListPlugin.getTaskListManager().deactivateTask(task);%0A%09%09%09%09%09TaskListView.getDefault().refreshAndFocus();
 %0A%09%09%09%09%09
-%09%09%22Mylar Tasks%22, %22Task must be deactivated in order to delete.%22
+TaskListUiUtil.closeEditorInActivePage(task
 );
-%0A
 %09%09%09%09%09
-return;
 %0A%09%09%09

Bug 123980: Deleting an active task should just deactivate it. https://bugs.eclipse.org/bugs/show_bug.cgi?id=123980