1 11 package org.eclipse.ui.views.navigator; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.MenuManager; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.ui.PlatformUI; 21 import org.eclipse.ui.actions.OpenFileAction; 22 import org.eclipse.ui.actions.OpenInNewWindowAction; 23 import org.eclipse.ui.actions.OpenWithMenu; 24 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 25 26 29 public class OpenActionGroup extends ResourceNavigatorActionGroup { 30 31 private OpenFileAction openFileAction; 32 33 36 public static final String OPEN_WITH_ID = PlatformUI.PLUGIN_ID 37 + ".OpenWithSubMenu"; 39 public OpenActionGroup(IResourceNavigator navigator) { 40 super(navigator); 41 } 42 43 protected void makeActions() { 44 openFileAction = new OpenFileAction(navigator.getSite().getPage()); 45 } 46 47 public void fillContextMenu(IMenuManager menu) { 48 IStructuredSelection selection = (IStructuredSelection) getContext() 49 .getSelection(); 50 51 boolean anyResourceSelected = !selection.isEmpty() 52 && ResourceSelectionUtil.allResourcesAreOfType(selection, 53 IResource.PROJECT | IResource.FOLDER | IResource.FILE); 54 boolean onlyFilesSelected = !selection.isEmpty() 55 && ResourceSelectionUtil.allResourcesAreOfType(selection, 56 IResource.FILE); 57 58 if (onlyFilesSelected) { 59 openFileAction.selectionChanged(selection); 60 menu.add(openFileAction); 61 fillOpenWithMenu(menu, selection); 62 } 63 64 if (anyResourceSelected) { 65 addNewWindowAction(menu, selection); 66 } 67 } 68 69 75 private void fillOpenWithMenu(IMenuManager menu, 76 IStructuredSelection selection) { 77 78 if (selection.size() != 1) { 80 return; 81 } 82 Object element = selection.getFirstElement(); 83 if (!(element instanceof IFile)) { 84 return; 85 } 86 87 MenuManager submenu = new MenuManager(ResourceNavigatorMessages.ResourceNavigator_openWith, OPEN_WITH_ID); 88 submenu.add(new OpenWithMenu(navigator.getSite().getPage(), 89 (IFile) element)); 90 menu.add(submenu); 91 } 92 93 99 private void addNewWindowAction(IMenuManager menu, 100 IStructuredSelection selection) { 101 102 if (selection.size() != 1) { 104 return; 105 } 106 Object element = selection.getFirstElement(); 107 if (!(element instanceof IContainer)) { 108 return; 109 } 110 if (element instanceof IProject && !(((IProject) element).isOpen())) { 111 return; 112 } 113 114 menu.add(new OpenInNewWindowAction(navigator.getSite() 115 .getWorkbenchWindow(), (IContainer) element)); 116 } 117 118 121 public void runDefaultAction(IStructuredSelection selection) { 122 Object element = selection.getFirstElement(); 123 if (element instanceof IFile) { 124 openFileAction.selectionChanged(selection); 125 openFileAction.run(); 126 } 127 } 128 } 129 | Popular Tags |