Skip to content

Commit

Permalink
[2140] Add sample project
Browse files Browse the repository at this point in the history
Bug: 2140
Change-Id: I5a2c23f4cddd38c1843b939154faba0b2e007559
Signed-off-by: Philippe DUL <philippe.dul@thalesgroup.com>
  • Loading branch information
pdulth committed Aug 8, 2018
1 parent d3baa17 commit 1579c2c
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ====================================================================
# Copyright (c) 2006, 2015 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 @@ -10,4 +10,8 @@
# ====================================================================
SCM_Page_Name=SCM
providerName = Eclipse.org
pluginName = Capella Project Plug-in
pluginName = Capella Project Plug-in
CapellaCategoryWizard=Capella
CapellaCategorySample=Capella
CapellaSample=Capella Project Example
CapellaProject=Capella Project
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,36 @@
hasPages="true"
icon="icons/capella_16.png"
id="capella.project.wizard"
name="Capella Project"
name="%CapellaProject"
project="true">
<description>
Create a Capella project
</description>
</wizard>
<category
id="org.polarsys.capella.wizards"
name="Capella">
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
point="org.eclipse.ui.perspectiveExtensions">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*******************************************************************************
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.platform.sirius.ui.project.sample;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.internal.resources.ProjectDescription;
import org.eclipse.core.internal.resources.ProjectDescriptionReader;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.AssertionFailedException;
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.osgi.service.datalocation.Location;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
import org.xml.sax.InputSource;

/**
* The wizard allowing to initialize sample Capella model. Unlike the extended class, it import only the selected one,
* not all available projects from the wizard.
*
* By default, it read the Samples folder located aside the Capella installation location, looking for Zip model files
*/
public class SampleModelWizard extends AbstractExampleInstallerWizard {

List<ProjectDescriptor> descriptors = null;

ProjectDescriptor selected = null;

boolean install = false;

public class ExampleProjectDescriptor extends ProjectDescriptor {

public ExampleProjectDescriptor(URI uri) throws AssertionFailedException {
setContentURI(uri);

ProjectDescription project = createProjectDescription(contentURI);
Assert.isNotNull(project);

setName(project.getName());
setDescription(project.getComment());
}
}

/**
* For a zip uri, look for a .project file
*/
protected ProjectDescription createProjectDescription(URI contentURI) {
try {
String location = contentURI.toFileString();
if (location != null) {
File file = new File(location);
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;
}
}
}
}
}
}

} catch (Exception e) {
// Something wrong, the zip has not the wanted structure
}
return null;
}

@Override
protected List<ProjectDescriptor> getProjectDescriptors() {
if (install == true) {
return Collections.singletonList(selected);
}

if (descriptors == null) {
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
}
}

} catch (Exception e) {
// Samples folder has not the wanted structure
}

}

return descriptors;
}

protected Collection<File> getOwnedZips(URI sampleLocation) {
Collection<File> zips = new ArrayList<File>();

File file = new File(sampleLocation.toFileString());
if (file.exists() && file.isDirectory()) {
for (File fil : file.listFiles()) {
if (fil.isDirectory()) {
for (File zip : fil.listFiles(new ZipFilenameFilter())) {
zips.add(zip);
}
}
}
}
return zips;
}

@Override
public void addPages() {
projectPage = new ProjectPage("projectPage", CommonUIPlugin.INSTANCE.getString("_UI_ProjectPage_title"), null) {

@Override
protected void itemSelected() {
ProjectDescriptor projectDescriptor = getSelectedProjectDescriptor();
if (projectDescriptor != null) {
selected = projectDescriptor;
}
super.itemSelected();
}
};
projectPage.setDescription("Select a sample project to import");
addPage(projectPage);
}

@Override
public boolean performFinish() {
// By default, the wizard loads all available projects, we restrict it to the selected one
try {
install = true;
return super.performFinish();

} finally {
install = false;
}
}

/**
* Highly inspired by the super method, but Capella sample models have a root folder which is not the expected case.
*/
protected void installProjectFromFile(ProjectDescriptor projectDescriptor, IProgressMonitor progressMonitor)
throws Exception {
URI contentURI = projectDescriptor.getContentURI();
if (contentURI.isPlatform()) {
contentURI = CommonPlugin.asLocalURI(contentURI);
}

ImportOperation importOperation = null;
ZipFile zipFile = null;
try {
String location = contentURI.toFileString();
if (location != null) {
File file = new File(location);
if (file.isFile() && file.canRead()) {
zipFile = createZipFile(file);
if (zipFile != null) {
ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(zipFile);
Object root = structureProvider.getRoot();
if (root != null) {
List child = structureProvider.getChildren(root);
if (child != null && child.size() > 0) {
Object mainProject = child.get(0);
importOperation = new ImportOperation(projectDescriptor.getProject().getFullPath(), mainProject,
structureProvider, OVERWRITE_ALL_QUERY);
}
}
}
}
}

if (importOperation != null) {
installProject(projectDescriptor, importOperation, progressMonitor);
} else {
throw new Exception(
CommonUIPlugin.INSTANCE.getString("_UI_FileError_message", new String[] { contentURI.toString() }));
}

} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
// Ignore.
}
}
}
}

protected void installProject(ProjectDescriptor projectDescriptor, ImportOperation importOperation,
IProgressMonitor progressMonitor) throws Exception {
importOperation.setCreateContainerStructure(false);
super.installProject(projectDescriptor, importOperation, progressMonitor);
}

@Override
protected List<FileToOpen> getFilesToOpen() {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.platform.sirius.ui.project.sample;

import java.io.File;
import java.io.FilenameFilter;

public class ZipFilenameFilter implements FilenameFilter {

@Override
public boolean accept(File dir, String name) {
return name.endsWith("zip");
}

}

0 comments on commit 1579c2c

Please sign in to comment.