2 package org.simantics.fileimport.ui;
4 import java.nio.file.Paths;
6 import java.util.Optional;
7 import java.util.function.Consumer;
9 import javax.inject.Named;
11 import org.eclipse.e4.core.di.annotations.CanExecute;
12 import org.eclipse.e4.core.di.annotations.Execute;
13 import org.eclipse.e4.ui.services.IServiceConstants;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.FileDialog;
16 import org.eclipse.swt.widgets.Shell;
17 import org.simantics.fileimport.FileImportService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class ImportFileHandler {
23 private static final Logger LOGGER = LoggerFactory.getLogger(ImportFileHandler.class);
26 public boolean canExecute() {
27 return !FileImportService.supportedExtensionsWithFilters().isEmpty();
31 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
33 Map<String, String> extensions = FileImportService.supportedExtensionsWithFilters();
34 String[] filterExtensions = (String[]) extensions.keySet().toArray(new String[extensions.keySet().size()]);
35 String[] filterNames = (String[]) extensions.values().toArray(new String[extensions.values().size()]);
38 for (int i = 0; i < filterExtensions.length; i++) {
39 String extension = filterExtensions[i];
40 if (!extension.startsWith("*.")) {
41 System.err.println("Invalid extension filter provied: " + extension);
45 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
46 dialog.setText("Choose File");
47 dialog.setFilterExtensions(filterExtensions);
48 dialog.setFilterNames(filterNames);
49 final String fileName = dialog.open();
53 FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer<Throwable>) t -> {
54 LOGGER.error("Could not import file " + fileName, t);