/***************************************************************************** * Copyright (c) 2010 CEA LIST. * * * 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: * * Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation * *****************************************************************************/ package org.eclipse.papyrus.views.modelexplorer.handler; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eclipse.core.commands.IHandler; import org.eclipse.emf.common.command.Command; import org.eclipse.emf.common.command.UnexecutableCommand; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.URIConverter; import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; import org.eclipse.emf.edit.domain.EditingDomain; import org.eclipse.gmf.runtime.common.core.command.CompositeCommand; import org.eclipse.gmf.runtime.common.core.command.ICommand; import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils; import org.eclipse.papyrus.infra.services.edit.service.IElementEditService; /** * Default handler for Delete command used in the ModelExplorer contextual menu. * */ public class DeleteCommandHandler extends AbstractCommandHandler implements IHandler { /** Current deleteCommand for selection (updated in {@link DeleteCommandHandler#isEnabled()}) */ private Command deleteCommand; /** * <pre> * * Build the delete command for a set of EObject selected in the ModelExplorer. * The delete command is given by the {@link IElementEditService} of selected * elements. * * @return the composite deletion command for current selection * * @TODO : Manage possible Diagrams listed in the selection * * </pre> */ private Command buildCommand() { ICommand gmfCommand = null; // Parameters store the Request parameters of the previous request // if multiple elements are selected for deletion. Map parameters = new HashMap(); for(EObject selectedEObject : getSelectedElements()) { if(selectedEObject == null) { continue; } IElementEditService provider = ElementEditServiceUtils.getCommandProvider(selectedEObject); if(provider == null) { continue; } // Retrieve delete command from the Element Edit service DestroyElementRequest request = new DestroyElementRequest(selectedEObject, false); // Add parameters (contains the list of previously dependents to be deleted // by previous commands. request.getParameters().putAll(parameters); ICommand deleteCommand = provider.getEditCommand(request); // Add current EObject destroy command to the global command gmfCommand = CompositeCommand.compose(gmfCommand, deleteCommand); // Store the new parameters for next delete command. parameters.clear(); parameters.putAll(request.getParameters()); } if(gmfCommand == null) { return UnexecutableCommand.INSTANCE; } Command emfCommand = new org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper(gmfCommand.reduce()); return emfCommand; } /** * * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#getCommand() * * @return current command (only built here when the stored command is null) */ protected Command getCommand() { // Don't cache the command, as it is no more refreshed by isEnabled(). return buildCommand(); } /** * {@inheritDoc} */ @Override public boolean isEnabled() { //we need to test if selected element is not a meta-class List<EObject> selectedElements = getSelectedElements(); for(EObject current : selectedElements) { //FIXME use the method isReadOnly provided by the class EMFHelper //TODO after the refactoring (currently, there is circular dependencies) if(isReadOnly(current)) { return false; } //the root of the model can't be deleted! if(current.eContainer() == null) { return false; } } if(selectedElements.size()==0){ return false; } // Don't compute the delete command to know if it is enabled, // it can be WAY too slow... return true; } /** * Tests if an EObject is read only * Delegates to the EObject's editing domain if it can be found * * @param eObject * @return * True if the EObject is read only */ public static boolean isReadOnly(EObject eObject) { if(eObject != null) { EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(eObject); return isReadOnly(eObject, domain); } return false; } /** * Tests if an EObject is read only * Delegates to the given editing domain if it isn't null * * @param eObject * @param domain * @return * True if the EObject is read only */ public static boolean isReadOnly(EObject eObject, EditingDomain domain) { return isReadOnly(eObject.eResource(), domain); } /** * Tests if the Resource is read only * Delegates to the given editing domain if it isn't null * * @param resource * @param domain * @return * True if the Resource is read only */ public static boolean isReadOnly(Resource resource, EditingDomain domain) { if(domain instanceof AdapterFactoryEditingDomain) { return ((AdapterFactoryEditingDomain)domain).isReadOnly(resource); } if(resource == null) { return false; } ResourceSet resourceSet = resource.getResourceSet(); if(resourceSet == null) { return false; } Map<String, ?> attributes = resourceSet.getURIConverter().getAttributes(resource.getURI(), null); Boolean readOnly = (Boolean)attributes.get(URIConverter.ATTRIBUTE_READ_ONLY); return readOnly == null ? false : readOnly; } }