blob: 2883bb9f1d85c1736e05f7bebe8a8361a5f08529 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.team.internal.ui.sync.actions;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.internal.ui.actions.TeamAction;
import org.eclipse.team.internal.ui.sync.views.SynchronizeView;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.actions.OpenWithMenu;
import org.eclipse.ui.views.navigator.ResourceNavigatorMessages;
/**
* This is the action group for the open actions.
*/
public class OpenWithActionGroup extends SyncViewerActionGroup {
private OpenFileAction openFileAction;
private OpenInCompareAction openInCompareAction;
public OpenWithActionGroup(SynchronizeView viewer) {
super(viewer);
makeActions();
}
protected void makeActions() {
openFileAction = new OpenFileAction(getSyncView().getSite().getPage());
openInCompareAction = new OpenInCompareAction(getSyncView());
}
public void fillContextMenu(IMenuManager menu) {
ActionContext context = getContext();
IStructuredSelection selection = null;
if (context != null) {
selection = (IStructuredSelection) context.getSelection();
}
fillOpenWithMenu(menu, selection);
}
/**
* Adds the OpenWith submenu to the context menu.
*
* @param menu the context menu
* @param selection the current selection
*/
private void fillOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
// Only supported if exactly one file is selected.
if (selection == null || selection.size() != 1)
return;
Object element = selection.getFirstElement();
IResource resource = getResource(element);
if (!(resource instanceof IFile)) {
return;
}
if(!((resource.exists()))) {
return;
}
menu.add(openInCompareAction);
openFileAction.selectionChanged(selection);
menu.add(openFileAction);
MenuManager submenu =
new MenuManager(ResourceNavigatorMessages.getString("ResourceNavigator.openWith")); //$NON-NLS-1$
submenu.add(new OpenWithMenu(getSyncView().getSite().getPage(), (IFile) resource));
menu.add(submenu);
}
/**
* Runs the default action (open file).
*/
public void runDefaultAction(IStructuredSelection selection) {
Object element = selection.getFirstElement();
if (element instanceof IFile) {
openFileAction.selectionChanged(selection);
openFileAction.run();
}
}
private IResource getResource(Object obj) {
return (IResource)TeamAction.getAdapter(obj, IResource.class);
}
public void openInCompareEditor() {
openInCompareAction.run();
}
}