A couple of fixes for the TextUI classes. It juggles a little with the initialization. The UI properties only have to be installed if the corresponding current property is null or a UIResource. A document listener has to be installed early(-ier), so that notifications get sent correctly. Also I removed two fields in BasicTextUI that are only used by the BasicTextFieldUI and made the BasicTextFieldUI fetch the 2 colors from the SharedUIDefaults instead.
2006-06-21 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/plaf/basic/BasicTextUI.java
(PropertyChangeHandler.propertyChange): Handle document listener
update here.
(background): Removed unneeded fields.
(inactiveBackground): Remove unneeded fields.
(installUI): Install the document listener. Slightly changed
order of operations. Don't trigger modelChanged().
(installDefaults): Only install properties when the
current properties are null or instances of UIResource.
(installListeners): Removed unnecessary listener installs.
(installDocumentListeners): Removed unneeded method.
(uninstallListeners): Removed unnecessary listener uninstalls.
(modelChanged): Removed call to installDocumentListeners().
* javax/swing/plaf/basic/BasicTextFieldUI.java
(propertyChange): Update the colors by fetching them from
SharedUIDefaults. Fixed conditions.
* javax/swing/plaf/basic/SharedUIDefaults.java
(getColor): New method.
/Roman
--
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: javax/swing/plaf/basic/BasicTextUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextUI.java,v
retrieving revision 1.86
diff -u -1 -0 -r1.86 BasicTextUI.java
--- javax/swing/plaf/basic/BasicTextUI.java 13 Jun 2006 09:28:57 -0000 1.86
+++ javax/swing/plaf/basic/BasicTextUI.java 21 Jun 2006 12:56:12 -0000
@@ -411,21 +411,33 @@
/**
* Notifies when a property of the text component changes.
*
* @param event the PropertyChangeEvent describing the change
*/
public void propertyChange(PropertyChangeEvent event)
{
if (event.getPropertyName().equals("document"))
{
// Document changed.
- modelChanged();
+ Object oldValue = event.getOldValue();
+ if (oldValue != null)
+ {
+ Document oldDoc = (Document) oldValue;
+ oldDoc.removeDocumentListener(documentHandler);
+ }
+ Object newValue = event.getNewValue();
+ if (newValue != null)
+ {
+ Document newDoc = (Document) newValue;
+ newDoc.addDocumentListener(documentHandler);
+ }
+ modelChanged();
}
BasicTextUI.this.propertyChange(event);
}
}
/**
* Listens for changes on the underlying model and forwards notifications
* to the View. This also updates the caret position of the text component.
*
@@ -494,32 +506,20 @@
/**
* Receives notification when the model changes.
*/
private PropertyChangeHandler updateHandler = new PropertyChangeHandler();
/** The DocumentEvent handler. */
DocumentHandler documentHandler = new DocumentHandler();
/**
- * The standard background color. This is the color which is used to paint
- * text in enabled text components.
- */
- Color background;
-
- /**
- * The inactive background color. This is the color which is used to paint
- * text in disabled text components.
- */
- Color inactiveBackground;
-
- /**
* Creates a new <code>BasicTextUI</code> instance.
*/
public BasicTextUI()
{
// Nothing to do here.
}
/**
* Creates a [EMAIL PROTECTED] Caret} that should be installed into the text component.
*
@@ -551,71 +551,98 @@
return textComponent;
}
/**
* Installs this UI on the text component.
*
* @param c the text component on which to install the UI
*/
public void installUI(final JComponent c)
{
- super.installUI(c);
-
textComponent = (JTextComponent) c;
+ installDefaults();
+ textComponent.addPropertyChangeListener(updateHandler);
Document doc = textComponent.getDocument();
if (doc == null)
{
doc = getEditorKit(textComponent).createDefaultDocument();
textComponent.setDocument(doc);
}
- installDefaults();
+ else
+ {
+ doc.addDocumentListener(documentHandler);
+ modelChanged();
+ }
+
installListeners();
installKeyboardActions();
-
- // We need to trigger this so that the view hierarchy gets initialized.
- modelChanged();
-
}
/**
* Installs UI defaults on the text components.
*/
protected void installDefaults()
{
+ String prefix = getPropertyPrefix();
+ // Install the standard properties.
+ LookAndFeel.installColorsAndFont(textComponent, prefix + ".background",
+ prefix + ".foreground", prefix + ".font");
+ LookAndFeel.installBorder(textComponent, prefix + ".border");
+ textComponent.setMargin(UIManager.getInsets(prefix + ".margin"));
+
+ // Some additional text component only properties.
+ Color color = textComponent.getCaretColor();
+ if (color == null || color instanceof UIResource)
+ {
+ color = UIManager.getColor(prefix + ".caretForeground");
+ textComponent.setCaretColor(color);
+ }
+
+ // Fetch the colors for enabled/disabled text components.
+ color = textComponent.getDisabledTextColor();
+ if (color == null || color instanceof UIResource)
+ {
+ color = UIManager.getColor(prefix + ".inactiveBackground");
+ textComponent.setDisabledTextColor(color);
+ }
+ color = textComponent.getSelectedTextColor();
+ if (color == null || color instanceof UIResource)
+ {
+ color = UIManager.getColor(prefix + ".selectionForeground");
+ textComponent.setSelectedTextColor(color);
+ }
+ color = textComponent.getSelectionColor();
+ if (color == null || color instanceof UIResource)
+ {
+ color = UIManager.getColor(prefix + ".selectionBackground");
+ textComponent.setSelectionColor(color);
+ }
+
+ Insets margin = textComponent.getMargin();
+ if (margin == null || margin instanceof UIResource)
+ {
+ margin = UIManager.getInsets(prefix + ".margin");
+ textComponent.setMargin(margin);
+ }
+
Caret caret = textComponent.getCaret();
- if (caret == null)
+ if (caret == null || caret instanceof UIResource)
{
caret = createCaret();
textComponent.setCaret(caret);
+ caret.setBlinkRate(UIManager.getInt(prefix + ".caretBlinkRate"));
}
Highlighter highlighter = textComponent.getHighlighter();
- if (highlighter == null)
+ if (highlighter == null || highlighter instanceof UIResource)
textComponent.setHighlighter(createHighlighter());
- String prefix = getPropertyPrefix();
- LookAndFeel.installColorsAndFont(textComponent, prefix + ".background",
- prefix + ".foreground", prefix + ".font");
- LookAndFeel.installBorder(textComponent, prefix + ".border");
- textComponent.setMargin(UIManager.getInsets(prefix + ".margin"));
-
- caret.setBlinkRate(UIManager.getInt(prefix + ".caretBlinkRate"));
-
- // Fetch the colors for enabled/disabled text components.
- background = UIManager.getColor(prefix + ".background");
- inactiveBackground = UIManager.getColor(prefix + ".inactiveBackground");
- textComponent.setDisabledTextColor(UIManager.getColor(prefix
- + ".inactiveForeground"));
- textComponent.setSelectedTextColor(UIManager.getColor(prefix
- + ".selectionForeground"));
- textComponent.setSelectionColor(UIManager.getColor(prefix
- + ".selectionBackground"));
}
/**
* This FocusListener triggers repaints on focus shift.
*/
private FocusListener focuslistener = new FocusListener() {
public void focusGained(FocusEvent e)
{
textComponent.repaint();
}
@@ -663,32 +690,20 @@
}
}
};
/**
* Install all listeners on the text component.
*/
protected void installListeners()
{
textComponent.addFocusListener(focuslistener);
- textComponent.addPropertyChangeListener(updateHandler);
- installDocumentListeners();
- }
-
- /**
- * Installs the document listeners on the textComponent's model.
- */
- private void installDocumentListeners()
- {
- Document doc = textComponent.getDocument();
- if (doc != null)
- doc.addDocumentListener(documentHandler);
}
/**
* Returns the name of the keymap for this type of TextUI.
*
* This is implemented so that the classname of this TextUI
* without the package prefix is returned. This way subclasses
* don't have to override this method.
*
* @return the name of the keymap for this TextUI
@@ -838,23 +853,21 @@
{
// Do nothing here.
}
/**
* Uninstalls all listeners that have previously been installed by
* this UI.
*/
protected void uninstallListeners()
{
- textComponent.removePropertyChangeListener(updateHandler);
textComponent.removeFocusListener(focuslistener);
- textComponent.getDocument().removeDocumentListener(documentHandler);
}
/**
* Uninstalls all keyboard actions that have previously been installed by
* this UI.
*/
protected void uninstallKeyboardActions()
{
SwingUtilities.replaceUIInputMap(textComponent, JComponent.WHEN_FOCUSED,
null);
@@ -1354,21 +1367,20 @@
protected void modelChanged()
{
if (textComponent == null || rootView == null)
return;
ViewFactory factory = rootView.getViewFactory();
if (factory == null)
return;
Document doc = textComponent.getDocument();
if (doc == null)
return;
- installDocumentListeners();
Element elem = doc.getDefaultRootElement();
if (elem == null)
return;
View view = factory.create(elem);
setView(view);
}
/**
* Receives notification whenever one of the text component's bound
* properties changes. This default implementation does nothing.
Index: javax/swing/plaf/basic/BasicTextFieldUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTextFieldUI.java,v
retrieving revision 1.8
diff -u -1 -0 -r1.8 BasicTextFieldUI.java
--- javax/swing/plaf/basic/BasicTextFieldUI.java 20 Apr 2006 19:21:08 -0000 1.8
+++ javax/swing/plaf/basic/BasicTextFieldUI.java 21 Jun 2006 12:56:12 -0000
@@ -31,20 +31,21 @@
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.awt.Color;
import java.beans.PropertyChangeEvent;
import javax.swing.JComponent;
import javax.swing.UIDefaults;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.text.Element;
import javax.swing.text.FieldView;
import javax.swing.text.View;
@@ -87,19 +88,31 @@
*
* <p>The colors are only changed if they are not a
* <code>ColorUIResource</code>.</p>
*
* @param event the property change event
*/
protected void propertyChange(PropertyChangeEvent event)
{
if (event.getPropertyName().equals("editable"))
{
- boolean editable = ((Boolean) event.getNewValue()).booleanValue();
-
// Changing the color only if the current background is an instance of
// ColorUIResource is the behavior of the RI.
if (textComponent.getBackground() instanceof ColorUIResource)
- textComponent.setBackground(editable ? background : inactiveBackground);
+ {
+ Color c = null;
+ Color old = textComponent.getBackground();
+ String prefix = getPropertyPrefix();
+ if (! textComponent.isEnabled())
+ c = SharedUIDefaults.getColor(prefix + ".disabledBackground");
+ if (c == null && ! textComponent.isEditable())
+ c = SharedUIDefaults.getColor(prefix + ".inactiveBackground");
+ if (c == null)
+ c = SharedUIDefaults.getColor(prefix + ".background");
+ if (c != null && c != old)
+ {
+ textComponent.setBackground(c);
+ }
+ }
}
}
}
Index: javax/swing/plaf/basic/SharedUIDefaults.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/SharedUIDefaults.java,v
retrieving revision 1.1
diff -u -1 -0 -r1.1 SharedUIDefaults.java
--- javax/swing/plaf/basic/SharedUIDefaults.java 6 Jun 2006 10:46:43 -0000 1.1
+++ javax/swing/plaf/basic/SharedUIDefaults.java 21 Jun 2006 12:56:12 -0000
@@ -31,20 +31,21 @@
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.awt.Color;
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])
@@ -68,11 +69,23 @@
static Object get(String key)
{
Object o = sharedDefaults.get(key);
if (o == null)
{
o = UIManager.get(key);
sharedDefaults.put(key, o);
}
return o;
}
+
+ /**
+ * Returns a shared UI color.
+ *
+ * @param key the key
+ *
+ * @return the shared color instance
+ */
+ static Color getColor(String key)
+ {
+ return (Color) get(key);
+ }
}
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
