Search in sources :

Example 6 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class SVGImageExporter method doExport.

@Override
public void doExport(@Nonnull final MindMapPanel panel, @Nullable final JComponent options, @Nullable final OutputStream out) throws IOException {
    if (options instanceof HasOptions) {
        final HasOptions opts = (HasOptions) options;
        this.flagExpandAllNodes = Boolean.parseBoolean(opts.getOption(Options.KEY_EXPAND_ALL));
        this.flagDrawBackground = Boolean.parseBoolean(opts.getOption(Options.KEY_DRAW_BACK));
    } else {
        for (final Component compo : Assertions.assertNotNull(options).getComponents()) {
            if (compo instanceof JCheckBox) {
                final JCheckBox cb = (JCheckBox) compo;
                if ("unfold".equalsIgnoreCase(cb.getActionCommand())) {
                    this.flagExpandAllNodes = cb.isSelected();
                } else if ("back".equalsIgnoreCase(cb.getActionCommand())) {
                    this.flagDrawBackground = cb.isSelected();
                }
            }
        }
    }
    final MindMap workMap = new MindMap(panel.getModel(), null);
    workMap.resetPayload();
    if (this.flagExpandAllNodes) {
        MindMapUtils.removeCollapseAttr(workMap);
    }
    final MindMapPanelConfig newConfig = new MindMapPanelConfig(panel.getConfiguration(), false);
    newConfig.setDrawBackground(this.flagDrawBackground);
    newConfig.setScale(1.0f);
    final Dimension2D blockSize = calculateSizeOfMapInPixels(workMap, null, newConfig, flagExpandAllNodes, RenderQuality.DEFAULT);
    if (blockSize == null) {
        return;
    }
    final StringBuilder buffer = new StringBuilder(16384);
    buffer.append(String.format(SVG_HEADER, 100, 100, dbl2str(blockSize.getWidth()), dbl2str(blockSize.getHeight()))).append(NEXT_LINE);
    final BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = image.createGraphics();
    final MMGraphics gfx = new SVGMMGraphics(buffer, g);
    gfx.setClip(0, 0, (int) Math.round(blockSize.getWidth()), (int) Math.round(blockSize.getHeight()));
    try {
        layoutFullDiagramWithCenteringToPaper(gfx, workMap, newConfig, blockSize);
        drawOnGraphicsForConfiguration(gfx, newConfig, workMap, false, null);
    } finally {
        gfx.dispose();
    }
    buffer.append("</svg>");
    final String text = buffer.toString();
    File fileToSaveMap = null;
    OutputStream theOut = out;
    if (theOut == null) {
        fileToSaveMap = MindMapUtils.selectFileToSaveForFileFilter(panel, Texts.getString("SvgExporter.saveDialogTitle"), ".svg", Texts.getString("SvgExporter.filterDescription"), Texts.getString("SvgExporter.approveButtonText"));
        // NOI18N
        fileToSaveMap = MindMapUtils.checkFileAndExtension(panel, fileToSaveMap, ".svg");
        theOut = fileToSaveMap == null ? null : new BufferedOutputStream(new FileOutputStream(fileToSaveMap, false));
    }
    if (theOut != null) {
        try {
            IOUtils.write(text, theOut, "UTF-8");
        } finally {
            if (fileToSaveMap != null) {
                IOUtils.closeQuietly(theOut);
            }
        }
    }
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics) MindMapPanelConfig(com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig) BufferedImage(java.awt.image.BufferedImage) HasOptions(com.igormaznitsa.mindmap.plugins.api.HasOptions)

Aggregations

MMGraphics (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)6 MMGraphics2DWrapper (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper)4 BufferedImage (java.awt.image.BufferedImage)4 Dimension2D (java.awt.geom.Dimension2D)2 Nullable (javax.annotation.Nullable)2 MindMap (com.igormaznitsa.mindmap.model.MindMap)1 HasOptions (com.igormaznitsa.mindmap.plugins.api.HasOptions)1 MindMapPanelConfig (com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)1 AbstractElement (com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement)1 Rectangle (java.awt.Rectangle)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Nonnull (javax.annotation.Nonnull)1