]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Updated file importer interface and fixed Spreadsheet import 50/1950/7
authorMiro Richard Eklund <miro.eklund@semantum.fi>
Wed, 25 Jul 2018 12:56:46 +0000 (15:56 +0300)
committerMiro Richard Eklund <miro.eklund@semantum.fi>
Thu, 26 Jul 2018 09:49:05 +0000 (12:49 +0300)
Spreadsheet import through the generic file importer always failed,
since the books cannot be imported to Development Project. Now the
generic file interface takes this into account and provides the
selection as a resource. Currently only ExcelFileImport uses

Amend 1: Changed the way the selected resource is found
Amend 2: Take into account Tuukka's review comments (1,2,3 and 5)
Amend 3 and 4: Changes by Jani and Miro to fix some issues with the
interface

gitlab #53
gitlab #56

Change-Id: Ibfb9d54d8c36a3dc2aa7da8f1043613159ae8383

bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF
bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/IGenericFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/dropins/FileImportDropins.java
bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SheetNode.java
bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetBook.java

index db1100bc370c780c823d029993b6c597e4c36540..55e40bf5438a5cc85678bf8e4874fc2fa9a00bb9 100644 (file)
@@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.e4.core.di,
  org.eclipse.e4.ui.services,
  org.simantics.fileimport;bundle-version="1.0.0",
- org.slf4j.api
+ org.slf4j.api,
+ org.simantics.db
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Import-Package: javax.inject;version="1.0.0"
 Bundle-ActivationPolicy: lazy
index 21f8d3c4d8b6dc52767e3b1b9b5b8357df192191..fbe3ce7240f0ad55455d188cfb6872aa26affdd3 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()]);
@@ -49,8 +52,20 @@ public class ImportFileHandler {
         final String fileName = dialog.open();
         if (fileName == null)
             return;
-
-        FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer<Throwable>) 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<Throwable>) t -> {
             LOGGER.error("Could not import file " + fileName, t);
         }));
     }
index 96276154237e11b31209dc1707c207edd68d8c71..5f9c7257b7da977057d5ac6ef3951aff787724bb 100644 (file)
@@ -93,27 +93,35 @@ public class FileImportService {
         Files.write(file, bytes);
         
         ConsumerHolder holder = new ConsumerHolder();
-        String result = performFileImport(file, Optional.of(holder));
+        String result = performFileImport(file, Optional.empty(), Optional.of(holder));
         if (holder.getThrowable() != null)
             throw holder.getThrowable();
         return result;
     }
-
+    
     /**
      * Method that performs the import of the given file. This method is called when e.g. {@link FileImportDropins} watcher detects {@link java.nio.file.StandardWatchEventKinds.ENTRY_CREATE} operation
      * 
      * @param file Path file to be imported
+     * @param possibleSelection - the selected resource (if exists)
      * @param callback Optional callback which can be used to catch Throwables thrown in the import process
      */
