Search in sources :

Example 11 with MindMap

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

the class RefactoringUtils method replaceMMUriToNewFile.

@Nonnull
public static String replaceMMUriToNewFile(@Nonnull final PsiExtraFile mindMapFile, @Nonnull final MMapURI oldFile, @Nonnull final MMapURI newFile) throws IOException {
    final File projectFolder = IdeaUtils.findProjectFolder(mindMapFile);
    if (projectFolder == null)
        throw new NullPointerException("Project folder is not found for " + mindMapFile);
    final MindMap parsedMap = new MindMap(null, new StringReader(mindMapFile.getContainingFile().getText()));
    parsedMap.replaceAllLinksToFile(projectFolder, oldFile, newFile);
    return parsedMap.write(new StringWriter(16384)).toString();
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) PsiExtraFile(com.igormaznitsa.ideamindmap.lang.psi.PsiExtraFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Nonnull(javax.annotation.Nonnull)

Example 12 with MindMap

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

the class MainFrame method createMindMapFile.

@Nullable
@Override
public File createMindMapFile(@Nullable final File folder) {
    final JFileChooser chooser = new JFileChooser(folder);
    chooser.setDialogTitle("Create new Mind Map");
    chooser.setFileFilter(MMDEditor.MMD_FILE_FILTER);
    chooser.setMultiSelectionEnabled(false);
    chooser.setApproveButtonText("Create");
    File result = null;
    if (chooser.showSaveDialog(Main.getApplicationFrame()) == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        if (!file.getName().endsWith(".mmd")) {
            // NOI18N
            // NOI18N
            file = new File(file.getAbsolutePath() + ".mmd");
        }
        if (file.exists()) {
            DialogProviderManager.getInstance().getDialogProvider().msgError(null, "File '" + file + "' already exists!");
        } else {
            try {
                final MindMap mindMap = new MindMap(null, true);
                final String text = mindMap.write(new StringWriter()).toString();
                SystemUtils.saveUTFText(file, text);
                result = file;
            } catch (IOException ex) {
                DialogProviderManager.getInstance().getDialogProvider().msgError(null, "Can't save mind map into file '" + file.getName() + "'");
            }
        }
    }
    return result;
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) StringWriter(java.io.StringWriter) IOException(java.io.IOException) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 13 with MindMap

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

the class FindUsagesPanel method startSearchThread.

private void startSearchThread(@Nonnull @MustNotContainNull final List<NodeProject> scope, @Nonnull final NodeFileOrFolder itemToFind) {
    int size = 0;
    for (final NodeProject p : scope) {
        size += p.size();
    }
    final File nodeFileToSearch = itemToFind.makeFileForNode();
    if (nodeFileToSearch == null) {
        safeSetProgressValue(Integer.MAX_VALUE);
    } else {
        final Runnable runnable = new Runnable() {

            int value = 0;

            private void processFile(final NodeFileOrFolder file) {
                value++;
                final File f = file.makeFileForNode();
                final NodeProject project = file.findProject();
                if (project != null) {
                    final String extension = FilenameUtils.getExtension(f.getName()).toLowerCase(Locale.ENGLISH);
                    if ("mmd".equals(extension)) {
                        // NOI18N
                        Reader reader = null;
                        try {
                            // NOI18N
                            reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
                            final MindMap map = new MindMap(null, reader);
                            if (!MapUtils.findTopicsRelatedToFile(project.getFolder(), nodeFileToSearch, map).isEmpty()) {
                                addFileIntoList(file);
                            }
                        } catch (Exception ex) {
                            // NOI18N
                            LOGGER.error("Can't parse map", ex);
                        } finally {
                            IOUtils.closeQuietly(reader);
                        }
                    } else if (findEverywhere) {
                        try {
                            // NOI18N
                            final LineIterator lineIterator = org.apache.commons.io.FileUtils.lineIterator(f, "UTF-8");
                            try {
                                while (lineIterator.hasNext()) {
                                    if (Thread.currentThread().isInterrupted()) {
                                        return;
                                    }
                                    final String lineFromFile = lineIterator.nextLine();
                                    if (lineFromFile.contains(fullNormalizedPath)) {
                                        addFileIntoList(file);
                                        break;
                                    }
                                }
                            } finally {
                                LineIterator.closeQuietly(lineIterator);
                            }
                        } catch (Exception ex) {
                            // NOI18N
                            LOGGER.error("Error during text search in file : " + f);
                        }
                    }
                }
                if (!Thread.currentThread().isInterrupted()) {
                    safeSetProgressValue(value);
                }
            }

            private void processFolder(final NodeFileOrFolder folder) {
                value++;
                for (final NodeFileOrFolder f : folder) {
                    if (f.isLeaf()) {
                        processFile(f);
                    } else {
                        processFolder(f);
                    }
                }
                if (!Thread.currentThread().isInterrupted()) {
                    safeSetProgressValue(value);
                }
            }

            @Override
            public void run() {
                for (final NodeProject p : scope) {
                    for (final NodeFileOrFolder f : p) {
                        if (Thread.currentThread().isInterrupted()) {
                            return;
                        }
                        if (f.isLeaf()) {
                            processFile(f);
                        } else {
                            processFolder(f);
                        }
                    }
                }
                safeSetProgressValue(Integer.MAX_VALUE);
            }
        };
        // NOI18N
        final Thread thread = new Thread(runnable, "SciaRetoSearchUsage");
        thread.setDaemon(true);
        final Thread oldThread = this.searchingThread.getAndSet(thread);
        if (oldThread != null) {
            oldThread.interrupt();
            try {
                oldThread.join(1000L);
            } catch (InterruptedException ex) {
                // NOI18N
                LOGGER.error("Exception during waiting of search thread interruption", ex);
            }
        }
        this.progressBarSearch.setMinimum(0);
        this.progressBarSearch.setMaximum(size);
        this.progressBarSearch.setValue(0);
        thread.start();
    }
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) InputStreamReader(java.io.InputStreamReader) NodeFileOrFolder(com.igormaznitsa.sciareto.ui.tree.NodeFileOrFolder) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) LineIterator(org.apache.commons.io.LineIterator) FileInputStream(java.io.FileInputStream) NodeProject(com.igormaznitsa.sciareto.ui.tree.NodeProject) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 14 with MindMap

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

