001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.actions;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.swt.widgets.Shell;
018: import org.eclipse.ui.PlatformUI;
019: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
020: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
021: import org.eclipse.ui.views.markers.internal.DialogTaskProperties;
022:
023: /**
024: * Standard action for adding a task to the currently selected file
025: * resource(s).
026: * <p>
027: * This class may be instantiated; it is not intended to be subclassed.
028: * </p>
029: * @since 2.1
030: */
031: public class AddTaskAction extends SelectionListenerAction {
032: /**
033: * The id of this action.
034: */
035: public static final String ID = PlatformUI.PLUGIN_ID
036: + ".AddTaskAction";//$NON-NLS-1$
037:
038: /**
039: * The shell in which to show any dialogs.
040: */
041: private Shell shell;
042:
043: /**
044: * Creates a new instance of the receiver.
045: *
046: * @param shell shell to use to show any dialogs
047: */
048: public AddTaskAction(Shell shell) {
049: super (IDEWorkbenchMessages.AddTaskLabel);
050: setId(ID);
051: this .shell = shell;
052: Assert.isNotNull(shell);
053: setToolTipText(IDEWorkbenchMessages.AddTaskToolTip);
054: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
055: IIDEHelpContextIds.ADD_TASK_ACTION);
056: }
057:
058: private IResource getElement(IStructuredSelection selection) {
059: if (selection.size() != 1) {
060: return null;
061: }
062:
063: Object element = selection.getFirstElement();
064: IResource resource = null;
065: if (element instanceof IResource) {
066: resource = (IResource) element;
067: }
068: if (element instanceof IAdaptable) {
069: resource = (IResource) ((IAdaptable) element)
070: .getAdapter(IResource.class);
071: }
072:
073: if (resource != null && resource instanceof IProject) {
074: IProject project = (IProject) resource;
075: if (project.isOpen() == false) {
076: resource = null;
077: }
078: }
079: return resource;
080: }
081:
082: /* (non-Javadoc)
083: * Method declared on IAction.
084: */
085: public void run() {
086: IResource resource = getElement(getStructuredSelection());
087: if (resource != null) {
088: DialogTaskProperties dialog = new DialogTaskProperties(
089: shell);
090: dialog.setResource(resource);
091: dialog.open();
092: }
093: }
094:
095: /**
096: * The <code>AddTaskAction</code> implementation of this
097: * <code>SelectionListenerAction</code> method enables the action only
098: * if the selection contains a single resource and the resource is
099: * not a closed project.
100: *
101: * @param selection the selection to update the enabled state for
102: */
103: protected boolean updateSelection(IStructuredSelection selection) {
104: return super.updateSelection(selection)
105: && getElement(selection) != null;
106: }
107: }
|