Skip to main content

Exporting plasmid maps with Java Plugin API

Comments

1 comment

  • Richard Moir

    Here's a method which you can hopefully adapt to your needs:

    /**
    * Export the sequence view of each document as a PNG image. NOTE: The sequence view will adopt whatever setting the user
    * has most recently used.
    *
    * @param documents documents to export
    * @param outputFolder folder to save PNGs in, each file will be named after the document: 'document.getName() + ".png"'
    * @param imageSize the resolution (in pixels) of the png file to export to, imageSize x imageSize (square)
    */
    static void batchExportSequenceViewAsPng(AnnotatedPluginDocument[] documents, File outputFolder, int imageSize) throws DocumentOperationException {
    outputFolder.mkdirs();
    for (AnnotatedPluginDocument doc : documents) {
    List<DocumentViewerFactory> documentViewerFactories = PluginUtilities.getDocumentViewerFactories(doc);
    for(DocumentViewerFactory factory : documentViewerFactories) {
    AtomicReference<DocumentOperationException> exception = new AtomicReference<>();
    String className = factory.getClass().getCanonicalName();
    if(className != null && className.equals("com.biomatters.plugins.sequenceviewer.SequenceViewerPlugin.SequenceViewerFactory")
    && !factory.getName().contains("Annotation")) {
    ThreadUtilities.invokeNowOrWait(() -> {
    final DocumentViewer documentViewer = factory.createViewer(new AnnotatedPluginDocument[] {doc});
    if (documentViewer == null) {
    return;
    }
    JFrame dummyFrame = new JFrame();
    dummyFrame.getContentPane().add(documentViewer.getComponent());
    dummyFrame.pack();
    ExtendedPrintable extendedPrintable = documentViewer.getExtendedPrintable();
    if (extendedPrintable == null) {
    return;
    }
    final BufferedImage image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
    try {
    Graphics2D g = image.createGraphics();
    g.setClip(0,0, imageSize, imageSize);
    Options options = extendedPrintable.getOptions(true);
    options.setStringValue("printVisibleRegionOnly", "false");
    extendedPrintable.print(g, new Dimension(imageSize, imageSize), 0, options);
    g.dispose();
    } catch (PrinterException e) {
    exception.set(new DocumentOperationException(e));
    }
    try {
    String fileName = FileUtilities.getLegalFileName(doc.getName(), " ");
    ImageIO.write(image, "png", new File(outputFolder, fileName + ".png"));
    } catch (IOException e) {
    exception.set(new DocumentOperationException(e));
    }
    NoLongerViewedListener listener = documentViewer.getNoLongerViewedListener();
    if(listener != null) {
    listener.noLongerViewed(false);
    }
    });
    }
    if (exception.get() != null) {
    throw exception.get();
    }
    }
    }
    }
    0

Please sign in to leave a comment.