| /** |
| * ******************************************************************************* |
| * Copyright (c) 2017 Timing-Architects Embedded Systems GmbH and others. |
| * 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: |
| * Timing-Architects Embedded Systems GmbH - initial API and implementation |
| * |
| * ******************************************************************************* |
| */ |
| package org.eclipse.app4mc.amalthea.generator.ui; |
| |
| import org.eclipse.jface.operation.IRunnableWithProgress; |
| import org.eclipse.jface.wizard.Wizard; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.lang.reflect.InvocationTargetException; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.app4mc.amalthea.generator.ModelGenerator; |
| import org.eclipse.app4mc.amalthea.generator.configuration.RTMGC; |
| import org.eclipse.app4mc.amalthea.generator.configuration.RTMGCPackage; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.emf.ecore.xmi.XMLResource; |
| |
| public class ConfigurationWizard extends Wizard implements IRunnableWithProgress { |
| |
| private ImportPage importPage; |
| private ConfigurationPage configurationPage; |
| private ExportPage exportPage; |
| private boolean canFinish = false; |
| private RTMGC rtmgc = null; |
| |
| public ConfigurationWizard() { |
| super(); |
| setNeedsProgressMonitor(true); |
| } |
| |
| public void setCanFinish(boolean canFinish) { |
| this.canFinish = canFinish; |
| } |
| |
| @Override |
| public String getWindowTitle() { |
| return "AMALTHEA Model Generator"; |
| } |
| |
| @Override |
| public void addPages() { |
| importPage = new ImportPage(); |
| configurationPage = new ConfigurationPage(); |
| exportPage = new ExportPage(); |
| addPage(importPage); |
| addPage(configurationPage); |
| addPage(exportPage); |
| } |
| |
| @Override |
| public boolean canFinish() { |
| return this.canFinish & configurationPage.isPageComplete(); |
| } |
| |
| @Override |
| public boolean performFinish() { |
| boolean result = true; |
| this.rtmgc = this.configurationPage.getConfiguration(); |
| try { |
| getContainer().run(true, true, this); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| result = false; |
| } |
| |
| return result; |
| } |
| |
| @Override |
| public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| // determine how many models to be generated |
| int amount = this.rtmgc.getGeneration().getCount().intValue(); |
| monitor.beginTask("Model Generation", amount + 1); |
| // export configuration |
| if(true == this.exportPage.doExport()) { |
| monitor.subTask("Export Configuration"); |
| File file = this.exportPage.getFile(); |
| exportConfiguration(file, this.rtmgc); |
| monitor.worked(1); |
| } |
| |
| // start model generation |
| ModelGenerator modelGenerator = new ModelGenerator(); |
| try { |
| modelGenerator.run(this.rtmgc, monitor); |
| } catch (Exception e) { |
| // TODO Auto-generated catch block |
| e.printStackTrace(); |
| } |
| |
| // for (int i=0; i<amount;i++) { |
| // monitor.internalWorked(1); |
| // Thread.sleep(2000); |
| // if (monitor.isCanceled()) { |
| // break; |
| // } |
| // } |
| } |
| |
| private void exportConfiguration(final File file, RTMGC rtmgc) { |
| file.getParentFile().mkdirs(); |
| try { |
| file.createNewFile(); |
| |
| final URI uri = URI.createFileURI(file.getAbsolutePath()); |
| final ResourceSet resourceSet = new ResourceSetImpl(); |
| |
| final Resource outResource = resourceSet.createResource(uri); |
| outResource.getContents().clear(); |
| outResource.getContents().add(rtmgc); |
| |
| // fixXmlnsAndXsiSchemaLocation(documentRoot); |
| |
| final Map<Object, Object> saveOptions = new HashMap<Object, Object>(); |
| saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE); |
| saveOptions.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); |
| saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); |
| saveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); |
| saveOptions.put(XMLResource.XML_SCHEMA_URI, RTMGCPackage.eNS_PREFIX); |
| |
| outResource.save(saveOptions); |
| |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (Exception e1) { |
| e1.printStackTrace(); |
| } |
| } |
| |
| } |