Search in sources :

Example 1 with LongTask

use of org.gephi.utils.longtask.spi.LongTask in project gephi by gephi.

the class LongTaskExecutor method finished.

private synchronized void finished(RunningLongTask runningLongTask) {
    if (cancelTimer != null) {
        cancelTimer.cancel();
    }
    LongTask task = runningLongTask.task;
    currentTask = null;
    if (listener != null) {
        listener.taskFinished(task);
    }
}
Also used : LongTask(org.gephi.utils.longtask.spi.LongTask)

Example 2 with LongTask

use of org.gephi.utils.longtask.spi.LongTask in project gephi by gephi.

the class PDFExporter method execute.

@Override
public boolean execute() {
    Progress.start(progress);
    PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
    controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
    controller.refreshPreview(workspace);
    PreviewProperties props = controller.getModel(workspace).getProperties();
    Rectangle size = new Rectangle(pageSize);
    if (landscape) {
        size = new Rectangle(pageSize.rotate());
    }
    Color col = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
    size.setBackgroundColor(new BaseColor(col.getRed(), col.getGreen(), col.getBlue()));
    Document document = new Document(size);
    PdfWriter pdfWriter = null;
    try {
        pdfWriter = PdfWriter.getInstance(document, stream);
        pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_5);
        pdfWriter.setFullCompression();
    } catch (DocumentException ex) {
        Exceptions.printStackTrace(ex);
    }
    document.open();
    PdfContentByte cb = pdfWriter.getDirectContent();
    cb.saveState();
    props.putValue(PDFTarget.LANDSCAPE, landscape);
    props.putValue(PDFTarget.PAGESIZE, size);
    props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop));
    props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft));
    props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom));
    props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight));
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb);
    target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace);
    if (target instanceof LongTask) {
        ((LongTask) target).setProgressTicket(progress);
    }
    try {
        controller.render(target, workspace);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cb.restoreState();
    document.close();
    Progress.finish(progress);
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, null);
    props.putValue(PDFTarget.PAGESIZE, null);
    return !cancel;
}
Also used : PreviewProperties(org.gephi.preview.api.PreviewProperties) PdfWriter(com.itextpdf.text.pdf.PdfWriter) Color(java.awt.Color) BaseColor(com.itextpdf.text.BaseColor) Rectangle(com.itextpdf.text.Rectangle) Document(com.itextpdf.text.Document) PreviewController(org.gephi.preview.api.PreviewController) DocumentException(com.itextpdf.text.DocumentException) BaseColor(com.itextpdf.text.BaseColor) LongTask(org.gephi.utils.longtask.spi.LongTask) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 3 with LongTask

use of org.gephi.utils.longtask.spi.LongTask 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 4 with LongTask

use of org.gephi.utils.longtask.spi.LongTask 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 5 with LongTask

use of org.gephi.utils.longtask.spi.LongTask in project gephi by gephi.

the class DesktopExportController method exportFile.

@Override
public void exportFile(final FileObject fileObject, final Exporter exporter) {
    if (exporter == null) {
        throw new RuntimeException(NbBundle.getMessage(getClass(), "error_no_matching_file_exporter"));
    }
    //Export Task
    LongTask task = null;
    if (exporter instanceof LongTask) {
        task = (LongTask) exporter;
    }
    String taskmsg = NbBundle.getMessage(DesktopExportController.class, "DesktopExportController.exportTaskName", fileObject.getNameExt());
    executor.execute(task, new Runnable() {

        @Override
        public void run() {
            try {
                controller.exportFile(FileUtil.toFile(fileObject), exporter);
                StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(DesktopExportController.class, "DesktopExportController.status.exportSuccess", fileObject.getNameExt()));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }, taskmsg, errorHandler);
}
Also used : LongTask(org.gephi.utils.longtask.spi.LongTask)

Aggregations

LongTask (org.gephi.utils.longtask.spi.LongTask)18 JPanel (javax.swing.JPanel)7 ChangeEvent (javax.swing.event.ChangeEvent)7 ChangeListener (javax.swing.event.ChangeListener)7 ValidationPanel (org.netbeans.validation.api.ui.ValidationPanel)7 DialogDescriptor (org.openide.DialogDescriptor)7 NotifyDescriptor (org.openide.NotifyDescriptor)7 IOException (java.io.IOException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Container (org.gephi.io.importer.api.Container)6 ImporterUI (org.gephi.io.importer.spi.ImporterUI)6 FileObject (org.openide.filesystems.FileObject)6 MostRecentFiles (org.gephi.desktop.mrufiles.api.MostRecentFiles)4 FileImporter (org.gephi.io.importer.spi.FileImporter)4 PreviewController (org.gephi.preview.api.PreviewController)3 StatisticsController (org.gephi.statistics.api.StatisticsController)3 LongTaskListener (org.gephi.utils.longtask.api.LongTaskListener)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2