<Swing Dev> I bow before swing.text.
Paulo Levi
i30817 at gmail.com
Tue Feb 12 02:03:28 UTC 2008
Masters i beg your patience and mercy before a humble petitioner.
There are two matters - one pertains to the
HTMLEditorKit.ParserCallbackempty class, designed to be a callback to
the html parser.
I've tried to use its get HTMLCallback thus:
htmlEditor = new ParserDelegator();
StyledDocument newDoc = new DefaultStyledDocument();
HTMLCallBack c = getHtmlCallback(newDoc);
htmlEditor.parse(reader, c, ignoreCharset);
To my horror the method flush() that is supposted to be called at the end of
the parsing, is not. Only handleEndOfLineString(String s) is still faithful.
The other matter is In the DefaultStyledDocument class the bulk insert
method.
protected void insert(int offset, ElementSpec [] data) throws
BadLocationException
has method code is
if (data == null || data.length == 0) {
return;
}
try {
writeLock();
// install the content
Content c = getContent();
int n = data.length;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < n; i++) {
ElementSpec es = data[i];
if (es.getLength() > 0) {
sb.append(es.getArray(), es.getOffset(), es.getLength());
}
}
if (sb.length() == 0) {
// Nothing to insert, bail.
return;
}
UndoableEdit cEdit = c.insertString(offset, sb.toString());
// create event and build the element structure
int length = sb.length();
DefaultDocumentEvent evnt =
new DefaultDocumentEvent(offset, length,
DocumentEvent.EventType.INSERT);
evnt.addEdit(cEdit);
buffer.insert(offset, length, data, evnt);
//update bidi
super.insertUpdate(evnt, null);
// notify the listeners
evnt.end();
fireInsertUpdate(evnt);
fireUndoableEditUpdate(new UndoableEditEvent(this, evnt));
} finally {
writeUnlock();
}
Undeserving student of the Masters of Swing that i am, may i direct your
attention to the StringBuffer created in each bulk insert (that will
probably be called buffered, so more than once).
With a default size of 16.
Masters - i then call your attention to the client code arguments - my
unworthy eyes spied the array that had to be created by client code,
creation thereof that include a length argument for each and every one of
the lovely ElementSpec objects. Added to a list-like datastructure -
lovingly - by great and gentle code users.
My hands tremble as i dare to suggest something - I AM NOT WORTHY - i abase
myself before the glorious and terrible might of the magnificent JDK coders
- i offer this.
Could you possibly consider replacing StringBuffer with the spry and
luscious StringBuilder and refactoring slightly the existing code to a
method
@Override
protected void insert(int offset, ElementSpec [] data) throws
BadLocationException{
int charArraysSize = 0;
for(ElementSpec e : data)
charArraysSize += e.getLength();
insert(offset, data, charArraysSize);
}
protected void insert(int offset, ElementSpec [] data, int
charArraysSize) throws BadLocationException{
.. same except stringbuilder starts with charArraysSize
}
Also - I cringe to say it - Masters I've sinned by transplanting this code
to my custom GPL'd StyledDocument - i see you frown in terrible wisdom - you
are right Masters. The effect can't be recreated by these unskilled hands -
the super.insertUpdate(evnt, null); call won't allow it. My sin is twofold -
for i have commented it.
Forgive for i have i sinned.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20080212/5231574d/attachment.html>
More information about the swing-dev
mailing list