I noticed that we don't use shared InputMaps in the text components. This leads to significant slowdown when initializing many text components (like in bigger input masks). This is fixed by the following patch. It introduces a helper class SharedUIDefaults, which will also be used by the other UIs for the same task (I plan to implement all the missing keyboard methods in the UIs next). I am not 100% sure if this is the best possible implementation. The HashMap never releases its hold on the shared instances. However, using a WeakHashMap in some way seems to contradict the purpose of this class. I leave it as it is for now, because we load a very limited number of objects through this only.
2006-06-06 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/plaf/basic/BasicTextUI.java
(installKeyboardActions): Use shared input map. Correctly
install the input/action maps in the component's input/action
map hierarchies.
(getActionMap): New helper method for fetching an ActionMap from
the UIManager or creating a default one if there is none
supplied
by the UIManager.
(createActionMap): Add the TransferHandler's actions here. Made
method private.
(getInputMap): Leave out unnecessary method parameter. Load
shared input map.
* javax/swing/plaf/basic/SharedUIDefaults.java: New file.
/Roman
--
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: javax/swing/plaf/basic/SharedUIDefaults.java
===================================================================
RCS file: javax/swing/plaf/basic/SharedUIDefaults.java
diff -N javax/swing/plaf/basic/SharedUIDefaults.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ javax/swing/plaf/basic/SharedUIDefaults.java 6 Jun 2006 10:42:19 -0000
@@ -0,0 +1,78 @@
+/* SharedUIDefaults.java -- Manages shared instances for UIDefaults
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+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.plaf.basic;
+
+import java.util.HashMap;
+
+import javax.swing.UIManager;
+
+/**
+ * Manages shared instances for UI defaults. For example, all Swing components
+ * of one type usually share one InputMap/ActionMap pair. In order to avoid
+ * duplication of such objects we store them in a Map here.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public class SharedUIDefaults
+{
+
+ /**
+ * Stores the shared instances, indexed by their UI names
+ * (i.e. "TextField.InputMap").
+ */
+ private static HashMap sharedDefaults = new HashMap();
+
+ /**
+ * Returns a shared UI defaults object.
+ *
+ * @param key the key for the shared object
+ *
+ * @return a shared UI defaults object for the specified key
+ */
+ static Object get(String key)
+ {
+ Object o = sharedDefaults.get(key);
+ if (o == null)
+ {
+ o = UIManager.get(key);
+ sharedDefaults.put(key, o);
+ }
+ return o;
+ }
+}
Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.84
diff -u -1 -0 -r1.84 BasicTextUI.java
--- javax/swing/plaf/basic/BasicTextUI.java 3 Jun 2006 16:07:58 -0000 1.84
+++ javax/swing/plaf/basic/BasicTextUI.java 6 Jun 2006 10:42:20 -0000
@@ -55,24 +55,26 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
+import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ActionMapUIResource;
+import javax.swing.plaf.InputMapUIResource;
import javax.swing.plaf.TextUI;
import javax.swing.plaf.UIResource;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
@@ -725,73 +727,94 @@
/**
* Installs the keyboard actions on the text components.
*/
protected void installKeyboardActions()
{
// This is only there for backwards compatibility.
textComponent.setKeymap(createKeymap());
// load any bindings for the newer InputMap / ActionMap interface
SwingUtilities.replaceUIInputMap(textComponent, JComponent.WHEN_FOCUSED,
- getInputMap(JComponent.WHEN_FOCUSED));
- SwingUtilities.replaceUIActionMap(textComponent, createActionMap());
-
- ActionMap parentActionMap = new ActionMapUIResource();
- Action[] actions = textComponent.getActions();
- for (int j = 0; j < actions.length; j++)
- {
- Action currAction = actions[j];
- parentActionMap.put(currAction.getValue(Action.NAME), currAction);
- }
-
- SwingUtilities.replaceUIActionMap(textComponent, parentActionMap);
+ getInputMap());
+ SwingUtilities.replaceUIActionMap(textComponent, getActionMap());
}
/**
* Creates an ActionMap to be installed on the text component.
*
* @return an ActionMap to be installed on the text component
*/
- ActionMap createActionMap()
+ private ActionMap getActionMap()
+ {
+ // Note: There are no .actionMap entries in the standard L&Fs. However,
+ // with the RI it is possible to install action maps via such keys, so
+ // we must load them too. It can be observed that when there is no
+ // .actionMap entry in the UIManager, one gets installed after a text
+ // component of that type has been loaded.
+ String prefix = getPropertyPrefix();
+ String amName = prefix + ".actionMap";
+ ActionMap am = (ActionMap) UIManager.get(amName);
+ if (am == null)
+ {
+ am = createActionMap();
+ UIManager.put(amName, am);
+ }
+
+ ActionMap map = new ActionMapUIResource();
+ map.setParent(am);
+
+ return map;
+ }
+
+ /**
+ * Creates a default ActionMap for text components that have no UI default
+ * for this (the standard for the built-in L&Fs). The ActionMap is copied
+ * from the text component's getActions() method.
+ *
+ * @returna default ActionMap
+ */
+ private ActionMap createActionMap()
{
- Action[] actions = textComponent.getActions();
ActionMap am = new ActionMapUIResource();
- for (int i = 0; i < actions.length; ++i)
+ Action[] actions = textComponent.getActions();
+ for (int i = actions.length - 1; i >= 0; i--)
{
- String name = (String) actions[i].getValue(Action.NAME);
- if (name != null)
- am.put(name, actions[i]);
+ Action action = actions[i];
+ am.put(action.getValue(Action.NAME), action);
}
+ // Add TransferHandler's actions here. They don't seem to be in the
+ // JTextComponent's default actions, and I can't make up a better place
+ // to add them.
+ Action copyAction = TransferHandler.getCopyAction();
+ am.put(copyAction.getValue(Action.NAME), copyAction);
+ Action cutAction = TransferHandler.getCutAction();
+ am.put(cutAction.getValue(Action.NAME), cutAction);
+ Action pasteAction = TransferHandler.getPasteAction();
+ am.put(pasteAction.getValue(Action.NAME), pasteAction);
+
return am;
}
/**
* Gets the input map for the specified <code>condition</code>.
*
- * @param condition the condition for the InputMap
- *
* @return the InputMap for the specified condition
*/
- InputMap getInputMap(int condition)
+ private InputMap getInputMap()
{
+ InputMap im = new InputMapUIResource();
String prefix = getPropertyPrefix();
- switch (condition)
- {
- case JComponent.WHEN_IN_FOCUSED_WINDOW:
- // FIXME: is this the right string? nobody seems to use it.
- return (InputMap) UIManager.get(prefix + ".windowInputMap");
- case JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
- return (InputMap) UIManager.get(prefix + ".ancestorInputMap");
- default:
- case JComponent.WHEN_FOCUSED:
- return (InputMap) UIManager.get(prefix + ".focusInputMap");
- }
+ InputMap shared =
+ (InputMap) SharedUIDefaults.get(prefix + ".focusInputMap");
+ if (shared != null)
+ im.setParent(shared);
+ return im;
}
/**
* Uninstalls this TextUI from the text component.
*
* @param component the text component to uninstall the UI from
*/
public void uninstallUI(final JComponent component)
{
super.uninstallUI(component);
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
