Friday July 27, 2007
Indentation Code Correction for Page 350
Page 350 of Rich Client Programming: Plugging into the NetBeans Platform, is problematic because it uses the BaseDocument class, which will be deprecated soon, as mentioned in Errata for "Rich Client Programming: Plugging into the NetBeans Platform". The code concerns the indentation of code that is dropped from the Palette into an editor. Today I received from Jarda a code snippet that replaces and corrects the problematic code. So, on page 350, the complete JavaSourceFilePaletteUtilities class should be as follows, although note that I have included some commented-out code lines, which are some of the original lines in the code that are now obsolete:
public class JavaSourceFilePaletteUtilities {
public static void insert(final String s, final JTextComponent target) throws BadLocationException {
// Not needed, because string will never be null:
// if (s == null) {
// s = "";
// }
//Use StyledDocument, because needed
//by NbDocument.runAtomicAsUser, later:
final StyledDocument doc = (StyledDocument) target.getDocument();
if (doc == null) {
return;
}
// No more BaseDocument:
// if (doc instanceof BaseDocument) {
// ((BaseDocument) doc).atomicLock();
// }
// int start = insert(s, target, doc);
// if (reformat && start >= 0 && doc instanceof BaseDocument) {
// int end = start + s.length();
// Formatter f = ((BaseDocument) doc).getFormatter();
// f.reformat((BaseDocument) doc, start, end);
// }
class InsertFormatedText implements Runnable {
public void run() {
try {
insertFormated(s, target, doc);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}
InsertFormatedText insert = new InsertFormatedText();
// No more BaseDocument check:
// if (doc instanceof BaseDocument) {
// ((BaseDocument) doc).atomicUnlock();
// }
//This starts the run() in the Runnable above:
NbDocument.runAtomicAsUser(doc, insert);
}
private static int insertFormated(String s, JTextComponent target, Document doc) throws BadLocationException {
int start = -1;
try {
//Find the location of the cusor in the editor,
//and if it is a selection, remove it,
//to be replaced by the dropped item:
Caret caret = target.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);
start = caret.getDot();
//Insert the string in the document,
//using the indentation engine
//to create the correct indentation:
IndentEngine engine = IndentEngine.find(doc);
StringWriter textWriter = new StringWriter();
Writer indentWriter = engine.createWriter(doc, start, textWriter);
indentWriter.write(s);
indentWriter.close();
doc.insertString(start, textWriter.toString(), null);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (BadLocationException ble) {
Exceptions.printStackTrace(ble);
}
return start;
}
}
If you want to see the above code within the context of a sample, go here in the Plugin Portal, where I've updated the Java Source File Palette Sample to use the code above.
Jul 27 2007, 07:41:35 AM PDT Permalink


