1 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 32 public class AddTaskAction extends SelectionListenerAction { 33 36 public static final String ID = PlatformUI.PLUGIN_ID + ".AddTaskAction"; 38 41 private Shell shell; 42 43 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 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 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 102 protected boolean updateSelection(IStructuredSelection selection) { 103 return super.updateSelection(selection) 104 && getElement(selection) != null; 105 } 106 } 107 | Popular Tags |