-    public static String performFileImport(Path file, Optional<Consumer<Throwable>> callback) {
+    public static String performFileImport(Path file, Optional<Resource> possibleSelection, Optional<Consumer<Throwable>> callback) {
         if (file.getFileName().toString().equals(DB_FILE)) {
             return null;
         }
         String result = "Import failed";
         IGenericFileImport service = findServiceForFileExtension(file);
+        
         if (service != null) {
             try {
-                Optional<String> resource = service.perform(file);
+               Optional<String> resource;
+               if (possibleSelection.isPresent() && service.defaultParentResource() == null) {
+                       resource = Optional.of(Long.toString(service.perform(possibleSelection.get(), file).get().getResourceId()));
+               }
+               else {
+                       resource = service.performWithDefaultParent(file);
+               }
                 saveResourceForPath(file, resource);
                 result = resource.get();
             } catch (Throwable t) {
@@ -130,7 +138,6 @@ public class FileImportService {
         }
         return result;
     }
-
     
     /**
      * Remove the entity that matches the file. This method is called when e.g. the {@link FileImportDropins} watcher detects {@link java.nio.file.StandardWatchEventKinds.ENTRY_DELETE} operation
@@ -353,12 +360,27 @@ public class FileImportService {
         return Optional.of(value);
     }
     
+    /**
+     * Calls the proper imported without a selection (null possibleSelection)
+     * @param path
+     * @param extension
+     * @return
+     * @throws Exception
+     */
     public static String importGenericFileWithExtension(String path, String extension) throws Exception {
         IGenericFileImport service = findServiceForExtension(extension);
-        Optional<String> result = service.perform(Paths.get(path));
+        Optional<String> result = service.performWithDefaultParent(Paths.get(path));
         return result.get();
     }
     
+    /**
+     * Calls the proper imported without a selection (null possibleSelection)
+     * @param parent
+     * @param path
+     * @param extension
+     * @return
+     * @throws Exception
+     */
     public static Resource importGenericFileWithExtensionAndParent(Resource parent, String path, String extension) throws Exception {
         IGenericFileImport service = findServiceForExtension(extension);
         Optional<Resource> result = service.perform(parent, Paths.get(path));
index a8bb132c98f8245013d250d5fd2f8c3f44e6e113..16df3ab457dd84b4b5217518b2b83c907a61f000 100644 (file)
@@ -18,14 +18,15 @@ import org.simantics.db.Resource;
 public interface IGenericFileImport {
 
     /**
-     * Performs the import for the given file
+     * Performs the import for the given file, using the default parent defined in the file-specific import implementation
+     * Calls perform(parent, file) with the defaultParent it finds
      * 
      * @param file Path to file to import
      * @return Optional string which will be the identifier of the imported file which can later be used for removing the imported entity
      * @throws Exception
      */
-    Optional<String> perform(Path file) throws Exception;
-
+    Optional<String> performWithDefaultParent(Path file) throws Exception;
+    
     Optional<Resource> perform(Resource parent, Path file) throws Exception;
     
     /**
@@ -42,4 +43,11 @@ public interface IGenericFileImport {
      */
     Map<String, String> allowedExtensionsWithFilters();
 
+    /**
+     * Choose the default parent resource in the specific implementations of the importer
+     * Can be null, in which case the default parent must always be provided from a selection UI or explicitly defined
+     * @return
+     */
+    public abstract Resource defaultParentResource();
+    
 }
index 4d2bcbe69105ff9a402931446718ef5e70a4c7b8..67ff51dab9c029ae9ef7e1222bf83968058d220f 100644 (file)
@@ -29,7 +29,7 @@ import org.simantics.layer0.Layer0;
 public abstract class SimanticsResourceFileImport implements IGenericFileImport {
 
     @Override
-    final public Optional<String> perform(Path file) throws Exception {
+    final public Optional<String> performWithDefaultParent(Path file) throws Exception {
         
         Path dropins = Activator.getDropinsFolder();
         
@@ -40,9 +40,13 @@ public abstract class SimanticsResourceFileImport implements IGenericFileImport
                parts = file.getFileName();
         }
         
-        Resource parent = resolveParent(null, parts);
+        Resource parent = defaultParentResource();
+        if(parent == null)
+               parent = resolveParent(null, parts);
+        
         if (parent == null)
             return Optional.empty();
+        
         Optional<Resource> imported = perform(parent, file); 
         if (imported.isPresent()) {
             return Optional.of(serialize(imported.get()));
@@ -128,7 +132,5 @@ public abstract class SimanticsResourceFileImport implements IGenericFileImport
             return null;
         }
     }
-    
-    public abstract Resource defaultParentResource();
 
 }
index a2632362baaef8f060291d48ea53d779c27374d4..fb01e42e5cf3f61ba5f5db97c70fefb550e3358a 100644 (file)
@@ -141,14 +141,14 @@ public class FileImportDropins {
                                 current++;
                             }
                             
-                            FileImportService.performFileImport(newPath, Optional.of(t -> {
+                            FileImportService.performFileImport(newPath, Optional.empty(), Optional.of(t -> {
                                 if ((t instanceof FileSystemException) || (t instanceof FileNotFoundException)) {
                                     try {
                                         syncPath(newPath);
                                     } catch (IOException e) {
                                         e.printStackTrace();
                                     }
-                                    FileImportService.performFileImport(newPath, Optional.empty());
+                                    FileImportService.performFileImport(newPath, Optional.empty(), Optional.empty());
                                 } else {
                                     t.printStackTrace();
                                 }
index c3a84f7a4ef6cf0709f17b0e494b376b4bbc05a4..d1f8924a58b5c245c6b450d4b6c33769bf81999d 100644 (file)
@@ -20,8 +20,13 @@ public class ExcelFileImport extends SimanticsResourceFileImport {
     }
     
     @Override
-    public Optional<Resource> perform(Resource parent, Path file) throws Exception {
-        return Optional.ofNullable(ExcelImport.importBookR(parent, file.toFile()));
+    public Optional<Resource> perform(Resource possibleSelection, Path file) throws Exception {
+       if(possibleSelection != null) {
+               //Make sure the selection is of valid type here
+               return Optional.ofNullable(ExcelImport.importBookR(possibleSelection, file.toFile()));
+       } else {
+               throw new NullPointerException("No selection provided - Cannot import book");
+       }
     }
 
     @Override
@@ -31,7 +36,7 @@ public class ExcelFileImport extends SimanticsResourceFileImport {
 
     @Override
     public Resource defaultParentResource() {
-        return Simantics.getSession().getRootLibrary();
+        return null;
     }
 
 }
index c92807a458bf53b9c18adc061725287f563b9ddf..d2ec679b7f8054cc7558abc8f48bae6afca08d31 100644 (file)
@@ -1,8 +1,9 @@
 package org.simantics.spreadsheet.graph;
 
+import java.io.Serializable;
 import java.util.Map;
 
-public interface SheetNode<Child extends SheetNode<?, ?>, Property extends SheetNode<?, ?>> {
+public interface SheetNode<Child extends SheetNode<?, ?>, Property extends SheetNode<?, ?>> extends Serializable {
 
        String getName();
        Map<String, Child> getChildren();
index d64d008edf713b1cfaf33a3e0fe28261bf188447..5a7dcd5ee0aae8f1f129158c284ac00cd666deba 100644 (file)
@@ -42,7 +42,9 @@ public class SpreadsheetBook implements StandardNodeManagerSupport<SheetNode>, S
 
        private static final long serialVersionUID = 7417208688311691396L;
        
-       public Serializable NotAvailableError = new Serializable() {};
+       public Serializable NotAvailableError = new Serializable() {
+               private static final long serialVersionUID = 2535371785498129460L;
+       };
        
        public Long2ObjectOpenHashMap<AbstractLongSet> referenceMap = new Long2ObjectOpenHashMap<>();