1 11 package org.eclipse.debug.internal.ui.actions; 12 13 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.MultiStatus; 17 import org.eclipse.debug.core.DebugException; 18 import org.eclipse.debug.internal.ui.DebugUIPlugin; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.swt.custom.BusyIndicator; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.ui.IActionDelegate2; 27 import org.eclipse.ui.IViewActionDelegate; 28 import org.eclipse.ui.IViewPart; 29 import org.eclipse.ui.IWorkbenchWindow; 30 31 38 public abstract class AbstractDebugActionDelegate implements IViewActionDelegate, IActionDelegate2 { 39 40 43 private IAction fAction; 44 48 private IViewPart fViewPart; 49 50 53 private IStructuredSelection fSelection = StructuredSelection.EMPTY; 54 55 58 private boolean fInitialized = false; 59 60 65 public AbstractDebugActionDelegate() {} 66 67 70 public void dispose(){ 71 fSelection= null; 72 } 73 74 77 public void run(IAction action){ 78 if (action.isEnabled()) { 79 IStructuredSelection selection = getSelection(); 80 action.setEnabled(false); 83 runInForeground(selection); 84 } 85 } 86 87 90 private void runInForeground(final IStructuredSelection selection) { 91 final MultiStatus status= 92 new MultiStatus(DebugUIPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, getStatusMessage(), null); 93 BusyIndicator.showWhile(Display.getCurrent(), new Runnable () { 94 public void run() { 95 Iterator selectionIter = selection.iterator(); 96 while (selectionIter.hasNext()) { 97 Object element= selectionIter.next(); 98 try { 99 if (isEnabledFor(element)) 103 doAction(element); 104 } catch (DebugException e) { 105 status.merge(e.getStatus()); 106 } 107 } 108 } 109 }); 110 reportErrors(status); 111 } 112 113 117 private void reportErrors(final MultiStatus ms) { 118 if (!ms.isOK()) { 119 IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow(); 120 if (window != null) { 121 DebugUIPlugin.errorDialog(window.getShell(), ActionMessages.AbstractDebugActionDelegate_0, getErrorDialogMessage(), ms); 122 } else { 123 DebugUIPlugin.log(ms); 124 } 125 } 126 } 127 128 131 public void selectionChanged(IAction action, ISelection s) { 132 boolean wasInitialized= initialize(action, s); 133 if (!wasInitialized) { 134 if (getView() != null) { 135 update(action, s); 136 } 137 } 138 } 139 140 146 protected void update(IAction action, ISelection s) { 147 if (s instanceof IStructuredSelection) { 148 IStructuredSelection ss = (IStructuredSelection)s; 149 action.setEnabled(getEnableStateForSelection(ss)); 150 setSelection(ss); 151 } else { 152 action.setEnabled(false); 153 setSelection(StructuredSelection.EMPTY); 154 } 155 } 156 157 160 protected abstract void doAction(Object element) throws DebugException; 161 162 168 protected String getErrorDialogMessage(){ 169 return null; 170 } 171 177 protected String getStatusMessage(){ 178 return ""; } 180 181 184 public void init(IViewPart view) { 185 fViewPart = view; 186 } 187 188 194 protected IViewPart getView() { 195 return fViewPart; 196 } 197 198 207 protected boolean initialize(IAction action, ISelection selection) { 208 if (!isInitialized()) { 209 setAction(action); 210 update(action, selection); 211 setInitialized(true); 212 return true; 213 } 214 return false; 215 } 216 217 222 protected IStructuredSelection getSelection() { 223 return fSelection; 224 } 225 226 231 private void setSelection(IStructuredSelection selection) { 232 fSelection = selection; 233 } 234 235 239 protected void setAction(IAction action) { 240 fAction = action; 241 } 242 243 247 protected IAction getAction() { 248 return fAction; 249 } 250 251 255 protected boolean isInitialized() { 256 return fInitialized; 257 } 258 259 263 protected void setInitialized(boolean initialized) { 264 fInitialized = initialized; 265 } 266 267 270 protected boolean getEnableStateForSelection(IStructuredSelection selection) { 271 if (selection.size() == 0) { 272 return false; 273 } 274 Iterator itr= selection.iterator(); 275 while (itr.hasNext()) { 276 Object element= itr.next(); 277 if (!isEnabledFor(element)) { 278 return false; 279 } 280 } 281 return true; 282 } 283 284 289 protected boolean isEnabledFor(Object element) { 290 return true; 291 } 292 293 296 public void runWithEvent(IAction action, Event event) { 297 run(action); 298 } 299 300 303 public void init(IAction action) { 304 fAction = action; 305 } 306 307 } 308 | Popular Tags |