KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > AddTaskAction


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 package org.eclipse.ui.actions;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
21 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
22 import org.eclipse.ui.views.markers.internal.DialogTaskProperties;
23
24 /**
25  * Standard action for adding a task to the currently selected file
26  * resource(s).
27  * <p>
28  * This class may be instantiated; it is not intended to be subclassed.
29  * </p>
30  * @since 2.1
31  */

32 public class AddTaskAction extends SelectionListenerAction {
33     /**
34      * The id of this action.
35      */

36     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".AddTaskAction";//$NON-NLS-1$
37

38     /**
39      * The shell in which to show any dialogs.
40      */

41     private Shell shell;
42
43     /**
44      * Creates a new instance of the receiver.
45      *
46      * @param shell shell to use to show any dialogs
47      */

48     public AddTaskAction(Shell shell) {
49         super(IDEWorkbenchMessages.AddTaskLabel);
50         setId(ID);
51         this.shell = shell;
52         Assert.isNotNull(shell);
53         setToolTipText(IDEWorkbenchMessages.AddTaskToolTip);
54         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
55                 IIDEHelpContextIds.ADD_TASK_ACTION);
56     }
57
58     private IResource getElement(IStructuredSelection selection) {
59         if (selection.size() != 1) {
60             return null;
61         }
62
63         Object JavaDoc element = selection.getFirstElement();
64         IResource resource = null;
65         if (element instanceof IResource) {
66             resource = (IResource) element;
67         }
68         if (element instanceof IAdaptable) {
69             resource = (IResource) ((IAdaptable) element)
70                     .getAdapter(IResource.class);
71         }
72
73         if (resource != null && resource instanceof IProject) {
74             IProject project = (IProject) resource;
75             if (project.isOpen() == false) {
76                 resource = null;
77             }
78         }
79         return resource;
80     }
81
82     /* (non-Javadoc)
83      * Method declared on IAction.
84      */

85     public void run() {
86         IResource resource = getElement(getStructuredSelection());
87         if (resource != null) {
88             DialogTaskProperties dialog = new DialogTaskProperties(shell);
89             dialog.setResource(resource);
90             dialog.open();
91         }
92     }
93
94     /**
95      * The <code>AddTaskAction</code> implementation of this
96      * <code>SelectionListenerAction</code> method enables the action only
97      * if the selection contains a single resource and the resource is
98      * not a closed project.
99      *
100      * @param selection the selection to update the enabled state for
101      */

102     protected boolean updateSelection(IStructuredSelection selection) {
103         return super.updateSelection(selection)
104                 && getElement(selection) != null;
105     }
106 }
107
Popular Tags