blob: bc73d2ad1c55c138c258a69aaecd3c613275f96f [file] [log] [blame]
/***********************************************************************
* Copyright (c) 2008 by SAP AG, Walldorf.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
***********************************************************************/
package org.eclipse.jst.jee.ui.internal.navigator;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.j2ee.internal.provider.J2EEUtilityJarItemProvider;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionConstants;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonMenuConstants;
/**
* Standard operations Java EE Action Provider.
* Introduces standard operations for Deployment descriptor tree nodes. (like open)
*
* @author Dimitar Giormov
*
*/
public class JEEActionProvider extends CommonActionProvider {
private static final Class IRESOURCE_CLASS = IResource.class;
private OpenJEEResourceAction openAction;
/**
*
*/
public JEEActionProvider() {
}
/*
* (non-Javadoc)
*
* @see org.eclipse.wst.common.navigator.internal.views.actions.ICommonActionProvider#init(org.eclipse.ui.IViewPart,
* org.eclipse.jface.viewers.StructuredViewer,
* org.eclipse.wst.common.navigator.internal.views.extensions.NavigatorContentService)
*/
public void init(ICommonActionExtensionSite aConfig) {
openAction = new OpenJEEResourceAction();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.wst.common.navigator.internal.views.actions.ICommonActionProvider#setActionContext(org.eclipse.ui.actions.ActionContext)
*/
public void setContext(ActionContext aContext) {
if (aContext != null && aContext.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) aContext.getSelection();
openAction.selectionChanged(selection);
Object[] array = selection.toArray();
if (isUtilityJars(array)) {
J2EEUtilityJarItemProvider utilityJarItemProvider = null;
List newSelection = new ArrayList();
for (int i = 0; i < array.length; i++) {
utilityJarItemProvider = (J2EEUtilityJarItemProvider) array[i];
newSelection.addAll(utilityJarItemProvider.getChildren(null));
}
selection = new StructuredSelection(newSelection);
}
}
super.setContext(aContext);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.wst.common.navigator.internal.views.actions.ICommonActionProvider#fillActionBars(org.eclipse.ui.IActionBars)
*/
public void fillActionBars(IActionBars theActionBars) {
if(openAction.isEnabled())
theActionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, openAction);
return;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.wst.common.navigator.internal.views.actions.ICommonActionProvider#fillContextMenu(org.eclipse.jface.action.IMenuManager)
*/
public void fillContextMenu(IMenuManager aMenu) {
if (getContext()==null || getContext().getSelection().isEmpty())
return;
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
if(!adaptsToResource(selection.toArray())) {
openAction.selectionChanged(selection);
// createAction.selectionChanged(selection);
if (openAction.isEnabled())
aMenu.insertAfter(ICommonMenuConstants.GROUP_OPEN, openAction);
// if (createAction.isEnabled())
// aMenu.insertAfter(ICommonMenuConstants.GROUP_OPEN, createAction);
}
}
private boolean adaptsToResource(Object[] objects) {
for (int i = 0; i < objects.length; i++) {
if(objects[i] instanceof IResource) {
return true;
} else if (objects[i] instanceof IAdaptable && (((IAdaptable)objects[i]).getAdapter(IRESOURCE_CLASS) != null)) {
return true;
}
}
return false;
}
private boolean isUtilityJars(Object[] items) {
if (items != null) {
for (int i = 0; i < items.length; i++) {
if (items[i] == null || items[i].getClass() != J2EEUtilityJarItemProvider.class)
return false;
}
return true;
}
return false;
}
}