Search in sources :

Example 16 with MindMap

use of com.igormaznitsa.mindmap.model.MindMap in project netbeans-mmd-plugin by raydac.

the class ExplorerTree method addChildTo.

private void addChildTo(@Nonnull final NodeFileOrFolder folder, @Nullable final String extension) {
    String fileName = JOptionPane.showInputDialog(Main.getApplicationFrame(), extension == null ? "Folder name" : "File name", extension == null ? "New folder" : "New " + extension.toUpperCase(Locale.ENGLISH) + " file", JOptionPane.QUESTION_MESSAGE);
    if (fileName != null) {
        fileName = fileName.trim();
        if (NodeProjectGroup.FILE_NAME.matcher(fileName).matches()) {
            if (extension != null) {
                final String providedExtension = FilenameUtils.getExtension(fileName);
                if (!extension.equalsIgnoreCase(providedExtension)) {
                    fileName += '.' + extension;
                }
            }
            final File file = new File(folder.makeFileForNode(), fileName);
            if (file.exists()) {
                DialogProviderManager.getInstance().getDialogProvider().msgError(null, "File '" + fileName + "' already exists!");
                return;
            }
            boolean ok = false;
            if (extension == null) {
                if (!file.mkdirs()) {
                    // NOI18N
                    LOGGER.error("Can't create folder");
                    DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Can't create folder '" + fileName + "'!");
                } else {
                    ok = true;
                }
            } else {
                switch(extension) {
                    case "mmd":
                        {
                            // NOI18N
                            final MindMap model = new MindMap(null, true);
                            // NOI18N
                            model.setAttribute("showJumps", "true");
                            final Topic root = model.getRoot();
                            if (root != null) {
                                // NOI18N
                                root.setText("Root");
                            }
                            try {
                                // NOI18N
                                FileUtils.write(file, model.write(new StringWriter()).toString(), "UTF-8");
                                ok = true;
                            } catch (IOException ex) {
                                // NOI18N
                                LOGGER.error("Can't create MMD file", ex);
                                DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Can't create mind map '" + fileName + "'!");
                            }
                        }
                        break;
                    case "puml":
                        {
                            // NOI18N
                            final String nextLine = GetUtils.ensureNonNull(System.getProperty("line.separator"), "\n");
                            final String text = "@startuml" + nextLine + nextLine + "@enduml";
                            try {
                                // NOI18N
                                FileUtils.write(file, text, "UTF-8");
                                ok = true;
                            } catch (IOException ex) {
                                // NOI18N
                                LOGGER.error("Can't create PUML file", ex);
                                DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Can't create puml file '" + fileName + "'!");
                            }
                        }
                        break;
                    case "txt":
                        {
                            // NOI18N
                            try {
                                // NOI18N
                                FileUtils.write(file, "", "UTF-8");
                                ok = true;
                            } catch (IOException ex) {
                                // NOI18N
                                LOGGER.error("Can't create TXT file", ex);
                                DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Can't create txt file '" + fileName + "'!");
                            }
                        }
                        break;
                    default:
                        // NOI18N
                        throw new Error("Unexpected extension : " + extension);
                }
            }
            if (ok) {
                getCurrentGroup().addChild(folder, file);
                context.openFileAsTab(file);
                context.focusInTree(file);
            }
        } else {
            DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Illegal file name!");
        }
    }
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Topic(com.igormaznitsa.mindmap.model.Topic) ExtraFile(com.igormaznitsa.mindmap.model.ExtraFile) File(java.io.File)

Example 17 with MindMap

use of com.igormaznitsa.mindmap.model.MindMap in project netbeans-mmd-plugin by raydac.

the class MMDNavigator method updateContent.

private void updateContent() {
    if (this.treeModel != null) {
        this.treeModel.dispose();
    }
    final String text = getDocumentText();
    if (text != null) {
        try {
            this.treeModel = new SortedTreeModelWrapper(new MindMap(null, new StringReader(text)), this);
            this.mindMapTree.setModel(this.treeModel);
            Utils.foldUnfoldTree(this.mindMapTree, true);
        } catch (IOException ex) {
            // NOI18N
            LOGGER.error("Can't parse mind map text", ex);
            this.mindMapTree.setModel(null);
        }
    } else {
        this.mindMapTree.setModel(null);
    }
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) StringReader(java.io.StringReader) IOException(java.io.IOException) SortedTreeModelWrapper(com.igormaznitsa.nbmindmap.nb.swing.SortedTreeModelWrapper)

Example 18 with MindMap

use of com.igormaznitsa.mindmap.model.MindMap in project netbeans-mmd-plugin by raydac.

the class ExplorerTree method addTreeAsTopic.

private void addTreeAsTopic(@Nullable final NodeProject project, @Nonnull final NodeFileOrFolder node, @Nonnull final MMDEditor editor) {
    final File projectFolder = project == null ? null : project.getFolder();
    if (project != null) {
        if (node.findProject() != project) {
            if (!DialogProviderManager.getInstance().getDialogProvider().msgConfirmOkCancel(null, "Different projects", "Opened Map file from another project. File paths will not be relative ones.")) {
                return;
            }
        }
    }
    final List<Topic> targetTopics = new ArrayList<>(Arrays.asList(editor.getMindMapPanel().getSelectedTopics()));
    if (targetTopics.size() > 1) {
        if (!DialogProviderManager.getInstance().getDialogProvider().msgConfirmOkCancel(null, "Multiple selection detected", "New children will be generated for all focused topics.")) {
            return;
        }
    } else {
        if (targetTopics.isEmpty() && editor.getMindMapPanel().getModel().getRoot() != null) {
            if (!DialogProviderManager.getInstance().getDialogProvider().msgConfirmOkCancel(null, "No selected parent", "There is not selected topic. The Root will be used as the parent.")) {
                return;
            }
            targetTopics.add(editor.getMindMapPanel().getModel().getRoot());
        }
    }
    editor.getMindMapPanel().executeModelJobs(new MindMapPanel.ModelJob() {

        @Nonnull
        private Topic recursiveGenerateTopics(@Nullable final File projectFolder, @Nonnull final MindMap model, @Nullable final Topic parent, @Nonnull final NodeFileOrFolder node) {
            final ExtraFile fileLink = new ExtraFile(new MMapURI(projectFolder, node.makeFileForNode(), null));
            final Topic theTopic;
            if (parent == null) {
                theTopic = new Topic(model, null, node.toString(), fileLink);
            } else {
                theTopic = parent.makeChild(node.toString(), null);
                theTopic.setExtra(fileLink);
            }
            if (!node.isLeaf()) {
                final Enumeration<NodeFileOrFolder> children = node.children();
                while (children.hasMoreElements()) {
                    recursiveGenerateTopics(projectFolder, model, theTopic, children.nextElement());
                }
            }
            return theTopic;
        }

        @Override
        public boolean doChangeModel(@Nonnull final MindMap model) {
            Topic createdTopic = null;
            if (targetTopics.isEmpty()) {
                createdTopic = recursiveGenerateTopics(projectFolder, model, null, node);
            } else {
                boolean first = true;
                for (final Topic t : targetTopics) {
                    final Topic generated = recursiveGenerateTopics(projectFolder, model, t, node);
                    if (first) {
                        createdTopic = generated;
                    }
                    first = false;
                }
            }
            if (editor.getMindMapPanel().getSelectedTopics().length == 0 && createdTopic != null) {
                final Topic forfocus = createdTopic;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        editor.getMindMapPanel().focusTo(forfocus);
                    }
                });
            }
            return true;
        }
    });
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) Enumeration(java.util.Enumeration) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) MindMapPanel(com.igormaznitsa.mindmap.swing.panel.MindMapPanel) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) Topic(com.igormaznitsa.mindmap.model.Topic) ExtraFile(com.igormaznitsa.mindmap.model.ExtraFile) File(java.io.File) ExtraFile(com.igormaznitsa.mindmap.model.ExtraFile)

