| Constructor: |
public InsertHTMLTextAction(String name,
String html,
HTML.Tag parentTag,
HTML.Tag addTag) {
this(name, html, parentTag, addTag, null, null);
}
|
public InsertHTMLTextAction(String name,
String html,
HTML.Tag parentTag,
HTML.Tag addTag,
HTML.Tag alternateParentTag,
HTML.Tag alternateAddTag) {
this(name, html, parentTag, addTag, alternateParentTag,
alternateAddTag, true);
}
|
InsertHTMLTextAction(String name,
String html,
HTML.Tag parentTag,
HTML.Tag addTag,
HTML.Tag alternateParentTag,
HTML.Tag alternateAddTag,
boolean adjustSelection) {
super(name);
this.html = html;
this.parentTag = parentTag;
this.addTag = addTag;
this.alternateParentTag = alternateParentTag;
this.alternateAddTag = alternateAddTag;
this.adjustSelection = adjustSelection;
}
|
| Method from javax.swing.text.html.HTMLEditorKit$InsertHTMLTextAction Detail: |
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor != null) {
HTMLDocument doc = getHTMLDocument(editor);
int offset = editor.getSelectionStart();
int length = doc.getLength();
boolean inserted;
// Try first choice
if (!insertIntoTag(editor, doc, offset, parentTag, addTag) &&
alternateParentTag != null) {
// Then alternate.
inserted = insertIntoTag(editor, doc, offset,
alternateParentTag,
alternateAddTag);
}
else {
inserted = true;
}
if (adjustSelection && inserted) {
adjustSelection(editor, doc, offset, length);
}
}
}
Inserts the HTML into the document. |
void adjustSelection(JEditorPane pane,
HTMLDocument doc,
int startOffset,
int oldLength) {
int newLength = doc.getLength();
if (newLength != oldLength && startOffset < newLength) {
if (startOffset > 0) {
String text;
try {
text = doc.getText(startOffset - 1, 1);
} catch (BadLocationException ble) {
text = null;
}
if (text != null && text.length() > 0 &&
text.charAt(0) == '\n") {
pane.select(startOffset, startOffset);
}
else {
pane.select(startOffset + 1, startOffset + 1);
}
}
else {
pane.select(1, 1);
}
}
}
Called after an insertion to adjust the selection. |
protected void insertAtBoundary(JEditorPane editor,
HTMLDocument doc,
int offset,
Element insertElement,
String html,
HTML.Tag parentTag,
HTML.Tag addTag) {
insertAtBoundry(editor, doc, offset, insertElement, html,
parentTag, addTag);
}
This is invoked when inserting at a boundary. It determines
the number of pops, and then the number of pushes that need
to be performed, and then invokes insertHTML. |
protected void insertAtBoundry(JEditorPane editor,
HTMLDocument doc,
int offset,
Element insertElement,
String html,
HTML.Tag parentTag,
HTML.Tag addTag) {
// Find the common parent.
Element e;
Element commonParent;
boolean isFirst = (offset == 0);
if (offset > 0 || insertElement == null) {
e = doc.getDefaultRootElement();
while (e != null && e.getStartOffset() != offset &&
!e.isLeaf()) {
e = e.getElement(e.getElementIndex(offset));
}
commonParent = (e != null) ? e.getParentElement() : null;
}
else {
// If inserting at the origin, the common parent is the
// insertElement.
commonParent = insertElement;
}
if (commonParent != null) {
// Determine how many pops to do.
int pops = 0;
int pushes = 0;
if (isFirst && insertElement != null) {
e = commonParent;
while (e != null && !e.isLeaf()) {
e = e.getElement(e.getElementIndex(offset));
pops++;
}
}
else {
e = commonParent;
offset--;
while (e != null && !e.isLeaf()) {
e = e.getElement(e.getElementIndex(offset));
pops++;
}
// And how many pushes
e = commonParent;
offset++;
while (e != null && e != insertElement) {
e = e.getElement(e.getElementIndex(offset));
pushes++;
}
}
pops = Math.max(0, pops - 1);
// And insert!
insertHTML(editor, doc, offset, html, pops, pushes, addTag);
}
} Deprecated! As - of Java 2 platform v1.3, use insertAtBoundary
This is invoked when inserting at a boundary. It determines
the number of pops, and then the number of pushes that need
to be performed, and then invokes insertHTML. |
protected void insertHTML(JEditorPane editor,
HTMLDocument doc,
int offset,
String html,
int popDepth,
int pushDepth,
HTML.Tag addTag) {
try {
getHTMLEditorKit(editor).insertHTML(doc, offset, html,
popDepth, pushDepth,
addTag);
} catch (IOException ioe) {
throw new RuntimeException("Unable to insert: " + ioe);
} catch (BadLocationException ble) {
throw new RuntimeException("Unable to insert: " + ble);
}
}
A cover for HTMLEditorKit.insertHTML. If an exception it
thrown it is wrapped in a RuntimeException and thrown. |
boolean insertIntoTag(JEditorPane editor,
HTMLDocument doc,
int offset,
HTML.Tag tag,
HTML.Tag addTag) {
Element e = findElementMatchingTag(doc, offset, tag);
if (e != null && e.getStartOffset() == offset) {
insertAtBoundary(editor, doc, offset, e, html,
tag, addTag);
return true;
}
else if (offset > 0) {
int depth = elementCountToTag(doc, offset - 1, tag);
if (depth != -1) {
insertHTML(editor, doc, offset, html, depth, 0, addTag);
return true;
}
}
return false;
}
If there is an Element with name tag at
offset, this will invoke either insertAtBoundary
or insertHTML. This returns true if there is
a match, and one of the inserts is invoked. |