1 11 12 package org.eclipse.debug.ui.actions; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.debug.core.model.IDebugElement; 17 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 18 import org.eclipse.debug.internal.ui.DebugPluginImages; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 21 import org.eclipse.debug.internal.ui.actions.ActionMessages; 22 import org.eclipse.debug.ui.DebugUITools; 23 import org.eclipse.debug.ui.contexts.DebugContextEvent; 24 import org.eclipse.debug.ui.contexts.IDebugContextListener; 25 import org.eclipse.debug.ui.memory.IMemoryRenderingType; 26 import org.eclipse.jface.action.Action; 27 import org.eclipse.jface.action.ActionContributionItem; 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.action.IMenuCreator; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.jface.viewers.IStructuredSelection; 32 import org.eclipse.swt.events.MenuAdapter; 33 import org.eclipse.swt.events.MenuEvent; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Event; 36 import org.eclipse.swt.widgets.Menu; 37 import org.eclipse.swt.widgets.MenuItem; 38 import org.eclipse.ui.IActionDelegate2; 39 import org.eclipse.ui.IEditorActionDelegate; 40 import org.eclipse.ui.IEditorPart; 41 import org.eclipse.ui.IObjectActionDelegate; 42 import org.eclipse.ui.IViewActionDelegate; 43 import org.eclipse.ui.IViewPart; 44 import org.eclipse.ui.IWorkbenchPart; 45 import org.eclipse.ui.IWorkbenchWindow; 46 47 59 public class AddMemoryRenderingActionDelegate extends Action implements IViewActionDelegate, IEditorActionDelegate, IObjectActionDelegate, IActionDelegate2{ 60 61 private IAction fAction; 62 private IWorkbenchPart fPart; 63 private ISelection fCurrentSelection; 64 private IAddMemoryRenderingsTarget fActionDelegate; 65 private IMenuCreator fMenuCreator; 66 private IAdaptable fDebugContext; 67 private IWorkbenchWindow fWindow; 68 private DebugContextListener fDebugContextListener = new DebugContextListener(); 69 70 private class AddMemoryRenderingAction extends Action 71 { 72 private IMemoryRenderingType fRenderingType; AddMemoryRenderingAction(IMemoryRenderingType renderingType) 74 { 75 super(renderingType.getLabel()); 76 fRenderingType = renderingType; 77 } 78 79 public void runWithEvent(Event event) { 80 if (fActionDelegate != null) 81 { 82 try { 83 fActionDelegate.addMemoryRenderings(fPart, fCurrentSelection, new IMemoryRenderingType[]{fRenderingType}); 84 } catch (CoreException e) { 85 DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.AddMemoryRenderingActionDelegate_0, ActionMessages.AddMemoryRenderingActionDelegate_1, e); 86 } 87 } 88 } 89 } 90 91 private class AddMemoryRenderingMenuCreator implements IMenuCreator 92 { 93 94 public void dispose() { 95 96 } 97 98 public Menu getMenu(Control parent) { 99 return null; 100 } 101 102 public Menu getMenu(Menu parent) { 103 Menu menu = new Menu(parent); 104 menu.addMenuListener(new MenuAdapter() { 105 public void menuShown(MenuEvent e) { 106 Menu m = (Menu)e.widget; 107 MenuItem[] items = m.getItems(); 108 for (int i=0; i < items.length; i++) { 109 items[i].dispose(); 110 } 111 fillMenu(m); 112 } 113 }); 114 return menu; 115 } 116 117 private void fillMenu(Menu parent) 118 { 119 if (fActionDelegate != null) 120 { 121 IMemoryRenderingType[] types = fActionDelegate.getMemoryRenderingTypes(fPart, fCurrentSelection); 122 123 for (int i=0; i<types.length; i++) 124 { 125 AddMemoryRenderingAction action = new AddMemoryRenderingAction(types[i]); 126 ActionContributionItem item = new ActionContributionItem(action); 127 item.fill(parent, -1); 128 } 129 } 130 } 131 } 132 133 private class DebugContextListener implements IDebugContextListener 134 { 135 136 private void contextActivated(ISelection selection) { 137 setupActionDelegate(selection); 138 updateAction(fAction, fCurrentSelection); 139 140 } 141 142 public void debugContextChanged(DebugContextEvent event) { 143 contextActivated(event.getContext()); 144 } 145 146 147 } 148 149 private void setupActionDelegate(ISelection context) 150 { 151 IAdaptable debugContext = null; 152 if (context instanceof IStructuredSelection) 153 { 154 if (((IStructuredSelection)context).getFirstElement() instanceof IAdaptable) 155 debugContext = (IAdaptable)((IStructuredSelection)context).getFirstElement(); 156 } 157 158 if (debugContext == null) 159 fActionDelegate = null; 160 161 if (debugContext == fDebugContext) 162 return; 163 164 fDebugContext = debugContext; 165 166 if (fDebugContext == null) 167 return; 168 169 IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)fDebugContext.getAdapter(IMemoryBlockRetrieval.class); 170 if (retrieval == null && fDebugContext instanceof IDebugElement) 171 { 172 retrieval = ((IDebugElement)fDebugContext).getDebugTarget(); 173 } 174 175 if (retrieval == null) 176 return; 177 178 IAddMemoryRenderingsTarget target = null; 179 if (fCurrentSelection instanceof IStructuredSelection) 180 { 181 IStructuredSelection strucSel = (IStructuredSelection)fCurrentSelection; 183 Object obj = strucSel.getFirstElement(); 184 target = getAddMemoryRenderingTarget(obj); 185 } 186 if (target == null) 187 { 188 target = getAddMemoryRenderingTarget(fDebugContext); 190 } 191 if (target == null) 192 { 193 target = getAddMemoryRenderingTarget(retrieval); 195 } 196 197 fActionDelegate = target; 198 } 199 200 203 public void init(IViewPart view) { 204 bindPart(view); 205 } 206 207 210 public void run(IAction action) { 211 } 213 214 217 public void selectionChanged(IAction action, ISelection selection) { 218 bindAction(action); 219 fCurrentSelection = selection; 220 updateAction(action, selection); 221 } 222 223 private void updateAction(IAction action, ISelection selection) 224 { 225 if (fActionDelegate != null) 226 { 227 action.setEnabled(fActionDelegate.canAddMemoryRenderings(fPart, selection)); 228 bindAction(action); 229 } 230 else 231 { 232 action.setEnabled(false); 233 } 234 } 235 236 private void bindAction(IAction action) { 237 if (action == null) 238 return; 239 240 if (action != fAction) { 241 if (fMenuCreator == null) 242 fMenuCreator = new AddMemoryRenderingMenuCreator(); 243 action.setMenuCreator(fMenuCreator); 244 fAction= action; 245 } 246 } 247 248 private IAddMemoryRenderingsTarget getAddMemoryRenderingTarget(Object elmt) { 249 IAddMemoryRenderingsTarget target = null; 250 251 if (elmt instanceof IAddMemoryRenderingsTarget) 252 { 253 target = (IAddMemoryRenderingsTarget)elmt; 254 } 255 else if (elmt instanceof IAdaptable) 256 { 257 target = (IAddMemoryRenderingsTarget)((IAdaptable)elmt).getAdapter(IAddMemoryRenderingsTarget.class); 258 } 259 return target; 260 } 261 262 265 public void setActiveEditor(IAction action, IEditorPart targetEditor) { 266 bindPart(targetEditor); 267 bindAction(action); 268 updateAction(action, fCurrentSelection); 269 } 270 271 274 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 275 bindPart(targetPart); 276 bindAction(action); 277 updateAction(action, fCurrentSelection); 278 } 279 280 283 public void init(IAction action) { 284 bindAction(action); 285 if (action != null) 286 { 287 action.setText(ActionMessages.AddMemoryRenderingActionDelegate_2); 288 action.setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_MONITOR_EXPRESSION)); 289 action.setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_MONITOR_EXPRESSION)); 290 action.setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_MONITOR_EXPRESSION)); 291 } 292 } 293 294 297 public void dispose() { 298 fAction = null; 300 fPart = null; 301 fCurrentSelection = null; 302 fActionDelegate = null; 303 304 bindPart(null); 306 } 307 308 311 public void runWithEvent(IAction action, Event event) { 312 } 314 315 private void bindPart(IWorkbenchPart part) 316 { 317 IWorkbenchWindow window = null; 318 if (part != null) 319 { 320 window = part.getSite().getWorkbenchWindow(); 321 } 322 if (window != fWindow) 323 { 324 325 if (fWindow != null) 326 { 327 DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(fDebugContextListener); 328 } 329 330 if (window != null) 331 { 332 DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(fDebugContextListener); 333 } 334 fWindow = window; 335 } 336 337 if (part != fPart) 338 fPart = part; 339 340 if (fWindow != null) 341 setupActionDelegate(DebugUITools.getDebugContextManager().getContextService(fWindow).getActiveContext()); 342 } 343 344 } 345 346 | Popular Tags |