/*******************************************************************************
 * Copyright (c) 2004, 2006 Subclipse project 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:
 *     Subclipse project committers - initial API and implementation
 ******************************************************************************/
package org.tigris.subversion.subclipse.core.commands;

import java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.tigris.subversion.subclipse.core.ISVNCoreConstants;
import org.tigris.subversion.subclipse.core.Policy;
import org.tigris.subversion.subclipse.core.SVNException;
import org.tigris.subversion.subclipse.core.client.OperationManager;
import org.tigris.subversion.subclipse.core.client.OperationProgressNotifyListener;
import org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot;
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
import org.tigris.subversion.svnclientadapter.SVNClientException;
import org.tigris.subversion.svnclientadapter.SVNRevision;
import org.tigris.subversion.svnclientadapter.SVNUrl;

/**
 * Switch URL for selected resource
 */
public class SwitchToUrlCommand implements ISVNCommand {
	// resource to switch
    private IResource resource;  
    
    private SVNUrl svnUrl;
    
    private SVNRevision svnRevision;
    
    private SVNWorkspaceRoot root;
    
    private int depth = ISVNCoreConstants.DEPTH_UNKNOWN;
    private boolean setDepth = false;
    private boolean ignoreExternals = false;
    private boolean force = true;

    public SwitchToUrlCommand(SVNWorkspaceRoot root, IResource resource, SVNUrl svnUrl, SVNRevision svnRevision) {
        super();
        this.root = root;
        this.resource = resource;
        this.svnUrl = svnUrl;
        this.svnRevision = svnRevision;
    }

	/* (non-Javadoc)
	 * @see org.tigris.subversion.subclipse.core.commands.ISVNCommand#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void run(IProgressMonitor monitor) throws SVNException {       
		final IProgressMonitor subPm = Policy.infiniteSubMonitorFor(monitor, 100);
        try {
    		subPm.beginTask(null, Policy.INFINITE_PM_GUESS_FOR_SWITCH);
            ISVNClientAdapter svnClient = root.getRepository().getSVNClient();
            OperationManager.getInstance().beginOperation(svnClient, new OperationProgressNotifyListener(subPm, svnClient));
            File file = resource.getLocation().toFile();
            SVNRevision pegRevision = null;
//            ISVNLocalResource svnResource = SVNWorkspaceRoot.getSVNResourceFor(resource);
//            if (svnResource != null && svnResource.getRevision() != null) pegRevision = svnResource.getRevision();
//            else pegRevision = svnRevision;
            pegRevision = SVNRevision.HEAD;
            svnClient.switchToUrl(file, svnUrl, svnRevision, pegRevision, depth, setDepth, ignoreExternals, force);
            try {
                // Refresh the resource after merge
                resource.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
            } catch (CoreException e1) {
            }
        } catch (SVNClientException e) {
            throw SVNException.wrapException(e);
        } finally {
            OperationManager.getInstance().endOperation();
            subPm.done();
        }
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}
	
	public void setSetDepth(boolean setDepth) {
		this.setDepth = setDepth;
	}

	public void setIgnoreExternals(boolean ignoreExternals) {
		this.ignoreExternals = ignoreExternals;
	}

	public void setForce(boolean force) {
		this.force = force;
	}

}

@@ -624,16 +624,38 @@
 io.File;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -697,55 +697,8 @@
 ce;%0A
-import org.eclipse.core.runtime.CoreException;%0A
 impo
@@ -747,61 +747,8 @@
 or;%0A
-import org.eclipse.core.runtime.NullProgressMonitor;%0A
 impo
@@ -1065,24 +1065,103 @@
 fyListener;%0A
+import org.tigris.subversion.subclipse.core.client.OperationResourceCollector;%0A
 import org.t
@@ -1343,32 +1343,91 @@
 lientException;%0A
+import org.tigris.subversion.svnclientadapter.SVNNodeKind;%0A
 import org.tigri
@@ -1974,16 +1974,123 @@
  = true;
+%0A    %0A    private OperationResourceCollector operationResourceCollector = new OperationResourceCollector();
 %0A%0A    pu
@@ -2793,24 +2793,93 @@
 VNClient();%0A
+            svnClient.addNotifyListener(operationResourceCollector);%0A
             
@@ -3523,212 +3523,102 @@
     
-try %7B%0A                // Refresh the resource after merge%0A                resource.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());%0A            %7D catch (CoreException e1) %7B%0A            %7D
+OperationManager.getInstance().onNotify(resource.getLocation().toFile(), SVNNodeKind.UNKNOWN);
 %0A   
@@ -3720,24 +3720,121 @@
 %7D finally %7B%0A
+        %09Set%3CIResource%3E operationResources = operationResourceCollector.getOperationResources();%0A
             
@@ -3877,16 +3877,40 @@
 eration(
+true, operationResources
 );%0A     

Refresh statuses and decorators after switch