[cp-patches] FYI: JComponent - more fixes

David Gilbert david.gilbert at object-refinery.com
Fri Jun 30 09:46:37 UTC 2006


This patch (committed) adds some more fixes to the JComponent class (I wrote some 
Mauve tests for these).

2006-06-30  David Gilbert  <david.gilbert at object-refinery.com>

	* javax/swing/JComponent.java
	(registerKeyboardAction(ActionListener, KeyStroke, int): Added API
	docs,
	(registerKeyboardAction(ActionListener, String, KeyStroke, int):
	Add proxy to ActionMap,
	(setInputMap): Added API docs,
	(getActionForKeyStroke): Look in all three input maps.

By mistake, I've committed the above along with some formatting changes to the 
TextComponent and TextField classes:

2006-06-30  David Gilbert  <david.gilbert at object-refinery.com>

	* java/awt/TextComponent.java: Reformatted source code,
	* java/awt/TextField.java: Likewise.

Regards,

Dave
-------------- next part --------------
Index: java/awt/TextComponent.java
===================================================================
RCS file: /sources/classpath/classpath/java/awt/TextComponent.java,v
retrieving revision 1.23
diff -u -r1.23 TextComponent.java
--- java/awt/TextComponent.java	14 Jun 2006 19:09:40 -0000	1.23
+++ java/awt/TextComponent.java	30 Jun 2006 09:29:19 -0000
@@ -1,5 +1,5 @@
 /* TextComponent.java -- Widgets for entering text
-   Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2003, 2006, Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -54,54 +54,45 @@
 import javax.swing.text.AttributeSet;
 
 /**
-  * This class provides common functionality for widgets than 
-  * contain text.
-  *
-  * @author Aaron M. Renn (arenn at urbanophile.com)
-  */
+ * This class provides common functionality for widgets than 
+ * contain text.
+ *
+ * @author Aaron M. Renn (arenn at urbanophile.com)
+ */
 public class TextComponent extends Component
   implements Serializable, Accessible
 {
 
-/*
- * Static Variables
- */
-
-// Constant for serialization
-private static final long serialVersionUID = -2214773872412987419L;
+  private static final long serialVersionUID = -2214773872412987419L;
 
-/*
- * Instance Variables
- */
-
-/**
-  * @serial Indicates whether or not this component is editable.
-  * This is package-private to avoid an accessor method.
-  */
-boolean editable;
+  /**
+   * @serial Indicates whether or not this component is editable.
+   * This is package-private to avoid an accessor method.
+   */
+  boolean editable;
 
-/**
-  * @serial The starting position of the selected text region.
-  * This is package-private to avoid an accessor method.
-  */
-int selectionStart;
+  /**
+   * @serial The starting position of the selected text region.
+   * This is package-private to avoid an accessor method.
+   */
+  int selectionStart;
 
-/**
-  * @serial The ending position of the selected text region.
-  * This is package-private to avoid an accessor method.
-  */
-int selectionEnd;
+  /**
+   * @serial The ending position of the selected text region.
+   * This is package-private to avoid an accessor method.
+   */
+  int selectionEnd;
 
-/**
-  * @serial The text in the component
-  * This is package-private to avoid an accessor method.
-  */
-String text;
+  /**
+   * @serial The text in the component
+   * This is package-private to avoid an accessor method.
+   */
+  String text;
 
-/**
-  * A list of listeners that will receive events from this object.
-  */
-protected transient TextListener textListener;
+  /**
+   * A list of listeners that will receive events from this object.
+   */
+  protected transient TextListener textListener;
 
   protected class AccessibleAWTTextComponent
     extends AccessibleAWTComponent
@@ -318,360 +309,294 @@
     
   }
 
-/*************************************************************************/
-
-/*
- * Constructors
- */
-
-TextComponent(String text)
-{
-  this.text = text;
-  this.editable = true;
-}
-
-/*************************************************************************/
-
-/*
- * Instance Methods
- */
 
-/**
-  * Returns the text in this component
-  *
-  * @return The text in this component.
-  */
-public synchronized String
-getText()
-{
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    text = tcp.getText();
+  TextComponent(String text)
+  {
+    this.text = text;
+    this.editable = true;
+  }
 
-  return(text);
-}
 
-/*************************************************************************/
+  /**
+   * Returns the text in this component
+   *
+   * @return The text in this component.
+   */
+  public synchronized String getText()
+  {
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      text = tcp.getText();
 
-/**
-  * Sets the text in this component to the specified string.
-  *
-  * @param text The new text for this component.
-  */
-public synchronized void
-setText(String text)
-{
-  if (text == null)
-    text = "";
+    return(text);
+  }
 
-  this.text = text;
+  /**
+   * Sets the text in this component to the specified string.
+   *
+   * @param text The new text for this component.
+   */
+  public synchronized void setText(String text)
+  {
+    if (text == null)
+      text = "";
 
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    tcp.setText(text);
-  setCaretPosition(0);
-}
+    this.text = text;
 
-/*************************************************************************/
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      tcp.setText(text);
+    setCaretPosition(0);
+  }
 
-/**
-  * Returns a string that contains the text that is currently selected.
-  *
-  * @return The currently selected text region.
-  */
-public synchronized String
-getSelectedText()
-{
-  String alltext = getText();
-  int start = getSelectionStart();
-  int end = getSelectionEnd();
+  /**
+   * Returns a string that contains the text that is currently selected.
+   *
+   * @return The currently selected text region.
+   */
+  public synchronized String getSelectedText()
+  {
+    String alltext = getText();
+    int start = getSelectionStart();
+    int end = getSelectionEnd();
   
-  return(alltext.substring(start, end));
-}
-
-/*************************************************************************/
-
-/**
-  * Returns the starting position of the selected text region.
-  * If the text is not selected then caret position is returned. 
-  *
-  * @return The starting position of the selected text region.
-  */
-public synchronized int
-getSelectionStart()
-{
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    selectionStart = tcp.getSelectionStart();
-
-  return(selectionStart);
-}
-
-/*************************************************************************/
-
-/**
-  * Sets the starting position of the selected region to the
-  * specified value.  If the specified value is out of range, then it
-  * will be silently changed to the nearest legal value.
-  *
-  * @param selectionStart The new start position for selected text.
-  */
-public synchronized void
-setSelectionStart(int selectionStart)
-{
-  select(selectionStart, getSelectionEnd());
-}
-
-/*************************************************************************/
-
-/**
-  * Returns the ending position of the selected text region.
-  * If the text is not selected, then caret position is returned 
-  *
-  * @return The ending position of the selected text region.
-  */
-public synchronized int
-getSelectionEnd()
-{
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    selectionEnd = tcp.getSelectionEnd();
-
-  return(selectionEnd);
-}
+    return(alltext.substring(start, end));
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the starting position of the selected text region.
+   * If the text is not selected then caret position is returned. 
+   *
+   * @return The starting position of the selected text region.
+   */
+  public synchronized int getSelectionStart()
+  {
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      selectionStart = tcp.getSelectionStart();
 
-/**
-  * Sets the ending position of the selected region to the
-  * specified value.  If the specified value is out of range, then it
-  * will be silently changed to the nearest legal value.
-  *
-  * @param selectionEnd The new start position for selected text.
-  */
-public synchronized void
-setSelectionEnd(int selectionEnd)
-{
-  select(getSelectionStart(), selectionEnd);
-}
+    return(selectionStart);
+  }
 
-/*************************************************************************/
+  /**
+   * Sets the starting position of the selected region to the
+   * specified value.  If the specified value is out of range, then it
+   * will be silently changed to the nearest legal value.
+   *
+   * @param selectionStart The new start position for selected text.
+   */
+  public synchronized void setSelectionStart(int selectionStart)
+  {
+    select(selectionStart, getSelectionEnd());
+  }
 
-/**
-  * This method sets the selected text range to the text between the
-  * specified start and end positions.  Illegal values for these
-  * positions are silently fixed.
-  *
-  * @param selectionStart The new start position for the selected text.
-  * @param selectionEnd The new end position for the selected text.
-  */
-public synchronized void
-select(int selectionStart, int selectionEnd)
-{
-  if (selectionStart < 0)
-    selectionStart = 0;
+  /**
+   * Returns the ending position of the selected text region.
+   * If the text is not selected, then caret position is returned 
+   *
+   * @return The ending position of the selected text region.
+   */
+  public synchronized int getSelectionEnd()
+  {
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      selectionEnd = tcp.getSelectionEnd();
 
-  if (selectionStart > getText().length())
-    selectionStart = text.length();
+    return(selectionEnd);
+  }
 
-  if (selectionEnd > text.length())
-    selectionEnd = text.length();
+  /**
+   * Sets the ending position of the selected region to the
+   * specified value.  If the specified value is out of range, then it
+   * will be silently changed to the nearest legal value.
+   *
+   * @param selectionEnd The new start position for selected text.
+   */
+  public synchronized void setSelectionEnd(int selectionEnd)
+  {
+    select(getSelectionStart(), selectionEnd);
+  }
 
-  if (selectionStart > selectionEnd)
-    selectionStart = selectionEnd;
+  /**
+   * This method sets the selected text range to the text between the
+   * specified start and end positions.  Illegal values for these
+   * positions are silently fixed.
+   *
+   * @param selectionStart The new start position for the selected text.
+   * @param selectionEnd The new end position for the selected text.
+   */
+  public synchronized void select(int selectionStart, int selectionEnd)
+  {
+    if (selectionStart < 0)
+      selectionStart = 0;
 
-  this.selectionStart = selectionStart;
-  this.selectionEnd = selectionEnd;
+    if (selectionStart > getText().length())
+      selectionStart = text.length();
 
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    tcp.select(selectionStart, selectionEnd);
-}
+    if (selectionEnd > text.length())
+      selectionEnd = text.length();
 
-/*************************************************************************/
+    if (selectionStart > selectionEnd)
+      selectionStart = selectionEnd;
 
-/**
-  * Selects all of the text in the component.
-  */
-public synchronized void
-selectAll()
-{
-  select(0, getText().length());
-}
+    this.selectionStart = selectionStart;
+    this.selectionEnd = selectionEnd;
 
-/*************************************************************************/
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      tcp.select(selectionStart, selectionEnd);
+  }
 
-/**
-  * Returns the current caret position in the text.
-  *
-  * @return The caret position in the text.
-  */
-public synchronized int
-getCaretPosition()
-{
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    return(tcp.getCaretPosition());
-  else
-    return(0);
-}
+  /**
+   * Selects all of the text in the component.
+   */
+  public synchronized void selectAll()
+  {
+    select(0, getText().length());
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the current caret position in the text.
+   *
+   * @return The caret position in the text.
+   */
+  public synchronized int getCaretPosition()
+  {
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      return(tcp.getCaretPosition());
+    else
+      return(0);
+  }
 
-/**
-  * Sets the caret position to the specified value.
-  *
-  * @param caretPosition The new caret position.
-  *
-  * @exception IllegalArgumentException If the value supplied for position
-  * is less than zero.
-  *
-  * @since 1.1
-  */
-public synchronized void
-setCaretPosition(int caretPosition)
-{
-  if (caretPosition < 0)
-    throw new IllegalArgumentException ();
+  /**
+   * Sets the caret position to the specified value.
+   *
+   * @param caretPosition The new caret position.
+   *
+   * @exception IllegalArgumentException If the value supplied for position
+   * is less than zero.
+   *
+   * @since 1.1
+   */
+  public synchronized void setCaretPosition(int caretPosition)
+  {
+    if (caretPosition < 0)
+      throw new IllegalArgumentException();
   
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    tcp.setCaretPosition(caretPosition);
-}
-
-/*************************************************************************/
-
-/**
-  * Tests whether or not this component's text can be edited.
-  *
-  * @return <code>true</code> if the text can be edited, <code>false</code>
-  * otherwise.
-  */
-public boolean
-isEditable()
-{
-  return(editable);
-}
-
-/*************************************************************************/
-
-/**
-  * Sets whether or not this component's text can be edited.
-  *
-  * @param editable <code>true</code> to enable editing of the text,
-  * <code>false</code> to disable it.
-  */
-public synchronized void
-setEditable(boolean editable)
-{
-  this.editable = editable;
-
-  TextComponentPeer tcp = (TextComponentPeer)getPeer();
-  if (tcp != null)
-    tcp.setEditable(editable);
-}
-
-/*************************************************************************/
-
-/**
-  * Notifies the component that it should destroy its native peer.
-  */
-public void
-removeNotify()
-{
-  super.removeNotify();
-}
-
-/*************************************************************************/
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      tcp.setCaretPosition(caretPosition);
+  }
 
-/**
-  * Adds a new listener to the list of text listeners for this
-  * component.
-  *
-  * @param listener The listener to be added.
-  */
-public synchronized void
-addTextListener(TextListener listener)
-{
-  textListener = AWTEventMulticaster.add(textListener, listener);
+  /**
+   * Tests whether or not this component's text can be edited.
+   *
+   * @return <code>true</code> if the text can be edited, <code>false</code>
+   * otherwise.
+   */
+  public boolean isEditable()
+  {
+    return(editable);
+  }
 
-  enableEvents(AWTEvent.TEXT_EVENT_MASK);  
-}
+  /**
+   * Sets whether or not this component's text can be edited.
+   *
+   * @param editable <code>true</code> to enable editing of the text,
+   * <code>false</code> to disable it.
+   */
+  public synchronized void setEditable(boolean editable)
+  {
+    this.editable = editable;
 
-/*************************************************************************/
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
+    if (tcp != null)
+      tcp.setEditable(editable);
+  }
 
-/**
-  * Removes the specified listener from the list of listeners
-  * for this component.
-  *
-  * @param listener The listener to remove.
-  */
-public synchronized void
-removeTextListener(TextListener listener)
-{
-  textListener = AWTEventMulticaster.remove(textListener, listener);
-}
+  /**
+   * Notifies the component that it should destroy its native peer.
+   */
+  public void removeNotify()
+  {
+    super.removeNotify();
+  }
 
-/*************************************************************************/
+  /**
+   * Adds a new listener to the list of text listeners for this
+   * component.
+   *
+   * @param listener The listener to be added.
+   */
+  public synchronized void addTextListener(TextListener listener)
+  {
+    textListener = AWTEventMulticaster.add(textListener, listener);
 
-/**
-  * Processes the specified event for this component.  Text events are
-  * processed by calling the <code>processTextEvent()</code> method.
-  * All other events are passed to the superclass method.
-  * 
-  * @param event The event to process.
-  */
-protected void
-processEvent(AWTEvent event)
-{
-  if (event instanceof TextEvent)
-    processTextEvent((TextEvent)event);
-  else
-    super.processEvent(event);
-}
+    enableEvents(AWTEvent.TEXT_EVENT_MASK);  
+  }
 
-/*************************************************************************/
+  /**
+   * Removes the specified listener from the list of listeners
+   * for this component.
+   *
+   * @param listener The listener to remove.
+   */
+  public synchronized void removeTextListener(TextListener listener)
+  {
+    textListener = AWTEventMulticaster.remove(textListener, listener);
+  }
 
-/**
-  * Processes the specified text event by dispatching it to any listeners
-  * that are registered.  Note that this method will only be called
-  * if text event's are enabled.  This will be true if there are any
-  * registered listeners, or if the event has been specifically
-  * enabled using <code>enableEvents()</code>.
-  *
-  * @param event The text event to process.
-  */
-protected void
-processTextEvent(TextEvent event)
-{
-  if (textListener != null)
-    textListener.textValueChanged(event);
-}
+  /**
+   * Processes the specified event for this component.  Text events are
+   * processed by calling the <code>processTextEvent()</code> method.
+   * All other events are passed to the superclass method.
+   * 
+   * @param event The event to process.
+   */
+  protected void processEvent(AWTEvent event)
+  {
+    if (event instanceof TextEvent)
+      processTextEvent((TextEvent)event);
+    else
+      super.processEvent(event);
+  }
 
-void
-dispatchEventImpl(AWTEvent e)
-{
-  if (e.id <= TextEvent.TEXT_LAST 
-      && e.id >= TextEvent.TEXT_FIRST
-      && (textListener != null 
-	  || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
-    processEvent(e);
-  else
-    super.dispatchEventImpl(e);
-}
+  /**
+   * Processes the specified text event by dispatching it to any listeners
+   * that are registered.  Note that this method will only be called
+   * if text event's are enabled.  This will be true if there are any
+   * registered listeners, or if the event has been specifically
+   * enabled using <code>enableEvents()</code>.
+   *
+   * @param event The text event to process.
+   */
+  protected void processTextEvent(TextEvent event)
+  {
+    if (textListener != null)
+      textListener.textValueChanged(event);
+  }
 
-/*************************************************************************/
+  void dispatchEventImpl(AWTEvent e)
+  {
+    if (e.id <= TextEvent.TEXT_LAST 
+        && e.id >= TextEvent.TEXT_FIRST
+        && (textListener != null 
+	    || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
+      processEvent(e);
+    else
+      super.dispatchEventImpl(e); 
+  }
 
-/**
-  * Returns a debugging string.
-  *
-  * @return A debugging string.
-  */
-protected String
-paramString()
-{
-  return(getClass().getName() + "(text=" + getText() + ")");
-}
+  /**
+   * Returns a debugging string.
+   *
+   * @return A debugging string.
+   */
+  protected String paramString()
+  {
+    return(getClass().getName() + "(text=" + getText() + ")");
+  }
 
   /**
    * Returns an array of all the objects currently registered as FooListeners
@@ -681,20 +606,20 @@
    * @exception ClassCastException If listenerType doesn't specify a class or
    * interface that implements java.util.EventListener.
    */
-  public EventListener[] getListeners (Class listenerType)
+  public EventListener[] getListeners(Class listenerType)
   {
     if (listenerType == TextListener.class)
-      return AWTEventMulticaster.getListeners (textListener, listenerType);
+      return AWTEventMulticaster.getListeners(textListener, listenerType);
 
-    return super.getListeners (listenerType);
+    return super.getListeners(listenerType);
   }
 
   /**
    * Returns all text listeners registered to this object.
    */
-  public TextListener[] getTextListeners ()
+  public TextListener[] getTextListeners()
   {
-    return (TextListener[]) getListeners (TextListener.class);
+    return (TextListener[]) getListeners(TextListener.class);
   }
 
   /**
@@ -712,23 +637,20 @@
   }
 
   
-  /*******************************/
   // Provide AccessibleAWTTextComponent access to several peer functions that
   // aren't publicly exposed.  This is package-private to avoid an accessor
   // method.
-  synchronized int
-  getIndexAtPoint(Point p)
+  synchronized int getIndexAtPoint(Point p)
   {
-    TextComponentPeer tcp = (TextComponentPeer)getPeer();
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
     if (tcp != null)
       return tcp.getIndexAtPoint(p.x, p.y);
     return -1;
   }
   
-  synchronized Rectangle
-  getCharacterBounds(int i)
+  synchronized Rectangle getCharacterBounds(int i)
   {
-    TextComponentPeer tcp = (TextComponentPeer)getPeer();
+    TextComponentPeer tcp = (TextComponentPeer) getPeer();
     if (tcp != null)
       return tcp.getCharacterBounds(i);
     return null;
@@ -740,8 +662,7 @@
    * 
    * @return true to ignore all old mouse events.
    */
-  static boolean
-  ignoreOldMouseEvents()
+  static boolean ignoreOldMouseEvents()
   {
     return true;
   }
Index: java/awt/TextField.java
===================================================================
RCS file: /sources/classpath/classpath/java/awt/TextField.java,v
retrieving revision 1.18
diff -u -r1.18 TextField.java
--- java/awt/TextField.java	23 Jun 2006 17:24:20 -0000	1.18
+++ java/awt/TextField.java	30 Jun 2006 09:29:20 -0000
@@ -1,5 +1,5 @@
 /* TextField.java -- A one line text entry field
-   Copyright (C) 1999, 2002, 2004  Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2004, 2006,  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -48,454 +48,363 @@
 import javax.accessibility.AccessibleStateSet;
 
 /**
-  * This class implements a single line text entry field widget
-  *
-  * @author Aaron M. Renn (arenn at urbanophile.com)
-  */
-public class TextField extends TextComponent
-{
-
-/*
- * Static Variables
- */
-
-// Serialization constant
-private static final long serialVersionUID = -2966288784432217853L;
-
-/*************************************************************************/
-
-/*
- * Instance Variables
- */
-
-/**
-  * @serial The number of columns in the text entry field.
-  */
-private int columns;
-
-/**
-  * @serial The character that is echoed when doing protected input
-  */
-private char echoChar;
-
-// List of registered ActionListener's for this object.
-private ActionListener action_listeners;
-
-/*************************************************************************/
-
-/*
- * Constructors
- */
-
-/**
- * Initializes a new instance of <code>TextField</code> that is empty
- * and has one column.
+ * This class implements a single line text entry field widget
  *
- * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
+ * @author Aaron M. Renn (arenn at urbanophile.com)
  */
-public
-TextField()
-{
-  this("", 0);
-}
-
-/*************************************************************************/
-
-/**
-  * Initializes a new instance of <code>TextField</code> containing
-  * the specified text.  The number of columns will be equal to the
-  * length of the text string.
-  *
-  * @param text The text to display in the field.
-  *
-  * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
-  */
-public
-TextField(String text)
-{
-  this(text, (text == null) ? 0 : text.length());
-}
-
-/*************************************************************************/
-
-/**
-  * Initializes a new instance of <code>TextField</code> that is empty
-  * and has the specified number of columns.
-  *
-  * @param columns The number of columns in the text field.
-  *
-  * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
-  */
-public
-TextField(int columns)
-{
-  this("", columns);
-}
-
-/*************************************************************************/
-
-/**
-  * Initializes a new instance of <code>TextField</code> with the
-  * specified text and number of columns.
-  *
-  * @param text The text to display in the field.
-  * @param columns The number of columns in the field.
-  *
-  * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
-  */
-public
-TextField(String text, int columns)
-{
-  super(text);
-  
-  if (columns < 0)
-    this.columns = 0;
-  else
-    this.columns = columns;
-
-  if (GraphicsEnvironment.isHeadless())
-    throw new HeadlessException ();
-}
-
-/*************************************************************************/
-
-/*
- * Instance Methods
- */
-
-/**
-  * Returns the number of columns in the field.
-  *
-  * @return The number of columns in the field.
-  */
-public int
-getColumns()
-{
-  return(columns);
-}
-
-/*************************************************************************/
-
-/**
-  * Sets the number of columns in this field to the specified value.
-  *
-  * @param columns The new number of columns in the field.
-  *
-  * @exception IllegalArgumentException If columns is less than zero.
-  */
-public synchronized void
-setColumns(int columns)
-{
-  if (columns < 0)
-    throw new IllegalArgumentException("Value is less than zero: " +
-                                       columns);
-
-  this.columns = columns;
-  // FIXME: How to we communicate this to our peer?
-}
-
-/*************************************************************************/
-
-/**
-  * Returns the character that is echoed to the screen when a text 
-  * field is protected (such as when a password is being entered).
-  *
-  * @return The echo character for this text field.
-  */
-public char
-getEchoChar()
-{
-  return(echoChar);
-}
-
-/*************************************************************************/
-
-/**
-  * Sets the character that is echoed when protected input such as
-  * a password is displayed.
-  *
-  * @param echoChar The new echo character.
-  */
-public void
-setEchoChar(char echoChar)
+public class TextField extends TextComponent
 {
-  setEchoCharacter (echoChar);
-}
 
-/*************************************************************************/
+  private static final long serialVersionUID = -2966288784432217853L;
 
-/**
-  * Sets the character that is echoed when protected input such as
-  * a password is displayed.
-  *
-  * @param echoChar The new echo character.
-  *
-  * @deprecated This method is deprecated in favor of 
-  * <code>setEchoChar()</code>
-  */
-public void
-setEchoCharacter(char echoChar)
-{
-  this.echoChar = echoChar;
-
-  TextFieldPeer peer = (TextFieldPeer) getPeer ();
-  if (peer != null)
-    peer.setEchoChar (echoChar);
-}
 
-/*************************************************************************/
+  /**
+   * @serial The number of columns in the text entry field.
+   */
+  private int columns;
 
-/**
-  * Tests whether or not this text field has an echo character set
-  * so that characters the user type are not echoed to the screen.
-  *
-  * @return <code>true</code> if an echo character is set,
-  * <code>false</code> otherwise.
-  */
-public boolean
-echoCharIsSet()
-{
-  if (echoChar == '\u0000')
-    return(false);
-  else
-    return(true);
-}
+  /**
+   * @serial The character that is echoed when doing protected input
+   */
+  private char echoChar;
 
-/*************************************************************************/
+  // List of registered ActionListener's for this object.
+  private ActionListener action_listeners;
 
-/**
-  * Returns the minimum size for this text field.
-  *
-  * @return The minimum size for this text field.
-  */
-public Dimension
-getMinimumSize()
-{
-  return getMinimumSize (getColumns ());
-}
+  /**
+   * Initializes a new instance of <code>TextField</code> that is empty
+   * and has one column.
+   *
+   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
+   */
+  public TextField()
+  {
+    this("", 0);
+  }
 
-/*************************************************************************/
+  /**
+   * Initializes a new instance of <code>TextField</code> containing
+   * the specified text.  The number of columns will be equal to the
+   * length of the text string.
+   *
+   * @param text The text to display in the field.
+   *
+   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
+   */
+  public TextField(String text)
+  {
+    this(text, (text == null) ? 0 : text.length());
+  }
 
-/**
-  * Returns the minimum size of a text field with the specified number
-  * of columns.
-  *
-  * @param columns The number of columns to get the minimum size for.
-  */
-public Dimension
-getMinimumSize(int columns)
-{
-  return minimumSize (columns);
-}
+  /**
+   * Initializes a new instance of <code>TextField</code> that is empty
+   * and has the specified number of columns.
+   *
+   * @param columns The number of columns in the text field.
+   *
+   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
+   */
+  public TextField(int columns)
+  {
+    this("", columns);
+  }
 
-/*************************************************************************/
+  /**
+   * Initializes a new instance of <code>TextField</code> with the
+   * specified text and number of columns.
+   *
+   * @param text The text to display in the field.
+   * @param columns The number of columns in the field.
+   *
+   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
+   */
+  public TextField(String text, int columns)
+  {
+    super(text);
+  
+    if (columns < 0)
+      this.columns = 0;
+    else
+      this.columns = columns;
 
-/**
-  * Returns the minimum size for this text field.
-  *
-  * @return The minimum size for this text field.
-  *
-  * @deprecated This method is deprecated in favor of
-  * <code>getMinimumSize()</code>.
-  */
-public Dimension
-minimumSize()
-{
-  return minimumSize (getColumns ());
-}
+    if (GraphicsEnvironment.isHeadless())
+      throw new HeadlessException ();
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the number of columns in the field.
+   *
+   * @return The number of columns in the field.
+   */
+  public int getColumns()
+  {
+    return(columns);
+  }
 
-/**
-  * Returns the minimum size of a text field with the specified number
-  * of columns.
-  *
-  * @param columns The number of columns to get the minimum size for.
-  *
-  * @deprecated This method is deprecated in favor of 
-  * <code>getMinimumSize(int)</code>.
-  */
-public Dimension
-minimumSize(int columns)
-{
-  TextFieldPeer peer = (TextFieldPeer) getPeer ();
-  if (peer == null)
-    return null; // FIXME: What do we do if there is no peer?
+  /**
+   * Sets the number of columns in this field to the specified value.
+   *
+   * @param columns The new number of columns in the field.
+   *
+   * @exception IllegalArgumentException If columns is less than zero.
+   */
+  public synchronized void setColumns(int columns)
+  {
+    if (columns < 0)
+      throw new IllegalArgumentException("Value is less than zero: " +
+                                         columns);
 
-  return peer.getMinimumSize (columns);
-}
+    this.columns = columns;
+    // FIXME: How to we communicate this to our peer?
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the character that is echoed to the screen when a text 
+   * field is protected (such as when a password is being entered).
+   *
+   * @return The echo character for this text field.
+   */
+  public char getEchoChar()
+  {
+    return(echoChar);
+  }
 
-/**
-  * Returns the preferred size for this text field.
-  *
-  * @return The preferred size for this text field.
-  */
-public Dimension
-getPreferredSize()
-{
-  return getPreferredSize (getColumns ());
-}
+  /**
+   * Sets the character that is echoed when protected input such as
+   * a password is displayed.
+   *
+   * @param echoChar The new echo character.
+   */
+  public void setEchoChar(char echoChar)
+  {
+    setEchoCharacter(echoChar);
+  }
 
-/*************************************************************************/
+  /**
+   * Sets the character that is echoed when protected input such as
+   * a password is displayed.
+   *
+   * @param echoChar The new echo character.
+   *
+   * @deprecated This method is deprecated in favor of 
+   * <code>setEchoChar()</code>
+   */
+  public void setEchoCharacter(char echoChar)
+  {
+    this.echoChar = echoChar;
 
-/**
-  * Returns the preferred size of a text field with the specified number
-  * of columns.
-  *
-  * @param columns The number of columns to get the preferred size for.
-  */
-public Dimension
-getPreferredSize(int columns)
-{
-  return preferredSize (columns);
-}
+    TextFieldPeer peer = (TextFieldPeer) getPeer ();
+    if (peer != null)
+      peer.setEchoChar (echoChar);
+  }
 
-/*************************************************************************/
+  /**
+   * Tests whether or not this text field has an echo character set
+   * so that characters the user type are not echoed to the screen.
+   *
+   * @return <code>true</code> if an echo character is set,
+   * <code>false</code> otherwise.
+   */
+  public boolean echoCharIsSet()
+  {
+    if (echoChar == '\u0000')
+      return(false);
+    else
+      return(true);
+  }
 
-/**
-  * Returns the preferred size for this text field.
-  *
-  * @return The preferred size for this text field.
-  *
-  * @deprecated This method is deprecated in favor of 
-  * <code>getPreferredSize()</code>.
-  */
-public Dimension
-preferredSize()
-{
-  return preferredSize (getColumns ());
-}
+  /**
+   * Returns the minimum size for this text field.
+   *
+   * @return The minimum size for this text field.
+   */
+  public Dimension getMinimumSize()
+  {
+    return getMinimumSize (getColumns ());
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the minimum size of a text field with the specified number
+   * of columns.
+   *
+   * @param columns The number of columns to get the minimum size for.
+   */
+  public Dimension getMinimumSize(int columns)
+  {
+    return minimumSize(columns);
+  }
 
-/**
-  * Returns the preferred size of a text field with the specified number
-  * of columns.
-  *
-  * @param columns The number of columns to get the preferred size for.
-  *
-  * @deprecated This method is deprecated in favor of 
-  * <code>getPreferredSize(int)</code>.
-  */
-public Dimension
-preferredSize(int columns)
-{
-  TextFieldPeer peer = (TextFieldPeer) getPeer ();
-  if (peer == null)
-    return new Dimension (0, 0);
+  /**
+   * Returns the minimum size for this text field.
+   *
+   * @return The minimum size for this text field.
+   *
+   * @deprecated This method is deprecated in favor of
+   * <code>getMinimumSize()</code>.
+   */
+  public Dimension minimumSize()
+  {
+    return minimumSize(getColumns ());
+  }
 
-  return peer.getPreferredSize (columns);
-}
+  /**
+   * Returns the minimum size of a text field with the specified number
+   * of columns.
+   *
+   * @param columns The number of columns to get the minimum size for.
+   *
+   * @deprecated This method is deprecated in favor of 
+   * <code>getMinimumSize(int)</code>.
+   */
+  public Dimension minimumSize(int columns)
+  {
+    TextFieldPeer peer = (TextFieldPeer) getPeer ();
+    if (peer == null)
+      return null; // FIXME: What do we do if there is no peer?
 
-/*************************************************************************/
+    return peer.getMinimumSize (columns);
+  }
 
-/**
-  * Notifies this object that it should create its native peer.
-  */
-public void
-addNotify()
-{
-  if (getPeer() != null)
-    return;
+  /**
+   * Returns the preferred size for this text field.
+   *
+   * @return The preferred size for this text field.
+   */
+  public Dimension getPreferredSize()
+  {
+    return getPreferredSize(getColumns ());
+  }
 
-  setPeer((ComponentPeer)getToolkit().createTextField(this));
-  super.addNotify();
-}
+  /**
+   * Returns the preferred size of a text field with the specified number
+   * of columns.
+   *
+   * @param columns The number of columns to get the preferred size for.
+   */
+  public Dimension getPreferredSize(int columns)
+  {
+    return preferredSize(columns);
+  }
 
-/*************************************************************************/
+  /**
+   * Returns the preferred size for this text field.
+   *
+   * @return The preferred size for this text field.
+   *
+   * @deprecated This method is deprecated in favor of 
+   * <code>getPreferredSize()</code>.
+   */
+  public Dimension preferredSize()
+  {
+    return preferredSize(getColumns ());
+  }
 
-/**
-  * Addes a new listener to the list of action listeners for this
-  * object.
-  *
-  * @param listener The listener to add to the list.
-  */
-public synchronized void
-addActionListener(ActionListener listener)
-{
-  action_listeners = AWTEventMulticaster.add(action_listeners, listener);
+  /**
+   * Returns the preferred size of a text field with the specified number
+   * of columns.
+   *
+   * @param columns The number of columns to get the preferred size for.
+   *
+   * @deprecated This method is deprecated in favor of 
+   * <code>getPreferredSize(int)</code>.
+   */
+  public Dimension preferredSize(int columns)
+  {
+    TextFieldPeer peer = (TextFieldPeer) getPeer ();
+    if (peer == null)
+      return new Dimension (0, 0);
 
-  enableEvents(AWTEvent.ACTION_EVENT_MASK);
-}
+    return peer.getPreferredSize (columns);
+  }
 
-/*************************************************************************/
+  /**
+   * Notifies this object that it should create its native peer.
+   */
+  public void addNotify()
+  {
+    if (getPeer() != null)
+      return;
 
-/**
-  * Removes the specified listener from the list of action listeners
-  * for this object.
-  *
-  * @param listener The listener to remove from the list.
-  */
-public synchronized void
-removeActionListener(ActionListener listener)
-{
-  action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
-}
+    setPeer((ComponentPeer)getToolkit().createTextField(this));
+    super.addNotify();
+  }
 
-/*************************************************************************/
+  /**
+   * Addes a new listener to the list of action listeners for this
+   * object.
+   *
+   * @param listener The listener to add to the list.
+   */
+  public synchronized void addActionListener(ActionListener listener)
+  {
+    action_listeners = AWTEventMulticaster.add(action_listeners, listener);
 
-/**
-  * Processes the specified event.  If the event is an instance of
-  * <code>ActionEvent</code> then <code>processActionEvent()</code> is
-  * called to process it, otherwise the event is sent to the
-  * superclass.
-  *
-  * @param event The event to process.
-  */
-protected void
-processEvent(AWTEvent event)
-{
-  if (event instanceof ActionEvent)
-    processActionEvent((ActionEvent)event);
-  else
-    super.processEvent(event);
-}
+    enableEvents(AWTEvent.ACTION_EVENT_MASK);
+  }
 
-/*************************************************************************/
+  /**
+   * Removes the specified listener from the list of action listeners
+   * for this object.
+   *
+   * @param listener The listener to remove from the list.
+   */
+  public synchronized void removeActionListener(ActionListener listener)
+  {
+    action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
+  }
 
-/**
-  * Processes an action event by calling any registered listeners.
-  * Note to subclasses: This method is not called unless action events
-  * are enabled on this object.  This will be true if any listeners
-  * are registered, or if action events were specifically enabled
-  * using <code>enableEvents()</code>.
-  * 
-  * @param event The event to process.
-  */
-protected void
-processActionEvent(ActionEvent event)
-{
-  if (action_listeners != null)
-    action_listeners.actionPerformed(event);
-}
+  /**
+   * Processes the specified event.  If the event is an instance of
+   * <code>ActionEvent</code> then <code>processActionEvent()</code> is
+   * called to process it, otherwise the event is sent to the
+   * superclass.
+   *
+   * @param event The event to process.
+   */
+  protected void processEvent(AWTEvent event)
+  {
+    if (event instanceof ActionEvent)
+      processActionEvent((ActionEvent)event);
+    else
+      super.processEvent(event);
+  }
 
-void
-dispatchEventImpl(AWTEvent e)
-{
-  if (e.id <= ActionEvent.ACTION_LAST 
-      && e.id >= ActionEvent.ACTION_FIRST
-      && (action_listeners != null 
-	  || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
-    processEvent(e);
-  else
-    super.dispatchEventImpl(e);
-}
+  /**
+   * Processes an action event by calling any registered listeners.
+   * Note to subclasses: This method is not called unless action events
+   * are enabled on this object.  This will be true if any listeners
+   * are registered, or if action events were specifically enabled
+   * using <code>enableEvents()</code>.
+   * 
+   * @param event The event to process.
+   */
+  protected void processActionEvent(ActionEvent event)
+  {
+    if (action_listeners != null)
+      action_listeners.actionPerformed(event);
+  }
 
-/*************************************************************************/
+  void dispatchEventImpl(AWTEvent e)
+  {
+    if (e.id <= ActionEvent.ACTION_LAST 
+        && e.id >= ActionEvent.ACTION_FIRST
+        && (action_listeners != null 
+	    || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
+      processEvent(e);
+    else
+      super.dispatchEventImpl(e);
+  }
 
-/**
+ /**
   * Returns a debug string for this object.
   *
   * @return A debug string for this object.
   */
-protected String
-paramString()
-{
-  return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
-         getEchoChar());
-}
+  protected String paramString()
+  {
+    return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
+           getEchoChar());
+  }
 
   /**
    * Returns an array of all the objects currently registered as FooListeners
@@ -545,4 +454,4 @@
     return new AccessibleAWTTextField();
   }
 
-} // class TextField
+}
Index: javax/swing/JComponent.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JComponent.java,v
retrieving revision 1.137
diff -u -r1.137 JComponent.java
--- javax/swing/JComponent.java	29 Jun 2006 14:45:58 -0000	1.137
+++ javax/swing/JComponent.java	30 Jun 2006 09:29:27 -0000
@@ -2383,7 +2383,13 @@
   /**
    * A variant of {@link
    * #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which
-   * provides <code>null</code> for the command name.   
+   * provides <code>null</code> for the command name.
+   * 
+   * @param act  the action listener to notify when the keystroke occurs.
+   * @param stroke  the key stroke.
+   * @param cond  the condition (one of {@link #WHEN_FOCUSED}, 
+   *     {@link #WHEN_IN_FOCUSED_WINDOW} and 
+   *     {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}).
    */
   public void registerKeyboardAction(ActionListener act,
                                      KeyStroke stroke, 
@@ -2460,9 +2466,22 @@
                                      KeyStroke stroke, 
                                      int cond)
   {
-    getInputMap(cond).put(stroke, new ActionListenerProxy(act, cmd));
+    ActionListenerProxy proxy = new ActionListenerProxy(act, cmd);
+    getInputMap(cond).put(stroke, proxy);
+    getActionMap().put(proxy, proxy);
   }
 
+  /**
+   * Sets the input map for the given condition.
+   * 
+   * @param condition  the condition (one of {@link #WHEN_FOCUSED}, 
+   *     {@link #WHEN_IN_FOCUSED_WINDOW} and 
+   *     {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}).
+   * @param map  the map.
+   * 
+   * @throws IllegalArgumentException if <code>condition</code> is not one of
+   *     the specified values.
+   */
   public final void setInputMap(int condition, InputMap map)
   {
     enableEvents(AWTEvent.KEY_EVENT_MASK);
@@ -2598,13 +2617,17 @@
    */
   public ActionListener getActionForKeyStroke(KeyStroke ks)
   {
-    Object cmd = getInputMap().get(ks);
-    if (cmd != null)
+    Object key = getInputMap(JComponent.WHEN_FOCUSED).get(ks);
+    if (key == null)
+      key = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(ks);
+    if (key == null)
+      key = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).get(ks);
+    if (key != null)
       {
-        if (cmd instanceof ActionListenerProxy)
-          return (ActionListenerProxy) cmd;
-        else if (cmd instanceof String)
-          return getActionMap().get(cmd);
+        if (key instanceof ActionListenerProxy)
+          return ((ActionListenerProxy) key).target;
+        else
+          return getActionMap().get(key);
       }
     return null;
   }


More information about the Classpath-patches mailing list