1 11 package org.eclipse.debug.internal.ui.actions; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.core.runtime.IAdapterManager; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.debug.internal.ui.DebugUIPlugin; 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.ui.IActionDelegate2; 27 import org.eclipse.ui.IEditorPart; 28 import org.eclipse.ui.IPartListener; 29 import org.eclipse.ui.IPartService; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 33 34 39 public abstract class RetargetAction implements IWorkbenchWindowActionDelegate, IPartListener, IActionDelegate2 { 40 41 protected IWorkbenchWindow fWindow = null; 42 private IWorkbenchPart fActivePart = null; 43 private Object fTargetAdapter = null; 44 private IAction fAction = null; 45 private static final ISelection EMPTY_SELECTION = new EmptySelection(); 46 47 static class EmptySelection implements ISelection { 48 49 52 public boolean isEmpty() { 53 return true; 54 } 55 } 56 57 63 private ISelection getTargetSelection() { 64 if (fActivePart != null) { 65 ISelectionProvider selectionProvider = fActivePart.getSite().getSelectionProvider(); 66 if (selectionProvider != null) { 67 return selectionProvider.getSelection(); 68 } 69 } 70 return EMPTY_SELECTION; 71 } 72 73 76 public void dispose() { 77 fWindow.getPartService().removePartListener(this); 78 fActivePart = null; 79 fTargetAdapter = null; 80 81 } 82 85 public void init(IWorkbenchWindow window) { 86 this.fWindow = window; 87 IPartService partService = window.getPartService(); 88 partService.addPartListener(this); 89 IWorkbenchPart part = partService.getActivePart(); 90 if (part != null) { 91 partActivated(part); 92 } 93 } 94 97 public void run(IAction action) { 98 if (fTargetAdapter != null) { 99 try { 100 if (isTargetEnabled()) { 101 performAction(fTargetAdapter, getTargetSelection(), fActivePart); 102 } else { 103 String message = getOperationUnavailableMessage(); 104 IStatus status = new Status(IStatus.INFO, DebugUIPlugin.getUniqueIdentifier(), message); 105 DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), DebugUIPlugin.removeAccelerators(action.getText()), message, status); 106 } 107 } catch (CoreException e) { 108 DebugUIPlugin.errorDialog(fWindow.getShell(), ActionMessages.RetargetAction_2, ActionMessages.RetargetAction_3, e.getStatus()); } 110 } 111 } 112 113 120 protected abstract String getOperationUnavailableMessage(); 121 122 129 protected abstract void performAction(Object target, ISelection selection, IWorkbenchPart part) throws CoreException; 130 131 134 public void selectionChanged(IAction action, ISelection selection) { 135 if (fTargetAdapter == null && selection instanceof IStructuredSelection) { 137 IStructuredSelection ss = (IStructuredSelection) selection; 138 if (!ss.isEmpty()) { 139 Object object = ss.getFirstElement(); 140 if (object instanceof IAdaptable) { 141 fTargetAdapter = getAdapter((IAdaptable) object); 142 } 143 } 144 } 145 boolean enabled = fTargetAdapter != null; 146 if (selection instanceof IStructuredSelection) { 147 enabled = isTargetEnabled(); 148 } 149 action.setEnabled(enabled); 150 } 151 152 155 public void partActivated(IWorkbenchPart part) { 156 fActivePart = part; 157 IResource resource = (IResource) part.getAdapter(IResource.class); 158 if (resource == null && part instanceof IEditorPart) { 159 resource = (IResource) ((IEditorPart)part).getEditorInput().getAdapter(IResource.class); 160 } 161 if (resource != null) { 162 fTargetAdapter = getAdapter(resource); 163 } 164 if (fTargetAdapter == null) { 165 fTargetAdapter = getAdapter(part); 166 } 167 if (fAction != null) { 168 fAction.setEnabled(fTargetAdapter != null); 169 } 170 } 171 172 protected Object getAdapter(IAdaptable adaptable) { 173 Object adapter = adaptable.getAdapter(getAdapterClass()); 174 if (adapter == null) { 175 IAdapterManager adapterManager = Platform.getAdapterManager(); 176 if (adapterManager.hasAdapter(adaptable, getAdapterClass().getName())) { 177 fTargetAdapter = adapterManager.loadAdapter(adaptable, getAdapterClass().getName()); 178 } 179 } 180 return adapter; 181 } 182 183 188 protected abstract Class getAdapterClass(); 189 190 193 public void partBroughtToTop(IWorkbenchPart part) { 194 } 195 198 public void partClosed(IWorkbenchPart part) { 199 clearPart(part); 200 } 201 202 208 protected void clearPart(IWorkbenchPart part) { 209 if (part.equals(fActivePart)) { 210 fActivePart = null; 211 fTargetAdapter = null; 212 } 213 } 214 217 public void partDeactivated(IWorkbenchPart part) { 218 clearPart(part); 219 } 220 223 public void partOpened(IWorkbenchPart part) { 224 } 225 226 231 protected boolean isTargetEnabled() { 232 if (fTargetAdapter != null) { 233 if (fActivePart != null) { 234 return canPerformAction(fTargetAdapter, getTargetSelection(), fActivePart); 235 } 236 } 237 return false; 238 } 239 240 248 protected abstract boolean canPerformAction(Object target, ISelection selection, IWorkbenchPart part); 249 250 253 public void init(IAction action) { 254 fAction = action; 255 } 256 257 260 public void runWithEvent(IAction action, Event event) { 261 run(action); 262 } 263 264 269 protected IAction getAction() { 270 return fAction; 271 } 272 273 278 protected boolean hasTargetAdapter() { 279 return fTargetAdapter != null; 280 } 281 282 } 283 | Popular Tags |