1 11 12 package org.eclipse.debug.internal.ui.actions.context; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor; 17 import org.eclipse.debug.internal.ui.contexts.DebugContextManager; 18 import org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener; 19 import org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextManager; 20 import org.eclipse.jface.action.Action; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.PlatformUI; 29 30 public abstract class AbstractDebugContextAction extends Action implements IDebugContextListener { 31 32 private IStructuredSelection fActiveContext; 33 34 private IWorkbenchWindow fWindow; 35 36 private AbstractDebugContextActionDelegate fDelegate; 37 38 public AbstractDebugContextAction() { 39 super(); 40 String helpContextId = getHelpContextId(); 41 if (helpContextId != null) 42 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId); 43 setEnabled(false); 44 } 45 46 public void setDelegate(AbstractDebugContextActionDelegate delegate) { 47 fDelegate = delegate; 48 } 49 50 protected abstract void doAction(Object target); 51 52 public AbstractDebugContextAction(String text) { 53 super(text); 54 } 55 56 public AbstractDebugContextAction(String text, ImageDescriptor image) { 57 super(text, image); 58 } 59 60 public AbstractDebugContextAction(String text, int style) { 61 super(text, style); 62 } 63 64 68 public void contextActivated(ISelection context, IWorkbenchPart part) { 69 fActiveContext = null; 70 update(context); 71 } 72 73 77 public void contextChanged(ISelection context, IWorkbenchPart part) { 78 contextActivated(context, part); 79 } 80 81 protected void update(ISelection context) { 82 if (context instanceof IStructuredSelection) { 83 IStructuredSelection ss = (IStructuredSelection) context; 84 updateEnableStateForContext(ss); 85 fActiveContext = (IStructuredSelection) context; 86 } else { 87 setEnabled(false); 88 fActiveContext = StructuredSelection.EMPTY; 89 } 90 } 91 92 96 public void setEnabled(boolean enabled) { 97 super.setEnabled(enabled); 98 if (fDelegate != null) { 99 fDelegate.setEnabled(enabled); 100 } 101 } 102 103 107 protected void updateEnableStateForContext(IStructuredSelection selection) { 108 int size = selection.size(); 109 BooleanRequestMonitor monitor = new BooleanRequestMonitor(this, size); 110 if (size > 0) { 111 Iterator itr = selection.iterator(); 112 while (itr.hasNext()) { 113 Object element = itr.next(); 114 isEnabledFor(element, monitor); 115 } 116 } else { 117 notSupported(monitor); 118 } 119 } 120 121 protected abstract void isEnabledFor(Object element, IBooleanRequestMonitor monitor); 122 123 129 protected void notSupported(IBooleanRequestMonitor monitor) { 130 monitor.setResult(false); 131 monitor.done(); 132 } 133 134 public void init(IWorkbenchWindow window) { 135 setWindow(window); 136 IDebugContextManager manager = DebugContextManager.getDefault(); 137 manager.addDebugContextListener(this, window); 138 ISelection activeContext = manager.getActiveContext(window); 139 if (activeContext != null) { 140 contextActivated(activeContext, null); 141 } else { 142 setEnabled(getInitialEnablement()); 143 } 144 } 145 146 152 protected boolean getInitialEnablement() { 153 return false; 154 } 155 156 protected void setWindow(IWorkbenchWindow window) { 157 fWindow = window; 158 } 159 160 165 protected IStructuredSelection getContext() { 166 return fActiveContext; 167 } 168 169 173 public void run() { 174 IStructuredSelection selection = getContext(); 175 if (selection != null && isEnabled()) { 176 setEnabled(false); 179 for (Iterator iter = selection.iterator(); iter.hasNext();) { 180 Object element = iter.next(); 181 doAction(element); 182 } 183 } 184 } 185 186 190 public void runWithEvent(Event event) { 191 run(); 192 } 193 194 public void dispose() { 195 IWorkbenchWindow window = getWindow(); 196 if (getWindow() != null) { 197 DebugContextManager.getDefault().removeDebugContextListener(this, window); 198 } 199 } 200 201 protected IWorkbenchWindow getWindow() { 202 return fWindow; 203 } 204 205 210 protected String getErrorDialogMessage() { 211 return null; 212 } 213 214 219 protected String getStatusMessage() { 220 return ""; } 222 223 public abstract String getHelpContextId(); 224 225 229 public abstract String getId(); 230 231 235 public abstract String getText(); 236 237 241 public abstract String getToolTipText(); 242 243 247 public abstract ImageDescriptor getDisabledImageDescriptor(); 248 249 253 public abstract ImageDescriptor getHoverImageDescriptor(); 254 255 259 public abstract ImageDescriptor getImageDescriptor(); 260 } 261 | Popular Tags |