This is a partial (about half) fix for bug 24192. Actually this fixes
all of bug 24192, but all the implementations in "insert" functions
still need to be mirrored in the "remove" functions. That will be done
tomorrow.
The testcase for that bug report now works much better, you can find it
at:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24192
Once this is fully and properly implemented, View classes can override
insertUpdate and they'll be notified when text is inserted into
Documents and can make necessary adjustments. This will allow better
caching for things like preferred sizes because they can be calculated
on the fly as text is inserted or removed. This will, for instance,
improve the terrible performance reported in bug 24152.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24152
2005-10-04 Anthony Balkissoon <[EMAIL PROTECTED]>
* javax/swing/text/AbstractDocument.java:
(insertString): If inserting a string into the Content returns an
UndoableEdit, then add an ElementEdit to the DocumentEvent before
firing.
(remove): Don't fire a removeUpdate unless some content was actually
removed.
* javax/swing/text/GapContent.java:
(UndoInsertString): New class to implement UndoableEdit functions.
(insertString): Return an UndoableEdit instead of null. Also use
locally calculated length of String rather than calculating again.
* javax/swing/text/JTextComponent.java:
(setText): If the Document is an AbstractDocument this should pass
through AbstractDocument.replace rather than calling remove and insert.
* javax/swing/text/PlainView.java:
(determineMaxLength): Keep track of which line was the longest as well
as the length of it. We'll need this to know when the longest line is
removed and we need to redetermine the longest line.
--Tony
Index: javax/swing/text/AbstractDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/AbstractDocument.java,v
retrieving revision 1.30
diff -u -r1.30 AbstractDocument.java
--- javax/swing/text/AbstractDocument.java 28 Sep 2005 20:11:10 -0000 1.30
+++ javax/swing/text/AbstractDocument.java 4 Oct 2005 19:21:19 -0000
@@ -519,8 +519,30 @@
DefaultDocumentEvent event =
new DefaultDocumentEvent(offset, text.length(),
DocumentEvent.EventType.INSERT);
- content.insertString(offset, text);
+
+ UndoableEdit temp = content.insertString(offset, text);
+ GapContent.UndoInsertString changes = null;
+ if (content instanceof GapContent)
+ changes = (GapContent.UndoInsertString) temp;
insertUpdate(event, attributes);
+
+ if (changes != null)
+ {
+ // We need to add an ElementChange to our DocumentEvent
+ // so let's set up the parameters
+ Element root = getDefaultRootElement();
+ int start = root.getElementIndex(changes.where);
+ int end = root.getElementIndex(changes.where+changes.length);
+
+ Element[] removed = new Element[1];
+ removed[0] = root;
+ Element[] added = new Element[end - start + 1];
+ for (int i = start; i <= end; i++)
+ added[i - start] = root.getElement(i);
+
+ ElementEdit edit = new ElementEdit(root, root.getElementIndex(changes.where), removed, added);
+ event.addEdit(edit);
+ }
fireInsertUpdate(event);
}
@@ -595,9 +617,11 @@
new DefaultDocumentEvent(offset, length,
DocumentEvent.EventType.REMOVE);
removeUpdate(event);
+ boolean shouldFire = content.getString(offset, length).length() != 0;
content.remove(offset, length);
postRemoveUpdate(event);
- fireRemoveUpdate(event);
+ if (shouldFire)
+ fireRemoveUpdate(event);
}
/**
@@ -1775,7 +1799,7 @@
return (DocumentEvent.ElementChange) changes.get(elem);
}
}
-
+
/**
* An implementation of [EMAIL PROTECTED] DocumentEvent.ElementChange} to be added
* to [EMAIL PROTECTED] DefaultDocumentEvent}s.
Index: javax/swing/text/GapContent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GapContent.java,v
retrieving revision 1.28
diff -u -r1.28 GapContent.java
--- javax/swing/text/GapContent.java 30 Sep 2005 14:38:02 -0000 1.28
+++ javax/swing/text/GapContent.java 4 Oct 2005 19:21:19 -0000
@@ -44,6 +44,9 @@
import java.util.ListIterator;
import java.util.Vector;
+import javax.swing.undo.AbstractUndoableEdit;
+import javax.swing.undo.CannotRedoException;
+import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;
/**
@@ -126,6 +129,45 @@
}
}
+ class UndoInsertString extends AbstractUndoableEdit
+ {
+ public int where, length;
+ String text;
+ public UndoInsertString(int start, int len)
+ {
+ where = start;
+ length = len;
+ }
+
+ public void undo () throws CannotUndoException
+ {
+ super.undo();
+ try
+ {
+ text = getString(where, length);
+ remove(where, length);
+ }
+ catch (BadLocationException ble)
+ {
+ throw new CannotUndoException();
+ }
+ }
+
+ public void redo () throws CannotUndoException
+ {
+ super.redo();
+ try
+ {
+ insertString(where, text);
+ }
+ catch (BadLocationException ble)
+ {
+ throw new CannotRedoException();
+ }
+ }
+
+ }
+
/** The serialization UID (compatible with JDK1.5). */
private static final long serialVersionUID = -6226052713477823730L;
@@ -234,9 +276,9 @@
throw new BadLocationException("the where argument cannot be greater"
+ " than the content length", where);
- replace(where, 0, str.toCharArray(), str.length());
+ replace(where, 0, str.toCharArray(), strLen);
- return null;
+ return new UndoInsertString(where, strLen);
}
/**
Index: javax/swing/text/JTextComponent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/JTextComponent.java,v
retrieving revision 1.39
diff -u -r1.39 JTextComponent.java
--- javax/swing/text/JTextComponent.java 4 Oct 2005 15:10:54 -0000 1.39
+++ javax/swing/text/JTextComponent.java 4 Oct 2005 19:21:20 -0000
@@ -1018,8 +1018,13 @@
{
try
{
- doc.remove(0, doc.getLength());
- doc.insertString(0, text, null);
+ if (doc instanceof AbstractDocument)
+ ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
+ else
+ {
+ doc.remove(0, doc.getLength());
+ doc.insertString(0, text, null);
+ }
}
catch (BadLocationException e)
{
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/PlainView.java,v
retrieving revision 1.13
diff -u -r1.13 PlainView.java
--- javax/swing/text/PlainView.java 3 Oct 2005 20:02:57 -0000 1.13
+++ javax/swing/text/PlainView.java 4 Oct 2005 19:21:20 -0000
@@ -62,6 +62,9 @@
/** The length of the longest line in the Document **/
float maxLineLength = -1;
+ /** The longest line in the Document **/
+ Element longestLine = null;
+
protected FontMetrics metrics;
public PlainView(Element elem)
@@ -237,7 +240,11 @@
{
}
int width = metrics.charsWidth(seg.array, seg.offset, seg.count);
- span = Math.max(span, width);
+ if (width > span)
+ {
+ longestLine = child;
+ span = width;
+ }
}
maxLineLength = span;
return maxLineLength;
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches