001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.navigator;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFile;
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.action.MenuManager;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.actions.OpenFileAction;
021: import org.eclipse.ui.actions.OpenInNewWindowAction;
022: import org.eclipse.ui.actions.OpenWithMenu;
023: import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
024:
025: /**
026: * This is the action group for the open actions.
027: */
028: public class OpenActionGroup extends ResourceNavigatorActionGroup {
029:
030: private OpenFileAction openFileAction;
031:
032: /**
033: * The id for the Open With submenu.
034: */
035: public static final String OPEN_WITH_ID = PlatformUI.PLUGIN_ID
036: + ".OpenWithSubMenu"; //$NON-NLS-1$
037:
038: public OpenActionGroup(IResourceNavigator navigator) {
039: super (navigator);
040: }
041:
042: protected void makeActions() {
043: openFileAction = new OpenFileAction(navigator.getSite()
044: .getPage());
045: }
046:
047: public void fillContextMenu(IMenuManager menu) {
048: IStructuredSelection selection = (IStructuredSelection) getContext()
049: .getSelection();
050:
051: boolean anyResourceSelected = !selection.isEmpty()
052: && ResourceSelectionUtil.allResourcesAreOfType(
053: selection, IResource.PROJECT | IResource.FOLDER
054: | IResource.FILE);
055: boolean onlyFilesSelected = !selection.isEmpty()
056: && ResourceSelectionUtil.allResourcesAreOfType(
057: selection, IResource.FILE);
058:
059: if (onlyFilesSelected) {
060: openFileAction.selectionChanged(selection);
061: menu.add(openFileAction);
062: fillOpenWithMenu(menu, selection);
063: }
064:
065: if (anyResourceSelected) {
066: addNewWindowAction(menu, selection);
067: }
068: }
069:
070: /**
071: * Adds the OpenWith submenu to the context menu.
072: *
073: * @param menu the context menu
074: * @param selection the current selection
075: */
076: private void fillOpenWithMenu(IMenuManager menu,
077: IStructuredSelection selection) {
078:
079: // Only supported if exactly one file is selected.
080: if (selection.size() != 1) {
081: return;
082: }
083: Object element = selection.getFirstElement();
084: if (!(element instanceof IFile)) {
085: return;
086: }
087:
088: MenuManager submenu = new MenuManager(
089: ResourceNavigatorMessages.ResourceNavigator_openWith,
090: OPEN_WITH_ID);
091: submenu.add(new OpenWithMenu(navigator.getSite().getPage(),
092: (IFile) element));
093: menu.add(submenu);
094: }
095:
096: /**
097: * Adds the Open in New Window action to the context menu.
098: *
099: * @param menu the context menu
100: * @param selection the current selection
101: */
102: private void addNewWindowAction(IMenuManager menu,
103: IStructuredSelection selection) {
104:
105: // Only supported if exactly one container (i.e open project or folder) is selected.
106: if (selection.size() != 1) {
107: return;
108: }
109: Object element = selection.getFirstElement();
110: if (!(element instanceof IContainer)) {
111: return;
112: }
113: if (element instanceof IProject
114: && !(((IProject) element).isOpen())) {
115: return;
116: }
117:
118: menu.add(new OpenInNewWindowAction(navigator.getSite()
119: .getWorkbenchWindow(), (IContainer) element));
120: }
121:
122: /**
123: * Runs the default action (open file).
124: */
125: public void runDefaultAction(IStructuredSelection selection) {
126: Object element = selection.getFirstElement();
127: if (element instanceof IFile) {
128: openFileAction.selectionChanged(selection);
129: openFileAction.run();
130: }
131: }
132: }
|