/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.refactoring;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
public class ContainerDeleteParticipant extends DeleteParticipant {
private RefactoringStatus _refactoringStatus = new RefactoringStatus();
private List<RefactoringInfo> _refactoringInformations = new ArrayList<RefactoringInfo>();
private List<Change> _changes = new ArrayList<Change>();
@Override
public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) throws OperationCanceledException {
ResourceChangeChecker checker = (ResourceChangeChecker) context.getChecker(ResourceChangeChecker.class);
IResourceChangeDescriptionFactory deltaFactory= checker.getDeltaFactory();
Iterator<RefactoringInfo> infos = _refactoringInformations.iterator();
while (infos.hasNext()) {
RefactoringInfo info = infos.next();
try {
Change change = RefactoringManager.createChange(info);
if(change != null){
boolean ok =true;
Object[] objs = change.getAffectedObjects();
if(objs !=null){
for(Object obj : objs){
if(obj instanceof IResource){
IResource currentResource = (IResource)obj;
if(deltaFactory.getDelta().findMember(currentResource.getFullPath())!=null){
ok=false;
break;
}
}
}
}
if(ok){
_changes .add(change);
}
}
} catch (Exception e) {
_refactoringStatus.addEntry(new RefactoringStatusEntry(RefactoringStatus.WARNING, "Unable to create change for '" + info.getDescription() + "'. See log for details."));
WGADesignerPlugin.getDefault().logError("Unable to create change for '" + info.getDescription() + "'.", e);
}
}
return _refactoringStatus;
}
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
if(!_changes.isEmpty()){
CompositeChange change = new CompositeChange("WGA resource changes");
change.addAll(_changes.toArray(new Change[0]));
return change;
}
return null;
}
@Override
public String getName() {
return "Container delete participant";
}
@Override
protected boolean initialize(Object element) {
if (element instanceof IContainer) {
IContainer container = (IContainer) element;
try {
_refactoringInformations = RefactoringManager.getAffectedResources(container, getArguments());
} catch (Exception e) {
_refactoringStatus.addEntry(new RefactoringStatusEntry(RefactoringStatus.ERROR,"Unable to gather refactoring information. See error log for details. Essential refactoring operations will be skipped. Workspace structure might get invalid."));
WGADesignerPlugin.getDefault().logError("Unable to gather refactoring information.", e);
}
}
return !_refactoringInformations.isEmpty();
}
}