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
-
package org.simantics.fileimport.ui;
import java.nio.file.Paths;
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;
}
@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()]);
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);
}));
}
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) {
}
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
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));
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;
/**
*/
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();
+
}
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();
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()));
return null;
}
}
-
- public abstract Resource defaultParentResource();
}
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();
}
}
@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
@Override
public Resource defaultParentResource() {
- return Simantics.getSession().getRootLibrary();
+ return null;
}
}
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();
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<>();