-------
Introduce a new Skin.install() method with an empty default implementation. Modify Control.setSkin(Skin) implementation to invoke install() on the new skin after the old skin has been removed with dispose().
Problem
-------
Presently, switching skins is a two-step process: first, a new skin is constructed against the target Control instance, and is attached (i.s. listeners added, child nodes added) to that instance in the constructor. Then, Control.setSkin() is invoked with a new skin - and inside, the old skin is detached via its dispose() method.
This creates two problems:
1. if the new skin instance is discarded before setSkin(), it remains attached, leaving the control in a weird state with two skins attached, causing memory leaks and performance degradation.
2. if, in addition to adding listeners and child nodes, the skin sets a property, such as an event listener, or a handler, it overwrites the current value irreversibly. As a result, either the old skin would not be able to cleanly remove itself, or the new skin would not be able to set the new values, as it does not know whether it should overwrite or keep a handler installed earlier (possibly by design). Unsurprisingly, this also might cause memory leaks.
A number of related bugs have been collected under JDK-8241364 ☂ Cleanup skin implementations to allow switching, which refers a number of bugs:
JDK-8245303 InputMap: memory leak due to incomplete cleanup on remove mapping
Solution
--------
This problem does not exist in e.g. Swing because the steps of instantiation, uninstalling the old ComponentUI ("skin"), and installing a new skin are cleanly separated. ComponentUI constructor does not alter the component itself, ComponentUI.uninstallUI(JComponent) cleanly removes the old skin, ComponentUI.installUI(JComponent) installs the new skin. We should follow the same model in javafx.
Specifically, I'd like to propose the following changes:
1. Add Skin.install() with a default no-op implementation.
2. Clarify skin life cycle (creation-attachment-detachment sequence) in Skin and Skin.install() javadoc
3. Modify Control.setSkin(Skin) method (== invalidate listener in skin property) to call oldSkin.dispose() followed by newSkin.install()
4. Check whether (newSkin.getSkinnable() == this) inside of Control.setSkin(Skin) property handler, and throw an Error if it isn't so (when newSkin is != null of course).
5. Many existing skins that do not set properties in the corresponding control may remain unchanged. The skins that do, such as TextInputControlSkin (
Impact Analysis
-------------
see corresponding CSR
Specification
-------------
see corresponding CSR
- blocks
-
JDK-8295531 ComboBoxBaseSkin: memory leak when changing skin
- Resolved
-
JDK-8268877 TextInputControlSkin: incorrect inputMethod event handler after switching skin
- Resolved
- csr for
-
JDK-8292213 Add Skin.install() method
- Closed
- relates to
-
JDK-8245303 InputMap: memory leak due to incomplete cleanup on remove mapping
- Open
-
JDK-8245145 Spinner: throws IllegalArgumentException when replacing skin
- Resolved
-
JDK-8268877 TextInputControlSkin: incorrect inputMethod event handler after switching skin
- Resolved
-
JDK-8260364 Doc error: Skinnable methods over-specified to Control
- Closed
-
JDK-8241364 ☂ Cleanup skin implementations to allow switching
- Open
-
JDK-8295754 PaginationSkin: memory leak when changing skin
- Resolved