X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.fileimport.ui%2Fsrc%2Forg%2Fsimantics%2Ffileimport%2Fui%2FImportFileHandler.java;h=fbe3ce7240f0ad55455d188cfb6872aa26affdd3;hb=664f37a026967c90a9a8a4ef3c5336ee426f67aa;hp=21f8d3c4d8b6dc52767e3b1b9b5b8357df192191;hpb=fcaaf23487d5aacf4fb2482e61ddbc55112ca17b;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java b/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java index 21f8d3c4d..fbe3ce724 100644 --- a/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java +++ b/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java @@ -1,4 +1,3 @@ - package org.simantics.fileimport.ui; import java.nio.file.Paths; @@ -8,12 +7,16 @@ import java.util.function.Consumer; import javax.inject.Named; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.e4.core.di.annotations.CanExecute; import org.eclipse.e4.core.di.annotations.Execute; import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; +import org.simantics.db.Resource; import org.simantics.fileimport.FileImportService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,8 +31,8 @@ public class ImportFileHandler { } @Execute - public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) { - + public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, + @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) { Map extensions = FileImportService.supportedExtensionsWithFilters(); String[] filterExtensions = (String[]) extensions.keySet().toArray(new String[extensions.keySet().size()]); String[] filterNames = (String[]) extensions.values().toArray(new String[extensions.values().size()]); @@ -49,8 +52,20 @@ public class ImportFileHandler { final String fileName = dialog.open(); if (fileName == null) return; - - FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer) t -> { + + Resource selectedResource = null; + try { + if(selection instanceof StructuredSelection) { + StructuredSelection structuredSelection = (StructuredSelection)selection; + Object elem = structuredSelection.getFirstElement(); + IAdaptable a = (IAdaptable)elem; + selectedResource = a.getAdapter(Resource.class); + } + } catch(NullPointerException | ClassCastException npe) { + LOGGER.warn("Failed to find selection, passing null to file importer", npe); + } + + FileImportService.performFileImport(Paths.get(fileName), Optional.of(selectedResource), Optional.of((Consumer) t -> { LOGGER.error("Could not import file " + fileName, t); })); }