Search in sources :

Example 1 with Container

use of org.gephi.io.importer.api.Container in project gephi by gephi.

the class DesktopImportControllerUI method importFile.

@Override
public void importFile(final Reader reader, String importerName) {
    try {
        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;
        }
        ImporterUI ui = controller.getUI(importer);
        if (ui != null) {
            FileImporter[] fi = (FileImporter[]) Array.newInstance(importer.getClass(), 1);
            fi[0] = importer;
            ui.setup(fi);
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            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
        final String containerSource = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.streamSource", importerName);
        String taskName = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.taskName", containerSource);
        executor.execute(task, new Runnable() {

            @Override
            public void run() {
                try {
                    Container container = controller.importFile(reader, 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) JPanel(javax.swing.JPanel) 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 2 with Container

use of org.gephi.io.importer.api.Container in project gephi by gephi.

the class DesktopImportControllerUI method importFiles.

@Override
public void importFiles(FileObject[] fileObjects) {
    try {
        Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<>();
        List<FileImporter> importers = new ArrayList<>();
        for (FileObject fileObject : fileObjects) {
            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;
            }
            importers.add(importer);
            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);
            }
            //MRU
            MostRecentFiles mostRecentFiles = Lookup.getDefault().lookup(MostRecentFiles.class);
            mostRecentFiles.addFile(fileObject.getPath());
        }
        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);
            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);
        }
        final List<Container> result = new ArrayList<>();
        for (int i = 0; i < importers.size(); i++) {
            final FileImporter importer = importers.get(i);
            FileObject fileObject = fileObjects[i];
            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);
                            result.add(container);
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }, taskName, errorHandler);
        }
        executor.execute(null, new Runnable() {

            @Override
            public void run() {
                if (!result.isEmpty()) {
                    finishImport(result.toArray(new Container[0]));
                }
            }
        });
    } 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) HashMap(java.util.HashMap) FileImporter(org.gephi.io.importer.spi.FileImporter) ArrayList(java.util.ArrayList) NotifyDescriptor(org.openide.NotifyDescriptor) Container(org.gephi.io.importer.api.Container) List(java.util.List) ArrayList(java.util.ArrayList) ChangeListener(javax.swing.event.ChangeListener) FileObject(org.openide.filesystems.FileObject) GZIPInputStream(java.util.zip.GZIPInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ChangeEvent(javax.swing.event.ChangeEvent) LongTask(org.gephi.utils.longtask.spi.LongTask) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Container

use of org.gephi.io.importer.api.Container in project gephi by gephi.

the class DesktopImportControllerUI method finishImport.

private void finishImport(Container[] containers) {
    Report finalReport = new Report();
    for (Container container : containers) {
        if (container.verify()) {
            Report report = container.getReport();
            report.close();
            finalReport.append(report);
        } else {
        //TODO
        }
    }
    finalReport.close();
    //Report panel
    ReportPanel reportPanel = new ReportPanel();
    reportPanel.setData(finalReport, containers);
    DialogDescriptor dd = new DialogDescriptor(reportPanel, NbBundle.getMessage(DesktopImportControllerUI.class, "ReportPanel.title"));
    Object response = DialogDisplayer.getDefault().notify(dd);
    reportPanel.destroy();
    finalReport.clean();
    for (Container c : containers) {
        c.getReport().clean();
    }
    if (!response.equals(NotifyDescriptor.OK_OPTION)) {
        return;
    }
    final Processor processor = reportPanel.getProcessor();
    //Project
    Workspace workspace = null;
    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
    ProjectControllerUI pcui = Lookup.getDefault().lookup(ProjectControllerUI.class);
    if (pc.getCurrentProject() == null) {
        pcui.newProject();
        workspace = pc.getCurrentWorkspace();
    }
    //Process
    final ProcessorUI pui = getProcessorUI(processor);
    final ValidResult validResult = new ValidResult();
    if (pui != null) {
        try {
            final JPanel panel = pui.getPanel();
            if (panel != null) {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.processor.ui.dialog.title");
                        pui.setup(processor);
                        final DialogDescriptor dd2 = new DialogDescriptor(panel, title);
                        if (panel instanceof ValidationPanel) {
                            ValidationPanel vp = (ValidationPanel) panel;
                            vp.addChangeListener(new ChangeListener() {

                                @Override
                                public void stateChanged(ChangeEvent e) {
                                    dd2.setValid(!((ValidationPanel) e.getSource()).isProblem());
                                }
                            });
                            dd2.setValid(!vp.isProblem());
                        }
                        Object result = DialogDisplayer.getDefault().notify(dd2);
                        if (result.equals(NotifyDescriptor.CANCEL_OPTION) || result.equals(NotifyDescriptor.CLOSED_OPTION)) {
                            validResult.setResult(false);
                        } else {
                            //true
                            pui.unsetup();
                            validResult.setResult(true);
                        }
                    }
                });
            }
        } catch (InterruptedException ex) {
            Exceptions.printStackTrace(ex);
        } catch (InvocationTargetException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    if (validResult.isResult()) {
        controller.process(containers, processor, workspace);
        //StatusLine notify
        StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.status.multiImportSuccess", containers.length));
    }
}
Also used : JPanel(javax.swing.JPanel) Processor(org.gephi.io.processor.spi.Processor) Report(org.gephi.io.importer.api.Report) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) ProjectController(org.gephi.project.api.ProjectController) ProcessorUI(org.gephi.io.processor.spi.ProcessorUI) InvocationTargetException(java.lang.reflect.InvocationTargetException) Container(org.gephi.io.importer.api.Container) ChangeEvent(javax.swing.event.ChangeEvent) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) ChangeListener(javax.swing.event.ChangeListener) Workspace(org.gephi.project.api.Workspace) ProjectControllerUI(org.gephi.desktop.project.api.ProjectControllerUI)

