1 11 package org.eclipse.ui.internal.navigator.resources.actions; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.mapping.ResourceMapping; 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.jface.action.GroupMarker; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.ui.IActionBars; 22 import org.eclipse.ui.actions.OpenFileAction; 23 import org.eclipse.ui.actions.OpenWithMenu; 24 import org.eclipse.ui.internal.navigator.AdaptabilityUtility; 25 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages; 26 import org.eclipse.ui.navigator.CommonActionProvider; 27 import org.eclipse.ui.navigator.ICommonActionConstants; 28 import org.eclipse.ui.navigator.ICommonActionExtensionSite; 29 import org.eclipse.ui.navigator.ICommonMenuConstants; 30 import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite; 31 32 38 public class OpenActionProvider extends CommonActionProvider { 39 40 private OpenFileAction openFileAction; 41 42 private ICommonViewerWorkbenchSite viewSite = null; 43 44 private boolean contribute = false; 45 46 public void init(ICommonActionExtensionSite aConfig) { 47 if (aConfig.getViewSite() instanceof ICommonViewerWorkbenchSite) { 48 viewSite = (ICommonViewerWorkbenchSite) aConfig.getViewSite(); 49 openFileAction = new OpenFileAction(viewSite.getPage()); 50 contribute = true; 51 } 52 } 53 54 public void fillContextMenu(IMenuManager aMenu) { 55 if (!contribute || getContext().getSelection().isEmpty()) { 56 return; 57 } 58 59 IStructuredSelection selection = (IStructuredSelection) getContext() 60 .getSelection(); 61 62 openFileAction.selectionChanged(selection); 63 if (openFileAction.isEnabled()) { 64 aMenu.insertAfter(ICommonMenuConstants.GROUP_OPEN, openFileAction); 65 } 66 addOpenWithMenu(aMenu); 67 } 68 69 public void fillActionBars(IActionBars theActionBars) { 70 if (!contribute) { 71 return; 72 } 73 IStructuredSelection selection = (IStructuredSelection) getContext() 74 .getSelection(); 75 if (selection.size() == 1 76 && selection.getFirstElement() instanceof IFile) { 77 openFileAction.selectionChanged(selection); 78 theActionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, 79 openFileAction); 80 } 81 82 } 83 84 private void addOpenWithMenu(IMenuManager aMenu) { 85 IStructuredSelection ss = (IStructuredSelection) getContext() 86 .getSelection(); 87 88 if (ss == null || ss.size() != 1) { 89 return; 90 } 91 92 Object o = ss.getFirstElement(); 93 94 IAdaptable openable = (IAdaptable) AdaptabilityUtility.getAdapter(o, 96 IResource.class); 97 if (openable == null) { 99 openable = (IAdaptable) AdaptabilityUtility.getAdapter(o, 100 ResourceMapping.class); 101 } else if (((IResource) openable).getType() != IResource.FILE) { 102 openable = null; 103 } 104 105 if (openable != null) { 106 IMenuManager submenu = new MenuManager( 108 WorkbenchNavigatorMessages.OpenActionProvider_OpenWithMenu_label, 109 ICommonMenuConstants.GROUP_OPEN_WITH); 110 submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_TOP)); 111 submenu.add(new OpenWithMenu(viewSite.getPage(), openable)); 112 submenu.add(new GroupMarker(ICommonMenuConstants.GROUP_ADDITIONS)); 113 114 if (submenu.getItems().length > 2 && submenu.isEnabled()) { 116 aMenu.appendToGroup(ICommonMenuConstants.GROUP_OPEN_WITH, 117 submenu); 118 } 119 } 120 } 121 122 } 123 | Popular Tags |