]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.fileimport.ui / src / org / simantics / fileimport / ui / ImportFileHandler.java
index 21f8d3c4d8b6dc52767e3b1b9b5b8357df192191..1bdcd15757985cd2ef14366630fc6499414f4aa0 100644 (file)
@@ -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<String, String> 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()]);
@@ -37,21 +40,33 @@ public class ImportFileHandler {
         // Sanity check
         for (int i = 0; i < filterExtensions.length; i++) {
             String extension = filterExtensions[i];
-            if (!extension.startsWith("*.")) {
-                System.err.println("Invalid extension filter provied: " + extension);
+            if (!extension.startsWith("*.")) { //$NON-NLS-1$
+                System.err.println("Invalid extension filter provied: " + extension); //$NON-NLS-1$
             }
         }
 
         FileDialog dialog = new FileDialog(shell, SWT.OPEN);
-        dialog.setText("Choose File");
+        dialog.setText(Messages.ImportFileHandler_ChooseFile);
         dialog.setFilterExtensions(filterExtensions);
         dialog.setFilterNames(filterNames);
         final String fileName = dialog.open();
         if (fileName == null)
             return;
-
-        FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer<Throwable>) t -> {
-            LOGGER.error("Could not import file " + fileName, 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); //$NON-NLS-1$
+        }
+        
+        FileImportService.performFileImport(Paths.get(fileName), Optional.of(selectedResource), Optional.of((Consumer<Throwable>) t -> {
+            LOGGER.error("Could not import file " + fileName, t); //$NON-NLS-1$
         }));
     }
 }
\ No newline at end of file