Simantics Generic File Import

From Developer Documents
Revision as of 11:18, 13 September 2016 by Jani Simomaa (talk | contribs) (Created page with "Simantics Generic File Import is a utility service that enables [http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fp2_dropins_f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Simantics Generic File Import is a utility service that enables Eclipse's Dropins-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:

  1. Call directly in Java FileImportDropins.watchDropinsFolder()
  2. Use SCL function watchDropinsFolder from "Dropins/Core"


Creating own File importer

To create own File importer it is usually enough to extend the abstract class SimanticsResourceFileImport and implement the two methods:

    /**
     * Returns a key-value map for file extensions this importer can handle
     * 
     * @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
     */
    Map<String, String> allowedExtensionsWithFilters();

    /**
     * Performs the import for the given file
     * 
     * @param parent Resource parent of the imported entity in Simantics database
     * @param file Path file location of file
     * @return Optional Resource of the imported entity in Simantics database
     * @throws Exception
     */
    public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;
    

An example of such a class for importing FMU-files to Simantics workbench would be following:

public class FMIFileImport extends SimanticsResourceFileImport {

    private static final Map<String, String> EXTENSIONS_FILTERS = Collections.singletonMap("*.fmu",
            "Functional Mock-up Unit (*.fmu)");

    @Override
    public Optional<Resource> perform(Resource parent, Path file) throws IOException{
        try {
            Resource model = FMIStudioSCL.createFMIModel(parent, file);
            return Optional.of(model);
        } catch (FMILException | DatabaseException e) {
            e.printStackTrace();
            return Optional.empty();
        }
    }

    @Override
    public Map<String, String> allowedExtensionsWithFilters() {
        return EXTENSIONS_FILTERS;
    }

}

FileImportService utility class

The class org.simantics.fileimport.FileImportService contains utility methods for e.g. listing all current services and performing file imports:

    /**
     * Lists all supported file extensions which have a registered service for handling the import
     * 
     * @return Map containing the extension and the description of the extension in that order
     */
    public static Map<String, String> supportedExtensionsWithFilters();

    /**
     * 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 callback Optional callback which can be used to catch Throwables thrown in the import process
     */
    public static void performFileImport(Path file, Optional<Consumer<Throwable>> callback);