-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: 2140 Change-Id: I5a2c23f4cddd38c1843b939154faba0b2e007559 Signed-off-by: Philippe DUL <philippe.dul@thalesgroup.com>
- Loading branch information
Showing
4 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
240 changes: 240 additions & 0 deletions
240
...ct/src/org/polarsys/capella/core/platform/sirius/ui/project/sample/SampleModelWizard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...ct/src/org/polarsys/capella/core/platform/sirius/ui/project/sample/ZipFilenameFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |