Alexandru Chirila
Alexandru Chirila

Reputation: 2352

Netbeans API: How to save a file, or all files in the current project?

I'm developing a plugin module for Netbeans using the provided API in order to provide support for a new programming language.

I've created an Action in the Context Menu, using the MIME-type defined in a File Type.

I looking for a method to save the file that's being selected. Preferably only if it's changed.

Also is it possible to save all the changes in all the files of the project?

Part of my code from the Action Listener class:

    @Override
    public void actionPerformed(ActionEvent ev) {
        FileObject f = context.getPrimaryFile();
        String path = f.getPath();     
        String name = f.getName();

        //Save file here

        ExecutionDescriptor descriptor = new ExecutionDescriptor()
                .controllable(true)
                .frontWindow(true)
                .preExecution(before)
                .postExecution(after);
        ExecutionService exeService = ExecutionService.newService(
                new ProcessLaunch(cmdLine),
                descriptor, "Run " + name);
        Future<Integer> exitCode = exeService.run();

    } 

Upvotes: 3

Views: 3487

Answers (2)

JWahle
JWahle

Reputation: 101

try this:

FileObject f = ...
DataObject data = DataObject.find(f);
EditorCookie cookie = data.getLookup().lookup(EditorCookie.class);
cookie.saveDocument();

Upvotes: 1

magnus
magnus

Reputation: 31

LifecycleManager.getDefault().saveAll();

This will save all the objects in your project.

Upvotes: 3

Related Questions