This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GUI: Simplify InputMap handling


This patch simplifies the keyboard InputMap code in JComponent by using an array, indexed by condition, instead of having separate inputmap fields for each condition type. I wrote this thinking it would be more memory efficient too, but looking at the code more it seems that many/most JComponents will have getInputMap() called on them and thus need InputMaps created, so perhaps the field approach is better in terms of memory usage.

Graydon, what do you think of this patch?

Bryce

2004-08-27  Bryce McKinlay  <mckinlay@redhat.com>

	* javax/swing/JComponent.java (inputMap_whenFocused, 
	inputMap_whenAncestorOfFocused,	inputMap_whenInFocusedWindow): Removed.
	(inputMap): New field. Array of InputMaps.
	(registerKeyboardAction): Fix param names in javadoc.
	(checkInputMap): New method.
	(setInputMap): Use checkInputMap and inputMap.
	(getInputMap): Likewise.
	(getConditionForKeyStroke): Iterate through inputMap to find
	condition.
	(resetKeyboardActions): Iterate through inputMap.

Index: JComponent.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JComponent.java,v
retrieving revision 1.7.2.17
diff -u -r1.7.2.17 JComponent.java
--- JComponent.java	27 Aug 2004 15:45:36 -0000	1.7.2.17
+++ JComponent.java	27 Aug 2004 22:01:17 -0000
@@ -200,7 +200,7 @@
    * @see #setToolTipText
    * @see #getToolTipText
    */
-   String toolTipText;
+  String toolTipText;
 
   /** 
    * <p>Whether to double buffer this component when painting. This flag
@@ -310,7 +310,6 @@
    */
   private SwingPropertyChangeSupport changeSupport;
 
-
   /** 
    * Storage for "client properties", which are key/value pairs associated
    * with this component by a "client", such as a user application or a
@@ -319,9 +318,10 @@
    */
   private Hashtable clientProperties;
   
-  private InputMap inputMap_whenFocused;
-  private InputMap inputMap_whenAncestorOfFocused;
-  private InputMap inputMap_whenInFocusedWindow;
+  /** Keyboard input maps, indexed by condition. */
+  private InputMap[] inputMap;
+
+  /** Key stroke action map. */
   private ActionMap actionMap;
 
   /** 
@@ -1551,11 +1551,11 @@
    * to fetch mapping tables from keystrokes to commands, and commands to
    * actions, respectively, and modify those mappings directly.
    *
-   * @param anAction The action to be registered
-   * @param aCommand The command to deliver in the delivered {@link
+   * @param act The action to be registered
+   * @param command The command to deliver in the delivered {@link
    * java.awt.ActionEvent}
-   * @param aKeyStroke The keystroke to register on
-   * @param aCondition One of the values {@link #UNDEFINED_CONDITION},
+   * @param stroke The keystroke to register on
+   * @param condition One of the values 
    * {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or
    * {@link #WHEN_IN_FOCUSED_WINDOW}, indicating the condition which must
    * be met for the action to be fired
@@ -1567,60 +1567,43 @@
   public void registerKeyboardAction(ActionListener act, 
                                      String cmd,
                                      KeyStroke stroke, 
-                                     int cond)
+                                     int condition)
   {
-    getInputMap(cond).put(stroke, new ActionListenerProxy(act, cmd));
+    getInputMap(condition).put(stroke, new ActionListenerProxy(act, cmd));
   }
 
+  /** 
+   * Check that a condition is a valid argument for checkInputMap and
+   * setInputMap. Also initializes inputMap if neccessary.
+   *
+   * @exception IllegalArgumentException if <code>condition</code> is not 
+   * valid.
+   */
+  private void checkInputMap(int condition)
+  {
+    if (inputMap == null)
+      inputMap = new InputMap[3];    
 
+    if (condition != WHEN_FOCUSED
+        && condition != WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+	&& condition != WHEN_IN_FOCUSED_WINDOW)
+      throw new IllegalArgumentException("Invalid condition");
+  }
 
   public final void setInputMap(int condition, InputMap map)
   {
     enableEvents(AWTEvent.KEY_EVENT_MASK);
-    switch (condition)
-      {
-      case WHEN_FOCUSED:
-        inputMap_whenFocused = map;
-        break;
-
-      case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
-        inputMap_whenAncestorOfFocused = map;
-        break;
-
-      case WHEN_IN_FOCUSED_WINDOW:
-        inputMap_whenInFocusedWindow = map;
-        break;
-        
-      case UNDEFINED_CONDITION:
-      default:
-        throw new IllegalArgumentException();
-      }
+    checkInputMap(condition);
+    inputMap[condition] = map;
   }
 
   public final InputMap getInputMap(int condition)
   {
     enableEvents(AWTEvent.KEY_EVENT_MASK);
-    switch (condition)
-      {
-      case WHEN_FOCUSED:
-        if (inputMap_whenFocused == null)
-          inputMap_whenFocused = new InputMap();
-        return inputMap_whenFocused;
-
-      case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
-        if (inputMap_whenAncestorOfFocused == null)
-          inputMap_whenAncestorOfFocused = new InputMap();
-        return inputMap_whenAncestorOfFocused;
-
-      case WHEN_IN_FOCUSED_WINDOW:
-        if (inputMap_whenInFocusedWindow == null)
-          inputMap_whenInFocusedWindow = new InputMap();
-        return inputMap_whenInFocusedWindow;
-
-      case UNDEFINED_CONDITION:
-      default:
-        return null;
-      }
+    checkInputMap(condition);
+    if (inputMap[condition] == null)
+      inputMap[condition] = new InputMap();
+    return inputMap[condition];
   }
 
   public final InputMap getInputMap()
@@ -1659,17 +1642,16 @@
    */
   public int getConditionForKeyStroke(KeyStroke ks)
   {
-    if (inputMap_whenFocused != null 
-        && inputMap_whenFocused.get(ks) != null)
-      return WHEN_FOCUSED;
-    else if (inputMap_whenAncestorOfFocused != null 
-             && inputMap_whenAncestorOfFocused.get(ks) != null)
-      return WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
-    else if (inputMap_whenInFocusedWindow != null 
-             && inputMap_whenInFocusedWindow.get(ks) != null)
-      return WHEN_IN_FOCUSED_WINDOW;
-    else
-      return UNDEFINED_CONDITION;
+    if (inputMap != null)
+      {
+        for (int condition = 0; condition < inputMap.length; condition++)
+	  {
+	    if (inputMap[condition] != null
+	        && inputMap[condition].get(ks) != null)
+	      return condition;
+	  }
+      }
+    return UNDEFINED_CONDITION;
   }
 
   /**
@@ -1771,12 +1753,13 @@
    */
   public void resetKeyboardActions()
   {
-    if (inputMap_whenFocused != null)
-      inputMap_whenFocused.clear();
-    if (inputMap_whenAncestorOfFocused != null)
-      inputMap_whenAncestorOfFocused.clear();
-    if (inputMap_whenInFocusedWindow != null)
-      inputMap_whenInFocusedWindow.clear();
+    if (inputMap != null)
+      {
+        for (int condition = 0; condition < inputMap.length; condition++)
+	  if (inputMap[condition] != null)
+	    inputMap[condition].clear();
+      }
+
     if (actionMap != null)
       actionMap.clear();
   }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]