Skip to content

Commit

Permalink
[2140] Add Sample project import
Browse files Browse the repository at this point in the history
- Dispose zipfile entry
- Open session at the end of import
- Move to "Import menu"

Bug: 2140
Change-Id: I4a817453e35596d4c23402bc99373e470a83fd0c
Signed-off-by: Philippe DUL <philippe.dul@thalesgroup.com>
  • Loading branch information
pdulth committed Aug 10, 2018
1 parent bb3be3e commit 3994275
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 40 deletions.
Expand Up @@ -12,6 +12,6 @@ SCM_Page_Name=SCM
providerName = Eclipse.org
pluginName = Capella Project Plug-in
CapellaCategoryWizard=Capella
CapellaCategorySample=Capella
CapellaCategorySample=Capella Example
CapellaSample=Capella Project Example
CapellaProject=Capella Project
Expand Up @@ -30,6 +30,25 @@
natureId="org.polarsys.capella.project.nature">
</image>
</extension>
<extension
point="org.eclipse.ui.importWizards">

<wizard
category="capella.sample"
class="org.polarsys.capella.core.platform.sirius.ui.project.sample.SampleModelWizard"
icon="icons/capella_16.png"
id="capella.project.wizard.sample"
name="%CapellaSample">
<description>
Import Capella Project Example
</description>
</wizard>

<category
id="capella.sample"
name="%CapellaCategorySample">
</category>
</extension>
<extension
point="org.eclipse.ui.newWizards">
<wizard
Expand All @@ -49,24 +68,7 @@
name="%CapellaCategoryWizard">
</category>

<category
id="capella.sample"
name="%CapellaCategorySample"
parentCategory="org.eclipse.ui.Examples">
</category>

<wizard
category="org.eclipse.ui.Examples/capella.sample"
class="org.polarsys.capella.core.platform.sirius.ui.project.sample.SampleModelWizard"
hasPages="true"
icon="icons/capella_16.png"
id="capella.project.wizard.sample"
name="%CapellaSample"
project="true">
<description>
Import Capella Project Example
</description>
</wizard>

</extension>
<extension
Expand Down
Expand Up @@ -23,18 +23,25 @@

import org.eclipse.core.internal.resources.ProjectDescription;
import org.eclipse.core.internal.resources.ProjectDescriptionReader;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.ui.CommonUIPlugin;
import org.eclipse.emf.common.ui.wizard.AbstractExampleInstallerWizard;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
import org.polarsys.capella.core.model.handler.command.CapellaResourceHelper;
import org.polarsys.capella.core.sirius.ui.actions.OpenSessionAction;
import org.xml.sax.InputSource;

