AmaltheaModelMigrationHandler: Replaced use of  java.nio with eclipse.core.resources

 - only use local files as input for migration
diff --git a/plugins/org.eclipse.app4mc.amalthea.converters.ui/src/org/eclipse/app4mc/amalthea/converters/ui/handlers/AmaltheaModelMigrationHandler.java b/plugins/org.eclipse.app4mc.amalthea.converters.ui/src/org/eclipse/app4mc/amalthea/converters/ui/handlers/AmaltheaModelMigrationHandler.java
index 4cb157c..0c9d454 100644
--- a/plugins/org.eclipse.app4mc.amalthea.converters.ui/src/org/eclipse/app4mc/amalthea/converters/ui/handlers/AmaltheaModelMigrationHandler.java
+++ b/plugins/org.eclipse.app4mc.amalthea.converters.ui/src/org/eclipse/app4mc/amalthea/converters/ui/handlers/AmaltheaModelMigrationHandler.java
@@ -1,6 +1,6 @@
 /**

  ********************************************************************************

- * Copyright (c) 2015-2021 Robert Bosch GmbH and others.

+ * Copyright (c) 2015-2022 Robert Bosch GmbH and others.

  *

  * This program and the accompanying materials are made

  * available under the terms of the Eclipse Public License 2.0

@@ -17,15 +17,9 @@
 

 import java.io.File;

 import java.io.IOException;

-import java.nio.file.Files;

-import java.nio.file.Path;

-import java.nio.file.Paths;

 import java.util.ArrayList;

-import java.util.Arrays;

 import java.util.HashSet;

 import java.util.List;

-import java.util.stream.Collectors;

-import java.util.stream.Stream;

 

 import javax.inject.Named;

 

@@ -38,6 +32,8 @@
 import org.eclipse.core.resources.IContainer;

 import org.eclipse.core.resources.IFile;

 import org.eclipse.core.resources.IProject;

+import org.eclipse.core.resources.IResource;

+import org.eclipse.core.runtime.CoreException;

 import org.eclipse.core.runtime.IAdaptable;

 import org.eclipse.e4.core.di.annotations.Execute;

 import org.eclipse.e4.core.di.annotations.Optional;

@@ -49,7 +45,6 @@
 import org.eclipse.jface.viewers.TreeSelection;

 import org.eclipse.swt.widgets.Shell;

 

-@SuppressWarnings("restriction")

 public class AmaltheaModelMigrationHandler {

 

 	private static final String MODEL_MIGRATION = "AMALTHEA Model Migration";

@@ -158,8 +153,7 @@
 					projectSegment = firstSegment.toString();

 					iProject = ((IAdaptable) firstSegment).getAdapter(IProject.class);

 				}

-			}

-			else if (!projectSegment.equals(firstSegment.toString())) {

+			} else if (!projectSegment.equals(firstSegment.toString())) {

 				MessageDialog.openError(shell, MODEL_MIGRATION,

 						"Migration across multiple projects is not supported.");

 			}

@@ -169,63 +163,68 @@
 	}

 

 	private IContainer collectInput(TreeSelection selection, List<File> inputModels, Shell shell) {

-		TreePath[] paths = selection.getPaths();

+		HashSet<IContainer> iContainerSet = new HashSet<>();

 

-		IContainer selectedContainer = null;

+		populateContainerSet(selection, iContainerSet);

 

-		HashSet<File> inputFiles = new HashSet<>();

-		HashSet<File> parents = new HashSet<>();

+		for (IContainer iContainer : iContainerSet) {

+			// as the model scope is set to Folder, all the amxmi files present in the containers should be considered

 

-		for (TreePath treePath : paths) {

-

-			final Object lastSegment = treePath.getLastSegment();

-

-			if (lastSegment instanceof IAdaptable) {

-				IFile iFile = ((IAdaptable) lastSegment).getAdapter(IFile.class);

-

-				if (iFile != null) {

-					String path = iFile.getLocation().toOSString();

-					File file = new File(path);

-					try {

-						inputFiles.add(file.getCanonicalFile());

-						parents.add(file.getParentFile());

-					}

-					catch (IOException e) {

-						MessageDialog.openError(shell, MODEL_MIGRATION,

-								"Error fetching the file '" + iFile + "': " + e.getLocalizedMessage());

-					}

-				} else {

-					IContainer container = ((IAdaptable) lastSegment).getAdapter(IContainer.class);

-

-					if (container != null) {

-						selectedContainer = container;

-						Path modelFilePath = Paths.get(container.getLocationURI());

-						try (Stream<Path> directoryStream = Files.walk(modelFilePath, 1)) {

-							inputFiles.addAll(directoryStream

-									.filter(Files::isRegularFile)

-									.filter(file -> MigrationHelper.isModelFile(file.toString()))

-									.map(Path::toFile)

-									.collect(Collectors.toList()));

-

-						} catch (IOException e) {

-							MessageDialog.openError(shell, MODEL_MIGRATION,

-									"Failed to collect model files in container: " + e.getLocalizedMessage());

-						}

+			try {

+				for (IResource member : iContainer.members()) {

+					if (isLocalFileWithModelExtension(member)) {

+						addFileToList(member, inputModels, shell);

 					}

 				}

+			} catch (CoreException e) {

+				MessageDialog.openError(shell, MODEL_MIGRATION,

+						"Failed to collect model files in container: " + e.getLocalizedMessage());

 			}

 		}

 

-		for (File parent : parents) {

-			// as the model scope is set to Folder, all the amxmi files present in the folder should be considered

-			File[] allFilesInDirectory = parent.listFiles( (dir, name) -> MigrationHelper.isModelFile(name) );

+		return iContainerSet.isEmpty() ? null : iContainerSet.iterator().next();

+	}

 

-			inputFiles.addAll(Arrays.asList(allFilesInDirectory));

+	private void populateContainerSet(TreeSelection selection, HashSet<IContainer> iContainerSet) {

+		for (TreePath treePath : selection.getPaths()) {

+			final Object lastSegment = treePath.getLastSegment();

+

+			if (lastSegment instanceof IAdaptable) {

+				// try to adapt to IFile

+				IFile iFile = ((IAdaptable) lastSegment).getAdapter(IFile.class);

+

+				if (iFile != null) {

+					if (isLocalFileWithModelExtension(iFile)) {

+						iContainerSet.add(iFile.getParent());

+					}

+					continue;

+				}

+

+				// try to adapt to IContainer

+				IContainer iContainer = ((IAdaptable) lastSegment).getAdapter(IContainer.class);

+

+				if (iContainer != null) {

+					iContainerSet.add(iContainer);

+				}

+			}

 		}

+	}

 

-		inputModels.addAll(inputFiles);

+	private void addFileToList(IResource member, List<File> inputModels, Shell shell) {

+		File file = new File(member.getLocation().toString());

+		try {

+			inputModels.add(file.getCanonicalFile());

+		} catch (IOException e) {

+			MessageDialog.openError(shell, MODEL_MIGRATION,

+					"Error fetching the file '" + file + "': " + e.getLocalizedMessage());

+		}

+	}

 

-		return selectedContainer;

+	private boolean isLocalFileWithModelExtension(IResource resource) {

+		return resource instanceof IFile

+				&& resource.exists()

+				&& resource.getLocation() != null

+				&& MigrationHelper.isModelFileExtension(resource.getFileExtension());

 	}

 

 	private boolean isSimpleMigration(String context) {