This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit c12f80c853a733b2e0c53a3e906b570921741cdb Author: Martin Desruisseaux <[email protected]> AuthorDate: Fri Jan 29 11:28:15 2021 +0100 Refactor `ValueColorMapper` as a `Widget` subclass. The intent is to allow more complex GUI later (with controls for specifying a range of isoline levels instead than entering values one-by-one). --- .../apache/sis/gui/coverage/CoverageControls.java | 7 +- .../apache/sis/gui/coverage/IsolineRenderer.java | 6 +- .../sis/internal/gui/control/ValueColorMapper.java | 96 ++++++++++++++++------ .../internal/gui/control/ValueColorMapperApp.java | 11 ++- 4 files changed, 81 insertions(+), 39 deletions(-) diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java index 949193a..2fce8fa 100644 --- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java +++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/CoverageControls.java @@ -149,11 +149,10 @@ final class CoverageControls extends Controls { */ final VBox isolinesPane; { // Block for making variables locale to this scope. - final ValueColorMapper mapper = new ValueColorMapper(); - final TableView<ValueColorMapper.Step> table = mapper.createIsolineTable(vocabulary); + final ValueColorMapper mapper = new ValueColorMapper(vocabulary); isolines = new IsolineRenderer(view); - isolines.setIsolineTables(java.util.Collections.singletonList(table)); - isolinesPane = new VBox(table); // TODO: add band selector + isolines.setIsolineTables(java.util.Collections.singletonList(mapper.getSteps())); + isolinesPane = new VBox(mapper.getView()); // TODO: add band selector } /* * Put all sections together and have the first one expanded by default. diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/IsolineRenderer.java b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/IsolineRenderer.java index ebb3e83..d903b71 100644 --- a/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/IsolineRenderer.java +++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/coverage/IsolineRenderer.java @@ -114,10 +114,10 @@ final class IsolineRenderer { /** * Sets the isoline values for all bands from the content of tables edited by user. - * This method registers listener on the given tables for repainting the isolines + * This method registers listener on the given lists for repainting the isolines * when table content changed. */ - public void setIsolineTables(final List<TableView<Step>> tables) { + public void setIsolineTables(final List<ObservableList<Step>> tables) { if (bands != null) { for (final Band band : bands) { band.dispose(); @@ -127,7 +127,7 @@ final class IsolineRenderer { if (tables != null && !tables.isEmpty()) { bands = new Band[tables.size()]; for (int i=0; i<bands.length; i++) { - bands[i] = new Band(tables.get(i).getItems()); + bands[i] = new Band(tables.get(i)); } } } diff --git a/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/control/ValueColorMapper.java b/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/control/ValueColorMapper.java index c560422..0c09e9c 100644 --- a/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/control/ValueColorMapper.java +++ b/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/control/ValueColorMapper.java @@ -32,9 +32,11 @@ import javafx.scene.input.KeyEvent; import javafx.scene.control.TableView; import javafx.scene.control.TableColumn; import javafx.scene.control.cell.CheckBoxTableCell; +import javafx.scene.layout.Region; import org.apache.sis.internal.gui.Styles; import org.apache.sis.internal.util.Numerics; import org.apache.sis.util.resources.Vocabulary; +import org.apache.sis.gui.Widget; /** @@ -46,7 +48,7 @@ import org.apache.sis.util.resources.Vocabulary; * @since 1.1 * @module */ -public final class ValueColorMapper extends ColorColumnHandler<ValueColorMapper.Step> { +public final class ValueColorMapper extends Widget { /** * Colors to associate to a given value. * @@ -155,46 +157,87 @@ public final class ValueColorMapper extends ColorColumnHandler<ValueColorMapper. private final NumberFormat format; /** - * Creates a new handler. + * The table showing values associated to colors. */ - public ValueColorMapper() { + private final TableView<Step> table; + + /** + * Creates a new "value-color mapper" widget. + * + * @param vocabulary localized resources, given because already known by the caller + * (this argument would be removed if this constructor was public API). + */ + public ValueColorMapper(final Vocabulary vocabulary) { format = NumberFormat.getInstance(); + table = createIsolineTable(vocabulary); } /** - * Returns the colors to apply for the given step, or {@code null} for transparent. - * This method is defined for safety but should not be invoked; use {@link #getObservableValue(S)} instead. + * Returns the encapsulated JavaFX component to add in a scene graph for making the table visible. + * The {@code Region} subclass is implementation dependent and may change in any future SIS version. * - * @param level the value for which to get the color to show in color cell. + * @return the JavaFX component to insert in a scene graph. */ @Override - protected int[] getARGB(final Step level) { - final ColorRamp r = level.color.get(); - return (r != null) ? r.colors : null; + public Region getView() { + return table; } /** - * Returns the color associated to given row as an observable value. + * Returns the "value-color" entries shown in the table. * - * @param level the value for which to get the color to show in color cell. - * @return the color(s) to use for the given value, or {@code null} if none (transparent). + * @return the "value-color" entries. */ - @Override - protected ObservableValue<ColorRamp> getObservableValue(final Step level) { - return level.color; + public ObservableList<Step> getSteps() { + return table.getItems(); } /** - * Invoked when users confirmed that (s)he wants to use the selected colors. - * - * @param level the value for which to assign new color(s). - * @param colors the new color for the given value, or {@code null} for resetting default value. - * @return the type of color (always solid for this class). + * Manages the {@link TableColumn} showing colors. + * This class uses only solid colors (no gradients). */ - @Override - protected ColorRamp.Type applyColors(final Step level, ColorRamp colors) { - level.color.set(colors); - return ColorRamp.Type.SOLID; + private static final class ColumnHandler extends ColorColumnHandler<Step> { + /** + * Creates a new handler for the color column. + */ + ColumnHandler() { + } + + /** + * Returns the colors to apply for the given step, or {@code null} for transparent. + * This method is defined for safety but should not be invoked; use {@link #getObservableValue(S)} instead. + * + * @param level the value for which to get the color to show in color cell. + */ + @Override + protected int[] getARGB(final Step level) { + final ColorRamp r = level.color.get(); + return (r != null) ? r.colors : null; + } + + /** + * Returns the color associated to given row as an observable value. + * + * @param level the value for which to get the color to show in color cell. + * @return the color(s) to use for the given value, or {@code null} if none (transparent). + */ + @Override + protected ObservableValue<ColorRamp> getObservableValue(final Step level) { + return level.color; + } + + /** + * Invoked when users confirmed that (s)he wants to use the selected colors. + * + * @param level the value for which to assign new color(s). + * @param colors the new color for the given value, or {@code null} for resetting default value. + * @return the type of color (always solid for this class). + */ + @Override + protected ColorRamp.Type applyColors(final Step level, ColorRamp colors) { + level.color.set(colors); + return ColorRamp.Type.SOLID; + } } /** @@ -292,7 +335,7 @@ public final class ValueColorMapper extends ColorColumnHandler<ValueColorMapper. * (this argument would be removed if this method was public API). * @return table of isolines. */ - public final TableView<Step> createIsolineTable(final Vocabulary vocabulary) { + private TableView<Step> createIsolineTable(final Vocabulary vocabulary) { /* * First column containing a checkbox for choosing whether the isoline should be drawn or not. * Header text is 🖉 (lower left pencil). @@ -321,7 +364,8 @@ public final class ValueColorMapper extends ColorColumnHandler<ValueColorMapper. */ final TableView<Step> table = new TableView<>(); table.getColumns().setAll(visible, level); - addColumnTo(table, vocabulary.getString(Vocabulary.Keys.Color)); + final ColumnHandler handler = new ColumnHandler(); + handler.addColumnTo(table, vocabulary.getString(Vocabulary.Keys.Color)); /* * Add an empty row that user can edit for adding new data. This row will automatically enter in edition state * when a digit is typed (this is the purpose of `trigger`). For making easier to edit the cell in current row, diff --git a/application/sis-javafx/src/test/java/org/apache/sis/internal/gui/control/ValueColorMapperApp.java b/application/sis-javafx/src/test/java/org/apache/sis/internal/gui/control/ValueColorMapperApp.java index c1ac37b..303a8a9 100644 --- a/application/sis-javafx/src/test/java/org/apache/sis/internal/gui/control/ValueColorMapperApp.java +++ b/application/sis-javafx/src/test/java/org/apache/sis/internal/gui/control/ValueColorMapperApp.java @@ -21,7 +21,7 @@ import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.control.Button; -import javafx.scene.control.TableView; +import javafx.scene.layout.Region; import javafx.scene.layout.BorderPane; import javafx.application.Application; import org.apache.sis.util.resources.Vocabulary; @@ -65,15 +65,14 @@ public final strictfp class ValueColorMapperApp extends Application { /** * Creates a table with arbitrary isolines to show. */ - private static TableView<ValueColorMapper.Step> createIsolineTable() { - final ValueColorMapper handler = new ValueColorMapper(); - final TableView<ValueColorMapper.Step> table = handler.createIsolineTable(Vocabulary.getResources((Locale) null)); - table.getItems().setAll( + private static Region createIsolineTable() { + final ValueColorMapper handler = new ValueColorMapper(Vocabulary.getResources((Locale) null)); + handler.getSteps().setAll( new ValueColorMapper.Step( 10, Color.BLUE), new ValueColorMapper.Step( 25, Color.GREEN), new ValueColorMapper.Step( 50, Color.ORANGE), new ValueColorMapper.Step(100, Color.RED), new ValueColorMapper.Step()); // Empty row for inserting new values. - return table; + return handler.getView(); } }
