]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - docs/Developer/Utilities/GenericFileImport.md
First test on Simantics documentation using gitbook
[simantics/platform.git] / docs / Developer / Utilities / GenericFileImport.md
diff --git a/docs/Developer/Utilities/GenericFileImport.md b/docs/Developer/Utilities/GenericFileImport.md
new file mode 100644 (file)
index 0000000..87faea7
--- /dev/null
@@ -0,0 +1,89 @@
+Simantics Generic File Import is a utility service that enables [Eclipse's Dropins](http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fp2_dropins_format.html)-like functionality. The directory resides inside `~/workspace/.metadata/plugins/org.simantics.fileimport/dropins` and it is not watched by default. There are two ways to start the watchservice:\r
+\r
+1. Call directly in Java `FileImportDropins.watchDropinsFolder()`\r
+2. Use SCL function `watchDropinsFolder` from `"Dropins/Core"`\r
+\r
+## Creating own File importer\r
+\r
+To create own File importer it is usually enough to extend the abstract class `SimanticsResourceFileImport` and implement the two methods:\r
+\r
+    /**\r
+     * Returns a key-value map for file extensions this importer can handle\r
+     *\r
+     * @return Map<String, String> allowed extensions this service can handle. Key is the extension e.g. <code>.sharedLibrary</code> and the value is the description of the extension\r
+     */\r
+    Map<String, String> allowedExtensionsWithFilters();\r
+\r
+    /*\r
+     * Performs the import for the given file\r
+     *\r
+     * @param parent Resource parent of the imported entity in Simantics database\r
+     * @param file Path file location of file\r
+     * @return Optional Resource of the imported entity in Simantics database\r
+     * @throws Exception\r
+     */\r
+    public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;\r
+\r
+An example of such a class for importing FMU-files to Simantics workbench would be following:\r
+\r
+~~~\r
+public class FMIFileImport extends SimanticsResourceFileImport {\r
+\r
+    private static final Map<String, String> EXTENSIONS_FILTERS = Collections.singletonMap("*.fmu",\r
+            "Functional Mock-up Unit (*.fmu)");\r
+\r
+    @Override\r
+    public Optional<Resource> perform(Resource parent, Path file) throws IOException{\r
+        try {\r
+            Resource model = FMIStudioSCL.createFMIModel(parent, file);\r
+            return Optional.of(model);\r
+        } catch (FMILException | DatabaseException e) {\r
+            e.printStackTrace();\r
+            return Optional.empty();\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Map<String, String> allowedExtensionsWithFilters() {\r
+        return EXTENSIONS_FILTERS;\r
+    }\r
+\r
+}\r
+~~~\r
+\r
+It is important to create the component for defining declarative service. To do this right click the same plugin where the implmentation is and click New -> Other -> Plug-in Development -> Component Definition.\r
+\r
+Give the name for the XML file and and browse to the implementing class You just created.\r
+\r
+An example of XML-file is:\r
+\r
+<pre>\r
+<?xml version="1.0" encoding="UTF-8"?>\r
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.simantics.fmi.studio.fileimport">\r
+   <implementation class="org.simantics.fmi.studio.fileimport.FMIFileImport"/>\r
+   <service>\r
+      <provide interface="org.simantics.fileimport.IGenericFileImport"/>\r
+   </service>\r
+</scr:component>\r
+</pre>\r
+\r
+## FileImportService utility class\r
+\r
+The class `org.simantics.fileimport.FileImportService` contains utility methods for e.g. listing all current services and performing file imports:\r
+\r
+~~~\r
+    /**\r
+     * Lists all supported file extensions which have a registered service for handling the import\r
+     *\r
+     * @return Map containing the extension and the description of the extension in that order\r
+     */\r
+    public static Map<String, String> supportedExtensionsWithFilters();\r
+\r
+    /**\r
+     * 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\r
+     *\r
+     * @param file Path file to be imported\r
+     * @param callback Optional callback which can be used to catch Throwables thrown in the import process\r
+     */\r
+    public static void performFileImport(Path file, Optional<Consumer<Throwable>> callback);\r
+~~~\r