EditorFormattingAPIUpgrade

(Difference between revisions)
(Created page with '==Migrating Modules From The Old Formatting API== __TOC__ This page describes common situations that module owners are likely to encounter when migrating their modules from the…')
Line 7: Line 7:
owner and have problems with rewriting your module and this page doesn't  
owner and have problems with rewriting your module and this page doesn't  
help you, please send a question to [mailto:dev@editor.netbeans.org dev (at) editor (dot) netbeans (dot) org].
help you, please send a question to [mailto:dev@editor.netbeans.org dev (at) editor (dot) netbeans (dot) org].
 +
 +
===Getting a formatter/indenter for a document===
 +
 +
* Typical old API usage:
 +
<pre>
 +
  BaseDocument doc = ...
 +
  Formatter f = doc.getFormatter();
 +
  f.reformat(...);
 +
</pre>
 +
 +
* New API usage:
 +
<pre>
 +
  BaseDocument doc = ...
 +
  Reformat f = Reformat.get(doc);
 +
  ...
 +
 +
  // Or when you just need to indent lines
 +
  Indent i = Indent.get(doc);
 +
  ...
 +
</pre>
 +
 +
 +
===Calculating the formatted area length===
 +
 +
* Typical old API usage:
 +
<pre>
 +
  int formatLength = doc.getFormatter().reformat(doc, startOffset, endOffset);
 +
</pre>
 +
 +
* New API usage:
 +
<pre>
 +
  final int [] formatLength = new int[1];
 +
  final Reformat reformat = Reformat.get(doc);
 +
  reformat.lock();
 +
  try {
 +
    doc.runAtomic(new Runnable() {
 +
      public void run() {
 +
        try {
 +
          Position endPos = doc.createPosition(endOffset);
 +
          reformat.reformat(startOffset, endOffset);
 +
          formatLength[0] = Math.max(0, endPos.getOffset() - startOffset);
 +
        } catch (BadLocationException ble) {
 +
          Exceptions.printStackTrace(ble);
 +
        }
 +
      }
 +
    });
 +
  } finally {
 +
    reformat.unlock();
 +
  }
 +
</pre>
 +
 +
 +
===Getting the caret offset for a new indented line===
 +
 +
* Typical old API usage:
 +
<pre>
 +
int newCaretOffset = formatter.indentNewLine(doc, offset);
 +
</pre>
 +
 +
* New API usage:
 +
<pre>
 +
  final int [] newCaretOffset = new int[1];
 +
  final Indent indenter = Indent.get(doc);
 +
  indenter.lock();
 +
  try {
 +
    doc.runAtomic(new Runnable() {
 +
      public void run() {
 +
        try {
 +
          newCaretOffset[0] = indenter.indentNewLine(offset);
 +
        } catch (BadLocationException ble) {
 +
          Exceptions.printStackTrace(ble);
 +
        }
 +
      }
 +
    });
 +
  } finally {
 +
    indenter.unlock();
 +
  }
 +
</pre>

Revision as of 12:27, 19 August 2010

Migrating Modules From The Old Formatting API

Contents


This page describes common situations that module owners are likely to encounter when migrating their modules from the old editor formatting API. If you are a module owner and have problems with rewriting your module and this page doesn't help you, please send a question to dev (at) editor (dot) netbeans (dot) org.

Getting a formatter/indenter for a document

  • Typical old API usage:
  BaseDocument doc = ...
  Formatter f = doc.getFormatter();
  f.reformat(...);
  • New API usage:
  BaseDocument doc = ...
  Reformat f = Reformat.get(doc);
  ...

  // Or when you just need to indent lines
  Indent i = Indent.get(doc);
  ...


Calculating the formatted area length

  • Typical old API usage:
  int formatLength = doc.getFormatter().reformat(doc, startOffset, endOffset);
  • New API usage:
  final int [] formatLength = new int[1];
  final Reformat reformat = Reformat.get(doc);
  reformat.lock();
  try {
    doc.runAtomic(new Runnable() {
      public void run() {
        try {
          Position endPos = doc.createPosition(endOffset);
          reformat.reformat(startOffset, endOffset);
          formatLength[0] = Math.max(0, endPos.getOffset() - startOffset);
        } catch (BadLocationException ble) {
          Exceptions.printStackTrace(ble);
        }
      }
    });
  } finally {
    reformat.unlock();
  }


Getting the caret offset for a new indented line

  • Typical old API usage:
int newCaretOffset = formatter.indentNewLine(doc, offset);
  • New API usage:
  final int [] newCaretOffset = new int[1];
  final Indent indenter = Indent.get(doc);
  indenter.lock();
  try {
    doc.runAtomic(new Runnable() {
      public void run() {
        try {
          newCaretOffset[0] = indenter.indentNewLine(offset);
        } catch (BadLocationException ble) {
          Exceptions.printStackTrace(ble);
        }
      }
    });
  } finally {
    indenter.unlock();
  }
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