Search in sources :

Example 1 with FileImporter

use of org.gephi.io.importer.spi.FileImporter in project gephi by gephi.

the class ImportControllerImpl method getFileImporter.

@Override
public FileImporter getFileImporter(File file) {
    FileObject fileObject = FileUtil.toFileObject(file);
    //Unzip and return content file
    fileObject = getArchivedFile(fileObject);
    FileImporterBuilder builder = getMatchingImporter(fileObject);
    if (fileObject != null && builder != null) {
        FileImporter fi = builder.buildImporter();
        return fi;
    }
    return null;
}
Also used : FileImporterBuilder(org.gephi.io.importer.spi.FileImporterBuilder) FileImporter(org.gephi.io.importer.spi.FileImporter) FileObject(org.openide.filesystems.FileObject)

Example 2 with FileImporter

use of org.gephi.io.importer.spi.FileImporter in project gephi by gephi.

the class DesktopImportControllerUI method importFiles.

@Override
public void importFiles(FileObject[] fileObjects) {
    MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class);
    fileObjects = Arrays.copyOf(fileObjects, fileObjects.length);
    // Extract files if they are zipped:
    for (int i = 0; i < fileObjects.length; i++) {
        fileObjects[i] = ImportUtils.getArchivedFile(fileObjects[i]);
        if (FileUtil.isArchiveArtifact(fileObjects[i])) {
            try {
                // Copy the archived file so we never have problems converting it to a simple File during import:
                File tempDir = TempDirUtils.createTempDirectory();
                fileObjects[i] = FileUtil.copyFile(fileObjects[i], FileUtil.toFileObject(tempDir), fileObjects[i].getName());
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    Reader[] readers = new Reader[fileObjects.length];
    FileImporter[] importers = new FileImporter[fileObjects.length];
    try {
        for (int i = 0; i < fileObjects.length; i++) {
            FileObject fileObject = fileObjects[i];
            importers[i] = controller.getFileImporter(fileObject);
            if (importers[i] == null) {
                NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(getClass(), "DesktopImportControllerUI.error_no_matching_file_importer"), NotifyDescriptor.WARNING_MESSAGE);
                DialogDisplayer.getDefault().notify(msg);
                return;
            }
            readers[i] = ImportUtils.getTextReader(fileObject);
            // MRU
            mostRecentFiles.addFile(fileObject.getPath());
        }
        importFiles(readers, importers, fileObjects);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : MostRecentFiles(org.gephi.desktop.mrufiles.api.MostRecentFiles) FileImporter(org.gephi.io.importer.spi.FileImporter) Reader(java.io.Reader) IOException(java.io.IOException) NotifyDescriptor(org.openide.NotifyDescriptor) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 3 with FileImporter

use of org.gephi.io.importer.spi.FileImporter in project gephi by gephi.

the class DesktopImportControllerUI method importFile.

@Override
public void importFile(final Reader reader, String importerName) {
    final FileImporter importer = controller.getFileImporter(importerName);
    if (importer == null) {
        NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(getClass(), "DesktopImportControllerUI.error_no_matching_file_importer"), NotifyDescriptor.WARNING_MESSAGE);
        DialogDisplayer.getDefault().notify(msg);
        return;
    }
    importFile(reader, importer);
}
Also used : NotifyDescriptor(org.openide.NotifyDescriptor) FileImporter(org.gephi.io.importer.spi.FileImporter)

Example 4 with FileImporter

use of org.gephi.io.importer.spi.FileImporter in project gephi by gephi.

the class DesktopImportControllerUI method importFile.

@Override
public void importFile(FileObject fileObject) {
    try {
        final FileImporter importer = controller.getFileImporter(FileUtil.toFile(fileObject));
        if (importer == null) {
            NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(getClass(), "DesktopImportControllerUI.error_no_matching_file_importer"), NotifyDescriptor.WARNING_MESSAGE);
            DialogDisplayer.getDefault().notify(msg);
            return;
        }
        //MRU
        MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class);
        mostRecentFiles.addFile(fileObject.getPath());
        ImporterUI ui = controller.getUI(importer);
        if (ui != null) {
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            FileImporter[] fi = (FileImporter[]) Array.newInstance(importer.getClass(), 1);
            fi[0] = importer;
            ui.setup(fi);
            final DialogDescriptor dd = new DialogDescriptor(panel, title);
            if (panel instanceof ValidationPanel) {
                ValidationPanel vp = (ValidationPanel) panel;
                vp.addChangeListener(new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        dd.setValid(!((ValidationPanel) e.getSource()).isProblem());
                    }
                });
            }
            Object result = DialogDisplayer.getDefault().notify(dd);
            if (!result.equals(NotifyDescriptor.OK_OPTION)) {
                ui.unsetup(false);
                return;
            }
            ui.unsetup(true);
        }
        LongTask task = null;
        if (importer instanceof LongTask) {
            task = (LongTask) importer;
        }
        //Execute task
        fileObject = getArchivedFile(fileObject);
        final String containerSource = fileObject.getNameExt();
        final InputStream stream = fileObject.getInputStream();
        String taskName = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.taskName", containerSource);
        executor.execute(task, new Runnable() {

            @Override
            public void run() {
                try {
                    Container container = controller.importFile(stream, importer);
                    if (container != null) {
                        container.setSource(containerSource);
                        finishImport(container);
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }, taskName, errorHandler);
    } catch (Exception ex) {
        Logger.getLogger("").log(Level.WARNING, "", ex);
    }
}
Also used : ImporterUI(org.gephi.io.importer.spi.ImporterUI) MostRecentFiles(org.gephi.desktop.mrufiles.api.MostRecentFiles) JPanel(javax.swing.JPanel) GZIPInputStream(java.util.zip.GZIPInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileImporter(org.gephi.io.importer.spi.FileImporter) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) NotifyDescriptor(org.openide.NotifyDescriptor) Container(org.gephi.io.importer.api.Container) ChangeEvent(javax.swing.event.ChangeEvent) LongTask(org.gephi.utils.longtask.spi.LongTask) DialogDescriptor(org.openide.DialogDescriptor) ChangeListener(javax.swing.event.ChangeListener) FileObject(org.openide.filesystems.FileObject)

Example 5 with FileImporter

use of org.gephi.io.importer.spi.FileImporter in project gephi by gephi.

the class DesktopImportControllerUI method importFiles.

private void importFiles(final Reader[] readers, final FileImporter[] importers, FileObject[] fileObjects) {
    try {
        File[] files = new File[readers.length];
        Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<>();
        for (int i = 0; i < importers.length; i++) {
            FileImporter importer = importers[i];
            ImporterUI ui = controller.getUI(importer);
            if (ui != null) {
                List<FileImporter> l = importerUIs.get(ui);
                if (l == null) {
                    l = new ArrayList<>();
                    importerUIs.put(ui, l);
                }
                l.add(importer);
            }
            if (importer instanceof FileImporter.FileAware) {
                try (Reader reader = readers[i]) {
                    File file = null;
                    if (fileObjects != null) {
                        file = FileUtil.toFile(fileObjects[i]);
                    }
                    if (file == null) {
                        // There is no source file but the importer needs it, create temporary copy:
                        String fileName = "tmp_file" + 1;
                        String charset = "UTF-8";
                        if (fileObjects != null && fileObjects[i] != null) {
                            // Netbeans FileUtil.toFile bug returning null?? Try to recover:
                            fileName = fileObjects[i].getNameExt();
                            CharsetToolkit charsetToolkit = new CharsetToolkit(fileObjects[i].getInputStream());
                            charset = charsetToolkit.getCharset().name();
                        }
                        file = TempDirUtils.createTempDir().createFile(fileName);
                        try (FileOutputStream fos = new FileOutputStream(file)) {
                            FileUtil.copy(new ReaderInputStream(reader, charset), fos);
                        }
                    }
                    files[i] = file;
                    ((FileImporter.FileAware) importer).setFile(file);
                }
            }
        }
        for (Map.Entry<ImporterUI, List<FileImporter>> entry : importerUIs.entrySet()) {
            ImporterUI ui = entry.getKey();
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            FileImporter[] fi = (FileImporter[]) entry.getValue().toArray((FileImporter[]) Array.newInstance(entry.getValue().get(0).getClass(), 0));
            ui.setup(fi);
            if (panel != null) {
                final DialogDescriptor dd = new DialogDescriptor(panel, title);
                if (panel instanceof ValidationPanel) {
                    ValidationPanel vp = (ValidationPanel) panel;
                    vp.addChangeListener(new ChangeListener() {

                        @Override
                        public void stateChanged(ChangeEvent e) {
                            dd.setValid(!((ValidationPanel) e.getSource()).isProblem());
                        }
                    });
                }
                Object result = DialogDisplayer.getDefault().notify(dd);
                if (!result.equals(NotifyDescriptor.OK_OPTION)) {
                    ui.unsetup(false);
                    return;
                }
            }
            if (ui instanceof ImporterUI.WithWizard) {
                boolean finishedOk = showWizard(ui, ((ImporterUI.WithWizard) ui).getWizardDescriptor());
                if (!finishedOk) {
                    ui.unsetup(false);
                    return;
                }
            }
            ui.unsetup(true);
        }
        final List<Container> results = new ArrayList<>();
        for (int i = 0; i < importers.length; i++) {
            doImport(results, readers[i], files[i], importers[i]);
        }
        executor.execute(null, new Runnable() {

            @Override
            public void run() {
                if (!results.isEmpty()) {
                    finishImport(results.toArray(new Container[0]));
                }
            }
        });
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : ImporterUI(org.gephi.io.importer.spi.ImporterUI) JPanel(javax.swing.JPanel) HashMap(java.util.HashMap) CharsetToolkit(org.gephi.utils.CharsetToolkit) FileImporter(org.gephi.io.importer.spi.FileImporter) ArrayList(java.util.ArrayList) Reader(java.io.Reader) Container(org.gephi.io.importer.api.Container) List(java.util.List) ArrayList(java.util.ArrayList) ChangeListener(javax.swing.event.ChangeListener) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ReaderInputStream(org.openide.util.io.ReaderInputStream) ChangeEvent(javax.swing.event.ChangeEvent) FileOutputStream(java.io.FileOutputStream) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

FileImporter (org.gephi.io.importer.spi.FileImporter)7 IOException (java.io.IOException)4 NotifyDescriptor (org.openide.NotifyDescriptor)4 FileObject (org.openide.filesystems.FileObject)4 Reader (java.io.Reader)3 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JPanel (javax.swing.JPanel)2 ChangeEvent (javax.swing.event.ChangeEvent)2 ChangeListener (javax.swing.event.ChangeListener)2 MostRecentFiles (org.gephi.desktop.mrufiles.api.MostRecentFiles)2 Container (org.gephi.io.importer.api.Container)2 FileImporterBuilder (org.gephi.io.importer.spi.FileImporterBuilder)2 ImporterUI (org.gephi.io.importer.spi.ImporterUI)2 ValidationPanel (org.netbeans.validation.api.ui.ValidationPanel)2 DialogDescriptor (org.openide.DialogDescriptor)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1