Class StylePolicy
Setting a style is two separate questions, and only the first is about the
language. Which call to write — style.setColor(…) or
style.set("color", …) — is settled by Vaadin's Style API and
by how the property is spelled, and the answer is the same whether the file
is Java or Kotlin. Where in the source that call goes, and how to splice it
in, is not.
This class holds the first kind. It exists because the second Kotlin rewriter
would otherwise have to restate it, and a restatement that drifts is worse
than a duplicate: the two languages would disagree about what
align-items is called, and a style written in one would not be found
again by the other.
No JavaParser, no PSI. That is the invariant that makes this
shareable, and it is checked by eye rather than by tooling: an
Expression or a Kt* type appearing in this file means the
decision and the syntax have been mixed up again.
The reflection helpers it calls out to
(JavaRewriterUtil.getSetterName(java.lang.String, java.lang.Class<?>, boolean),
JavaRewriterUtil.getPropertyName(java.lang.String),
JavaRewriterUtil.hasSetterForType(java.lang.Class<?>, java.lang.String, java.lang.Class<?>)) are pure reflection over live
classes despite their package: they read Vaadin's API, not a parse tree. The
Kotlin property locator already calls the same ones.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordThe call that sets a style property.static final recordA style stated in source.static enumWhat setting a style did to the source. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcssValueOfEnumConstant(String constantName) The CSS value an enum constant stands for.static Stringstatic StringdashSeparated(String camelCaseProperty) static booleanmatches(StylePolicy.StyleInfo style, String dashSeparatedProperty) Whether a style found in source states the given property.static StringpropertyNameOf(String setter) The property a namedStylesetter sets.static StylePolicy.StyleCallChooses how to write a style property.
-
Method Details
-
setterFor
Chooses how to write a style property.Stylehas named setters for the properties it knows —setColor,setAlignItems— and a genericset(String, String)for everything else, custom properties included. The named one is preferred because it is what a developer would write, and it is only legal when the setter actually exists and takes aString: some ofStyle's setters take an enum, and passing a CSS string to those would not compile.- Parameters:
dashSeparatedProperty- the CSS property, dash-separated- Returns:
- the call to write
-
matches
Whether a style found in source states the given property.The comparison has to be made in one spelling, because the same property reaches here both ways:
set("align-items", …)andsetAlignItems(…)are the same style and an edit to one must find the other. Dash-separated is the spelling chosen, since that is what the panel and CSS itself use.- Parameters:
style- a style read from sourcedashSeparatedProperty- the property being looked for, dash-separated- Returns:
- true if they are the same property
-
dashSeparated
- Parameters:
style- a style read from source- Returns:
- its property, dash-separated whichever way the source spelled it
-
dashSeparated
- Parameters:
camelCaseProperty- a property in camelCase, as the sizing commands send it- Returns:
- the same property dash-separated
-
propertyNameOf
The property a namedStylesetter sets.- Parameters:
setter- the setter's name, such assetAlignItems- Returns:
- the property in camelCase, such as
alignItems
-
cssValueOfEnumConstant
The CSS value an enum constant stands for.Style's enum setters name their constants after the CSS keywords, upper-cased with underscores, soSPACE_BETWEENisspace-between. The rule is Vaadin's, not ours — it matches whatStyleitself does when it writes the value out — so reading a style back has to undo it the same way.The locale is pinned, which the code this was lifted from did not do. A default-locale
toLowerCaseturnsIinto a dotlessıin Turkish, soITALICwould have read back asıtalicfor a developer whose machine is set that way.- Parameters:
constantName- the enum constant's name as the source writes it- Returns:
- the CSS value
-