]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java
Fixed diagram copy/paste implementations to re-identify diagram elements
[simantics/platform.git] / bundles / org.simantics.fileimport.ui / src / org / simantics / fileimport / ui / ImportFileHandler.java
1
2 package org.simantics.fileimport.ui;
3
4 import java.nio.file.Paths;
5 import java.util.Map;
6 import java.util.Optional;
7 import java.util.function.Consumer;
8
9 import javax.inject.Named;
10
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;
20
21 public class ImportFileHandler {
22
23     private static final Logger LOGGER = LoggerFactory.getLogger(ImportFileHandler.class);
24     
25     @CanExecute
26     public boolean canExecute() {
27         return !FileImportService.supportedExtensionsWithFilters().isEmpty();
28     }
29
30     @Execute
31     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
32
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()]);
36         
37         // Sanity check
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);
42             }
43         }
44
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();
50         if (fileName == null)
51             return;
52
53         FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer<Throwable>) t -> {
54             LOGGER.error("Could not import file " + fileName, t);
55         }));
56     }
57 }