DevFaqMultipleProgrammaticEdits

(Difference between revisions)
m (I want to make some programmatic changes to the edited file. How can I do it so one Undo undoes it all?)
 
(3 intermediate revisions not shown)
Line 3: Line 3:
Create a <tt>Runnable</tt> that will do all of the code generation/munging you want to do.  Pass it to <tt>org.openide.text.NbDocument.runAtomic(doc, runnable)</tt>.
Create a <tt>Runnable</tt> that will do all of the code generation/munging you want to do.  Pass it to <tt>org.openide.text.NbDocument.runAtomic(doc, runnable)</tt>.
-
Or <tt>BaseDocument.runAtomic()</tt> can be used (the <tt>Document</tt> in the editor should be an instance of <tt>BaseDocument</tt> - it will be for Java files and most other things).
+
 
 +
Example:
 +
<pre>...
 +
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 ) );
 +
    }
 +
}
 +
</pre>
 +
-----
 +
 
 +
Applies to: All Netbeans versions
 +
 
 +
Platforms: All

Current revision as of 22:14, 6 March 2012

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:

...
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

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo