changeset 5927:3bdcd01853bf

Ensemble8: Fix for RT-34713 Ensemble8: TreeTableView sample is bad in editing: edited values disappear
author Alexander Kouznetsov
date Mon, 09 Dec 2013 12:52:34 -0800
parents d337fbc19887
children 53cb68c02d07
files apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Data.java apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Inventory.java apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Part.java apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/TreeTableViewApp.java
diffstat 4 files changed, 91 insertions(+), 153 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Data.java	Mon Dec 09 12:52:34 2013 -0800
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates.
+ * All rights reserved. Use is subject to license terms.
+ *
+ * This file is available and licensed under the following license:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *  - Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the distribution.
+ *  - Neither the name of Oracle Corporation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package ensemble.samples.controls.treetableview;
+
+import javafx.beans.property.SimpleStringProperty;
+import javafx.beans.property.StringProperty;
+
+public class Data {
+
+    private final StringProperty data;
+
+    public Data(String data) {
+        this.data = new SimpleStringProperty(data);
+    }
+
+    public StringProperty dataProperty() {
+        return data;
+    }
+
+    @Override
+    public String toString() {
+        return data.get();
+    }
+}
--- a/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Inventory.java	Mon Dec 09 12:37:46 2013 -0800
+++ b/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Inventory.java	Mon Dec 09 12:52:34 2013 -0800
@@ -31,44 +31,26 @@
  */
 package ensemble.samples.controls.treetableview;
 
