changeset 5284:093616690092

Ensemble8: Fix for RT-30138 Ensemble8: Stacked Bar Chart: class cast exception
author Alexander Kouznetsov
date Mon, 07 Oct 2013 15:02:55 +0400
parents b20673c09b07
children 8f1dc4526871
files apps/samples/Ensemble8/src/app/java/ensemble/samplepage/XYDataVisualizer.java
diffstat 1 files changed, 34 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/apps/samples/Ensemble8/src/app/java/ensemble/samplepage/XYDataVisualizer.java	Mon Oct 07 13:25:02 2013 +0200
+++ b/apps/samples/Ensemble8/src/app/java/ensemble/samplepage/XYDataVisualizer.java	Mon Oct 07 15:02:55 2013 +0400
@@ -72,6 +72,7 @@
 public class XYDataVisualizer<X, Y> extends TreeTableView<XYChartItem<X, Y>> {
     
     XYChart<X, Y> chart;
+    private Class<?> clzX;
     double minY, maxY;
 
     public XYDataVisualizer(final XYChart<X, Y> chart) {
@@ -128,22 +129,37 @@
 
             @Override
             public TreeTableCell<XYChartItem<X, Y>, X> call(TreeTableColumn<XYChartItem<X, Y>, X> p) {
-                return new TextFieldTreeTableCell<>(new StringConverter<X>() {
+                return new TextFieldTreeTableCell<XYChartItem<X, Y>, X>() {
+                    {
+                        setConverter(new StringConverter<X>() {
+                            @Override
+                            public String toString(X t) {
+                                return t == null ? null : t.toString();
+                            }
 
-                    @Override
-                    public String toString(X t) {
-                        return t == null ? null : t.toString();
+                            @Override
+                            public X fromString(String string) {
+                                if (string == null) {
+                                    return null;
+                                }
+                                try {
+                                    if (clzX.isAssignableFrom(String.class)) {
+                                        return (X) string;
+                                    } else if (clzX.isAssignableFrom(Double.class)) {
+                                        return (X) new Double(string);
+                                    } else if (clzX.isAssignableFrom(Integer.class)) {
+                                        return (X) new Integer(string);
+                                    }
+                                } catch (NumberFormatException ex) {
+                                    new IllegalArgumentException("Failed to parse " + string + " to type " + clzX, ex).printStackTrace(System.err);
+                                    return getItem();
+                                }
+                                new IllegalStateException("This valueX type is not supported: " + clzX).printStackTrace(System.err);
+                                return getItem();
+                            }
+                        });
                     }
-
-                    @Override
-                    public X fromString(String string) {
-                        if (string == null) {
-                            return null;
-                        }
-                        X x = (X) new Double(string);
-                        return x;
-                    }
-                });
+                };
             }
         });
         xValueColumn.setEditable(true);
@@ -429,6 +445,10 @@
         boolean editable = true;
         for (Series<X, Y> series : chart.getData()) {
             for (XYChart.Data<X, Y> data : series.getData()) {
+                X x = data.getXValue();
+                if (x != null) {
+                    clzX = x.getClass();
+                }
                 Y y = data.getYValue();
                 if (y != null) {
                     if (chart.getYAxis() instanceof NumberAxis) {