/**
Expand Down Expand Up @@ -75,16 +82,24 @@ protected ProjectDescription createProjectDescription(URI contentURI) {
if (file.isFile() && file.canRead()) {
ZipFile zipFile = createZipFile(file);
if (zipFile != null) {
ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(zipFile);
for (Object folder : structureProvider.getChildren(structureProvider.getRoot())) {
for (Object subfile : structureProvider.getChildren(folder)) {
if (subfile instanceof ZipEntry
&& ((ZipEntry) subfile).getName().endsWith(IProjectDescription.DESCRIPTION_FILE_NAME)) {
BufferedInputStream in = new BufferedInputStream(structureProvider.getContents((ZipEntry) subfile));
ProjectDescription description = new ProjectDescriptionReader().read(new InputSource(in));
return description;
try {
ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(zipFile);
for (Object folder : structureProvider.getChildren(structureProvider.getRoot())) {
for (Object subfile : structureProvider.getChildren(folder)) {
if (subfile instanceof ZipEntry
&& ((ZipEntry) subfile).getName().endsWith(IProjectDescription.DESCRIPTION_FILE_NAME)) {
BufferedInputStream in = new BufferedInputStream(structureProvider.getContents((ZipEntry) subfile));
ProjectDescription description = new ProjectDescriptionReader().read(new InputSource(in));
return description;
}
}
}
} finally {
try {
zipFile.close();
} catch (IOException e) {
// Ignore.
}
}
}
}
Expand All @@ -96,6 +111,16 @@ protected ProjectDescription createProjectDescription(URI contentURI) {
return null;
}

protected Collection<URI> getSampleLocations() {
Collection<URI> result = new ArrayList<URI>();
Location location = Platform.getInstallLocation();
URL url = location.getURL();
URI uri = URI.createURI(url.toString());
URI sampleLocation = uri.trimSegments(2).appendSegment("Samples");
result.add(sampleLocation);
return result;
}

@Override
protected List<ProjectDescriptor> getProjectDescriptors() {
if (install == true) {
Expand All @@ -106,18 +131,14 @@ protected List<ProjectDescriptor> getProjectDescriptors() {
descriptors = new ArrayList<AbstractExampleInstallerWizard.ProjectDescriptor>();

try {
Location location = Platform.getInstallLocation();
URL url = location.getURL();
URI uri = URI.createURI(url.toString());
URI sampleLocation = uri.trimSegments(2).appendSegment("Samples");

for (File zip : getOwnedZips(sampleLocation)) {
try {
URI fileUri = URI.createFileURI(zip.getAbsolutePath());
descriptors.add(new ExampleProjectDescriptor(fileUri));

} catch (Exception e) {
// Can't read the zip file
for (URI location : getSampleLocations()) {
for (File zip : getOwnedZips(location)) {
try {
URI fileUri = URI.createFileURI(zip.getAbsolutePath());
descriptors.add(new ExampleProjectDescriptor(fileUri));
} catch (Exception e) {
// Can't read the zip file
}
}
}

Expand Down Expand Up @@ -172,12 +193,28 @@ public boolean performFinish() {

} finally {
install = false;

}
}

@Override
protected void openFiles(IProgressMonitor progressMonitor) {
List<FileToOpen> filesToOpen = getFilesToOpen();
List<IFile> airds = new ArrayList<IFile>();
if (!filesToOpen.isEmpty()) {
for (FileToOpen open : filesToOpen) {
airds.add(open.getWorkspaceFile());
}
OpenSessionAction action = new OpenSessionAction();
action.selectionChanged(new StructuredSelection(airds));
action.run();
}
}

/**
* Highly inspired by the super method, but Capella sample models have a root folder which is not the expected case.
*/
@Override
protected void installProjectFromFile(ProjectDescriptor projectDescriptor, IProgressMonitor progressMonitor)
throws Exception {
URI contentURI = projectDescriptor.getContentURI();
Expand Down Expand Up @@ -226,6 +263,7 @@ protected void installProjectFromFile(ProjectDescriptor projectDescriptor, IProg
}
}

@Override
protected void installProject(ProjectDescriptor projectDescriptor, ImportOperation importOperation,
IProgressMonitor progressMonitor) throws Exception {
importOperation.setCreateContainerStructure(false);
Expand All @@ -234,6 +272,34 @@ protected void installProject(ProjectDescriptor projectDescriptor, ImportOperati

@Override
protected List<FileToOpen> getFilesToOpen() {
if (selected != null) {
Collection<IFile> airds = new ArrayList<IFile>();
List<FileToOpen> locations = new ArrayList<FileToOpen>();
try {
selected.getProject().accept(new IResourceVisitor() {

@Override
public boolean visit(IResource resource) throws CoreException {
if (CapellaResourceHelper.isAirdResource(resource, true)) {
airds.add((IFile) resource);
}
return true;
}
});
} catch (CoreException e) {
// Can't browse the project
}

for (IFile file : airds) {
locations.add(new FileToOpen() {
@Override
public IFile getWorkspaceFile() {
return file;
}
});
}
return locations;
}
return Collections.emptyList();
}

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<!--
Copyright (c) 2006, 2014 THALES GLOBAL SERVICES.
Copyright (c) 2006, 2018 THALES GLOBAL SERVICES.
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
Expand All @@ -15,7 +15,7 @@
<extension point="org.eclipse.ui.commands">

<command id="org.polarsys.capella.core.transition.diagram"
categoryId="org.polarsys.capella.core.transition"
categoryId="org.polarsys.capella.core.transitions"
description="%diagramTransition.description"
name="%diagramTransition.name">
</command>
Expand Down

0 comments on commit 3994275

Please sign in to comment.