1 17 package org.eclipse.emf.edit.ui.action; 18 19 20 import org.eclipse.jface.action.Action; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 import org.eclipse.jface.viewers.SelectionChangedEvent; 25 import org.eclipse.ui.IActionDelegate; 26 import org.eclipse.ui.IEditorActionDelegate; 27 import org.eclipse.ui.IEditorPart; 28 import org.eclipse.ui.ISelectionListener; 29 import org.eclipse.ui.IViewActionDelegate; 30 import org.eclipse.ui.IViewPart; 31 import org.eclipse.ui.IWorkbenchPart; 32 33 34 41 public class DelegatingCommandAction extends Action implements ISelectionListener, ISelectionChangedListener 42 { 43 47 protected IActionDelegate actionDelegate; 48 49 54 protected IEditorActionDelegate editorActionDelegate; 55 56 59 protected IWorkbenchPart workbenchPart; 60 61 66 protected IEditorPart editorPart; 67 68 72 public DelegatingCommandAction(IActionDelegate actionDelegate) 73 { 74 this.actionDelegate = actionDelegate; 75 if (actionDelegate instanceof IEditorActionDelegate) 76 { 77 editorActionDelegate = (IEditorActionDelegate)actionDelegate; 78 } 79 } 80 81 85 public DelegatingCommandAction(IEditorActionDelegate editorActionDelegate) 86 { 87 this((IActionDelegate)editorActionDelegate); 88 } 89 90 public void selectionChanged(SelectionChangedEvent event) 91 { 92 handleSelection(event.getSelection()); 93 } 94 95 public void selectionChanged(IWorkbenchPart part, ISelection selection) 96 { 97 handleSelection(selection); 98 } 99 100 protected void selectionChanged(ISelection selection) 101 { 102 if (actionDelegate != null) 103 { 104 editorActionDelegate.selectionChanged(this, selection); 108 } 109 else 110 { 111 actionDelegate.selectionChanged(this, selection); 112 } 113 } 114 115 protected void handleSelection(ISelection selection) 116 { 117 selectionChanged(selection); 118 } 119 120 123 protected void registerSelectionListener(IWorkbenchPart workbenchPart) 124 { 125 ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider(); 126 if (selectionProvider != null) 127 { 128 selectionProvider.addSelectionChangedListener(this); 129 handleSelection(selectionProvider.getSelection()); 130 } 131 } 132 133 136 protected void registerSelectionListener(IEditorPart editorPart) 137 { 138 registerSelectionListener((IWorkbenchPart)editorPart); 139 } 140 141 144 protected void unregisterSelectionListener(IWorkbenchPart workbenchPart) 145 { 146 ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider(); 147 if (selectionProvider != null) 148 { 149 selectionProvider.removeSelectionChangedListener(this); 150 } 151 } 152 153 156 protected void unregisterSelectionListener(IEditorPart editorPart) 157 { 158 unregisterSelectionListener((IWorkbenchPart)editorPart); 159 } 160 161 164 public void setActiveEditor(IEditorPart editorPart) 165 { 166 setActiveWorkbenchPart(editorPart); 167 this.editorPart = editorPart; 168 } 169 170 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) 171 { 172 if (this.workbenchPart != workbenchPart) 173 { 174 if (this.workbenchPart != null) 175 { 176 unregisterSelectionListener(this.workbenchPart); 177 } 178 this.workbenchPart = workbenchPart; 179 180 if (actionDelegate == null) 181 { 182 editorActionDelegate.setActiveEditor(this, (IEditorPart)workbenchPart); 186 } 187 else if (actionDelegate instanceof IEditorActionDelegate) 188 { 189 ((IEditorActionDelegate)actionDelegate).setActiveEditor(this, (IEditorPart)workbenchPart); 190 } 191 else 192 { 193 ((IViewActionDelegate)actionDelegate).init((IViewPart)workbenchPart); 194 } 195 196 if (workbenchPart != null) 197 { 198 registerSelectionListener(workbenchPart); 199 } 200 } 201 } 202 203 public void run() 204 { 205 actionDelegate.run(this); 206 } 207 } 208 | Popular Tags |