[Patch][gui] document/content handling in javax.swing.text
Michael Koch
konqueror@gmx.de
Sun Jan 2 12:51:00 GMT 2005
Hi list,
I commited the attached patch to fix some issues with document/content
handling in javax.swing.text. The document elements should be rebuild
correctly when the content gets updated.
Michael
2005-01-02 Michael Koch <konqueror@gmx.de>
* javax/swing/text/AbstractDocument.java
(AbstractElement.getLength): Fixed off-by-one error.
(AbstractElement.children): Made abstract.
(AbstractElement.getAllowsChildren): Likewise.
(AbstractElement.getElement): Likewise.
(AbstractElement.dumpElement): New private method.
(AbstractElement.dump): New method.
(BranchElememt.getName): Fixed implementation.
(BranchElememt.toString): Likewise.
(BranchElememt.getElement): Fixed arguments.
(LeafElement.getName): Fixed implementation.
(LeafElement.toString): Likewise.
* javax/swing/text/GapContent.java
(GapContent): Put default content into buffer.
* javax/swing/text/PlainDocument.java
(reindex): Use empty attribute sets instead of null.
(createDefaultRoot): Reimplemented.
(insertUpdate): Call super method.
(removeUpdate): Likewise.
(getParagraphElement): Implemented.
-------------- next part --------------
Index: javax/swing/text/AbstractDocument.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/AbstractDocument.java,v
retrieving revision 1.3.8.20
diff -u -r1.3.8.20 AbstractDocument.java
--- javax/swing/text/AbstractDocument.java 22 Oct 2004 12:41:27 -0000 1.3.8.20
+++ javax/swing/text/AbstractDocument.java 2 Jan 2005 12:48:57 -0000
@@ -38,6 +38,7 @@
package javax.swing.text;
+import java.io.PrintStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.Dictionary;
@@ -187,7 +188,7 @@
public int getLength()
{
- return content.length();
+ return content.length() - 1;
}
public EventListener[] getListeners(Class listenerType)
@@ -428,15 +429,9 @@
// TreeNode implementation
- public Enumeration children()
- {
- return Collections.enumeration(tree_children);
- }
+ public abstract Enumeration children();
- public boolean getAllowsChildren()
- {
- return true;
- }
+ public abstract boolean getAllowsChildren();
public TreeNode getChildAt(int index)
{
@@ -553,10 +548,7 @@
return AbstractDocument.this;
}
- public Element getElement(int index)
- {
- return (Element) element_children.get(index);
- }
+ public abstract Element getElement(int index);
public String getName()
{
@@ -575,6 +567,42 @@
public abstract int getElementIndex(int offset);
public abstract int getStartOffset();
+
+ private void dumpElement(PrintStream stream, String indent, Element element)
+ {
+ System.out.println(indent + "<" + element.getName() +">");
+
+ if (element.isLeaf())
+ {
+ int start = element.getStartOffset();
+ int end = element.getEndOffset();
+ String text = "";
+ try
+ {
+ text = getContent().getString(start, end - start);
+ }
+ catch (BadLocationException e)
+ {
+ }
+ System.out.println(indent + " ["
+ + start + ","
+ + end + "]["
+ + text + "]");
+ }
+ else
+ {
+ for (int i = 0; i < element.getElementCount(); ++i)
+ dumpElement(stream, indent + " ", element.getElement(i));
+ }
+ }
+
+ public void dump(PrintStream stream, int indent)
+ {
+ String indentStr = "";
+ for (int i = 0; i < indent; ++i)
+ indentStr += " ";
+ dumpElement(stream, indentStr, this);
+ }
}
public class BranchElement extends AbstractElement
@@ -631,7 +659,7 @@
public String getName()
{
- return "AbstractDocument.BranchElement";
+ return ParagraphElementName;
}
public int getStartOffset()
@@ -671,7 +699,8 @@
public String toString()
{
- return getName() + ": " + "content";
+ return ("BranchElement(" + getName() + ") "
+ + getStartOffset() + "," + getEndOffset() + "\n");
}
}
@@ -782,7 +811,7 @@
return false;
}
- public Element getElement()
+ public Element getElement(int index)
{
return null;
}
@@ -804,7 +833,7 @@
public String getName()
{
- return "AbstractDocument.LeafElement";
+ return ContentElementName;
}
public int getStartOffset()
@@ -819,7 +848,8 @@
public String toString()
{
- return getName() + ": " + "content";
+ return ("LeafElement(" + getName() + ") "
+ + getStartOffset() + "," + getEndOffset() + "\n");
}
}
}
Index: javax/swing/text/GapContent.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/GapContent.java,v
retrieving revision 1.2.8.3
diff -u -r1.2.8.3 GapContent.java
--- javax/swing/text/GapContent.java 11 Nov 2004 07:57:10 -0000 1.2.8.3
+++ javax/swing/text/GapContent.java 2 Jan 2005 12:48:57 -0000
@@ -35,6 +35,7 @@
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
+
package javax.swing.text;
import java.io.Serializable;
@@ -57,6 +58,7 @@
public GapContent(int size)
{
+ buf.append("\n");
}
public Position createPosition(final int offset) throws BadLocationException
Index: javax/swing/text/PlainDocument.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/text/PlainDocument.java,v
retrieving revision 1.1.52.4
diff -u -r1.1.52.4 PlainDocument.java
--- javax/swing/text/PlainDocument.java 11 Nov 2004 07:57:10 -0000 1.1.52.4
+++ javax/swing/text/PlainDocument.java 2 Jan 2005 12:48:57 -0000
@@ -35,6 +35,7 @@
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
+
package javax.swing.text;
import java.util.ArrayList;
@@ -70,24 +71,23 @@
ArrayList elts = new ArrayList();
int j = 0;
- for (int i = str.indexOf('\n', 0); i != -1; i = str.indexOf('\n', i+1))
+ for (int i = str.indexOf('\n', 0); i != -1; i = str.indexOf('\n', i + 1))
{
- elts.add(createLeafElement(rootElement, null, j, i));
- j = i;
+ elts.add(createLeafElement(rootElement, SimpleAttributeSet.EMPTY, j, i + 1));
+ j = i + 1;
}
if (j < content.length())
- elts.add(createLeafElement(rootElement, null, j, content.length()));
+ elts.add(createLeafElement(rootElement, SimpleAttributeSet.EMPTY, j, content.length()));
lines = new Element[elts.size()];
for (int i = 0; i < elts.size(); ++i)
lines[i] = (Element) elts.get(i);
-
}
catch (BadLocationException e)
{
lines = new Element[1];
- lines[0] = createLeafElement(rootElement, null, 0, 1);
+ lines[0] = createLeafElement(rootElement, SimpleAttributeSet.EMPTY, 0, 1);
}
((BranchElement) rootElement).replace(0, rootElement.getElementCount(), lines);
@@ -95,19 +95,28 @@
protected AbstractDocument.AbstractElement createDefaultRoot()
{
- rootElement = createBranchElement(null, null);
- reindex();
- return (AbstractElement) rootElement;
+ BranchElement root =
+ (BranchElement) createBranchElement(null, SimpleAttributeSet.EMPTY);
+
+ Element[] array = new Element[1];
+ array[0] = createLeafElement(root, SimpleAttributeSet.EMPTY, 0, 1);
+ root.replace(0, 0, array);
+
+ return root;
}
- protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr)
+ protected void insertUpdate(DefaultDocumentEvent event, AttributeSet attributes)
{
reindex();
+
+ super.insertUpdate(event, attributes);
}
- protected void removeUpdate(DefaultDocumentEvent chng)
+ protected void removeUpdate(DefaultDocumentEvent event)
{
reindex();
+
+ super.removeUpdate(event);
}
public Element getDefaultRootElement()
@@ -117,6 +126,7 @@
public Element getParagraphElement(int pos)
{
- return null;
+ Element root = getDefaultRootElement();
+ return root.getElement(root.getElementIndex(pos));
}
}
More information about the Java-patches
mailing list