1 17 package org.eclipse.emf.edit.ui.action; 18 19 20 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 21 import org.eclipse.jface.action.ActionContributionItem; 22 import org.eclipse.jface.action.IMenuListener; 23 import org.eclipse.jface.action.IMenuManager; 24 import org.eclipse.jface.action.IStatusLineManager; 25 import org.eclipse.jface.action.IToolBarManager; 26 import org.eclipse.jface.action.Separator; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.ui.IActionBars; 32 import org.eclipse.ui.IEditorPart; 33 import org.eclipse.ui.IPropertyListener; 34 import org.eclipse.ui.ISharedImages; 35 import org.eclipse.ui.IViewPart; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.actions.ActionFactory; 38 import org.eclipse.ui.part.IPage; 39 import org.eclipse.ui.part.MultiPageEditorActionBarContributor; 40 import org.eclipse.ui.views.properties.IPropertySheetPage; 41 import org.eclipse.ui.views.properties.PropertySheet; 42 43 44 58 public class EditingDomainActionBarContributor 59 extends 60 MultiPageEditorActionBarContributor 61 implements 62 IMenuListener, 63 IPropertyListener 64 { 65 68 protected IEditorPart activeEditor; 69 70 73 protected DeleteAction deleteAction; 74 75 78 protected CutAction cutAction; 79 80 83 protected CopyAction copyAction; 84 85 88 protected PasteAction pasteAction; 89 90 93 protected UndoAction undoAction; 94 95 98 protected RedoAction redoAction; 99 100 103 protected LoadResourceAction loadResourceAction; 104 105 108 protected ValidateAction validateAction; 109 110 113 public EditingDomainActionBarContributor() 114 { 115 super(); 116 } 117 118 public void init(IActionBars actionBars) 119 { 120 super.init(actionBars); 121 ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages(); 122 123 deleteAction = new DeleteAction(); 124 deleteAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); 125 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction); 126 127 cutAction = new CutAction(); 128 cutAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); 129 actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction); 130 131 copyAction = new CopyAction(); 132 copyAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); 133 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction); 134 135 pasteAction = new PasteAction(); 136 pasteAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); 137 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction); 138 139 undoAction = new UndoAction(); 140 undoAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)); 141 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction); 142 143 redoAction = new RedoAction(); 144 redoAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); 145 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction); 146 } 147 148 public void contributeToMenu(IMenuManager menuManager) 149 { 150 super.contributeToMenu(menuManager); 151 } 152 153 public void contributeToStatusLine(IStatusLineManager statusLineManager) 154 { 155 super.contributeToStatusLine(statusLineManager); 156 } 157 158 public void contributeToToolBar(IToolBarManager toolBarManager) 159 { 160 super.contributeToToolBar(toolBarManager); 161 } 162 163 public void shareGlobalActions(IPage page, IActionBars actionBars) 164 { 165 if (!(page instanceof IPropertySheetPage)) 166 { 167 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction); 168 actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction); 169 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction); 170 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction); 171 } 172 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction); 173 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction); 174 } 175 176 179 public void setActiveView(IViewPart part) 180 { 181 IActionBars actionBars = part.getViewSite().getActionBars(); 182 if (!(part instanceof PropertySheet)) 183 { 184 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction); 185 actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction); 186 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction); 187 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction); 188 } 189 actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction); 190 actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction); 191 192 actionBars.updateActionBars(); 193 } 194 195 public IEditorPart getActiveEditor() 196 { 197 return activeEditor; 198 } 199 200 public void setActiveEditor(IEditorPart part) 201 { 202 super.setActiveEditor(part); 203 204 if (part != activeEditor) 205 { 206 if (activeEditor != null) 207 { 208 deactivate(); 209 } 210 211 if (part instanceof IEditingDomainProvider) 212 { 213 activeEditor = part; 214 activate(); 215 216 } 217 } 218 } 219 220 public void setActivePage(IEditorPart part) 221 { 222 } 223 224 public void deactivate() 225 { 226 activeEditor.removePropertyListener(this); 227 228 deleteAction.setActiveWorkbenchPart(null); 229 cutAction.setActiveWorkbenchPart(null); 230 copyAction.setActiveWorkbenchPart(null); 231 pasteAction.setActiveWorkbenchPart(null); 232 undoAction.setActiveWorkbenchPart(null); 233 redoAction.setActiveWorkbenchPart(null); 234 235 if (loadResourceAction != null) 236 { 237 loadResourceAction.setActiveWorkbenchPart(null); 238 } 239 240 ISelectionProvider selectionProvider = 241 activeEditor instanceof ISelectionProvider ? 242 (ISelectionProvider)activeEditor : 243 activeEditor.getEditorSite().getSelectionProvider(); 244 selectionProvider.removeSelectionChangedListener(deleteAction); 245 selectionProvider.removeSelectionChangedListener(cutAction); 246 selectionProvider.removeSelectionChangedListener(copyAction); 247 selectionProvider.removeSelectionChangedListener(pasteAction); 248 249 if (validateAction != null) 250 { 251 validateAction.setActiveWorkbenchPart(null); 252 selectionProvider.removeSelectionChangedListener(validateAction); 253 } 254 } 255 256 public void activate() 257 { 258 activeEditor.addPropertyListener(this); 259 260 deleteAction.setActiveWorkbenchPart(activeEditor); 261 cutAction.setActiveWorkbenchPart(activeEditor); 262 copyAction.setActiveWorkbenchPart(activeEditor); 263 pasteAction.setActiveWorkbenchPart(activeEditor); 264 undoAction.setActiveWorkbenchPart(activeEditor); 265 redoAction.setActiveWorkbenchPart(activeEditor); 266 267 if (loadResourceAction != null) 268 { 269 loadResourceAction.setActiveWorkbenchPart(activeEditor); 270 } 271 272 ISelectionProvider selectionProvider = 273 activeEditor instanceof ISelectionProvider ? 274 (ISelectionProvider)activeEditor : 275 activeEditor.getEditorSite().getSelectionProvider(); 276 selectionProvider.addSelectionChangedListener(deleteAction); 277 selectionProvider.addSelectionChangedListener(cutAction); 278 selectionProvider.addSelectionChangedListener(copyAction); 279 selectionProvider.addSelectionChangedListener(pasteAction); 280 281 if (validateAction != null) 282 { 283 validateAction.setActiveWorkbenchPart(activeEditor); 284 selectionProvider.addSelectionChangedListener(validateAction); 285 } 286 287 update(); 288 } 289 290 public void update() 291 { 292 ISelectionProvider selectionProvider = 293 activeEditor instanceof ISelectionProvider ? 294 (ISelectionProvider)activeEditor : 295 activeEditor.getEditorSite().getSelectionProvider(); 296 ISelection selection = selectionProvider.getSelection(); 297 IStructuredSelection structuredSelection = 298 selection instanceof IStructuredSelection ? (IStructuredSelection)selection : StructuredSelection.EMPTY; 299 300 deleteAction.updateSelection(structuredSelection); 301 cutAction.updateSelection(structuredSelection); 302 copyAction.updateSelection(structuredSelection); 303 pasteAction.updateSelection(structuredSelection); 304 undoAction.update(); 305 redoAction.update(); 306 307 if (loadResourceAction != null) 308 { 309 loadResourceAction.update(); 310 } 311 312 if (validateAction != null) 313 { 314 validateAction.updateSelection(structuredSelection); 315 } 316 } 317 318 321 public void menuAboutToShow(IMenuManager menuManager) 322 { 323 menuManager.add(new Separator("additions")); 326 menuManager.add(new Separator("edit")); 327 328 menuManager.add(new ActionContributionItem(undoAction)); 331 menuManager.add(new ActionContributionItem(redoAction)); 332 menuManager.add(new Separator()); 333 menuManager.add(new ActionContributionItem(cutAction)); 334 menuManager.add(new ActionContributionItem(copyAction)); 335 menuManager.add(new ActionContributionItem(pasteAction)); 336 menuManager.add(new Separator()); 337 menuManager.add(new ActionContributionItem(deleteAction)); 338 menuManager.add(new Separator()); 339 340 menuManager.add(new Separator("additions-end")); 343 344 addGlobalActions(menuManager); 345 } 346 347 350 protected void addGlobalActions(IMenuManager menuManager) 351 { 352 if (validateAction != null) 353 { 354 menuManager.insertBefore("additions-end", new ActionContributionItem(validateAction)); 355 menuManager.insertBefore("additions-end", new Separator()); 356 } 357 358 if (loadResourceAction != null) 359 { 360 menuManager.insertBefore("additions-end", new ActionContributionItem(loadResourceAction)); 361 menuManager.insertBefore("additions-end", new Separator()); 362 } 363 } 364 365 public void propertyChanged(Object source, int id) 366 { 367 update(); 368 } 369 } 370 | Popular Tags |