/*******************************************************************************
 * Copyright (c) 2005, 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.ui.actions;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import org.tigris.subversion.subclipse.core.resources.SVNWorkspaceRoot;
import org.tigris.subversion.subclipse.ui.Policy;
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;

public class CopyAction extends WorkspaceAction {

	protected void execute(IAction action) throws InvocationTargetException, InterruptedException {
		final IResource[] resources = getSelectedResources();
		final IProject project = resources[0].getProject();
		ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), project, false, Policy.bind("CopyAction.selectionLabel")); //$NON-NLS-1$
		if (dialog.open() == ContainerSelectionDialog.CANCEL) return;
		Object[] result = dialog.getResult();
		if (result == null || result.length == 0) return;
		final Path path = (Path)result[0];
		IProject selectedProject;
		if (path.segmentCount() == 1)
			selectedProject = ResourcesPlugin.getWorkspace().getRoot().getProject(path.toString());
		else {
			IFile targetFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
			selectedProject = targetFile.getProject();
		}
		final IProject targetProject = selectedProject;
		final File destPath = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation() + File.separator + path.toString());
		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
			public void run() {
				try {
					ISVNClientAdapter client = null;
					for (int i = 0; i < resources.length; i++) {
						final IResource resource = resources[i];
						if (client == null)
						    client = SVNWorkspaceRoot.getSVNResourceFor(resources[i]).getRepository().getSVNClient();
						File checkFile = new File(destPath.getPath() + File.separator + resource.getName());
						File srcPath = new File(resource.getLocation().toString());
						File newDestPath = new File(destPath.getPath() + File.separator + resource.getName());
						if (checkFile.exists()) {
							IInputValidator inputValidator = new IInputValidator() {
								public String isValid(String newText) {
									if (newText.equals(resource.getName())) 
										return Policy.bind("CopyAction.nameConflictSame"); //$NON-NLS-1$
									return null;
								}								
							};
							InputDialog inputDialog = new InputDialog(getShell(), Policy.bind("CopyAction.nameConflictTitle"), Policy.bind("CopyAction.nameConflictMessage", resource.getName()), "Copy of " + resource.getName(), inputValidator); //$NON-NLS-1$
							if (inputDialog.open() == InputDialog.CANCEL) return;
							String newName = inputDialog.getValue();
							if (newName == null  || newName.trim().length() == 0) return;
							newDestPath = new File(destPath.getPath() + File.separator + newName);
						}
						if (project.getFullPath().isPrefixOf(path))
							client.copy(srcPath, newDestPath);
						else
							client.doExport(srcPath, newDestPath, true);
					}
					targetProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
				} catch (Exception e) {
					MessageDialog.openError(getShell(), Policy.bind("CopyAction.copy"), e.getMessage()); //$NON-NLS-1$
				}
			}			
		});
	}

	protected boolean isEnabled() throws TeamException {
		// Only enabled if all selections are from same project.
		boolean enabled = super.isEnabled();
		if (!enabled) return false;
		IResource[] resources = getSelectedResources();
		IProject project = null;
		for (int i = 0; i < resources.length; i++) {
			if (resources[i] instanceof IProject) return false;
			if (project != null && !resources[i].getProject().equals(project)) return false;
			project = resources[i].getProject();
		}
		return true;
	}

}

@@ -2134,16 +2134,38 @@
 roject;%0A
+%09%09File target = null;%0A
 %09%09if (pa
@@ -2187,16 +2187,18 @@
 () == 1)
+ %7B
 %0A%09%09%09sele
@@ -2283,16 +2283,70 @@
 g());%0A%09%09
+%09target = selectedProject.getLocation().toFile();%0A%09%09%7D 
 else %7B%0A%09
@@ -2464,24 +2464,71 @@
 tProject();%0A
+%09%09%09target = targetFile.getLocation().toFile();%0A
 %09%09%7D%0A%09%09final 
@@ -2597,107 +2597,14 @@
 h = 
-new File(ResourcesPlugin.getWorkspace().getRoot().getLocation() + File.separator + path.toString())
+target
 ;%0A%09%09

Fix Team -> Copy option when the project does not reside in the same folder as the workspace. Issue #: 463,464