/*******************************************************************************
 * Copyright (c) 2005-2009 VecTrace (Zingo Andersen) and others.
 * 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:
 * adam.berkes	implementation
 *******************************************************************************/
package com.vectrace.MercurialEclipse.synchronize.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.SynchronizeModelAction;
import org.eclipse.team.ui.synchronize.SynchronizeModelOperation;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.model.ChangeSet;
import com.vectrace.MercurialEclipse.model.WorkingChangeSet;
import com.vectrace.MercurialEclipse.utils.ResourceUtils;

/**
 * @author adam.berkes <adam.berkes@intland.com>
 */
public class DeleteAction extends SynchronizeModelAction {

	public static final String HG_DELETE_GROUP = "hg.delete";
	private final ISynchronizePageConfiguration configuration;

	public DeleteAction(String text,
			ISynchronizePageConfiguration configuration,
			ISelectionProvider selectionProvider) {
		super(text, configuration, selectionProvider);
		setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
				ISharedImages.IMG_ETOOL_DELETE));
		this.configuration = configuration;
	}

	@Override
	protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration config,
			IDiffElement[] elements) {
		List<IResource> selectedResources = new ArrayList<IResource>();
		IStructuredSelection sel = getStructuredSelection();
		Object[] objects = sel.toArray();
		for (Object object : objects) {
			if (object instanceof WorkingChangeSet){
				selectedResources.addAll(((WorkingChangeSet) object).getFiles());
			} else if(!(object instanceof ChangeSet)){
				IResource resource = ResourceUtils.getResource(object);
				if(resource != null) {
					selectedResources.add(resource);
				}
			}
		}
		IResource[] resources = new IResource[selectedResources.size()];
		selectedResources.toArray(resources);
		return new DeleteOperation(configuration, elements, resources);
	}

	@Override
	protected final boolean updateSelection(IStructuredSelection selection) {
		boolean updateSelection = super.updateSelection(selection);
		if(!updateSelection){
			Object[] array = selection.toArray();
			for (Object object : array) {
				if(!isSupported(object)){
					return false;
				}
			}
			return true;
		}
		return updateSelection;
	}

	private boolean isSupported(Object object) {
		IResource adapter = MercurialEclipsePlugin.getAdapter(object, IResource.class);
		if (adapter != null) {
			return true;
		}
		return false;
	}
}

@@ -408,16 +408,18 @@
 ors:%0A * 
+%09%09
 adam.ber
@@ -422,16 +422,18 @@
 .berkes%09
+- 
 implemen
@@ -439,16 +439,49 @@
 ntation%0A
+ * %09%09Andrei Loskutov - bug fixes%0A
  *******
@@ -614,16 +614,68 @@
 tions;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -825,16 +825,66 @@
 source;%0A
+import org.eclipse.core.runtime.IProgressMonitor;%0A
 import o
@@ -2325,16 +2325,403 @@
 tion();%0A
+%09%09if(sel.size() == 1 && sel.getFirstElement() instanceof WorkingChangeSet) %7B%0A%09%09%09final WorkingChangeSet changeSet = (WorkingChangeSet) sel.getFirstElement();%0A%09%09%09return new SynchronizeModelOperation(config, elements) %7B%0A%09%09%09%09public void run(IProgressMonitor monitor) throws InvocationTargetException,%0A%09%09%09%09%09%09InterruptedException %7B%0A%09%09%09%09%09changeSet.getGroup().delete(changeSet);%0A%09%09%09%09%7D%0A%09%09%09%7D;%0A%09%09%7D%0A
 %09%09Object
@@ -3407,16 +3407,16 @@
 ction);%0A
-
 %09%09if(!up
@@ -3431,16 +3431,266 @@
 ction)%7B%0A
+%09%09%09if(selection.size() == 1 && selection.getFirstElement() instanceof WorkingChangeSet) %7B%0A%09%09%09%09WorkingChangeSet changeSet = (WorkingChangeSet) selection.getFirstElement();%0A%09%09%09%09if(changeSet.isDefault()) %7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%09return true;%0A%09%09%09%7D%0A
 %09%09%09Objec

Allow delete of (non default) working changesets