ImportOperation - import project files into custom project [message #330274] |
Thu, 24 July 2008 05:13  |
Eclipse User |
|
|
|
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/
|
|
|
|
Powered by
FUDForum. Page generated in 0.05030 seconds