Search in sources :

Example 6 with MMapURI

use of com.igormaznitsa.mindmap.model.MMapURI 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 7 with MMapURI

use of com.igormaznitsa.mindmap.model.MMapURI 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 8 with MMapURI

use of com.igormaznitsa.mindmap.model.MMapURI 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)

Example 9 with MMapURI

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

the class MoveFileActionPlugin method processFile.

@Override
protected Problem processFile(final Project project, final int level, final File projectFolder, final FileObject fileObject) {
    final MMapURI fileAsURI = MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null);
    final Lookup targetLookup = this.refactoring.getTarget();
    if (targetLookup == null) {
        return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantFindLookup"));
    }
    final URL targetURL = targetLookup.lookup(URL.class);
    if (targetURL != null) {
        try {
            URI baseURI = targetURL.toURI();
            if (baseURI.isAbsolute()) {
                final URI projectURI = Utilities.toURI(projectFolder);
                baseURI = projectURI.relativize(baseURI);
            }
            final MMapURI newFileAsURI = MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null).replaceBaseInPath(true, baseURI, level);
            for (final FileObject mmap : allMapsInProject(project)) {
                try {
                    if (doesMindMapContainFileLink(project, mmap, fileAsURI)) {
                        final MoveElement element = new MoveElement(new MindMapLink(mmap), projectFolder, MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null));
                        element.setTarget(newFileAsURI);
                        addElement(element);
                    }
                } catch (Exception ex) {
                    ErrorManager.getDefault().notify(ex);
                    return new Problem(true, BUNDLE.getString("Refactoring.CantProcessMindMap"));
                }
            }
        } catch (URISyntaxException ex) {
            // NOI18N
            LOGGER.error("Can't make new file uri for " + fileObject.getPath(), ex);
            // NOI18N
            return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantMakeURIForFile"));
        }
        return null;
    } else {
        return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantFindURL"));
    }
}
Also used : Lookup(org.openide.util.Lookup) Problem(org.netbeans.modules.refactoring.api.Problem) FileObject(org.openide.filesystems.FileObject) URISyntaxException(java.net.URISyntaxException) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) URI(java.net.URI) URL(java.net.URL) MindMapLink(com.igormaznitsa.nbmindmap.nb.refactoring.MindMapLink) URISyntaxException(java.net.URISyntaxException)

Example 10 with MMapURI

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

the class RenameFileActionPlugin method processFile.

@Override
protected Problem processFile(final Project project, final int level, final File projectFolder, final FileObject fileObject) {
    final MMapURI fileAsURI = MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null);
    String newFileName = this.refactoring.getNewName();
    if (newFileName == null) {
        return new Problem(false, "Detected null as new file name for rename refactoring action");
    }
    if (level == 0 && !fileObject.isFolder()) {
        final String ext = FilenameUtils.getExtension(fileObject.getNameExt());
        if (!ext.isEmpty()) {
            if (!newFileName.toLowerCase(Locale.ENGLISH).endsWith('.' + ext)) {
                newFileName += '.' + ext;
            }
        }
    } else {
        newFileName = newFileName.replace('.', '/');
    }
    final MMapURI newFileAsURI;
    try {
        if (level == 0) {
            newFileAsURI = MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null).replaceName(newFileName);
        } else {
            newFileAsURI = MMapURI.makeFromFilePath(projectFolder, replaceNameInPath(level, fileObject.getPath(), newFileName), null);
        }
    } catch (URISyntaxException ex) {
        // NOI18N
        LOGGER.error("Can't make new file uri for " + fileObject.getPath(), ex);
        return new Problem(true, BUNDLE.getString("Refactoring.CantMakeURI"));
    }
    for (final FileObject mmap : allMapsInProject(project)) {
        if (isCanceled()) {
            break;
        }
        try {
            if (doesMindMapContainFileLink(project, mmap, fileAsURI)) {
                final RenameElement element = new RenameElement(new MindMapLink(mmap), projectFolder, MMapURI.makeFromFilePath(projectFolder, fileObject.getPath(), null));
                element.setNewFile(newFileAsURI);
                addElement(element);
            }
        } catch (Exception ex) {
            ErrorManager.getDefault().notify(ex);
            return new Problem(true, BUNDLE.getString("Refactoring.CantProcessMindMap"));
        }
    }
    return null;
}
Also used : Problem(org.netbeans.modules.refactoring.api.Problem) URISyntaxException(java.net.URISyntaxException) FileObject(org.openide.filesystems.FileObject) MMapURI(com.igormaznitsa.mindmap.model.MMapURI) MindMapLink(com.igormaznitsa.nbmindmap.nb.refactoring.MindMapLink) URISyntaxException(java.net.URISyntaxException)

Aggregations

MMapURI (com.igormaznitsa.mindmap.model.MMapURI)12 File (java.io.File)6 URISyntaxException (java.net.URISyntaxException)5 MindMap (com.igormaznitsa.mindmap.model.MindMap)4 ArrayList (java.util.ArrayList)4 Nonnull (javax.annotation.Nonnull)4 MustNotContainNull (com.igormaznitsa.meta.annotation.MustNotContainNull)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 URI (java.net.URI)3 Nullable (javax.annotation.Nullable)3 ExtraFile (com.igormaznitsa.mindmap.model.ExtraFile)2 Topic (com.igormaznitsa.mindmap.model.Topic)2 MindMapLink (com.igormaznitsa.nbmindmap.nb.refactoring.MindMapLink)2 PsiFile (com.intellij.psi.PsiFile)2 Dimension (java.awt.Dimension)2 Problem (org.netbeans.modules.refactoring.api.Problem)2 FileObject (org.openide.filesystems.FileObject)2 PsiExtraFile (com.igormaznitsa.ideamindmap.lang.psi.PsiExtraFile)1 UriEditPanel (com.igormaznitsa.ideamindmap.swing.UriEditPanel)1