Swing Synth Skin: Can you override Styles?
I have a style defined for a JLabel:
<style id="myLabel">
<state>
<opaque value="true"/>
<color value="blue" type="BACKGROUND"/>
<color value="red" type="FOREGROUND"/>
</state>
</style>
<bind style="myLabel" type="region" key="label"/>
However, I want certain JLabels (in certain JPanels) to be styled completely differently:
<style id="myUnrelatedPanel">
<state>
<opaque value="true"/>
<color value="green" type="BACKGROUND"/>
<color value="yellow" type="FOREGROUND"/>
</state>
</style>
<bind style="myLabel" type="name" key="_valueOfMyPanel.getName()_in_here"/>
The above does not work, because the style for the JLabel is being enforced.
Nesting styles does not appear to work. The compiler does not complain; Synth picks up the style region-anchored to 'label' and applies it t开发者_运维知识库o all labels within the stylesheet.
The following extract from the limited Java documentation on Synth suggests that it is possible to mix styles applied to Regions and Names, and that their values will be merged:
You can bind to individual, named components, whether or not they are also bound as regions. For example, suppose you want to have the "OK" and "Cancel" buttons in your GUI treated differently than all the other buttons. First, you would give the OK and Cancel buttons names, using the component.setName() method. Then, you would define three styles: one for buttons in general (region = "Button"), one for the OK button (name = "OK"), and one for the Cancel button (name = "Cancel"). Finally, you would bind these styles like this:
<bind style="styleButton" type="region" key="Button">
<bind style="styleOK" type="name" key="OK">
<bind style="styleCancel" type="name" key="Cancel">
When a component or region is bound to more than one style, the styles are merged
Note: Just as a style can be bound to multiple regions or names, multiple styles can be bound to a region or name. These multiple styles are merged for the region or name. Precedence is given to styles defined later in the file
However, it does not appear that a defined in the first Style can be overrided by a defined in the second style?
I can confirm that it is not possible to override previously used, anchored types (that is to say, you can't set a type="BACKGROUND" colour on a region key, and then set a different type="BACKGROUND" colour on a named key. Setting a different type, such as TEXT_BACKGROUND would work, as Synth merges the two styles into one. Because BACKGROUND was set in the first Style, however, you cannot set it again in the Second style.
Very disappointing. I've found a workaround, however. Set a 'default' set of colours in the catch-all, key=".*" style, and deviate from these colours in name-anchored styles:
<style id="backingStyle">
<state>
<opaque value="false"/>
<font name="Arial" size="12"/>
<color value="black" type="BACKGROUND"/>
<color value="white" type="FOREGROUND"/>
</state>
</style>
<bind style="backingStyle" type="region" key=".*"/>
and
<style id="backingStyle">
<state>
<opaque value="false"/>
<font name="Verdana" size="12"/>
<color value="blue" type="BACKGROUND"/>
<color value="green" type="FOREGROUND"/>
</state>
</style>
<bind style="backingStyle" type="name" key="nameOfMyPanel"/>
精彩评论