1 11 package org.eclipse.ui.internal.ide.actions; 12 13 import org.eclipse.core.resources.IProject; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.INullSelectionListener; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchWindow; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.actions.ActionFactory; 27 import org.eclipse.ui.actions.PartEventAction; 28 import org.eclipse.ui.dialogs.PropertyDialogAction; 29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 30 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 31 32 35 public class ProjectPropertyDialogAction extends PartEventAction implements 36 INullSelectionListener, ActionFactory.IWorkbenchAction { 37 38 42 private IWorkbenchWindow workbenchWindow; 43 44 49 public ProjectPropertyDialogAction(IWorkbenchWindow window) { 50 super(new String ()); 51 if (window == null) { 52 throw new IllegalArgumentException (); 53 } 54 this.workbenchWindow = window; 55 setText(IDEWorkbenchMessages.Workbench_projectProperties); 56 setToolTipText(IDEWorkbenchMessages.Workbench_projectPropertiesToolTip); 57 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 58 IIDEHelpContextIds.PROJECT_PROPERTY_DIALOG_ACTION); 59 workbenchWindow.getSelectionService().addSelectionListener(this); 60 workbenchWindow.getPartService().addPartListener(this); 61 setActionDefinitionId("org.eclipse.ui.project.properties"); } 63 64 67 public void run() { 68 IProject project = getProject(); 69 if (project == null) { 70 return; 71 } 72 73 SelProvider selProvider = new SelProvider(); 74 selProvider.projectSelection = new StructuredSelection(project); 75 PropertyDialogAction propAction = new PropertyDialogAction( 76 workbenchWindow.getShell(), selProvider); 77 propAction.run(); 78 } 79 80 83 public void selectionChanged(IWorkbenchPart part, ISelection sel) { 84 setEnabled(getProject() != null); 85 } 86 87 90 public void partActivated(IWorkbenchPart part) { 91 super.partActivated(part); 92 setEnabled(getProject() != null); 93 } 94 95 98 private IProject getProject() { 99 IWorkbenchPart part = getActivePart(); 100 Object selection = null; 101 if (part instanceof IEditorPart) { 102 selection = ((IEditorPart) part).getEditorInput(); 103 } else { 104 ISelection sel = workbenchWindow.getSelectionService() 105 .getSelection(); 106 if ((sel != null) && (sel instanceof IStructuredSelection)) { 107 selection = ((IStructuredSelection) sel).getFirstElement(); 108 } 109 } 110 if (selection == null) { 111 return null; 112 } 113 if (!(selection instanceof IAdaptable)) { 114 return null; 115 } 116 IResource resource = (IResource) ((IAdaptable) selection) 117 .getAdapter(IResource.class); 118 if (resource == null) { 119 return null; 120 } 121 return resource.getProject(); 122 } 123 124 127 public void dispose() { 128 if (workbenchWindow == null) { 129 return; 131 } 132 workbenchWindow.getSelectionService().removeSelectionListener(this); 133 workbenchWindow.getPartService().removePartListener(this); 134 workbenchWindow = null; 135 } 136 137 140 private static final class SelProvider implements ISelectionProvider { 141 protected IStructuredSelection projectSelection = StructuredSelection.EMPTY; 142 143 public void addSelectionChangedListener( 144 ISelectionChangedListener listener) { 145 } 147 148 public ISelection getSelection() { 149 return projectSelection; 150 } 151 152 public void removeSelectionChangedListener( 153 ISelectionChangedListener listener) { 154 } 156 157 public void setSelection(ISelection selection) { 158 } 160 } 161 } 162 | Popular Tags |