Format Files While Starting NetBeans IDE (Part 2)

Here's the improved code over part 1. Now it also works for Java files, which wasn't the case in part 1. The key is to use EditorCookie.openDocument().

As NetBeans Java editor engineer Jan Lahoda told me: "EditorCookie.openDocument won't open the editor, it will only load the file's content into a Document, with correct encoding, setting-up all needed properties, etc. EditorCookie.saveDocument should save the document."

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.modules.editor.indent.api.Reformat;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.Option;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.text.NbDocument;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = OptionProcessor.class)
public class FileFormattingCommandLineParser extends OptionProcessor {
    private static Option filesToBeFormatted =
            Option.requiredArgument(Option.NO_SHORT_NAME, "filesToBeFormatted");
    @Override
    protected Set<Option> getOptions() {
        return Collections.singleton(filesToBeFormatted);
    }
    @Override
    protected void process(Env env, Map<Option, String[]> value) throws CommandException {
        String[] args = (String[]) value.get(filesToBeFormatted);
        String[] allFiles = args[0].split(File.pathSeparator);
        for (final String stringOfFileToBeFormatted : allFiles) {
            File oneFileToBeFormatted = new File(stringOfFileToBeFormatted);
            try {
                FileObject fo = FileUtil.toFileObject(oneFileToBeFormatted);
                DataObject dobj = DataObject.find(fo);
                EditorCookie ec = dobj.getCookie(EditorCookie.class);
                final StyledDocument doc = ec.openDocument();
                final Reformat rf = Reformat.get(doc);
                rf.lock();
                try {
                    NbDocument.runAtomicAsUser(doc, new Runnable() {
                        @Override
                        public void run() {
                            try {
                                rf.reformat(0, doc.getLength());
                            } catch (BadLocationException ex) {
                            }
                        }
                    });
                } finally {
                    rf.unlock();
                }
                ec.saveDocument();
            } catch (BadLocationException ex) {
            } catch (IOException ex) {
            }
        }
    }
}
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today