Search in sources :

Example 6 with AbstractElement

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

the class MMDGraphEditor method drop.

@Override
public void drop(final DropTargetDropEvent dtde) {
    DataObject detectedDataObject = null;
    File detectedFileObject = null;
    String detectedNote = null;
    URI decodedLink = null;
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    try {
        final Object dataObjOrFile = extractDataObjectOrFileFromDnD(dtde);
        if (dataObjOrFile instanceof DataObject) {
            detectedDataObject = (DataObject) dataObjOrFile;
        } else {
            detectedFileObject = (File) dataObjOrFile;
        }
        final String detectedLink = DnDUtils.extractDropLink(dtde);
        detectedNote = DnDUtils.extractDropNote(dtde);
        if (detectedLink != null) {
            try {
                decodedLink = new URI(detectedLink);
            } catch (final URISyntaxException ex) {
                decodedLink = null;
            }
        }
        dtde.dropComplete(true);
    } catch (final Exception ex) {
        LOGGER.error("Can't extract data from DnD", ex);
        dtde.dropComplete(false);
    }
    final AbstractElement element = this.mindMapPanel.findTopicUnderPoint(dtde.getLocation());
    if (detectedDataObject != null) {
        addDataObjectToElement(detectedDataObject, element);
    } else if (detectedFileObject != null) {
        decodedLink = DnDUtils.extractUrlLinkFromFile(detectedFileObject);
        if (decodedLink != null) {
            addURItoElement(decodedLink, element);
        } else {
            addFileToElement(detectedFileObject, element);
        }
    } else if (decodedLink != null) {
        addURItoElement(decodedLink, element);
    } else if (detectedNote != null) {
        addNoteToElement(detectedNote, element);
    }
}
Also used : DataObject(org.openide.loaders.DataObject) AbstractElement(com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement) DataObject(org.openide.loaders.DataObject) FileObject(org.openide.filesystems.FileObject) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 7 with AbstractElement

use of com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement 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 8 with AbstractElement

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

the class MindMapDocumentEditor method topicToCentre.

public void topicToCentre(@Nullable Topic topic) {
    assertSwingDispatchThread();
    if (topic != null) {
        AbstractElement element = (AbstractElement) topic.getPayload();
        if (element == null && this.mindMapPanel.updateElementsAndSizeForCurrentGraphics(true, true)) {
            topic = this.mindMapPanel.getModel().findForPositionPath(topic.getPositionPath());
            if (topic != null) {
                element = (AbstractElement) topic.getPayload();
                this.mainScrollPane.getViewport().doLayout();
            }
        }
        if (element != null) {
            final Rectangle2D bounds = element.getBounds();
            final Dimension viewPortSize = mainScrollPane.getViewport().getExtentSize();
            final int x = Math.max(0, (int) Math.round(bounds.getX() - (viewPortSize.getWidth() - bounds.getWidth()) / 2));
            final int y = Math.max(0, (int) Math.round(bounds.getY() - (viewPortSize.getHeight() - bounds.getHeight()) / 2));
            mainScrollPane.getViewport().setViewPosition(new Point(x, y));
        }
    }
}
Also used : AbstractElement(com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement) Rectangle2D(java.awt.geom.Rectangle2D)

Aggregations

AbstractElement (com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement)8 Rectangle2D (java.awt.geom.Rectangle2D)4 File (java.io.File)4 URISyntaxException (java.net.URISyntaxException)4 IOException (java.io.IOException)3 URI (java.net.URI)3 Nonnull (javax.annotation.Nonnull)2 MindMap (com.igormaznitsa.mindmap.model.MindMap)1 MindMapController (com.igormaznitsa.mindmap.model.MindMapController)1 Topic (com.igormaznitsa.mindmap.model.Topic)1 HasOptions (com.igormaznitsa.mindmap.plugins.api.HasOptions)1 MMDPrint (com.igormaznitsa.mindmap.print.MMDPrint)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 ElementPart (com.igormaznitsa.mindmap.swing.panel.ui.ElementPart)1 MMGraphics (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)1 MMGraphics2DWrapper (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Component (java.awt.Component)1