Search in sources :

Example 1 with Problem

use of org.netbeans.modules.refactoring.api.Problem in project netbeans-mmd-plugin by raydac.

the class AbstractPlugin method _processFileObject.

private Problem _processFileObject(final Project project, int level, final FileObject fileObject) {
    final Project theProject;
    if (project == null) {
        theProject = FileOwnerQuery.getOwner(fileObject);
    } else {
        theProject = project;
    }
    if (theProject == null) {
        LOGGER.warn("Request process file object without a project as the owner : " + fileObject);
        return null;
    }
    final FileObject projectDirectory = theProject.getProjectDirectory();
    if (projectDirectory == null) {
        LOGGER.warn("Request process file object in a project which doesn't have folder : " + fileObject + ", project : " + project);
        return null;
    }
    final File projectFolder = FileUtil.toFile(projectDirectory);
    Problem result = processFile(theProject, level, projectFolder, fileObject);
    level++;
    if (fileObject.isFolder()) {
        for (final FileObject fo : fileObject.getChildren()) {
            if (result != null) {
                break;
            }
            if (fo.isFolder()) {
                result = _processFileObject(theProject, level, fo);
            } else {
                result = processFile(theProject, level, projectFolder, fo);
            }
        }
    }
    return result;
}
Also used : Project(org.netbeans.api.project.Project) Problem(org.netbeans.modules.refactoring.api.Problem) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 2 with Problem

use of org.netbeans.modules.refactoring.api.Problem in project netbeans-mmd-plugin by raydac.

the class MoveFileActionPlugin method checkParameters.

@Override
public Problem checkParameters() {
    if (this.refactoring.getRefactoringSource().lookup(AbstractMMDRefactoringUI.class) != null) {
        final Lookup targetLookup = this.refactoring.getTarget();
        if (targetLookup == null) {
            return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantFindLookup"));
        }
        final URL url = targetLookup.lookup(URL.class);
        if (url == null) {
            return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantFindURL"));
        }
        final FileObject obj = URLMapper.findFileObject(url);
        if (obj == null) {
            return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.cantFindTargetFolder"));
        }
        if (!obj.isFolder()) {
            return new Problem(true, BUNDLE.getString("MoveFileActionPlugin.targetIsNotFolder"));
        }
    }
    return null;
}
Also used : AbstractMMDRefactoringUI(com.igormaznitsa.nbmindmap.nb.refactoring.gui.AbstractMMDRefactoringUI) Lookup(org.openide.util.Lookup) Problem(org.netbeans.modules.refactoring.api.Problem) FileObject(org.openide.filesystems.FileObject) URL(java.net.URL)

Example 3 with Problem

use of org.netbeans.modules.refactoring.api.Problem in project netbeans-mmd-plugin by raydac.

the class AbstractPlugin method prepare.

@Override
public final Problem prepare(final RefactoringElementsBag session) {
    if (isCanceled()) {
        return null;
    }
    final Collection<? extends FileObject> files = findFileObjectInLookup(this.refactoring.getRefactoringSource());
    fireProgressListenerStart(RenameRefactoring.PREPARE, files.size());
    Problem result = null;
    try {
        for (final FileObject fileObject : findFileObjectInLookup(this.refactoring.getRefactoringSource())) {
            if (isCanceled()) {
                return null;
            }
            if (result != null) {
                break;
            }
            final Project project = FileOwnerQuery.getOwner(fileObject);
            result = processFileObject(project, fileObject);
            fireProgressListenerStep(1);
        }
    } finally {
        synchronized (this.elements) {
            LOGGER.info("Detected " + this.elements.size() + " elements for refactoring");
            if (!isCanceled()) {
                session.addAll(refactoring, this.elements);
            }
        }
        fireProgressListenerStop();
    }
    return result;
}
Also used : Project(org.netbeans.api.project.Project) Problem(org.netbeans.modules.refactoring.api.Problem) FileObject(org.openide.filesystems.FileObject)

Example 4 with Problem

use of org.netbeans.modules.refactoring.api.Problem 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 5 with Problem

use of org.netbeans.modules.refactoring.api.Problem 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

Problem (org.netbeans.modules.refactoring.api.Problem)5 FileObject (org.openide.filesystems.FileObject)5 MMapURI (com.igormaznitsa.mindmap.model.MMapURI)2 MindMapLink (com.igormaznitsa.nbmindmap.nb.refactoring.MindMapLink)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Project (org.netbeans.api.project.Project)2 Lookup (org.openide.util.Lookup)2 AbstractMMDRefactoringUI (com.igormaznitsa.nbmindmap.nb.refactoring.gui.AbstractMMDRefactoringUI)1 File (java.io.File)1 URI (java.net.URI)1