CreateFileAction problem

I've got a really annoying problem with creating new files.
So I extended CreateFileAction and rewrite the create() method. Now it looks like:

    private PsiFile createFileFromTemplate(String templName, Project project, PsiDirectory dir, String name) {
        String ext = FileUtil.getExtension(name);
        final FileTemplate templ = FileTemplateManager.getInstance().getInternalTemplate(templName);

        Properties prop = new Properties(FileTemplateManager.getInstance().getDefaultProperties());
        JavaTemplateUtil.setPackageNameAttribute(prop, dir);
        prop.setProperty(NAME_TEMPLATE_PROPERTY, name);
        String text = null;
        try {
            text = templ.getText(prop);
        } catch(Exception e) {
            e.printStackTrace();
        }
        final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(name+"."+ext, text);
        return (PsiFile)dir.add(file);
    }

    @NotNull
    @Override
    protected PsiElement[] create(String s, PsiDirectory psiDirectory) throws Exception {
        Project project = psiDirectory.getProject();

        VirtualFile vf = project.getBaseDir();

        PsiDirectory baseDir = PsiManager.getInstance(project).findDirectory(vf).findSubdirectory("src");
        PsiDirectory handlerDir = findDirectory("pl.handler", baseDir);
        PsiDirectory actionDir = findDirectory("pl.action", baseDir);
        PsiDirectory resultDir = findDirectory("pl.result", baseDir);

        return new PsiElement[]{createFileFromTemplate("Handler.java", project, handlerDir, s+"Handler"),
                                createFileFromTemplate("Action.java", project, actionDir, s+"Action"),
                                createFileFromTemplate("Result.java", project, resultDir, s+"Result")};
    }
    
    private PsiDirectory findDirectory(String path, PsiDirectory baseDir)
    {
        PsiDirectory res = baseDir;
        String[] s = StringUtils.splitPreserveAllTokens(path, ".");
        for(int i = 0; i<s.length; ++i) {
            res = res.findSubdirectory(s[i]);
            if(null == res) {
                return res;
            }
        }

        return res;
    }

If I leave code as it is above I get file names: ClassName.java.java /double extension/. If I change the line:

final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(name+"."+ext, text);

to:

final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(name, text);


IntelliJ gives me an allert that it can't find the extension so he can't create this files.

If someone could point me what am I doing wrong?

Also if someone could tell me a simplier solution for getting PsiDirectory of a package string /method findDirectory/ it would be great too x] I tried to get directory from Package but it didn't work so I wrote smth like that. It does it job but there has to be a better solution for that. x]


Generally speaking plugin has to create 3 files from 3 templates. Templates are added in the initComponent, plugin is ApplicationComponent. It is called from RightClickMenu on Project tree:  New > Create /registered in the plugin.xml/


J T

5 comments
Comment actions Permalink

Ok, i solved the problem by myself. Basically I post in the parameter filename and name of the class.

final PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(getFileName(fileName), text);


There is a small issue though. When I use the action dialog pops out and asks for the name of the class. That's ok but after I click 'Ok' pops out another window 'Register a new file type association' like if there was no extension to the newly created file... I choose 'Text file', Ok and it gives me correct files so I don't know what is going on. But I must do something wrong if that window shows. Passing file name in the wrong place or something... i don't know...

and still i've got the question about the other function to find psi directories. If anyone could point me where to look for solution it would be awesome x]

0
Comment actions Permalink

I think you don't want

FileTemplateManager.getInstance().getInternalTemplate(templName);


Instead use

FileTemplateManager.getInstance().getTemplate(templName);
0
Comment actions Permalink

yeah, i've changed this some time ago. Haven't actually sense the difference between those functions though as I recall.

Any idea on solving the problem with popping up window to select file type? x]

0
Comment actions Permalink

Why aren't you calling:

PsiFileFactory.getInstance(project).createFileFromText(name, <LanguageFileType>, text);


You might also have another problem if you load your templates without using the resource loader and just putting them in the correct directory, the doing it this way your templates are created as CustomTemplates instead of BundledTemplates. This seems to change their behavior somewhat.

If you don't need to load them yourself and just want them to appear in the existing template options tabs, then this may be the way to go. I just answered another question about this.

http://devnet.jetbrains.net/message/5316253#5316253

0
Comment actions Permalink

using createFileFromText with filetype parameter didn't make any difference. I still get this window.
But I've managed to solve it by myself. I provided my own InputValidator /CreatorValidator/ class in which i override method canClose. I've concatted to the inputString java default extension and execute method canClose from InputValidator. Now everything works perfect, but I know that there are ways to do the same thing without all that tricks. But for now it should be good enough.

0

Please sign in to leave a comment.