Example 19 with MindMap

use of com.igormaznitsa.mindmap.model.MindMap in project netbeans-mmd-plugin by raydac.

the class FileLinkGraphPanel method addMindMapAndFillByItsLinks.

@Nullable
private static FileVertex addMindMapAndFillByItsLinks(@Nullable final FileVertex parent, @Nonnull @Nullable final Graph<FileVertex, Number> graph, @Nullable final File projectFolder, @Nonnull final File mindMapFile, @Nonnull final AtomicInteger edgeCounter, @Nonnull Set<File> mapFilesInProcessing) {
    MindMap map;
    FileVertex thisVertex;
    try {
        thisVertex = new FileVertex(mindMapFile, FileVertexType.MINDMAP);
        // NOI18N
        map = new MindMap(null, new StringReader(FileUtils.readFileToString(mindMapFile, "UTF-8")));
        if (parent != null) {
            for (final MMapURI fileUri : MapUtils.extractAllFileLinks(map)) {
                if (parent.getFile().equals(fileUri.asFile(projectFolder))) {
                    graph.addEdge(edgeCounter.getAndIncrement(), thisVertex, parent, EdgeType.DIRECTED);
                    break;
                }
            }
            if (mapFilesInProcessing.contains(mindMapFile)) {
                return null;
            }
        }
    } catch (final Exception ex) {
        // NOI18N
        LOGGER.error("Can't load mind map : " + mindMapFile, ex);
        thisVertex = new FileVertex(mindMapFile, FileVertexType.UNKNOWN);
        map = null;
    }
    mapFilesInProcessing.add(mindMapFile);
    graph.addVertex(thisVertex);
    if (map != null) {
        for (final MMapURI fileUri : MapUtils.extractAllFileLinks(map)) {
            final FileVertex that;
            final File convertedFile = convertUriInFile(mindMapFile, projectFolder, fileUri);
            if (convertedFile == null) {
                that = new FileVertex(fileUri.asFile(projectFolder), FileVertexType.NOTFOUND);
            } else if (convertedFile.isDirectory()) {
                that = new FileVertex(convertedFile, FileVertexType.FOLDER);
            } else if (convertedFile.isFile()) {
                if (convertedFile.getName().endsWith(".mmd")) {
                    // NOI18N
                    if (convertedFile.equals(mindMapFile)) {
                        that = thisVertex;
                    } else {
                        that = addMindMapAndFillByItsLinks(thisVertex, graph, projectFolder, convertedFile, edgeCounter, mapFilesInProcessing);
                    }
                } else {
                    that = new FileVertex(convertedFile, FileVertexType.DOCUMENT);
                }
            } else {
                that = new FileVertex(convertedFile, convertedFile.exists() ? FileVertexType.UNKNOWN : FileVertexType.NOTFOUND);
            }
            if (that != null) {
                graph.addEdge(edgeCounter.getAndIncrement(), thisVertex, that, EdgeType.DIRECTED);
            }
        }
    }
    return thisVertex;
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) StringReader(java.io.StringReader) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 20 with MindMap

