Create a Runnable that will do all of the code generation/munging you want to do. Pass it to BaseDocument.runAtomic() (the Document in the editor should be an instance of BaseDocument - it will be for Java files and most other things).

Source: NetBeans FAQ