1.5 introduced update policies for DefaultCaret, this patch implements
them. It's already committed, but comments are appreciated if we have
some issues w/ putting 1.5 stuff in on the main trunk.
Basically, DefaultCaret has 3 update states - the Caret isn't always
updated when the Document changes. In the default state if the Document
changes occur on the EventDispatch thread, then the Caret is updated.
The caret can also be updated always (ALWAYS_UPDATE), or only when the
Document length becomes less than the current Caret position
(NEVER_UPDATE).
I made the appropriate changes to insertUpdate and removeUpdate to make
sure they're using the update policy properly.
2005-10-14 Anthony Balkissoon <[EMAIL PROTECTED]>
* javax/swing/text/DefaultCaret.java:
(ALWAYS_UPDATE): New field.
(NEVER_UPDATE): New field.
(UPDATE_WHEN_ON_EDIT): New field.
(insertUpdate): Fixed docs. Only update the dot if the policy is
ALWAYS_UPDATE or if the policy is UPDATE_WHEN_ON_EDT and the event
was generated on the Event Dispatch thread.
(removeUpdate): Fixed docs. Only update the dot if the policy is
ALWAYS_UPDATE, if the policy is UPDATE_WHEN_ON_EDT and the event was
generated on the Event Dispatch thread, or if the document length
has become less than the current dot position.
(setUpdatePolicy): New method.
(getUpdatePolicy): New method.
--Tony
Index: javax/swing/text/DefaultCaret.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v
retrieving revision 1.17
diff -u -r1.17 DefaultCaret.java
--- javax/swing/text/DefaultCaret.java 11 Oct 2005 18:34:09 -0000 1.17
+++ javax/swing/text/DefaultCaret.java 14 Oct 2005 17:15:36 -0000
@@ -49,6 +49,7 @@
import java.beans.PropertyChangeListener;
import java.util.EventListener;
+import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
@@ -84,27 +85,43 @@
}
/**
- * Receives notification that some text has been removed from the text
- * component. The caret is moved backwards accordingly.
+ * Receives notification that some text has been inserted from the text
+ * component. The caret is moved forward accordingly.
*
* @param event the document event
*/
public void insertUpdate(DocumentEvent event)
{
- int dot = getDot();
- setDot(dot + event.getLength());
+ if (policy == ALWAYS_UPDATE ||
+ (SwingUtilities.isEventDispatchThread() &&
+ policy == UPDATE_WHEN_ON_EDT))
+ {
+ int dot = getDot();
+ setDot(dot + event.getLength());
+ }
}
/**
- * Receives notification that some text has been inserte into the text
- * component. The caret is moved forwards accordingly.
+ * Receives notification that some text has been removed into the text
+ * component. The caret is moved backwards accordingly.
*
* @param event the document event
*/
public void removeUpdate(DocumentEvent event)
{
- int dot = getDot();
- setDot(dot - event.getLength());
+ if (policy == ALWAYS_UPDATE ||
+ (SwingUtilities.isEventDispatchThread() &&
+ policy == UPDATE_WHEN_ON_EDT))
+ {
+ int dot = getDot();
+ setDot(dot - event.getLength());
+ }
+ else if (policy == NEVER_UPDATE)
+ {
+ int docLength = event.getDocument().getLength();
+ if (getDot() > docLength)
+ setDot(docLength);
+ }
}
}
@@ -140,8 +157,37 @@
/** The serialization UID (compatible with JDK1.5). */
private static final long serialVersionUID = 4325555698756477346L;
+
+ /**
+ * Indicates the Caret position should always be updated after Document
+ * changes even if the updates are not performed on the Event Dispatching
+ * thread.
+ *
+ * @since 1.5
+ */
+ public static final int ALWAYS_UPDATE = 2;
/**
+ * Indicates the Caret position should not be changed unless the Document
+ * length becomes less than the Caret position, in which case the Caret
+ * is moved to the end of the Document.
+ *
+ * @since 1.5
+ */
+ public static final int NEVER_UPDATE = 1;
+
+ /**
+ * Indicates the Caret position should be updated only if Document changes
+ * are made on the Event Dispatcher thread.
+ *
+ * @since 1.5
+ */
+ public static final int UPDATE_WHEN_ON_EDT = 0;
+
+ /** Keeps track of the current update policy **/
+ int policy = UPDATE_WHEN_ON_EDT;
+
+ /**
* The <code>ChangeEvent</code> that is fired by [EMAIL PROTECTED] #fireStateChanged()}.
*/
protected ChangeEvent changeEvent = new ChangeEvent(this);
@@ -201,6 +247,43 @@
*/
private Object highlightEntry;
+ /**
+ * Sets the Caret update policy.
+ *
+ * @param policy the new policy. Valid values are:
+ * ALWAYS_UPDATE: always update the Caret position, even when Document
+ * updates don't occur on the Event Dispatcher thread.
+ * NEVER_UPDATE: don't update the Caret position unless the Document
+ * length becomes less than the Caret position (then update the
+ * Caret to the end of the Document).
+ * UPDATE_WHEN_ON_EDT: update the Caret position when the
+ * Document updates occur on the Event Dispatcher thread. This is the
+ * default.
+ *
+ * @since 1.5
+ * @throws IllegalArgumentException if policy is not one of the above.
+ */
+ public void setUpdatePolicy (int policy)
+ {
+ if (policy != ALWAYS_UPDATE && policy != NEVER_UPDATE
+ && policy != UPDATE_WHEN_ON_EDT)
+ throw new
+ IllegalArgumentException
+ ("policy must be ALWAYS_UPDATE, NEVER__UPDATE, or UPDATE_WHEN_ON_EDT");
+ this.policy = policy;
+ }
+
+ /**
+ * Gets the caret update policy.
+ *
+ * @return the caret update policy.
+ * @since 1.5
+ */
+ public int getUpdatePolicy ()
+ {
+ return policy;
+ }
+
/**
* Moves the caret position when the mouse is dragged over the text
* component, modifying the selection accordingly.
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches