Hi,
this patch prevents that multiple Swing components have a text selection.

The approach and patch was approved by Roman on IRC.

2006-03-21  Robert Schuster  <[EMAIL PROTECTED]>

        * javax/swing/text/DefaultCaret.java: Added class variable denoting
        the textcomponent having a selection.
        (clearSelection): Clear 'componentWithSelection' variable.
        (handleSelection): Clear selection of current component having a
        selection before setting a new selection in another component.

cya
Robert
Index: javax/swing/text/DefaultCaret.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultCaret.java,v
retrieving revision 1.34
diff -u -r1.34 DefaultCaret.java
--- javax/swing/text/DefaultCaret.java	17 Mar 2006 20:38:35 -0000	1.34
+++ javax/swing/text/DefaultCaret.java	21 Mar 2006 17:35:43 -0000
@@ -63,12 +63,17 @@
 /**
  * The default implementation of the [EMAIL PROTECTED] Caret} interface.
  *
- * @author orgininal author unknown
+ * @author original author unknown
  * @author Roman Kennke ([EMAIL PROTECTED])
  */
 public class DefaultCaret extends Rectangle
   implements Caret, FocusListener, MouseListener, MouseMotionListener
 {
+  
+  /** A text component in the current VM which currently has a
+   * text selection or <code>null</code>.
+   */ 
+  static JTextComponent componentWithSelection;
 
   /**
    * Controls the blinking of the caret.
@@ -346,7 +351,8 @@
    */
   public void mouseDragged(MouseEvent event)
   {
-    moveCaret(event);
+    if (event.getButton() == MouseEvent.BUTTON1)
+      moveCaret(event);
   }
 
   /**
@@ -379,7 +385,7 @@
   {
     int count = event.getClickCount();
     
-    if (count >= 2)
+    if (event.getButton() == MouseEvent.BUTTON1 && count >= 2)
       {
         int newDot = getComponent().viewToModel(event.getPoint());
         JTextComponent t = getComponent();
@@ -650,6 +656,11 @@
           highlightEntry = highlighter.addHighlight(0, 0, getSelectionPainter());
         else
           highlighter.changeHighlight(highlightEntry, 0, 0);
+        
+        // Free the global variable which stores the text component with an active
+        // selection.
+        if (componentWithSelection == textComponent)
+          componentWithSelection = null;
       }
     catch (BadLocationException e)
       {
@@ -685,6 +696,17 @@
 	      highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
 	    else
 	      highlighter.changeHighlight(highlightEntry, p0, p1);
+            
+            // If another component currently has a text selection clear that selection
+            // first.
+            if (componentWithSelection != null)
+              if (componentWithSelection != textComponent)
+                {
+                  Caret c = componentWithSelection.getCaret();
+                  c.setDot(c.getDot());
+                }
+            componentWithSelection = textComponent;
+            
 	  }
 	catch (BadLocationException e)
 	  {

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to