Skip to content

Commit

Permalink
Replace usages of PropertyEditor with Converter. Relates to #295
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Aug 9, 2018
1 parent ea9994a commit 33dfa9c
Show file tree
Hide file tree
Showing 257 changed files with 3,245 additions and 9,891 deletions.
Expand Up @@ -5,7 +5,7 @@
Resources may be automatically injected into any instance created via the application's
`Injector`. Injection points must be annotated with `{link_injected_resource}`
which can be set on properties (Groovy), fields (Java and Groovy) and setter methods (Java and Groovy).
@InjectedResource is a perfect companion to <<_models,models>>, as the following example shows:
`@InjectedResource` is a perfect companion to <<_models,models>>, as the following example shows:

.griffon-app/resources/resources.properties
[source,java,linenums,options="nowrap"]
Expand Down Expand Up @@ -39,8 +39,8 @@ to use. These are the rules applied by the default by `{link_resources_injector}
class name of the field's owner.

You may also specify a default value if the resource definition is not found; however,
be aware that this value must be set as a String, thus guaranteeing a type conversion.
An optional `format` value may be specified as a hint to the PropertyEditor used during
be aware that this value must be set as a `String`, thus guaranteeing a type conversion.
An optional `format` value may be specified as a hint to the `Converter` used during
value conversion, for example:

.griffon-app/models/sample/SampleModel.groovy
Expand Down
@@ -1,26 +1,25 @@

[[_resources_property_editors]]
= Property Editors
[[_resources_property_converters]]
= Converters

Resource injection makes use of the http://docs.oracle.com/javase/6/docs/api/java/beans/PropertyEditor.html[PropertyEditor]
mechanism provided by the `java.beans` package. The default `{link_resources_injector}` queries
`http://docs.oracle.com/javase/6/docs/api/java/beans/PropertyEditorManager.html[PropertyEditorManager]`
Resource injection makes use of the `javax.application.converter.Converter mechanism provided by JSR-377.
The default `{link_resources_injector}` queries the application's `javax.application.converter.ConverterRegistry`
whenever a resource value must be transformed to a target type.

PropertyEditorManager provides methods for registering custom PropertyEditors; it also
follows a class naming convention to load PropertyEditors should a custom one not be
ConverterRegistry provides methods for registering custom Converters; it also
follows a class naming convention to load Converters should a custom one may not be
programmatically registered. Griffon applications will automatically load and register
PropertyEditors from the following classpath resource: `/META-INF/services/java.beans.PropertyEditor`.
Converters from the following classpath resource: `/META-INF/services/javax.application.converter.spi.ConverterProvider`.
Each line follows the format

[source]
----
target.type = full.qualified.classname
fully.qualified.classname
----

The following table enumerates the default PropertyEditors loaded by Griffon at startup.
The following table enumerates the default Converters loaded by Griffon at startup.

.Core PropertyEditor mappings
.Core Converter mappings
[cols="3*", options="header"]
|===

Expand All @@ -29,79 +28,79 @@ The following table enumerates the default PropertyEditors loaded by Griffon at
| Format

| java.lang.String
| griffon.core.editors.StringPropertyEditor
| 
| org.kordamp.jsr377.converter.spi.StringConverter
|

| java.io.File
| griffon.core.editors.FilePropertyEditor
| org.kordamp.jsr377.converter.spi.FileConverter
|

| java.net.URL
| griffon.core.editors.URLPropertyEditor
| org.kordamp.jsr377.converter.spi.URLConverter
|

| java.net.URI
| griffon.core.editors.URIPropertyEditor
| org.kordamp.jsr377.converter.spi.URIConverter
|

| java.math.BigDecimal
| griffon.core.editors.BigDecimalPropertyEditor
| org.kordamp.jsr377.converter.spi.BigDecimalConverter
| 'currency', 'percent'

| java.math.BigInteger
| griffon.core.editors.BigIntegerPropertyEditor
| org.kordamp.jsr377.converter.spi.BigIntegerConverter
| 'currency', 'percent'

| java.lang.Boolean
| griffon.core.editors.BooleanPropertyEditor
| org.kordamp.jsr377.converter.spi.BooleanConverter
| 'boolean', 'query', 'switch'

| java.lang.Byte
| griffon.core.editors.BytePropertyEditor
| org.kordamp.jsr377.converter.spi.ByteConverter
| 'currency', 'percent'

| java.lang.Short
| griffon.core.editors.ShortPropertyEditor
| org.kordamp.jsr377.converter.spi.ShortConverter
| 'currency', 'percent'

| java.lang.Integer
| griffon.core.editors.IntegerPropertyEditor
| org.kordamp.jsr377.converter.spi.IntegerConverter
| 'currency', 'percent'

| java.lang.Long
| griffon.core.editors.LongPropertyEditor
| org.kordamp.jsr377.converter.spi.LongConverter
| 'currency', 'percent'

| java.lang.Float
| griffon.core.editors.FloatPropertyEditor
| org.kordamp.jsr377.converter.spi.FloatConverter
| 'currency', 'percent'

| java.lang.Double
| griffon.core.editors.DoublePropertyEditor
| org.kordamp.jsr377.converter.spi.DoubleConverter
| 'currency', 'percent'

| java.util.Calendar
| griffon.core.editors.CalendarPropertyEditor
| org.kordamp.jsr377.converter.spi.CalendarConverter
|

| java.util.Date
| griffon.core.editors.DatePropertyEditor
| org.kordamp.jsr377.converter.spi.DateConverter
|

| java.util.Locale
| griffon.core.editors.LocalePropertyEditor
| org.kordamp.jsr377.converter.spi.LocaleConverter
| <language>[\_<country>[_<variant>]]

| java.time.LocalDate
| griffon.core.editors.LocalDatePropertyEditor
| java.time.LocalDate
| org.kordamp.jsr377.converter.spi.LocalDateConverter
|

| java.time.LocalDateTime
| griffon.core.editors.LocalDateTimePropertyEditor
| java.time.LocalDateTime
| org.kordamp.jsr377.converter.spi.LocalDateTimeConverter
|

| java.time.LocalTime
| griffon.core.editors.LocalTimePropertyEditor
| java.time.LocalTime
| org.kordamp.jsr377.converter.spi.LocalTimeConverter
|

|===
Expand All @@ -114,9 +113,9 @@ Where the following apply:
* 'switch' accepts `on` and `off` as values.

Core UI Toolkit dependencies, such as `griffon-swing`, `griffon-javafx`, and `griffon-pivot` deliver
additional PropertyEditors. The following tables summarize these additions:
additional Converters. The following tables summarize these additions:

.Swing PropertyEditor mappings
.Swing Converter mappings
[cols="3*", options="header"]
|===

Expand All @@ -125,63 +124,63 @@ additional PropertyEditors. The following tables summarize these additions:
| Format

| java.awt.Color
| griffon.swing.editors.ColorPropertyEditor
| griffon.swing.converters.ColorConverter
| #RGB ; #RGBA ; #RRGGBB; #RRGGBBAA ; Color constant

| java.awt.Dimension
| griffon.swing.editors.DimensionPropertyEditor
| griffon.swing.converters.DimensionConverter
| width, height

| java.awt.Font
| griffon.swing.editors.FontPropertyEditor
| griffon.swing.converters.FontConverter
| family-style-size

| java.awt.GradientPaint
| griffon.swing.editors.GradientPaintPropertyEditor
| griffon.swing.converters.GradientPaintConverter
| x1, y1, #RGB, x2, y2, #RGB, CYCLIC

| java.awt.Image
| griffon.swing.editors.ImagePropertyEditor
| griffon.swing.converters.ImageConverter
| path/to/image_file

| java.awt.Insets
| griffon.swing.editors.InsetsPropertyEditor
| griffon.swing.converters.InsetsConverter
| top, left, bottom, right

| java.awt.LinearGradientPaint
| griffon.swing.editors.LinearGradientPaintPropertyEditor
| griffon.swing.converters.LinearGradientPaintConverter
| xy, y1, x2, x2, [0.0, 1.0], [#RGB, #RGB], REPEAT

| java.awt.Point
| griffon.swing.editors.PointPropertyEditor
| griffon.swing.converters.PointConverter
| x, y

| java.awt.Polygon
| griffon.swing.editors.PolygonPropertyEditor
| griffon.swing.converters.PolygonConverter
| x1, y1, x2, y2, ..., xn, yn

| java.awt.RadialGradientPaint
| griffon.swing.editors.RadialGradientPaintPropertyEditor
| griffon.swing.converters.RadialGradientPaintConverter
| xy, y1, r, fx, fy, [0.0, 1.0], [#RGB, #RGB], REPEAT

| java.awt.Rectangle
| griffon.swing.editors.RectanglePropertyEditor
| griffon.swing.converters.RectangleConverter
| x, y, width, height

| java.awt.geom.Point2D
| griffon.swing.editors.Point2DPropertyEditor
| griffon.swing.converters.Point2DConverter
| x, y

| java.awt.geom.Rectangle2D
| griffon.swing.editors.Rectangle2DPropertyEditor
| griffon.swing.converters.Rectangle2DConverter
| x, y, width, height

| java.awt.image.BufferedImage
| griffon.swing.editors.BufferedImagePropertyEditor
| griffon.swing.converters.BufferedImageConverter
| path/to/image_file

| javax.swing.Icon
| griffon.swing.editors.IconPropertyEditor
| griffon.swing.converters.IconConverter
| path/to/image_file

|===
Expand All @@ -193,16 +192,16 @@ Where the following apply:
* REPEAT must be one of `MultipleGradientPaint.CycleMethod`.
* GradientPaint supports another format: x1, y1 | x2, y2, | #RGB, #RGB | CYCLIC
* Color supports all color constants defined by `griffon.swing.support.Colors`.
* All color formats are supported by gradient editors.
* All color formats are supported by gradient converters.

The following styles are supported by `FontPropertyEditor`:
The following styles are supported by `FontConverter`:

* BOLD
* ITALIC
* BOLDITALIC
* PLAIN

.JavaFX PropertyEditor mappings
.JavaFX Converter mappings
[cols="3*", options="header"]
|===

Expand All @@ -211,39 +210,39 @@ The following styles are supported by `FontPropertyEditor`:
| Format

| javafx.geometry.Dimension2D
| griffon.javafx.editors.Dimension2DPropertyEditor
| griffon.javafx.converters.Dimension2DConverter
| width, height

| javafx.geometry.Insets
| griffon.javafx.editors.InsetsPropertyEditor
| griffon.javafx.converters.InsetsConverter
| top, left, bottom, right

| javafx.geometry.Point2D
| griffon.javafx.editors.Point2DPropertyEditor
| griffon.javafx.converters.Point2DConverter
| x, y

| javafx.geometry.Rectangle2D
| griffon.javafx.editors.Rectangle2DPropertyEditor
| griffon.javafx.converters.Rectangle2DConverter
| x, y , width, height

| javafx.scene.image.Image
| griffon.javafx.editors.ImagePropertyEditor
| griffon.javafx.converters.ImageConverter
| path/to/image_file

| javafx.scene.paint.Color
| griffon.javafx.editors.ColorPropertyEditor
| griffon.javafx.converters.ColorConverter
| #RGB ; #RGBA ; #RRGGBB; #RRGGBBAA ; Color constant

| javafx.scene.paint.LinearGradient
| griffon.javafx.editors.LinearGradientPropertyEditor
| griffon.javafx.converters.LinearGradientConverter
| LinearGradient.parse()

| javafx.scene.paint.RadialGradient
| griffon.javafx.editors.RadialGradientPropertyEditor
| griffon.javafx.converters.RadialGradientConverter
| RadialGradient.parse()

| javafx.scene.paint.Paint|
| griffon.javafx.editors.PaintPropertyEditor
| griffon.javafx.converters.PaintConverter
| all Color, LinearGradient and RadialGradient formats

|===
Expand All @@ -252,7 +251,7 @@ Where the following applies:

* `R`, `G`, `B` and `A` represent an hexadecimal number.

.Pivot PropertyEditor mappings
.Pivot Converter mappings
[cols="3*", options="header"]
|===

Expand All @@ -261,23 +260,23 @@ Where the following applies:
| Format

| java.awt.Color
| griffon.pivot.editors.ColorPropertyEditor
| griffon.pivot.converters.ColorConverter
| #RGB ; #RGBA ; #RRGGBB; #RRGGBBAA ; Color constant

| org.apache.pivot.wtk.Bounds
| griffon.pivot.editors.BoundsPropertyEditor
| griffon.pivot.converters.BoundsConverter
| x, y , width, height

| org.apache.pivot.wtk.Dimensions
| griffon.pivot.editors.DimensionsPropertyEditor
| griffon.pivot.converters.DimensionsConverter
| width, height

| org.apache.pivot.wtk.Insets
| griffon.pivot.editors.InsetsPropertyEditor
| griffon.pivot.converters.InsetsConverter
| top, left, right, bottom

| org.apache.pivot.wtk.Point
| griffon.pivot.editors.PointPropertyEditor
| griffon.pivot.converters.PointConverter
| x, y

|===
Expand Down
Expand Up @@ -30,7 +30,7 @@ these methods never throw `NoSuchResourceException` nor return `null` unless the
- Object resolveResource(String key, Map args, Object defaultValue)
- Object resolveResource(String key, Map args, Locale locale, Object defaultValue)

There is also another set of methods which convert the resource value using ``PropertyEditor``s:
There is also another set of methods which convert the resource value using ``Converter``s:

- <T> T resolveResourceConverted(String key, Class<T> type)
- <T> T resolveResourceConverted(String key, Locale locale, Class<T> type)
Expand Down Expand Up @@ -154,7 +154,7 @@ computed.instance = { x, y, w, h ->
== Type Conversion

Note that the return value of `resolveResource` is marked as `Object`, but you'll get
a `String` from the first two formats. You'll have to rely on <<_resources_property_editors,property editors>>
a `String` from the first two formats. You'll have to rely on <<_resources_property_converters,property converters>>
in order to transform the value into the correct type. <<_resources_injected_resources,Injected resources>>
are automatically transformed to the expected type.

Expand All @@ -163,14 +163,13 @@ Here's how it can be done:
[source,groovy,options="nowrap"]
----
import javax.swing.Icon
import java.beans.PropertyEditor
import griffon.core.editors.PropertyEditorResolver
import javax.application.converter.Converter
import javax.application.converter.ConverterRegistry
...
Object iconValue = getApplication().getResourceResolver()
.resolveResource('menu.icon', ['large'])
PropertyEditor propertyEditor = PropertyEditorResolver.findEditor(Icon)
propertyEditor.setAsText(String.valueOf(iconValue))
Icon icon = propertyEditor.getValue()
Converter<Icon> converter = ConverterRegistry.findConverter(Icon)
Icon icon = converter.fromObject(String.valueOf(iconValue))
----

As an alternative you may call `resolveResourceConverted` instead.
Expand Down

0 comments on commit 33dfa9c

Please sign in to comment.