Search in sources :

Example 1 with HasOptions

use of com.igormaznitsa.mindmap.plugins.api.HasOptions in project netbeans-mmd-plugin by raydac.

the class Main method makeConversion.

private static void makeConversion(@Nonnull final File from, @Nonnull final AbstractImporter fromFormat, @Nonnull final File to, @Nonnull final AbstractExporter toFormat, @Nonnull final MindMapPanelConfig config, @Nonnull final Properties options) throws Exception {
    final AtomicReference<Exception> error = new AtomicReference<>();
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            try {
                final DialogProvider dialog = new DialogProvider() {

                    @Override
                    public void msgError(@Nullable final Component parentComponent, @Nonnull final String text) {
                        LOGGER.error(text);
                    }

                    @Override
                    public void msgInfo(@Nullable final Component parentComponent, @Nonnull final String text) {
                        LOGGER.info(text);
                    }

                    @Override
                    public void msgWarn(@Nullable final Component parentComponent, @Nonnull final String text) {
                        LOGGER.warn(text);
                    }

                    @Override
                    public boolean msgConfirmOkCancel(@Nullable final Component parentComponent, @Nonnull final String title, @Nonnull final String question) {
                        // NOI18N
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public boolean msgOkCancel(@Nullable final Component parentComponent, @Nonnull final String title, @Nonnull final JComponent component) {
                        // NOI18N
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public boolean msgConfirmYesNo(@Nullable final Component parentComponent, @Nonnull final String title, @Nonnull final String question) {
                        // NOI18N
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public Boolean msgConfirmYesNoCancel(@Nullable final Component parentComponent, @Nonnull final String title, @Nonnull final String question) {
                        // NOI18N
                        throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public File msgSaveFileDialog(@Nullable final Component parentComponent, @Nonnull final String id, @Nonnull final String title, @Nullable final File defaultFolder, final boolean filesOnly, @Nonnull final FileFilter fileFilter, @Nonnull final String approveButtonText) {
                        return to;
                    }

                    @Override
                    public File msgOpenFileDialog(@Nullable final Component parentComponent, @Nonnull final String id, @Nonnull final String title, @Nullable final File defaultFolder, final boolean filesOnly, @Nonnull final FileFilter fileFilter, @Nonnull final String approveButtonText) {
                        return from;
                    }
                };
                final MindMapPanel panel = new MindMapPanel(new MindMapPanelController() {

                    @Override
                    public boolean isUnfoldCollapsedTopicDropTarget(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isCopyColorInfoFromParentToNewChildAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isTrimTopicTextBeforeSet(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isSelectionAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isElementDragAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isMouseMoveProcessingAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isMouseWheelProcessingAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    public boolean isMouseClickProcessingAllowed(@Nonnull final MindMapPanel source) {
                        return false;
                    }

                    @Override
                    @Nonnull
                    public MindMapPanelConfig provideConfigForMindMapPanel(@Nonnull final MindMapPanel source) {
                        return config;
                    }

                    @Override
                    @Nullable
                    public JPopupMenu makePopUpForMindMapPanel(@Nonnull final MindMapPanel source, @Nonnull final Point point, @Nullable final AbstractElement elementUnderMouse, @Nullable final ElementPart elementPartUnderMouse) {
                        return null;
                    }

                    @Override
                    @Nonnull
                    public DialogProvider getDialogProvider(@Nonnull final MindMapPanel source) {
                        return dialog;
                    }

                    @Override
                    public boolean processDropTopicToAnotherTopic(@Nonnull final MindMapPanel source, @Nonnull final Point dropPoint, @Nonnull final Topic draggedTopic, @Nonnull final Topic destinationTopic) {
                        return false;
                    }
                });
                MindMap map = new MindMap(new MindMapController() {

                    private static final long serialVersionUID = -5276000656494506314L;

                    @Override
                    public boolean canBeDeletedSilently(@Nonnull final MindMap map, @Nonnull final Topic topic) {
                        return true;
                    }
                }, false);
                panel.setModel(map);
                map = fromFormat.doImport(panel, dialog, null, new Topic[0]);
                if (map != null) {
                    panel.setModel(map);
                } else {
                    dialog.msgError(MAIN_FRAME, "Can't import map");
                }
                final JComponent optionsComponent = toFormat.makeOptions();
                if (!options.isEmpty()) {
                    if (optionsComponent instanceof HasOptions) {
                        final HasOptions optionable = (HasOptions) optionsComponent;
                        for (final String k : options.stringPropertyNames()) {
                            if (optionable.doesSupportKey(k)) {
                                optionable.setOption(k, options.getProperty(k));
                            } else {
                                // NOI18N
                                throw new IllegalArgumentException("Exporter " + toFormat.getMnemonic() + " doesn't support option '" + k + "\', it provides options " + Arrays.toString(optionable.getOptionKeys()));
                            }
                        }
                    } else {
                        // NOI18N
                        throw new IllegalArgumentException("Exporter " + toFormat.getMnemonic() + " doesn't support options");
                    }
                }
                final FileOutputStream result = new FileOutputStream(to, false);
                try {
                    toFormat.doExport(panel, optionsComponent, result);
                    result.flush();
                } finally {
                    IOUtils.closeQuietly(result);
                }
            } catch (Exception ex) {
                error.set(ex);
            }
        }
    });
    if (error.get() != null) {
        throw error.get();
    }
}
Also used : Component(java.awt.Component) JComponent(javax.swing.JComponent) FileFilter(javax.swing.filechooser.FileFilter) Topic(com.igormaznitsa.mindmap.model.Topic) MindMap(com.igormaznitsa.mindmap.model.MindMap) AbstractElement(com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement) Nonnull(javax.annotation.Nonnull) DialogProvider(com.igormaznitsa.mindmap.swing.panel.DialogProvider) JComponent(javax.swing.JComponent) AtomicReference(java.util.concurrent.atomic.AtomicReference) MindMapPanel(com.igormaznitsa.mindmap.swing.panel.MindMapPanel) Point(java.awt.Point) MindMapController(com.igormaznitsa.mindmap.model.MindMapController) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MindMapPanelController(com.igormaznitsa.mindmap.swing.panel.MindMapPanelController) FileOutputStream(java.io.FileOutputStream) HasOptions(com.igormaznitsa.mindmap.plugins.api.HasOptions) File(java.io.File) ElementPart(com.igormaznitsa.mindmap.swing.panel.ui.ElementPart) Nullable(javax.annotation.Nullable)

Example 2 with HasOptions

use of com.igormaznitsa.mindmap.plugins.api.HasOptions in project netbeans-mmd-plugin by raydac.

the class PNGImageExporter 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 MindMapPanelConfig newConfig = new MindMapPanelConfig(panel.getConfiguration(), false);
    newConfig.setDrawBackground(this.flagDrawBackground);
    newConfig.setScale(1.0f);
    final RenderedImage image = MindMapPanel.renderMindMapAsImage(panel.getModel(), newConfig, flagExpandAllNodes, RenderQuality.QUALITY);
    if (image == null) {
        if (out == null) {
            LOGGER.error("Can't render map as image");
            panel.getController().getDialogProvider(panel).msgError(null, Texts.getString("PNGImageExporter.msgErrorDuringRendering"));
            return;
        } else {
            throw new IOException("Can't render image");
        }
    }
    final ByteArrayOutputStream buff = new ByteArrayOutputStream(128000);
    // NOI18N
    ImageIO.write(image, "png", buff);
    final byte[] imageData = buff.toByteArray();
    File fileToSaveMap = null;
    OutputStream theOut = out;
    if (theOut == null) {
        fileToSaveMap = MindMapUtils.selectFileToSaveForFileFilter(panel, Texts.getString("PNGImageExporter.saveDialogTitle"), ".png", Texts.getString("PNGImageExporter.filterDescription"), Texts.getString("PNGImageExporter.approveButtonText"));
        // NOI18N
        fileToSaveMap = MindMapUtils.checkFileAndExtension(panel, fileToSaveMap, ".png");
        theOut = fileToSaveMap == null ? null : new BufferedOutputStream(new FileOutputStream(fileToSaveMap, false));
    }
    if (theOut != null) {
        try {
            IOUtils.write(imageData, theOut);
        } finally {
            if (fileToSaveMap != null) {
                IOUtils.closeQuietly(theOut);
            }
        }
    }
}
Also used : HasOptions(com.igormaznitsa.mindmap.plugins.api.HasOptions) RenderedImage(java.awt.image.RenderedImage) MindMapPanelConfig(com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)

Example 3 with HasOptions

use of com.igormaznitsa.mindmap.plugins.api.HasOptions 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

HasOptions (com.igormaznitsa.mindmap.plugins.api.HasOptions)3 MindMap (com.igormaznitsa.mindmap.model.MindMap)2 MindMapPanelConfig (com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)2 MindMapController (com.igormaznitsa.mindmap.model.MindMapController)1 Topic (com.igormaznitsa.mindmap.model.Topic)1 DialogProvider (com.igormaznitsa.mindmap.swing.panel.DialogProvider)1 MindMapPanel (com.igormaznitsa.mindmap.swing.panel.MindMapPanel)1 MindMapPanelController (com.igormaznitsa.mindmap.swing.panel.MindMapPanelController)1 AbstractElement (com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement)1 ElementPart (com.igormaznitsa.mindmap.swing.panel.ui.ElementPart)1 MMGraphics (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)1 Component (java.awt.Component)1 Point (java.awt.Point)1 BufferedImage (java.awt.image.BufferedImage)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1