// 
//     Licensed to the Apache Software Foundation (ASF) under one
//     or more contributor license agreements.  See the NOTICE file
//     distributed with this work for additional information
//     regarding copyright ownership.  The ASF licenses this file
//     to you under the Apache License, Version 2.0 (the
//     "License"); you may not use this file except in compliance
//     with the License.  You may obtain a copy of the License at
// 
//       http://www.apache.org/licenses/LICENSE-2.0
// 
//     Unless required by applicable law or agreed to in writing,
//     software distributed under the License is distributed on an
//     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//     KIND, either express or implied.  See the License for the
//     specific language governing permissions and limitations
//     under the License.
//

= DevFaqMultipleProgrammaticEdits
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqMultipleProgrammaticEdits
:description: Apache NetBeans wiki DevFaqMultipleProgrammaticEdits
:toc: left
:toc-title:
:syntax: true

== I want to make some programmatic changes to the edited file. How can I do it so one Undo undoes it all?

Create a `Runnable` that will do all of the code generation/munging you want to do.  Pass it to `org.openide.text.NbDocument.runAtomic(doc, runnable)`.

Example:

[source,java]
----

...
import org.openide.text.NbDocument;
import org.netbeans.api.editor.EditorRegistry;
...

public final class MyAction
    implements ActionListener
{
    private class RunnableAction
        implements Runnable
    {
        private JTextComponent ed;
        private Document doc;

        private RunnableAction( JTextComponent ed, Document doc )
        {
            this.ed = ed;
            this.doc = doc;
        }

        @Override
        public void run()
        {
            ...
        }
    }

    public void actionPerformed( ActionEvent e )
    {
        JTextComponent ed = EditorRegistry.lastFocusedComponent();
        StyledDocument doc = ( StyledDocument ) ed.getDocument();

        // Perform all of the changes atomically so that they can be undone with one undo.
        NbDocument.runAtomic( doc, new RunnableAction( ed, doc ) );
    }
}
----

Applies to: All Netbeans versions

Platforms: All

== Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.

This page was exported from link:http://wiki.netbeans.org/DevFaqMultipleProgrammaticEdits[http://wiki.netbeans.org/DevFaqMultipleProgrammaticEdits] , 
that was last modified by NetBeans user TheloniousBonk 
on 2012-03-06T22:14:01Z.


*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.
