1 11 12 package org.eclipse.debug.internal.ui.commands.actions; 13 14 import org.eclipse.debug.ui.DebugUITools; 15 import org.eclipse.debug.ui.contexts.DebugContextEvent; 16 import org.eclipse.debug.ui.contexts.IDebugContextListener; 17 import org.eclipse.debug.ui.contexts.IDebugContextService; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.swt.widgets.Event; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchWindow; 25 import org.eclipse.ui.PlatformUI; 26 27 32 public abstract class DebugCommandAction extends Action implements IDebugContextListener { 33 34 37 private IWorkbenchWindow fWindow; 38 39 43 private IWorkbenchPart fPart; 44 45 48 private DebugCommandService fUpdateService; 49 50 53 private DebugCommandActionDelegate fDelegate; 54 55 58 public DebugCommandAction() { 59 super(); 60 String helpContextId = getHelpContextId(); 61 if (helpContextId != null) 62 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId); 63 setEnabled(false); 64 } 65 66 70 public void setDelegate(DebugCommandActionDelegate delegate) { 71 fDelegate = delegate; 72 } 73 74 79 protected boolean execute(Object [] targets) { 80 return fUpdateService.executeCommand(getCommandType(), targets, getCommandParticipant(targets)); 81 } 82 83 88 protected ICommandParticipant getCommandParticipant(Object [] targets) { 89 return null; 90 } 91 92 97 abstract protected Class getCommandType(); 98 99 102 public void debugContextChanged(DebugContextEvent event) { 103 fUpdateService.postUpdateCommand(getCommandType(), this); 104 } 105 106 109 public void setEnabled(boolean enabled) { 110 super.setEnabled(enabled); 111 if (fDelegate != null) { 112 fDelegate.setEnabled(enabled); 113 } 114 } 115 116 121 public void init(IWorkbenchPart part) { 122 fPart = part; 123 fWindow = part.getSite().getWorkbenchWindow(); 124 fUpdateService = DebugCommandService.getService(fWindow); 125 IDebugContextService service = getDebugContextService(); 126 String partId = part.getSite().getId(); 127 service.addDebugContextListener(this, partId); 128 ISelection activeContext = service.getActiveContext(partId); 129 if (activeContext != null) { 130 fUpdateService.updateCommand(getCommandType(), this); 131 } else { 132 setEnabled(getInitialEnablement()); 133 } 134 } 135 136 140 public void init(IWorkbenchWindow window) { 141 fWindow = window; 142 fUpdateService = DebugCommandService.getService(fWindow); 143 IDebugContextService contextService = getDebugContextService(); 144 contextService.addDebugContextListener(this); 145 ISelection activeContext = contextService.getActiveContext(); 146 if (activeContext != null) { 147 fUpdateService.updateCommand(getCommandType(), this); 148 } else { 149 setEnabled(getInitialEnablement()); 150 } 151 } 152 153 159 protected boolean getInitialEnablement() { 160 return false; 161 } 162 163 168 protected ISelection getContext() { 169 if (fPart != null) { 170 getDebugContextService().getActiveContext(fPart.getSite().getId()); 171 } 172 return getDebugContextService().getActiveContext(); 173 } 174 175 179 public void run() { 180 ISelection selection = getContext(); 181 if (selection instanceof IStructuredSelection && isEnabled()) { 182 IStructuredSelection ss = (IStructuredSelection) selection; 183 boolean enabled = execute(ss.toArray()); 184 setEnabled(enabled); 186 } 187 } 188 189 193 public void runWithEvent(Event event) { 194 run(); 195 } 196 197 200 public void dispose() { 201 IDebugContextService service = getDebugContextService(); 202 if (fPart != null) { 203 service.removeDebugContextListener(this, fPart.getSite().getId()); 204 } else { 205 service.removeDebugContextListener(this); 206 } 207 fWindow = null; 208 fPart = null; 209 } 210 211 215 protected IDebugContextService getDebugContextService() { 216 return DebugUITools.getDebugContextManager().getContextService(fWindow); 217 } 218 219 222 public abstract String getHelpContextId(); 223 224 228 public abstract String getId(); 229 230 234 public abstract String getText(); 235 236 240 public abstract String getToolTipText(); 241 242 246 public abstract ImageDescriptor getDisabledImageDescriptor(); 247 248 252 public abstract ImageDescriptor getHoverImageDescriptor(); 253 254 258 public abstract ImageDescriptor getImageDescriptor(); 259 260 266 protected DebugCommandActionDelegate getDelegate() { 267 return fDelegate; 268 } 269 } 270 | Popular Tags |