use of com.igormaznitsa.mindmap.model.MindMap in project netbeans-mmd-plugin by raydac.

the class NodeProject method replaceAllLinksToFile.

@Nonnull
@MustNotContainNull
public List<File> replaceAllLinksToFile(@Nonnull @MustNotContainNull final List<File> listOfFilesToProcess, @Nonnull final File oldFile, @Nonnull final File newFile) {
    final List<File> affectedFiles = new ArrayList<>();
    final File baseFolder = makeFileForNode();
    final MMapURI oldFileURI = new MMapURI(baseFolder, oldFile, null);
    final MMapURI newFileURI = new MMapURI(baseFolder, newFile, null);
    for (final File file : listOfFilesToProcess) {
        if (file.isFile()) {
            try {
                // NOI18N
                final MindMap map = new MindMap(null, new StringReader(FileUtils.readFileToString(file, "UTF-8")));
                if (map.replaceAllLinksToFile(baseFolder, oldFileURI, newFileURI)) {
                    SystemUtils.saveUTFText(file, map.packToString());
                    affectedFiles.add(file);
                }
            } catch (IOException ex) {
                // NOI18N
                LOGGER.error("Can't process mind map file", ex);
            }
        }
    }
    return affectedFiles;
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) File(java.io.File) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) Nonnull(javax.annotation.Nonnull) MustNotContainNull(com.igormaznitsa.meta.annotation.MustNotContainNull)

Aggregations

MindMap (com.igormaznitsa.mindmap.model.MindMap)23 File (java.io.File)13 Topic (com.igormaznitsa.mindmap.model.Topic)8 IOException (java.io.IOException)7 StringReader (java.io.StringReader)7 Nonnull (javax.annotation.Nonnull)7 Test (org.junit.Test)7 Nullable (javax.annotation.Nullable)6 ArrayList (java.util.ArrayList)5 MMapURI (com.igormaznitsa.mindmap.model.MMapURI)4 MustNotContainNull (com.igormaznitsa.meta.annotation.MustNotContainNull)3 StringWriter (java.io.StringWriter)3 ExtraFile (com.igormaznitsa.mindmap.model.ExtraFile)2 HasOptions (com.igormaznitsa.mindmap.plugins.api.HasOptions)2 MindMapPanel (com.igormaznitsa.mindmap.swing.panel.MindMapPanel)2 FileInputStream (java.io.FileInputStream)2 PsiExtraFile (com.igormaznitsa.ideamindmap.lang.psi.PsiExtraFile)1 ExtraTopic (com.igormaznitsa.mindmap.model.ExtraTopic)1 MindMapController (com.igormaznitsa.mindmap.model.MindMapController)1 DialogProvider (com.igormaznitsa.mindmap.swing.panel.DialogProvider)1