Commit 58e8b333 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ wizard improvements

parent bb08a240
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -13,7 +13,8 @@ Require-Bundle: org.eclipse.ui,
 org.etsi.mts.tdl.helper,
 org.etsi.mts.tdl.json2tdl,
 org.etsi.mts.tdl.common,
 org.eclipse.emf.ecore
 org.eclipse.emf.ecore,
 org.eclipse.equinox.preferences
Bundle-RequiredExecutionEnvironment: JavaSE-11
Automatic-Module-Name: org.etsi.mts.tdl.wizards
Bundle-ActivationPolicy: lazy
+19 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.wizards.importWizards;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IImportWizard;
@@ -20,6 +25,20 @@ public class ImportWizard extends Wizard implements IImportWizard {
	public boolean performFinish() {
		IFile file = mainPage.createNewFile();
		//TODO: copy original (optionally)
		//TODO: use eclipse way?
		//TODO: separate location for source?
		if (mainPage.isCopySource()) {
			IPath sourcePath = mainPage.getSourcePath();
			try {
				java.nio.file.Files.copy(
						Path.of(sourcePath.toOSString()),
						Path.of(file.getParent().getLocation().append(sourcePath.lastSegment()).toOSString()), 
						StandardCopyOption.REPLACE_EXISTING);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        if (file == null)
            return false;
        return true;
+70 −14
Original line number Diff line number Diff line
package org.etsi.mts.tdl.wizards.importWizards;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
@@ -21,22 +24,27 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.xtext.util.StringInputStream;
import org.etsi.mts.tdl.helper.TDLHelper;
import org.etsi.mts.tdl.json2tdl.JSON2TDLTranslator;
import org.etsi.mts.tdl.json2tdl.JSONConverter;
import org.etsi.mts.tdl.openapi2tdl.next.ConverterNext;
import org.etsi.mts.tdl.transform.AbstractTranslator;
import org.osgi.service.prefs.Preferences;


public class ImportWizardPage extends WizardNewFileCreationPage {
	
	protected FileFieldEditor editor;
	private boolean inline = false;
	private boolean copySource = false;
	private IPath sourcePath;
	LinkedHashMap<String, String> dataFormats = new LinkedHashMap<>();

	public ImportWizardPage(String pageName, IStructuredSelection selection) {
		super(pageName, selection);
		setTitle(pageName); //NON-NLS-1
		setDescription("Import data definitions from a file from the local file system into the workspace"); //NON-NLS-1
		loadDataFormat("org.etsi.mts.tdl.openapi2tdl.next", "OpenAPI", "*.yaml");
		loadDataFormat("org.etsi.mts.tdl.asn2tdl", "ASN.1", "*.asn1;*.asn");
		loadDataFormat("org.etsi.mts.tdl.json2tdl", "JSON", "*.json");
		loadDataFormat("org.etsi.mts.tdl.yang2tdl", "YANG", "*.yang");
	}

	/* (non-Javadoc)
@@ -55,14 +63,27 @@ public class ImportWizardPage extends WizardNewFileCreationPage {
		fileSelectionLayout.marginHeight = 0;
		fileSelectionArea.setLayout(fileSelectionLayout);
		
		//DONE: ensure that is a valid path (not-empty and existing and having the right extension
		//DONE: store and pre-fill with last used -> consider expanding with injected preferences for other controls as well
		String lastUsed = "LAST_USED";
		Preferences preferences = InstanceScope.INSTANCE.getNode("org.etsi.mts.tdl.wizarads.import.data");

		editor = new FileFieldEditor("fileSelect","Select Data File: ",fileSelectionArea); //NON-NLS-1 //NON-NLS-2
		editor.getTextControl(fileSelectionArea).addModifyListener(e -> {
			IPath path = new Path(ImportWizardPage.this.editor.getStringValue());
			setFileName(path.lastSegment()+".tdltx"); //TODO: generalise
			setSourcePath(new Path(ImportWizardPage.this.editor.getStringValue()));
			if (!getSourcePath().lastSegment().equals("null")) {
				preferences.put(lastUsed, getSourcePath().toOSString());
				setFileName(getSourcePath().lastSegment()+".tdltx"); //TODO: generalise
			}
		});
		//TODO: add option to copy source 
		String[] extensions = new String[] { "*.yaml;*.json;*.asn1", "*.yaml", "*.json", "*.asn1" }; //NON-NLS-1
		editor.setFileExtensions(extensions);
		editor.getTextControl(fileSelectionArea).setText(preferences.get(lastUsed, ""));
		
		//DONE: add option to copy source 
		//TODO: adapt extensions based on available bundles
		List<String> extensions = new ArrayList<String>(); 
		extensions.add(String.join(";",dataFormats.values()));
		extensions.addAll(dataFormats.values());
		editor.setFileExtensions(extensions.toArray(String[]::new));
		fileSelectionArea.moveAbove(null);

		
@@ -75,6 +96,7 @@ public class ImportWizardPage extends WizardNewFileCreationPage {
		new Label(fileSelectionArea, SWT.NONE);

		Label labelFlatten = new Label(fileSelectionArea, SWT.NONE);
		//TODO: for OpenAPI only?
        labelFlatten.setText("Import Inline Definitions:");
        //TODO: link and verify results
        Button buttonFlatten = new Button(fileSelectionArea, SWT.CHECK);
@@ -82,9 +104,14 @@ public class ImportWizardPage extends WizardNewFileCreationPage {
        buttonFlatten.addSelectionListener(SelectionListener.widgetSelectedAdapter(this::flattenDataDefinitions));
//        fill empty cell
		new Label(fileSelectionArea, SWT.NONE);
	}
	
        
	private void loadDataFormat(String bundleName, String label, String extension) {
		if (Platform.getBundle(bundleName) != null) {
			dataFormats.put(label, extension);
		}
	}

	
	 /* (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#createLinkTarget()
@@ -143,10 +170,39 @@ public class ImportWizardPage extends WizardNewFileCreationPage {
		return Status.OK_STATUS;
	}

	@Override
	protected boolean validatePage() {
		boolean validatePage = super.validatePage();
		//basic check for referenced data file
		IPath sourcePath = Path.fromOSString(this.editor.getStringValue());
		boolean exists = sourcePath.toFile().exists();
		if (!exists) {
			setErrorMessage("The selected data file path is not valid.");
		}
		return exists && validatePage;
	}
	
	private void flattenDataDefinitions(SelectionEvent e) {
		inline = ((Button) e.getSource()).getSelection();
	}

	private void copyDataDefinitions(SelectionEvent e) {
		setCopySource(((Button) e.getSource()).getSelection());
	}

	public boolean isCopySource() {
		return copySource;
	}

	public void setCopySource(boolean copySource) {
		this.copySource = copySource;
	}

	public IPath getSourcePath() {
		return sourcePath;
	}

	public void setSourcePath(IPath sourcePath) {
		this.sourcePath = sourcePath;
	}
}