Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » ImportOperation - import project files into custom project
ImportOperation - import project files into custom project [message #330274] Thu, 24 July 2008 05:13 Go to next message
Eclipse UserFriend
Originally posted by: tkotisis.urbantech.gr

Hello,

is it possible to use ImportOperation to import files from a
ZipFileStructureProvider
into a newly created project?

Specifically, i have a zip file containing an entire project. On import
the project is imported into the runtime workspace. But, in case the (to
be) imported project might have the same name as an existing one, i need
to give the user the ability to "rename" it, e.g. import it's contents
under a different project name.

Code so far (mostly scraped from
org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsI mportPage.java):

final ZipFile zipSourceFile = getSpecifiedZipSourceFile(selectedFile);
if (zipSourceFile == null)
return;

ZipFileStructureProvider structureProvider =
new ZipFileStructureProvider(zipSourceFile);
if (structureProvider == null)
return;

Object projectFolder =
structureProvider.getChildren(structureProvider.getRoot()).g et(0);
List files = structureProvider.getChildren(projectFolder);
Object projectDescFile = null;
for (Object child : files) {
final String elementLabel = structureProvider.getLabel(child);
if (elementLabel.equals(IProjectDescription.DESCRIPTION_FILE_NA ME)) {
projectDescFile = child;
break;
}
}

InputStream stream = structureProvider.getContents(projectDescFile);
if (stream == null)
return;

final IWorkspace workspace = ResourcesPlugin.getWorkspace();

IProjectDescription description = null;
try {
description =
workspace.loadProjectDescription(stream);
stream.close();
} catch (CoreException e) {
// no good couldn't get the name
e.printStackTrace();
} catch (IOException e) {
// no good couldn't get the name
e.printStackTrace();
}

String projectName = description.getName();
final IProjectDescription newDesc =
workspace.newProjectDescription(projectName);

final IProject project = workspace.getRoot().getProject(projectName);

boolean overwrite = false;
if (project.exists()) {
overwrite =
MessageDialog.openQuestion(targetWindow.getShell(),
IDEWorkbenchMessages.Question,
"A poster with this name already exists. Overwrite?");
if (!overwrite)
return;
}

ProgressMonitorDialog pmd =
new ProgressMonitorDialog(targetWindow.getShell());
pmd.create();

IProgressMonitor monitor = pmd.getProgressMonitor();
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);

pmd.setCancelable(false);
pmd.open();

try {
new PreImportOperation(newDesc, workspace, project).run(
subMonitor.newChild(30));
subMonitor.worked(30);

ImportOperation op = new ImportOperation(
workspace.getRoot().getFullPath(),
structureProvider.getRoot(),
structureProvider,
new OverwriteStrategy(), files);

op.setContext(targetWindow.getShell());
op.setCreateContainerStructure(false);
op.run(subMonitor.newChild(70));
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (monitor != null)
monitor.done();
pmd.close();
}

PreImportOperation just creates the project the files will be imported in.

Changing ImportOperation's first argument (IPath containerPath) from
"workspace.getRoot().getFullPath()"
to
"workspace.getRoot().getProject(projectName).getFullPath()"
does not work by itself since the zip file structure is being
reconstructed under the newly created project,
e.g. /MyProject/.project is imported into
<WorkspaceRoot>/<NewProject>/MyProject/.project.

Is there a way to change the files to be imported to not include their
entire folder structure?

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Re: ImportOperation - import project files into custom project [message #330275 is a reply to message #330274] Thu, 24 July 2008 05:35 Go to previous message
Eclipse UserFriend
Originally posted by: tkotisis.urbantech.gr

The answer was really simple, call operation as following

ImportOperation op = new ImportOperation(
workspace.getRoot().getProject(projectName).getFullPath(),
projectFolder,
structureProvider,
new OverwriteStrategy(), files);

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Previous Topic:Access to MessageDialog object
Next Topic:TabbedPropertiesView - section refresh
Goto Forum:
  


Current Time: Thu May 08 13:07:12 EDT 2025

Powered by FUDForum. Page generated in 0.05030 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top