blob: a73c02f8ed53cc657ed93358fa6505123c1d0e5a [file] [log] [blame]
package org.eclipse.ant.tests.core.testplugin;
/**********************************************************************
Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
This file is made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.zip.ZipFile;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.wizards.datatransfer.*;
/**
* Helper methods to set up an IProject.
*/
public class ProjectHelper {
public static final IPath TEST_SCRIPTS_DIR= new Path("testscripts");
public static final IPath TEST_RESOURCES_DIR= new Path("testresources");
public static final IPath TEST_LIB_DIR= new Path("testlib");
/**
* Creates a IProject.
*/
public static IProject createProject(String projectName, String binFolderName) throws CoreException {
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
IProject project= root.getProject(projectName);
if (!project.exists()) {
project.create(null);
} else {
project.refreshLocal(IResource.DEPTH_INFINITE, null);
}
if (!project.isOpen()) {
project.open(null);
}
return project;
}
/**
* Removes an IProject.
*/
public static void delete(IProject project) throws CoreException {
project.delete(true, true, null);
}
/**
* Adds a folder to an IProject.
*/
public static IFolder addFolder(IProject project, String containerName) throws CoreException {
IFolder folder= project.getFolder(containerName);
if (!folder.exists()) {
folder.create(false, true, null);
}
return folder;
}
private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException {
IProjectDescription description = proj.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= natureId;
description.setNatureIds(newNatures);
proj.setDescription(description, monitor);
}
private static void importFilesFromZip(ZipFile srcZipFile, IPath destPath, IProgressMonitor monitor) throws InvocationTargetException {
ZipFileStructureProvider structureProvider= new ZipFileStructureProvider(srcZipFile);
try {
ImportOperation op= new ImportOperation(destPath, structureProvider.getRoot(), structureProvider, new ImportOverwriteQuery());
op.run(monitor);
} catch (InterruptedException e) {
// should not happen
}
}
public static void importFilesFromDirectory(File rootDir, IPath destPath, IProgressMonitor monitor) throws InvocationTargetException, IOException {
IImportStructureProvider structureProvider = FileSystemStructureProvider.INSTANCE;
List files = new ArrayList(100);
addFiles(rootDir, files);
try {
ImportOperation op= new ImportOperation(destPath, rootDir, structureProvider, new ImportOverwriteQuery(), files);
op.setCreateContainerStructure(false);
op.run(monitor);
} catch (InterruptedException e) {
// should not happen
}
}
private static void addFiles(File dir, List collection) throws IOException {
File[] files = dir.listFiles();
List subDirs = new ArrayList(2);
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
collection.add(files[i]);
} else if (files[i].isDirectory()) {
subDirs.add(files[i]);
}
}
Iterator iter = subDirs.iterator();
while (iter.hasNext()) {
File subDir = (File)iter.next();
addFiles(subDir, collection);
}
}
private static class ImportOverwriteQuery implements IOverwriteQuery {
public String queryOverwrite(String file) {
return ALL;
}
}
}