-
Bug
-
Resolution: Won't Fix
-
P3
-
None
-
1.2.0
-
None
-
x86
-
windows_nt
The following program, if succesful will print out nothing, otherwise an exception or errors will be printed out:
import java.io.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Bug4 {
public static void main(String[] args) {
for (int counter = 0; counter < 4; counter++) {
switch(counter) {
case 0:
test("InsertTable", HTML.Tag.TD);
break;
case 1:
test("InsertTable", HTML.Tag.TR);
break;
case 2:
test("InsertOrderedList", HTML.Tag.OL);
break;
default:
test(null, HTML.Tag.BODY);
}
}
System.exit(0);
}
public static void test(String actionName, HTML.Tag tag) {
JEditorPane pane = new JEditorPane();
// Insert the appropriate thing.
pane.setContentType("text/html");
HTMLDocument2 doc = new HTMLDocument2(((HTMLDocument)pane.
getDocument()).getStyleSheet());
pane.setDocument(doc);
if (actionName != null) {
pane.replaceSelection("A");
pane.select(1, 1);
Action[] actions = pane.getActions();
int counter = actions.length - 1;
for (; counter >= 0; counter--) {
if (actionName.equals(actions[counter].
getValue(Action.NAME))) {
break;
}
}
if (counter >= 0) {
actions[counter].actionPerformed
(new ActionEvent(pane, 0, null));
}
else {
System.out.println("No insert action!");
}
}
else {
pane.replaceSelection("AB");
}
// Find the appropriate element
Element e = pane.getDocument().getDefaultRootElement();
while (e != null && !e.isLeaf() &&
e.getAttributes().getAttribute(StyleConstants.NameAttribute) !=
tag) {
e = e.getElement(e.getElementIndex(2));
}
if (e != null && !e.isLeaf()) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, true);
doc.setElementAttributes(e, sas, false);
// Find the View, and make sure it is bold too!
View v = pane.getUI().getRootView(pane);
while (v != null && v.getElement() != e) {
if (v.getViewCount() == 0) {
v = null;
}
else {
v = v.getView(getViewIndex(v, 2));
}
}
if (v == null) {
System.out.println("Could not find VIEW!");
}
else {
if (!StyleConstants.isBold(v.getAttributes())) {
System.out.println("Should be BOLD!");
}
}
}
else {
System.out.println("Couldn't find tag!");
}
}
static public int getViewIndex(View view, int offset) {
int nchildren = view.getViewCount();
int index;
int lower = 0;
int upper = nchildren - 1;
int mid = 0;
int p0 = view.getStartOffset();
int p1;
if (nchildren == 0) {
return 0;
}
if (offset >= view.getEndOffset()) {
return nchildren - 1;
}
while (lower <= upper) {
mid = lower + ((upper - lower) / 2);
View v = view.getView(mid);
p0 = v.getStartOffset();
p1 = v.getEndOffset();
if ((offset >= p0) && (offset < p1)) {
// found the location
index = mid;
return index;
} else if (offset < p0) {
upper = mid - 1;
} else {
lower = mid + 1;
}
}
// didn't find it, but we indicate the index of where it would belong
if (offset < p0) {
index = mid;
} else {
index = mid + 1;
}
return index;
}
public static class HTMLDocument2 extends HTMLDocument {
public HTMLDocument2(StyleSheet ss) {
super(ss);
}
/**
* Sets the attributes of the passed in Element.
*/
public void setElementAttributes(Element e, AttributeSet newAttr,
boolean replace) {
writeLock();
try {
DefaultDocumentEvent changes = new DefaultDocumentEvent
(e.getStartOffset(), e.getEndOffset() -
e.getStartOffset(), DocumentEvent.EventType.CHANGE);
MutableAttributeSet attr = (MutableAttributeSet)e.
getAttributes();
changes.addEdit(new AttributeUndoableEdit
(e, newAttr.copyAttributes(), replace));
if (replace) {
attr.removeAttributes(attr);
}
attr.addAttributes(newAttr);
changes.end();
fireChangedUpdate(changes);
fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
}
finally {
writeUnlock();
}
}
}
}
import java.io.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Bug4 {
public static void main(String[] args) {
for (int counter = 0; counter < 4; counter++) {
switch(counter) {
case 0:
test("InsertTable", HTML.Tag.TD);
break;
case 1:
test("InsertTable", HTML.Tag.TR);
break;
case 2:
test("InsertOrderedList", HTML.Tag.OL);
break;
default:
test(null, HTML.Tag.BODY);
}
}
System.exit(0);
}
public static void test(String actionName, HTML.Tag tag) {
JEditorPane pane = new JEditorPane();
// Insert the appropriate thing.
pane.setContentType("text/html");
HTMLDocument2 doc = new HTMLDocument2(((HTMLDocument)pane.
getDocument()).getStyleSheet());
pane.setDocument(doc);
if (actionName != null) {
pane.replaceSelection("A");
pane.select(1, 1);
Action[] actions = pane.getActions();
int counter = actions.length - 1;
for (; counter >= 0; counter--) {
if (actionName.equals(actions[counter].
getValue(Action.NAME))) {
break;
}
}
if (counter >= 0) {
actions[counter].actionPerformed
(new ActionEvent(pane, 0, null));
}
else {
System.out.println("No insert action!");
}
}
else {
pane.replaceSelection("AB");
}
// Find the appropriate element
Element e = pane.getDocument().getDefaultRootElement();
while (e != null && !e.isLeaf() &&
e.getAttributes().getAttribute(StyleConstants.NameAttribute) !=
tag) {
e = e.getElement(e.getElementIndex(2));
}
if (e != null && !e.isLeaf()) {
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, true);
doc.setElementAttributes(e, sas, false);
// Find the View, and make sure it is bold too!
View v = pane.getUI().getRootView(pane);
while (v != null && v.getElement() != e) {
if (v.getViewCount() == 0) {
v = null;
}
else {
v = v.getView(getViewIndex(v, 2));
}
}
if (v == null) {
System.out.println("Could not find VIEW!");
}
else {
if (!StyleConstants.isBold(v.getAttributes())) {
System.out.println("Should be BOLD!");
}
}
}
else {
System.out.println("Couldn't find tag!");
}
}
static public int getViewIndex(View view, int offset) {
int nchildren = view.getViewCount();
int index;
int lower = 0;
int upper = nchildren - 1;
int mid = 0;
int p0 = view.getStartOffset();
int p1;
if (nchildren == 0) {
return 0;
}
if (offset >= view.getEndOffset()) {
return nchildren - 1;
}
while (lower <= upper) {
mid = lower + ((upper - lower) / 2);
View v = view.getView(mid);
p0 = v.getStartOffset();
p1 = v.getEndOffset();
if ((offset >= p0) && (offset < p1)) {
// found the location
index = mid;
return index;
} else if (offset < p0) {
upper = mid - 1;
} else {
lower = mid + 1;
}
}
// didn't find it, but we indicate the index of where it would belong
if (offset < p0) {
index = mid;
} else {
index = mid + 1;
}
return index;
}
public static class HTMLDocument2 extends HTMLDocument {
public HTMLDocument2(StyleSheet ss) {
super(ss);
}
/**
* Sets the attributes of the passed in Element.
*/
public void setElementAttributes(Element e, AttributeSet newAttr,
boolean replace) {
writeLock();
try {
DefaultDocumentEvent changes = new DefaultDocumentEvent
(e.getStartOffset(), e.getEndOffset() -
e.getStartOffset(), DocumentEvent.EventType.CHANGE);
MutableAttributeSet attr = (MutableAttributeSet)e.
getAttributes();
changes.addEdit(new AttributeUndoableEdit
(e, newAttr.copyAttributes(), replace));
if (replace) {
attr.removeAttributes(attr);
}
attr.addAttributes(newAttr);
changes.end();
fireChangedUpdate(changes);
fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
}
finally {
writeUnlock();
}
}
}
}