This fixes the TextAction class and subclasses. Basically this is a couple of null checks and the method TextAction.augmentLists() fixed.

2006-08-29  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/TextAction.java
        (getTextComponent): Check event for null and return null in
        this case.
        (augmentList): Augment Actions based on their names.
        * javax/swing/text/DefaultEditorKit.java
        (BeginAction.actionPerformed): Check target for null.
        (BeginLineAction.actionPerformed): Check target for null.
        (CopyAction.actionPerformed): Check target for null.
        (CutAction.actionPerformed): Check target for null.
        (EndAction.actionPerformed): Check target for null.
        (EndLineAction.actionPerformed): Check target for null.
        (InsertBreakAction.actionPerformed): Check target for null.
        (InsertTabAction.actionPerformed): Check target for null.
        (PasteAction.actionPerformed): Check target for null.
        (SelectAllAction.actionPerformed): Check target for null.
        (SelectionBeginAction.actionPerformed): Check target for null.
        (SelectionBeginLineAction.actionPerformed): Check target for null.
        (SelectionEndAction.actionPerformed): Check target for null.
        (SelectionEndLineAction.actionPerformed): Check target for null.
        (SelectLineAction.actionPerformed): Check target for null.
        (SelectWordAction.actionPerformed): Check target for null.

/Roman
Index: javax/swing/text/DefaultEditorKit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultEditorKit.java,v
retrieving revision 1.34
diff -u -1 -2 -r1.34 DefaultEditorKit.java
--- javax/swing/text/DefaultEditorKit.java	24 Aug 2006 16:08:58 -0000	1.34
+++ javax/swing/text/DefaultEditorKit.java	29 Aug 2006 14:30:44 -0000
@@ -302,223 +302,237 @@
 
   static class SelectAllAction
       extends TextAction
   {
     SelectAllAction()
     {
       super(selectAllAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      int offs = t.getDocument().getLength();
-      Caret c = t.getCaret();
-      c.setDot(0);
-      c.moveDot(offs);
-      
-      try
-      {   
-        c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-      }
-    catch(BadLocationException ble)
-      {
-        // Can't happen.
-      }
+      if (t != null)
+        {
+          int offs = t.getDocument().getLength();
+          Caret c = t.getCaret();
+          c.setDot(0);
+          c.moveDot(offs);
+          try
+            {   
+              c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
+        }
     }
   }
 
   static class SelectionBeginAction
       extends TextAction
   {
     SelectionBeginAction()
     {
       super(selectionBeginAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      c.moveDot(0);
-      try
-        {   
-          c.setMagicCaretPosition(t.modelToView(0).getLocation());
-        }
-      catch(BadLocationException ble)
+      if (t != null)
         {
-          // Can't happen.
+          Caret c = t.getCaret();
+          c.moveDot(0);
+          try
+            {   
+              c.setMagicCaretPosition(t.modelToView(0).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
     }
   }
 
   static class SelectionEndAction
       extends TextAction
   {
     SelectionEndAction()
     {
       super(selectionEndAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      int offs = t.getDocument().getLength();
-      Caret c = t.getCaret();
-      c.moveDot(offs);
-      try
-        {   
-          c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-        }
-      catch(BadLocationException ble)
+      if (t != null)
         {
-          // Can't happen.
+          int offs = t.getDocument().getLength();
+          Caret c = t.getCaret();
+          c.moveDot(offs);
+          try
+            {   
+              c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
     }
   }
   
   static class SelectionBeginLineAction
     extends TextAction
   {
     
     SelectionBeginLineAction()
     {
       super(selectionBeginLineAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      try
+      if (t != null)
         {
-          int offs = Utilities.getRowStart(t, c.getDot());
-          c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+          Caret c = t.getCaret();
+          try
+            {
+              int offs = Utilities.getRowStart(t, c.getDot());
+              c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
-      catch(BadLocationException ble)
-      {
-        // Can't happen.
-      }
-
     }
   }
 
   static class SelectionEndLineAction
       extends TextAction
   {
     SelectionEndLineAction()
     {
       super(selectionEndLineAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      try
-        {
-          int offs = Utilities.getRowEnd(t, c.getDot());
-          c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-        }
-      catch(BadLocationException ble)
+      if (t != null)
         {
-        // Can't happen.
+          Caret c = t.getCaret();
+          try
+            {
+              int offs = Utilities.getRowEnd(t, c.getDot());
+              c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
-
     }
   }
   
   static class SelectLineAction extends TextAction
   {
     SelectLineAction()
     {
       super(selectLineAction);
     }
   
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      try
-        {
-          int offs1 = Utilities.getRowStart(t, c.getDot());
-          int offs2 = Utilities.getRowEnd(t, c.getDot());
-          
-          c.setDot(offs2);
-          c.moveDot(offs1);
-          
-          c.setMagicCaretPosition(t.modelToView(offs2).getLocation());
-        }
-      catch(BadLocationException ble)
+      if (t != null)
         {
-          // Can't happen.
+          Caret c = t.getCaret();
+          try
+            {
+              int offs1 = Utilities.getRowStart(t, c.getDot());
+              int offs2 = Utilities.getRowEnd(t, c.getDot());
+              c.setDot(offs2);
+              c.moveDot(offs1);
+              c.setMagicCaretPosition(t.modelToView(offs2).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
     }
   }
   
   static class SelectWordAction extends TextAction
   {
     SelectWordAction()
     {
       super(selectWordAction);
     }
   
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      int dot = c.getDot();
-
-      try
+      if (t != null)
         {
-          int wordStart = Utilities.getWordStart(t, dot);
-      
-          if (dot == wordStart)
+          Caret c = t.getCaret();
+          int dot = c.getDot();
+          try
             {
-              // Current cursor position is on the first character in a word.
-              c.setDot(wordStart);
-              c.moveDot(Utilities.getWordEnd(t, wordStart));
-            }
-          else
-            {
-              // Current cursor position is not on the first character
-              // in a word. 
-              int nextWord = Utilities.getNextWord(t, dot);
-              int previousWord = Utilities.getPreviousWord(t, dot);
-              int previousWordEnd = Utilities.getWordEnd(t, previousWord);
-              
-              // Cursor position is in the space between two words. In such a
-              // situation just select the space.
-              if (dot >= previousWordEnd && dot <= nextWord)
+              int wordStart = Utilities.getWordStart(t, dot);
+
+              if (dot == wordStart)
                 {
-                  c.setDot(previousWordEnd);
-                  c.moveDot(nextWord);
+                  // Current cursor position is on the first character in a word.
+                  c.setDot(wordStart);
+                  c.moveDot(Utilities.getWordEnd(t, wordStart));
                 }
               else
                 {
-                  // Cursor position is inside a word. Just select it then.
-                  c.setDot(previousWord);
-                  c.moveDot(previousWordEnd);
+                  // Current cursor position is not on the first character
+                  // in a word. 
+                  int nextWord = Utilities.getNextWord(t, dot);
+                  int previousWord = Utilities.getPreviousWord(t, dot);
+                  int previousWordEnd = Utilities.getWordEnd(t, previousWord);
+                  
+                  // Cursor position is in the space between two words. In such a
+                  // situation just select the space.
+                  if (dot >= previousWordEnd && dot <= nextWord)
+                    {
+                      c.setDot(previousWordEnd);
+                      c.moveDot(nextWord);
+                    }
+                  else
+                    {
+                      // Cursor position is inside a word. Just select it then.
+                      c.setDot(previousWord);
+                      c.moveDot(previousWordEnd);
+                    }
                 }
-            }
 
-          // If the position was updated change the magic caret position
-          // as well.
-          if (c.getDot() != dot)
-            c.setMagicCaretPosition(t.modelToView(c.getDot()).getLocation());
-          
-        }
-      catch(BadLocationException ble)
-        {
-          // Can't happen.
+              // If the position was updated change the magic caret position
+              // as well.
+              if (c.getDot() != dot)
+                c.setMagicCaretPosition(t.modelToView(c.getDot()).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
     }
   }
 
   static class SelectionDownAction
       extends TextAction.VerticalMovementAction
   {
     SelectionDownAction()
     {
       super(selectionDownAction, SwingConstants.SOUTH);
     }
 
@@ -705,116 +719,126 @@
 
   static class EndLineAction
       extends TextAction
   {
     EndLineAction()
     {
       super(endLineAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      try
-     {
-       int offs = Utilities.getRowEnd(t, t.getCaretPosition());
-       
-       if (offs > -1)
-         {
-           Caret c = t.getCaret();
-           c.setDot(offs);
-           c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-         }
-     }
-     catch (BadLocationException ble)
-     {
-       // Nothing to do here
-     }
+      if (t != null)
+        {
+          try
+            {
+              int offs = Utilities.getRowEnd(t, t.getCaretPosition());
+              if (offs > -1)
+                {
+                  Caret c = t.getCaret();
+                  c.setDot(offs);
+                  c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+                }
+            }
+          catch (BadLocationException ble)
+            {
+              // Nothing to do here
+            }
+        }
     }
   }
 
   static class BeginLineAction
       extends TextAction
   {
     BeginLineAction()
     {
       super(beginLineAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      try
-      {
-        int offs = Utilities.getRowStart(t, t.getCaretPosition());
-        
-        if (offs > -1)
-          {
-            Caret c = t.getCaret();
-            c.setDot(offs);
-            c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-          }
-      }
-      catch (BadLocationException ble)
-      {
-        // Do nothing here.
-      }
+      if (t != null)
+        {
+          try
+            {
+              int offs = Utilities.getRowStart(t, t.getCaretPosition());
+              if (offs > -1)
+                {
+                  Caret c = t.getCaret();
+                  c.setDot(offs);
+                  c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+                }
+            }
+          catch (BadLocationException ble)
+            {
+              // Do nothing here.
+            }
+        }
     }
   }
 
   static class BeginAction extends TextAction
   {
     
     BeginAction()
     {
       super(beginAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      Caret c = t.getCaret();
-      c.setDot(0);
-      try
-      {   
-        c.setMagicCaretPosition(t.modelToView(0).getLocation());
-      }
-      catch(BadLocationException ble)
-      {
-        // Can't happen.
-      }
+      if (t != null)
+        {
+          Caret c = t.getCaret();
+          c.setDot(0);
+          try
+            {
+              c.setMagicCaretPosition(t.modelToView(0).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
+        }
     }
   }
 
   static class EndAction extends TextAction
   {
       
     EndAction()
     {
       super(endAction);
     }
 
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      int offs = t.getDocument().getLength();
-      Caret c = t.getCaret();
-      c.setDot(offs);
-      try
-        {   
-          c.setMagicCaretPosition(t.modelToView(offs).getLocation());
-        }
-      catch(BadLocationException ble)
+      if (t != null)
         {
-          // Can't happen.
+          int offs = t.getDocument().getLength();
+          Caret c = t.getCaret();
+          c.setDot(offs);
+          try
+            {   
+              c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+            }
+          catch(BadLocationException ble)
+            {
+              // Can't happen.
+            }
         }
     }
   }
   
   /**
    * Creates a beep on the PC speaker.
    *
    * @see Toolkit#beep()
    */
   public static class BeepAction extends TextAction
   {
     /**
@@ -852,25 +876,27 @@
     public CopyAction()
     {
       super(copyAction);
     }
 
     /**
      * Performs the <code>Action</code>.
      *
      * @param event the action event describing the user action
      */
     public void actionPerformed(ActionEvent event)
     {
-      getTextComponent(event).copy();
+      JTextComponent target = getTextComponent(event);
+      if (target != null)
+        target.copy();
     }
   }
 
 
   /**
    * Copies the selected content into the system clipboard and deletes the
    * selection.
    *
    * @see Toolkit#getSystemClipboard()
    * @see CopyAction
    * @see PasteAction
    */
@@ -883,25 +909,27 @@
     public CutAction()
     {
       super(cutAction);
     }
 
     /**
      * Performs the <code>Action</code>.
      *
      * @param event the action event describing the user action
      */
     public void actionPerformed(ActionEvent event)
     {
-      getTextComponent(event).cut();
+      JTextComponent target = getTextComponent(event);
+      if (target != null)
+        target.cut();
     }
   }
 
   /**
    * Copies content from the system clipboard into the editor.
    *
    * @see Toolkit#getSystemClipboard()
    * @see CopyAction
    * @see CutAction
    */
   public static class PasteAction extends TextAction
   {
@@ -912,25 +940,27 @@
     public PasteAction()
     {
       super(pasteAction);
     }
 
     /**
      * Performs the <code>Action</code>.
      *
      * @param event the action event describing the user action
      */
     public void actionPerformed(ActionEvent event)
     {
-      getTextComponent(event).paste();
+      JTextComponent target = getTextComponent(event);
+      if (target != null)
+        target.paste();
     }
   }
 
   /**
    * This action is executed as default action when a KEY_TYPED
    * event is received and no keymap entry exists for that. The purpose
    * of this action is to filter out a couple of characters. This includes
    * the control characters and characters with the ALT-modifier.
    * 
    * If an event does not get filtered, it is inserted into the document
    * of the text component. If there is some text selected in the text
    * component, this text will be replaced.
@@ -994,25 +1024,26 @@
     {
       super(insertBreakAction);
     }
 
     /**
      * Performs the <code>Action</code>.
      *
      * @param event the action event describing the user action
      */
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      t.replaceSelection("\n");
+      if (t != null)
+        t.replaceSelection("\n");
     }
   }
 
   /**
    * Places content into the associated editor. If there currently is a
    * selection, this selection is replaced.
    */
   // FIXME: Figure out what this Action is supposed to do. Obviously text
   // that is entered by the user is inserted through DefaultKeyTypedAction.
   public static class InsertContentAction extends TextAction
   {
 
@@ -1049,25 +1080,26 @@
     {
       super(insertTabAction);
     }
 
     /**
      * Performs the <code>Action</code>.
      *
      * @param event the action event describing the user action
      */
     public void actionPerformed(ActionEvent event)
     {
       JTextComponent t = getTextComponent(event);
-      t.replaceSelection("\t");
+      if (t != null)
+        t.replaceSelection("\t");
     }
   }
 
   /**
    * The serial version of DefaultEditorKit.
    */
   private static final long serialVersionUID = 9017245433028523428L;
 
   /**
    * The name of the <code>Action</code> that moves the caret one character
    * backwards.
    *
Index: javax/swing/text/TextAction.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/TextAction.java,v
retrieving revision 1.10
diff -u -1 -2 -r1.10 TextAction.java
--- javax/swing/text/TextAction.java	12 Aug 2006 22:16:12 -0000	1.10
+++ javax/swing/text/TextAction.java	29 Aug 2006 14:30:44 -0000
@@ -33,26 +33,26 @@
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 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.awt.Component;
 import java.awt.KeyboardFocusManager;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
-import java.util.ArrayList;
-import java.util.HashSet;
+import java.util.HashMap;
+import java.util.Iterator;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 
 /**
  * TextAction
  * @author Andrew Selkirk
  */
 public abstract class TextAction extends AbstractAction
 {
   /**
    * Constructor TextAction
@@ -65,50 +65,66 @@
 
   /**
    * Returns the <code>JTextComponent</code> object associated with the given
    * <code>ActionEvent</code>. If the source of the event is not a
    * <code>JTextComponent</code> the currently focused text component is returned.
    * 
    * @param event the action event
    * 
    * @return the <code>JTextComponent</code>
    */
   protected final JTextComponent getTextComponent(ActionEvent event)
   {
-    if (event.getSource() instanceof JTextComponent)
-      return (JTextComponent) event.getSource();
-
-    return getFocusedComponent();
+    JTextComponent target = null;
+    if (event != null)
+      {
+        Object source = event.getSource();
+        if (source instanceof JTextComponent)
+          target = (JTextComponent) source;
+      }
+    if (target == null)
+      target = getFocusedComponent();
+    return target;
   }
 
   /**
    * Creates a new array of <code>Action</code> containing both given arrays.
    * 
    * @param list1 the first action array
    * @param list2 the second action array
    *
    * @return the augmented array of actions
    */
   public static final Action[] augmentList(Action[] list1, Action[] list2)
   {
-    HashSet actionSet = new HashSet();
+    HashMap actions = new HashMap();
 
     for (int i = 0; i < list1.length; ++i)
-      actionSet.add(list1[i]);
-
+      {
+        Action a = list1[i];
+        Object name = a.getValue(Action.NAME);
+        actions.put(name != null ? name : "", a);
+      }
     for (int i = 0; i < list2.length; ++i)
-      actionSet.add(list2[i]);
-
-    ArrayList list = new ArrayList(actionSet);
-    return (Action[]) list.toArray(new Action[actionSet.size()]);
+      {
+        Action a = list2[i];
+        Object name = a.getValue(Action.NAME);
+        actions.put(name != null ? name : "", a);
+      }
+    Action[] augmented = new Action[actions.size()];
+    
+    int i = 0;
+    for (Iterator it = actions.values().iterator(); it.hasNext(); i++)
+      augmented[i] = (Action) it.next();
+    return augmented;
   }
 
   /**
    * Returns the current focused <code>JTextComponent</code> object.
    * 
    * @return the <code>JTextComponent</code>
    */
   protected final JTextComponent getFocusedComponent()
   {
     KeyboardFocusManager kfm =
       KeyboardFocusManager.getCurrentKeyboardFocusManager();
     Component focused = kfm.getPermanentFocusOwner();

Reply via email to