changeset 4035:07ee4aa067ee

3DViewer: Improved session settings persistence.
author Alexander Kouznetsov
date Fri, 21 Jun 2013 16:30:22 -0700
parents 35d6c5456e67
children 23c6f56490ca
files apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/MainController.java apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/SessionManager.java
diffstat 2 files changed, 18 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/MainController.java	Fri Jun 21 16:09:16 2013 -0700
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/MainController.java	Fri Jun 21 16:30:22 2013 -0700
@@ -173,6 +173,8 @@
                     }
                 });
 
+        sessionManager.bind(settingsBtn.selectedProperty(), "settingsBtn");
+        sessionManager.bind(splitPane.getDividers().get(0).positionProperty(), "settingsSplitPanePosition");
         sessionManager.bind(optimizeCheckBox.selectedProperty(), "optimize");
         sessionManager.bind(loadAsPolygonsCheckBox.selectedProperty(), "loadAsPolygons");
         sessionManager.bind(loopBtn.selectedProperty(), "loop");
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/SessionManager.java	Fri Jun 21 16:09:16 2013 -0700
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/SessionManager.java	Fri Jun 21 16:30:22 2013 -0700
@@ -37,9 +37,10 @@
 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.DoubleProperty;
 import javafx.beans.property.ObjectProperty;
+import javafx.beans.value.ChangeListener;
+import javafx.beans.value.ObservableValue;
 import javafx.scene.control.Accordion;
 import javafx.scene.control.TitledPane;
-import javafx.scene.control.Toggle;
 import javafx.scene.control.ToggleGroup;
 import javafx.scene.paint.Color;
 
@@ -137,23 +138,22 @@
     }
 
     public void bind(final ToggleGroup toggleGroup, final String propertyName) {
-        String selectedToggle = props.getProperty(propertyName);
-        for (Toggle t : toggleGroup.getToggles()) {
-            if (t.getUserData() != null && t.getUserData().equals(selectedToggle)) {
-                if (toggleGroup.getSelectedToggle() != t) {
-                    toggleGroup.selectToggle(t);
-                }
-                break;
+        try {
+            String value = props.getProperty(propertyName);
+            if (value != null) {
+                int selectedToggleIndex = Integer.parseInt(value);
+                toggleGroup.selectToggle(toggleGroup.getToggles().get(selectedToggleIndex));
             }
+        } catch (Exception ignored) {
         }
         toggleGroup.selectedToggleProperty().addListener(new InvalidationListener() {
-        
+
             @Override
             public void invalidated(Observable o) {
                 if (toggleGroup.getSelectedToggle() == null) {
                     props.remove(propertyName);
                 } else {
-                    props.setProperty(propertyName, toggleGroup.getSelectedToggle().getUserData().toString());
+                    props.setProperty(propertyName, Integer.toString(toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle())));
                 }
             }
         });
@@ -162,16 +162,18 @@
     public void bind(final Accordion accordion, final String propertyName) {
         Object selectedPane = props.getProperty(propertyName);
         for (TitledPane tp : accordion.getPanes()) {
-            if (tp.getText().equals(selectedPane)) {
+            if (tp.getText() != null && tp.getText().equals(selectedPane)) {
                 accordion.setExpandedPane(tp);
                 break;
             }
         }
-        accordion.expandedPaneProperty().addListener(new InvalidationListener() {
+        accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
 
             @Override
-            public void invalidated(Observable o) {
-//                props.setProperty(propertyName, accordion.getExpandedPane().getText());
+            public void changed(ObservableValue<? extends TitledPane> ov, TitledPane t, TitledPane expandedPane) {
+                if (expandedPane != null) {
+                    props.setProperty(propertyName, expandedPane.getText());
+                }
             }
         });
     }