/*******************************************************************************
 * Copyright (c) 2005-2008 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:
 * bastian	implementation
 *******************************************************************************/
package com.vectrace.MercurialEclipse.commands.extensions.mq;

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

import org.eclipse.core.runtime.Assert;

import com.aragost.javahg.ext.mq.Patch;
import com.vectrace.MercurialEclipse.commands.AbstractClient;
import com.vectrace.MercurialEclipse.commands.AbstractShellCommand;
import com.vectrace.MercurialEclipse.commands.HgCommand;
import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.model.ChangeSet;
import com.vectrace.MercurialEclipse.model.HgRoot;

/**
 * @author bastian
 *
 */
public class HgQDeleteClient extends AbstractClient {
	public static String delete(HgRoot root, boolean keep, String patch) throws HgException {
		List<String> patches = new ArrayList<String>(1);
		patches.add(patch);
		return doDelete(root, keep, null, patches);
	}

	public static String delete(HgRoot root, boolean keep,
			ChangeSet changeset, List<Patch> patches) throws HgException {
		Assert.isNotNull(patches);

		List<String> patcheNames = new ArrayList<String>(patches.size());
		for (Patch patch : patches) {
			patcheNames.add(patch.getName());
		}
		return doDelete(root, keep, changeset, patcheNames);
	}

	private static String doDelete(HgRoot root, boolean keep,
			ChangeSet changeset, List<String> patches) throws HgException {
		Assert.isNotNull(patches);
		Assert.isNotNull(root);
		AbstractShellCommand command = new HgCommand("qdelete", //$NON-NLS-1$
				"Invoking qdelete", root, true);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(root));
		command.addOptions("--config", "extensions.hgext.mq="); //$NON-NLS-1$ //$NON-NLS-2$

		if (keep) {
			command.addOptions("--keep"); //$NON-NLS-1$
		}
		if (changeset != null) {
			command.addOptions("--rev", changeset.getNode()); //$NON-NLS-1$
		} else {
			for (String patch : patches) {
				command.addOptions(patch);
			}
		}
		return command.executeToString();
	}
}

@@ -604,16 +604,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 import j
@@ -729,150 +729,184 @@
 ahg.
-ext.mq.Patch;%0Aimport com.vectrace.MercurialEclipse.commands.AbstractClient;%0Aimport com.vectrace.MercurialEclipse.commands.AbstractShell
+commands.ExecutionException;%0Aimport com.aragost.javahg.ext.mq.Patch;%0Aimport com.aragost.javahg.ext.mq.QDeleteCommand;%0Aimport com.aragost.javahg.ext.mq.flags.QDelete
 Command
+Flags
 ;%0Aim
@@ -949,25 +949,30 @@
 ommands.
-HgCommand
+AbstractClient
 ;%0Aimport
@@ -1222,38 +1222,36 @@
 %0A%09public static 
-String
+void
  delete(HgRoot r
@@ -1379,23 +1379,16 @@
 tch);%0A%09%09
-return 
 doDelete
@@ -1435,22 +1435,20 @@
  static 
-String
+void
  delete(
@@ -1465,36 +1465,33 @@
 t, boolean keep,
-%0A%09%09%09
+ 
 ChangeSet change
@@ -1507,33 +1507,36 @@
 %3CPatch%3E patches)
- 
+%0A%09%09%09
 throws HgExcepti
@@ -1713,23 +1713,16 @@
 ;%0A%09%09%7D%0A%09%09
-return 
 doDelete
@@ -1779,22 +1779,20 @@
  static 
-String
+void
  doDelet
@@ -1819,20 +1819,17 @@
 an keep,
-%0A%09%09%09
+ 
 ChangeSe
@@ -1836,25 +1836,28 @@
 t changeset,
- 
+%0A%09%09%09
 List%3CString%3E
@@ -1893,338 +1893,108 @@
 %7B%0A%09%09
-Assert.isNotNull(patches);%0A%09%09Assert.isNotNull(root);%0A%09%09AbstractShellCommand command = new HgCommand(%22qdelete%22, //$NON-NLS-1$%0A%09%09%09%09%22Invoking qdelete%22, root, true);%0A%09%09command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(root
+QDeleteCommand command = QDeleteCommandFlags.on(root.getRepository(
 ));%0A
+%0A
 %09%09
-command.addOptions(%22--config%22, %22extensions.hgext.mq=%22); //$NON-NLS-1$ //$NON-NLS-2$
+Assert.isNotNull(patches);
 %0A%0A%09%09
@@ -2020,43 +2020,15 @@
 and.
-addOptions(%22--
 keep
-%22
+(
 );
- //$NON-NLS-1$
 %0A%09%09%7D
@@ -2070,28 +2070,12 @@
 and.
-addOptions(%22--rev%22, 
+rev(
 chan
@@ -2095,80 +2095,70 @@
 ());
- //$NON-NLS-1$%0A%09%09%7D else %7B%0A%09%09%09for (String patch : patches)
+%0A%09%09%09patches = Collections.EMPTY_LIST;%0A%09%09%7D%0A%0A%09%09try
  %7B%0A%09%09%09
-%09
 command.
 addO
@@ -2157,71 +2157,157 @@
 and.
-addOptions(patch
+execute(patches.toArray(new String%5Bpatches.size()%5D)
 );%0A
-%09
 %09%09%7D
-%0A%09%09%7D%0A%09%09return command.executeToString();
+ catch (ExecutionException ex) %7B%0A%09%09%09throw new HgException(ex.getLocalizedMessage(), ex);%0A%09%09%7D
 %0A%09%7D%0A

Use JavaHg for QDelete