Example 4 with Container

use of org.gephi.io.importer.api.Container in project gephi by gephi.

the class ReportPanel method initGraphTypeCombo.

private void initGraphTypeCombo(final Container[] containers) {
    final String directedStr = NbBundle.getMessage(ReportPanel.class, "ReportPanel.graphType.directed");
    final String undirectedStr = NbBundle.getMessage(ReportPanel.class, "ReportPanel.graphType.undirected");
    final String mixedStr = NbBundle.getMessage(ReportPanel.class, "ReportPanel.graphType.mixed");
    EdgeDirectionDefault edd = null;
    for (Container container : containers) {
        EdgeDirectionDefault d = container.getUnloader().getEdgeDefault();
        if (edd == null) {
            edd = d;
        } else if (d.equals(EdgeDirectionDefault.UNDIRECTED) && !edd.equals(EdgeDirectionDefault.UNDIRECTED)) {
            edd = EdgeDirectionDefault.MIXED;
        } else if (d.equals(EdgeDirectionDefault.DIRECTED) && !edd.equals(EdgeDirectionDefault.DIRECTED)) {
            edd = EdgeDirectionDefault.MIXED;
        }
    }
    final EdgeDirectionDefault dir = edd;
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
            switch(dir) {
                case DIRECTED:
                    comboModel.addElement(directedStr);
                    comboModel.addElement(undirectedStr);
                    comboModel.addElement(mixedStr);
                    comboModel.setSelectedItem(directedStr);
                    break;
                case UNDIRECTED:
                    comboModel.addElement(undirectedStr);
                    comboModel.addElement(mixedStr);
                    comboModel.setSelectedItem(undirectedStr);
                    break;
                case MIXED:
                    comboModel.addElement(directedStr);
                    comboModel.addElement(undirectedStr);
                    comboModel.addElement(mixedStr);
                    comboModel.setSelectedItem(mixedStr);
                    break;
            }
            graphTypeCombo.setModel(comboModel);
        }
    });
    graphTypeCombo.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                Object g = e.getItem();
                if (g.equals(directedStr)) {
                    for (Container container : containers) {
                        container.getLoader().setEdgeDefault(EdgeDirectionDefault.DIRECTED);
                    }
                } else if (g.equals(undirectedStr)) {
                    for (Container container : containers) {
                        container.getLoader().setEdgeDefault(EdgeDirectionDefault.UNDIRECTED);
                    }
                } else if (g.equals(mixedStr)) {
                    for (Container container : containers) {
                        container.getLoader().setEdgeDefault(EdgeDirectionDefault.MIXED);
                    }
                }
            }
        }
    });
}
Also used : Container(org.gephi.io.importer.api.Container) ItemEvent(java.awt.event.ItemEvent) ItemListener(java.awt.event.ItemListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) EdgeDirectionDefault(org.gephi.io.importer.api.EdgeDirectionDefault)

Example 5 with Container

use of org.gephi.io.importer.api.Container in project gephi by gephi.

the class DesktopGeneratorController method generate.

@Override
public void generate(final Generator generator) {
    String title = generator.getName();
    GeneratorUI ui = generator.getUI();
    if (ui != null) {
        ui.setup(generator);
        JPanel panel = ui.getPanel();
        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 != NotifyDescriptor.OK_OPTION) {
            return;
        }
        ui.unsetup();
    }
    final Container container = Lookup.getDefault().lookup(Container.Factory.class).newContainer();
    container.setSource("" + generator.getName());
    container.setReport(new Report());
    String taskname = NbBundle.getMessage(DesktopGeneratorController.class, "DesktopGeneratorController.taskname", generator.getName());
    //Error handler
    LongTaskErrorHandler errorHandler = new LongTaskErrorHandler() {

        @Override
        public void fatalError(Throwable t) {
            Logger.getLogger("").log(Level.SEVERE, "", t.getCause() != null ? t.getCause() : t);
        }
    };
    //Execute
    executor.execute(generator, new Runnable() {

        @Override
        public void run() {
            generator.generate(container.getLoader());
            finishGenerate(container);
        }
    }, taskname, errorHandler);
}
Also used : JPanel(javax.swing.JPanel) Report(org.gephi.io.importer.api.Report) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) GeneratorUI(org.gephi.io.generator.spi.GeneratorUI) Container(org.gephi.io.importer.api.Container) ChangeEvent(javax.swing.event.ChangeEvent) DialogDescriptor(org.openide.DialogDescriptor) ChangeListener(javax.swing.event.ChangeListener) LongTaskErrorHandler(org.gephi.utils.longtask.api.LongTaskErrorHandler)

Aggregations

Container (org.gephi.io.importer.api.Container)14 IOException (java.io.IOException)9 JPanel (javax.swing.JPanel)8 ChangeEvent (javax.swing.event.ChangeEvent)8 ChangeListener (javax.swing.event.ChangeListener)8 ValidationPanel (org.netbeans.validation.api.ui.ValidationPanel)8 DialogDescriptor (org.openide.DialogDescriptor)8 FileObject (org.openide.filesystems.FileObject)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ImporterUI (org.gephi.io.importer.spi.ImporterUI)6 LongTask (org.gephi.utils.longtask.spi.LongTask)6 NotifyDescriptor (org.openide.NotifyDescriptor)6 Report (org.gephi.io.importer.api.Report)5 FileImporter (org.gephi.io.importer.spi.FileImporter)4 FileNotFoundException (java.io.FileNotFoundException)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)2 MostRecentFiles (org.gephi.desktop.mrufiles.api.MostRecentFiles)2