the class NodeProject method findAffectedFiles.

@Nonnull
@MustNotContainNull
public List<File> findAffectedFiles(@Nonnull final File changedFile) {
    final File baseFolder = makeFileForNode();
    final boolean folder = changedFile.isDirectory();
    final List<File> result = new ArrayList<>();
    for (final File mindMapFile : FileUtils.listFiles(baseFolder, new String[] { "mmd", "MMD" }, true)) {
        // NOI18N
        try {
            // NOI18N
            final MindMap map = new MindMap(null, new StringReader(FileUtils.readFileToString(mindMapFile, "UTF-8")));
            if (!MapUtils.findTopicsRelatedToFile(baseFolder, changedFile, map).isEmpty()) {
                result.add(mindMapFile);
            }
        } catch (IOException ex) {
            // NOI18N
            LOGGER.error("Can't process mind map file", ex);
        }
    }
    return result;
}
Also used : MindMap(com.igormaznitsa.mindmap.model.MindMap) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) File(java.io.File) Nonnull(javax.annotation.Nonnull) MustNotContainNull(com.igormaznitsa.meta.annotation.MustNotContainNull)

Example 15 with MindMap

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

the class NodeProject method deleteAllLinksToFile.

@Nonnull
@MustNotContainNull
public List<File> deleteAllLinksToFile(@Nonnull @MustNotContainNull final List<File> listOfFilesToProcess, @Nonnull final File fileToRemove) {
    final List<File> affectedFiles = new ArrayList<>();
    final File baseFolder = makeFileForNode();
    final MMapURI fileURI = new MMapURI(baseFolder, fileToRemove, 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.deleteAllLinksToFile(baseFolder, fileURI)) {
                    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