1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.runtime.IAdaptable; 16 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 24 import org.eclipse.ui.IActionBars; 25 import org.eclipse.ui.IViewPart; 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.actions.ActionGroup; 28 import org.eclipse.ui.actions.OpenWithMenu; 29 30 import org.eclipse.jdt.ui.IContextMenuConstants; 31 32 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 34 35 45 public class OpenEditorActionGroup extends ActionGroup { 46 47 private IWorkbenchSite fSite; 48 private boolean fIsEditorOwner; 49 private OpenAction fOpen; 50 51 58 public OpenEditorActionGroup(IViewPart part) { 59 fSite= part.getSite(); 60 fOpen= new OpenAction(fSite); 61 fOpen.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR); 62 initialize(fSite.getSelectionProvider()); 63 } 64 65 69 public OpenEditorActionGroup(JavaEditor editor) { 70 fIsEditorOwner= true; 71 fOpen= new OpenAction(editor); 72 fOpen.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR); 73 editor.setAction("OpenEditor", fOpen); fSite= editor.getEditorSite(); 75 initialize(fSite.getSelectionProvider()); 76 } 77 78 84 public IAction getOpenAction() { 85 return fOpen; 86 } 87 88 private void initialize(ISelectionProvider provider) { 89 ISelection selection= provider.getSelection(); 90 fOpen.update(selection); 91 if (!fIsEditorOwner) { 92 provider.addSelectionChangedListener(fOpen); 93 } 94 } 95 96 99 public void fillActionBars(IActionBars actionBar) { 100 super.fillActionBars(actionBar); 101 setGlobalActionHandlers(actionBar); 102 } 103 104 107 public void fillContextMenu(IMenuManager menu) { 108 super.fillContextMenu(menu); 109 appendToGroup(menu, fOpen); 110 if (!fIsEditorOwner) { 111 addOpenWithMenu(menu); 112 } 113 } 114 115 118 public void dispose() { 119 ISelectionProvider provider= fSite.getSelectionProvider(); 120 provider.removeSelectionChangedListener(fOpen); 121 super.dispose(); 122 } 123 124 private void setGlobalActionHandlers(IActionBars actionBars) { 125 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN, fOpen); 126 } 127 128 private void appendToGroup(IMenuManager menu, IAction action) { 129 if (action.isEnabled()) 130 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action); 131 } 132 133 private void addOpenWithMenu(IMenuManager menu) { 134 ISelection selection= getContext().getSelection(); 135 if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) 136 return; 137 IStructuredSelection ss= (IStructuredSelection)selection; 138 if (ss.size() != 1) 139 return; 140 141 Object o= ss.getFirstElement(); 142 if (!(o instanceof IAdaptable)) 143 return; 144 145 IAdaptable element= (IAdaptable)o; 146 Object resource= element.getAdapter(IResource.class); 147 if (!(resource instanceof IFile)) 148 return; 149 150 IMenuManager submenu= new MenuManager(ActionMessages.OpenWithMenu_label); 152 submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource)); 153 154 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu); 156 } 157 } 158 | Popular Tags |