-import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.ObjectProperty;
-import javafx.beans.property.SimpleBooleanProperty;
 import javafx.beans.property.SimpleObjectProperty;
 import javafx.beans.property.SimpleStringProperty;
 import javafx.beans.property.StringProperty;
 
 public class Inventory {
 
-        private BooleanProperty ordered;
-        private StringProperty name;
-        private ObjectProperty ob1;
-        private StringProperty d1;
-        private StringProperty d2;
+        private final StringProperty name;
+        private final ObjectProperty data;
+        private final StringProperty notes;
 
-        public Inventory(boolean ordered, String name, String d1, String d2, Part bigPart) {
-            this.ordered = new SimpleBooleanProperty(ordered);
+        public Inventory(String name, Data data, String notes) {
             this.name = new SimpleStringProperty(name);
-            this.ob1 = new SimpleObjectProperty<>(bigPart);
-            this.d1 = new SimpleStringProperty(d1);
-            this.d2 = new SimpleStringProperty(d2);
+            this.data = new SimpleObjectProperty<>(data);
+            this.notes = new SimpleStringProperty(notes);
         }
 
-        public BooleanProperty orderedProperty() { return ordered; }
-
         public StringProperty nameProperty() { return name; }
 
-        public StringProperty p1Property() { return d1; }
-
-        public StringProperty p2Property() { return d2; }
+        public StringProperty notesProperty() { return notes; }
         
-        public ObjectProperty<Part> ob1Property() { return ob1; }
-
-        public void setName(String name) { this.name.set(name); }
-
-        public void setP1(String d1) { this.d1.set(d1); }
-
-        public void setP2(String d2) { this.d2.set(d2); }
-        
-        public void setBigPart(Part bigPart) {this.ob1.set(bigPart);}
+        public ObjectProperty<Data> dataProperty() { return data; }
 }
--- a/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/Part.java	Mon Dec 09 12:37:46 2013 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2013, Oracle and/or its affiliates.
- * All rights reserved. Use is subject to license terms.
- *
- * This file is available and licensed under the following license:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- *  - Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  - Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the distribution.
- *  - Neither the name of Oracle Corporation nor the names of its
- *    contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package ensemble.samples.controls.treetableview;
-
-import javafx.beans.property.SimpleStringProperty;
-import javafx.beans.property.StringProperty;
-
-public class Part {
-        private StringProperty data;
-        private StringProperty part1;
-        private StringProperty part2;
-
-        public Part(String data, String part1, String part2) {
-            this.data = new SimpleStringProperty(data);
-            this.part1 = new SimpleStringProperty(part1);
-            this.part2 = new SimpleStringProperty(part2);           
-        }
-
-        public StringProperty dataProperty() { return data; }
-        
-        public StringProperty part1Property() { return part1; }
-
-        public StringProperty part2Property() { return part2; }
-
-        public void setPart1(String part1) { this.part1.set(part1); }
-
-        public void setpart2(String part2) { this.part2.set(part2); }
-}
--- a/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/TreeTableViewApp.java	Mon Dec 09 12:37:46 2013 -0800
+++ b/apps/samples/Ensemble8/src/samples/java/ensemble/samples/controls/treetableview/TreeTableViewApp.java	Mon Dec 09 12:52:34 2013 -0800
@@ -32,21 +32,14 @@
 package ensemble.samples.controls.treetableview;
 
 import javafx.application.Application;
-import javafx.beans.property.ReadOnlyObjectWrapper;
-import javafx.beans.property.ReadOnlyStringWrapper;
-import javafx.beans.value.ObservableValue;
-import javafx.event.EventHandler;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
-import javafx.scene.control.SelectionMode;
 import javafx.scene.control.TreeItem;
-import javafx.scene.control.TreeTableCell;
 import javafx.scene.control.TreeTableColumn;
 import javafx.scene.control.TreeTableView;
 import javafx.scene.control.cell.TextFieldTreeTableCell;
+import javafx.scene.control.cell.TreeItemPropertyValueFactory;
 import javafx.stage.Stage;
-import javafx.util.Callback;
-import javafx.util.converter.DefaultStringConverter;
 
 /**
  * A simple implementation of TreeTableView. The Notes column is editable.
@@ -54,96 +47,63 @@
  * @sampleName TreeTableView
  * @preview preview.png
  * @see javafx.scene.control.cell.TextFieldTreeTableCell
+ * @see javafx.scene.control.cell.TreeItemPropertyValueFactory
  * @see javafx.scene.control.TreeItem
  * @see javafx.scene.control.TreeTableCell
  * @see javafx.scene.control.TreeTableColumn
  * @see javafx.scene.control.TreeTableView
+ * @see javafx.beans.property.SimpleStringProperty
+ * @see javafx.beans.property.StringProperty
+ * @see javafx.beans.property.ObjectProperty
+ * @see javafx.beans.property.SimpleObjectProperty
  * @embedded
  */
 public class TreeTableViewApp extends Application {
 
-    public Parent createContent() {
-
-        final TreeItem<Inventory> rootItem = new TreeItem<>(new Inventory(false, "Root", "Data", "Data2", new Part("data 1", "part 1", "part 2")));
-
-        final TreeItem<Inventory> child1Item = new TreeItem<>(new Inventory(true, "Child 1", "Data 1", "My notes", new Part("my data", "part 1", "part 2")));
-        final TreeItem<Inventory> child2Item = new TreeItem<>(new Inventory(false, "Child 2", "Data", "Notes", new Part("no, my data", "part 3", "part 4")));
-        TreeItem<Inventory> child3Item = new TreeItem<>(new Inventory(false, "Child 3", "Data 3", "Observations", new Part("even I have data", "part 3", "part 4")));
-
+    private TreeItem<Inventory> getData() {
+        final TreeItem<Inventory> rootItem = new TreeItem<>(
+                new Inventory("Root", new Data("Root data"), ""));
+        final TreeItem<Inventory> child1Item = new TreeItem<>(
+                new Inventory("Child 1", new Data("Child 1 data"), "My notes"));
+        final TreeItem<Inventory> child2Item = new TreeItem<>(
+                new Inventory("Child 2", new Data("Child 2 data"), "Notes"));
+        TreeItem<Inventory> child3Item = new TreeItem<>(
+                new Inventory("Child 3", new Data("Child 3 data"), "Observations"));
         rootItem.setExpanded(true);
         rootItem.getChildren().addAll(child1Item, child2Item);
         child1Item.getChildren().add(child3Item);
+        return rootItem;
+    }
 
-        // Name column
+    public Parent createContent() {
+        
         final TreeTableColumn<Inventory, String> nameColumn = new TreeTableColumn<>("Name");
         nameColumn.setEditable(false);
         nameColumn.setMinWidth(130);
-        nameColumn.setCellValueFactory(
-                new Callback<TreeTableColumn.CellDataFeatures<Inventory, String>, ObservableValue<String>>() {
-            @Override
-            public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<Inventory, String> p) {
-                Inventory inv = p.getValue().getValue();
-                return new ReadOnlyObjectWrapper(inv.nameProperty().getValue());
-            }
-        });
+        nameColumn.setCellValueFactory(new TreeItemPropertyValueFactory("name"));
 
-        // Data column
         final TreeTableColumn<Inventory, String> dataColumn = new TreeTableColumn<>("Data");
-        dataColumn.setEditable(true);
+        dataColumn.setEditable(false);
         dataColumn.setMinWidth(150);
-        dataColumn.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<Inventory, String>, ObservableValue<String>>() {
-            @Override
-            public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<Inventory, String> p) {
-                final Inventory value = p.getValue().getValue();
-                if (value.equals(rootItem.getValue())) {
-                    return new ReadOnlyStringWrapper(" ");
-                } else {
-                    return new ReadOnlyStringWrapper(value.ob1Property().getValue().dataProperty().getValue());
-                }
-            }
-        });
+        dataColumn.setCellValueFactory(new TreeItemPropertyValueFactory("data"));
 
-        // Note column
-        final TreeTableColumn<Inventory, String> noteColumn = new TreeTableColumn<>("Notes (editable)");
-        noteColumn.setEditable(true);
-        noteColumn.setMinWidth(150);
-        noteColumn.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<Inventory, String>, ObservableValue<String>>() {
-            @Override
-            public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<Inventory, String> p) {
-                final Inventory value = p.getValue().getValue();
-                if (value.equals(rootItem.getValue())) {
-                    return new ReadOnlyStringWrapper(" ");
-                } else {
-                    return new ReadOnlyStringWrapper(value.p2Property().getValue());
-                }
-            }
-        });
-        noteColumn.setOnEditCommit(new EventHandler<TreeTableColumn.CellEditEvent<Inventory, String>>() {
-            @Override
-            public void handle(TreeTableColumn.CellEditEvent<Inventory, String> t) {
-                System.out.println("Note column entry was edited. Old value = " + t.getOldValue() + " New value = " + t.getNewValue());
-            }
-        });
-        noteColumn.setCellFactory(new Callback<TreeTableColumn<Inventory, String>, TreeTableCell<Inventory, String>>() {
-            @Override
-            public TreeTableCell<Inventory, String> call(TreeTableColumn<Inventory, String> p) {
-                return new TextFieldTreeTableCell(new DefaultStringConverter());
-            }
-        });
+        final TreeTableColumn<Inventory, String> notesColumn = new TreeTableColumn<>("Notes (editable)");
+        notesColumn.setEditable(true);
+        notesColumn.setMinWidth(150);
+        notesColumn.setCellValueFactory(new TreeItemPropertyValueFactory("notes"));
+        notesColumn.setCellFactory(TextFieldTreeTableCell.<Inventory>forTreeTableColumn());
 
-        final TreeTableView treeTableView = new TreeTableView(rootItem);
+        final TreeTableView treeTableView = new TreeTableView(getData());
         treeTableView.setEditable(true);
-        treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
         treeTableView.setPrefSize(430, 200);
-        treeTableView.setLayoutX(10);
-        treeTableView.setLayoutY(10);
-        treeTableView.getColumns().setAll(nameColumn, dataColumn, noteColumn);
+        treeTableView.getColumns().setAll(nameColumn, dataColumn, notesColumn);
 
         return treeTableView;
     }
 
     @Override
     public void start(Stage primaryStage) throws Exception {
+        primaryStage.setTitle("TreeTableViewApp");
         primaryStage.setScene(new Scene(createContent()));
         primaryStage.show();
     }