下面列出了怎么用org.eclipse.ui.wizards.datatransfer.ImportOperation的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("deprecation")
public IProject importExample(ExampleData edata, IProgressMonitor monitor) {
try {
IProjectDescription original = ResourcesPlugin.getWorkspace()
.loadProjectDescription(new Path(edata.getProjectDir().getAbsolutePath()).append("/.project"));
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(edata.getProjectDir().getName());
IProjectDescription clone = ResourcesPlugin.getWorkspace().newProjectDescription(original.getName());
clone.setBuildSpec(original.getBuildSpec());
clone.setComment(original.getComment());
clone.setDynamicReferences(original.getDynamicReferences());
clone.setNatureIds(original.getNatureIds());
clone.setReferencedProjects(original.getReferencedProjects());
if (project.exists()) {
return project;
}
project.create(clone, monitor);
project.open(monitor);
@SuppressWarnings("unchecked")
List<IFile> filesToImport = FileSystemStructureProvider.INSTANCE.getChildren(edata.getProjectDir());
ImportOperation io = new ImportOperation(project.getFullPath(), edata.getProjectDir(),
FileSystemStructureProvider.INSTANCE, new IOverwriteQuery() {
@Override
public String queryOverwrite(String pathString) {
return IOverwriteQuery.ALL;
}
}, filesToImport);
io.setOverwriteResources(true);
io.setCreateContainerStructure(false);
io.run(monitor);
project.refreshLocal(IProject.DEPTH_INFINITE, monitor);
return project;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* extract zip file and import files into project
*
* @param srcZipFile
* @param destPath
* @param monitor
* @param query
* @throws CoreException
*/
private static void importFilesFromZip( ZipFile srcZipFile, IPath destPath,
IProgressMonitor monitor, IOverwriteQuery query )
throws CoreException
{
try
{
ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(
srcZipFile );
List list = prepareFileList( structureProvider, structureProvider
.getRoot( ), null );
ImportOperation op = new ImportOperation( destPath,
structureProvider.getRoot( ), structureProvider, query,
list );
op.run( monitor );
}
catch ( Exception e )
{
String message = srcZipFile.getName( ) + ": " + e.getMessage( ); //$NON-NLS-1$
Logger.logException( e );
throw BirtCoreException.getException( message, e );
}
}
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
}
}
/**
* Extract all file system elements (Tar, Zip elements) to destination
* folder (typically workspace/TraceProject/.traceImport or a subfolder of
* it)
*/
private void extractArchiveContent(Iterator<TraceFileSystemElement> fileSystemElementsIter, IFolder tempFolder, IProgressMonitor progressMonitor) throws InterruptedException,
InvocationTargetException {
List<TraceFileSystemElement> subList = new ArrayList<>();
// Collect all the elements
while (fileSystemElementsIter.hasNext()) {
ModalContext.checkCanceled(progressMonitor);
TraceFileSystemElement element = fileSystemElementsIter.next();
if (element.isDirectory()) {
Object[] array = element.getFiles().getChildren();
for (int i = 0; i < array.length; i++) {
subList.add((TraceFileSystemElement) array[i]);
}
}
subList.add(element);
}
if (subList.isEmpty()) {
return;
}
TraceFileSystemElement root = getRootElement(subList.get(0));
ImportProvider fileSystemStructureProvider = new ImportProvider();
IOverwriteQuery myQueryImpl = file -> IOverwriteQuery.NO_ALL;
progressMonitor.setTaskName(Messages.ImportTraceWizard_ExtractImportOperationTaskName);
IPath containerPath = tempFolder.getFullPath();
ImportOperation operation = new ImportOperation(containerPath, root, fileSystemStructureProvider, myQueryImpl, subList);
operation.setContext(fShell);
operation.setCreateContainerStructure(true);
operation.setOverwriteResources(false);
operation.setVirtualFolders(false);
operation.run(SubMonitor.convert(progressMonitor).newChild(subList.size()));
}
private IProject doCreateProject(ProjectCandidate pc, IProgressMonitor monitor) throws CoreException, InterruptedException {
HybridProjectCreator projectCreator = new HybridProjectCreator();
Widget w = pc.getWidget();
String projectName = pc.getProjectName();
URI location = null;
if(!copyFiles){
location = pc.wwwLocation.getParentFile().toURI();
}
IProject project = projectCreator.createProject(projectName, location, w.getName(), w.getId(), null, monitor);
if(copyFiles){
ImportOperation operation = new ImportOperation(project
.getFullPath(), pc.wwwLocation.getParentFile(), FileSystemStructureProvider.INSTANCE
, this);
operation.setContext(getShell());
operation.setOverwriteResources(true);
operation.setCreateContainerStructure(false);
try {
operation.run(monitor);
} catch (InvocationTargetException e) {
if(e.getCause() != null && e.getCause() instanceof CoreException){
CoreException corex = (CoreException) e.getCause();
throw corex;
}
}
IStatus status = operation.getStatus();
if (!status.isOK())
throw new CoreException(status);
}
return project;
}
/**
* Imports a trace resource to project. In case of name collision the user
* will be asked to confirm overwriting the existing trace, overwriting or
* skipping the trace to be imported.
*
* @param fileSystemElement
* trace file system object to import
* @param monitor
* a progress monitor
* @return the imported resource or null if no resource was imported
*
* @throws InvocationTargetException
* if problems during import operation
* @throws InterruptedException
* if cancelled
* @throws CoreException
* if problems with workspace
*/
private IResource importResource(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException, CoreException {
IPath tracePath = getInitialDestinationPath(fileSystemElement);
String newName = fConflictHandler.checkAndHandleNameClash(tracePath, monitor);
if (newName == null) {
return null;
}
fileSystemElement.setLabel(newName);
List<TraceFileSystemElement> subList = new ArrayList<>();
FileSystemElement parentFolder = fileSystemElement.getParent();
IPath containerPath = fileSystemElement.getDestinationContainerPath();
tracePath = containerPath.addTrailingSeparator().append(fileSystemElement.getLabel());
boolean createLinksInWorkspace = (fImportOptionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0;
if (fileSystemElement.isDirectory() && !createLinksInWorkspace) {
containerPath = tracePath;
Object[] array = fileSystemElement.getFiles().getChildren();
for (int i = 0; i < array.length; i++) {
subList.add((TraceFileSystemElement) array[i]);
}
parentFolder = fileSystemElement;
} else {
if (!fileSystemElement.isDirectory()) {
// File traces
IFileInfo info = EFS.getStore(new File(fileSystemElement.getFileSystemObject().getAbsolutePath()).toURI()).fetchInfo();
if (info.getLength() == 0) {
// Don't import empty traces
return null;
}
}
subList.add(fileSystemElement);
}
ImportProvider fileSystemStructureProvider = new ImportProvider();
IOverwriteQuery myQueryImpl = file -> IOverwriteQuery.NO_ALL;
monitor.setTaskName(Messages.ImportTraceWizard_ImportOperationTaskName + " " + fileSystemElement.getFileSystemObject().getAbsolutePath()); //$NON-NLS-1$
ImportOperation operation = new ImportOperation(containerPath, parentFolder, fileSystemStructureProvider, myQueryImpl, subList);
operation.setContext(fShell);
operation.setCreateContainerStructure(false);
operation.setOverwriteResources(false);
operation.setCreateLinks(createLinksInWorkspace);
operation.setVirtualFolders(false);
operation.run(SubMonitor.convert(monitor).newChild(1));
String sourceLocation = fileSystemElement.getSourceLocation();
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(tracePath);
if ((sourceLocation != null) && (resource != null)) {
resource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation);
}
return resource;
}