KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > tasklist > RemoveTaskAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.views.tasklist;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.commands.operations.IUndoableOperation;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.swt.widgets.Table;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.ide.undo.DeleteMarkersOperation;
23 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
24 import org.eclipse.ui.internal.views.tasklist.TaskListMessages;
25
26 /**
27  * This action removes the selected task(s) from the task list.
28  * Only tasks can be removed. Problems can only disappear from
29  * the task list when they are fixed in the associated code.
30  */

31 class RemoveTaskAction extends TaskAction {
32
33     /**
34      * Creates the action.
35      *
36      * @param tasklist the task list
37      * @param id the id
38      */

39     public RemoveTaskAction(TaskList tasklist, String JavaDoc id) {
40         super(tasklist, id);
41         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
42                 ITaskListHelpContextIds.REMOVE_TASK_ACTION);
43     }
44
45     /**
46      * Removes all the tasks in the current selection from the task list.
47      */

48     public void run() {
49         TaskList taskList = getTaskList();
50         TableViewer viewer = taskList.getTableViewer();
51         IStructuredSelection selection = (IStructuredSelection) viewer
52                 .getSelection();
53         if (selection.isEmpty()) {
54             return;
55         }
56         taskList.cancelEditing();
57         // get the index of the selected item which has focus
58
Table table = viewer.getTable();
59         int focusIndex = table.getSelectionIndex();
60         List JavaDoc list = selection.toList();
61         IMarker[] markers = new IMarker[list.size()];
62         list.toArray(markers);
63         IUndoableOperation op = new DeleteMarkersOperation(markers, TaskListMessages.RemoveTask_undoText);
64         execute(op, TaskListMessages.RemoveTask_errorMessage, null,
65                 WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
66         // set the selection to be the one which took the place of the one with focus
67
int count = table.getItemCount();
68         if (focusIndex < count) {
69             table.setSelection(focusIndex);
70         } else if (count != 0) {
71             table.setSelection(count - 1);
72         }
73         // update the viewer's selection, since setting the table selection does not notify the viewer
74
viewer.setSelection(viewer.getSelection(), true);
75     }
76 }
77
Popular Tags