1 11 12 package org.eclipse.debug.internal.ui.actions; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.jobs.Job; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.ui.IActionDelegate2; 23 import org.eclipse.ui.IViewActionDelegate; 24 import org.eclipse.ui.IViewPart; 25 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 26 27 37 public abstract class AbstractSelectionActionDelegate implements IViewActionDelegate, IActionDelegate2 { 38 39 42 private IAction fAction; 43 44 48 private IViewPart fViewPart; 49 50 53 private IStructuredSelection fSelection = StructuredSelection.EMPTY; 54 55 58 private IWorkbenchSiteProgressService fProgressService = null; 59 60 65 public AbstractSelectionActionDelegate() {} 66 67 72 public void dispose() { 73 fSelection = null; 74 } 75 76 79 public void selectionChanged(IAction action, ISelection s) { 80 if (s instanceof IStructuredSelection) { 81 IStructuredSelection ss = (IStructuredSelection) s; 82 action.setEnabled(getEnableStateForSelection(ss)); 83 setSelection(ss); 84 } else { 85 action.setEnabled(false); 86 setSelection(StructuredSelection.EMPTY); 87 } 88 } 89 90 97 protected String getErrorDialogMessage() { 98 return null; 99 } 100 101 108 protected String getStatusMessage() { 109 return ""; } 111 112 117 public void init(IViewPart view) { 118 setView(view); 119 fProgressService = (IWorkbenchSiteProgressService) view.getAdapter(IWorkbenchSiteProgressService.class); 120 } 121 122 128 protected IViewPart getView() { 129 return fViewPart; 130 } 131 132 137 protected IStructuredSelection getSelection() { 138 return fSelection; 139 } 140 141 146 private void setSelection(IStructuredSelection context) { 147 fSelection = context; 148 } 149 150 154 protected void setAction(IAction action) { 155 fAction = action; 156 } 157 158 162 protected IAction getAction() { 163 return fAction; 164 } 165 166 170 protected void setView(IViewPart viewPart) { 171 fViewPart = viewPart; 172 } 173 174 181 protected boolean getEnableStateForSelection(IStructuredSelection selection) { 182 if (selection.size() == 0) { 183 return false; 184 } 185 Iterator itr = selection.iterator(); 186 while (itr.hasNext()) { 187 Object element = itr.next(); 188 if (!isEnabledFor(element)) { 189 return false; 190 } 191 } 192 return true; 193 } 194 195 201 protected boolean isEnabledFor(Object element) { 202 return true; 203 } 204 205 208 public void runWithEvent(IAction action, Event event) { 209 run(action); 210 } 211 212 215 public void init(IAction action) { 216 setAction(action); 217 } 218 219 224 protected void schedule(Job job) { 225 if (fProgressService == null) { 226 job.schedule(); 227 } else { 228 fProgressService.schedule(job); 229 } 230 } 231 } 232 | Popular Tags |