1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.oracle.javafx.scenebuilder.kit.fxom; 34 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.List; 38 import java.util.Map; 39 import javafx.scene.control.Accordion; 40 import javafx.scene.control.Tab; 41 import javafx.scene.control.TabPane; 42 import javafx.scene.control.TitledPane; 43 44 /** 45 * 46 */ 47 class TransientStateBackup { 48 49 private final FXOMDocument fxomDocument; 50 private final Map<FXOMObject, FXOMObject> tabPaneMap = new HashMap<>(); 51 private final Map<FXOMObject, FXOMObject> accordionMap = new HashMap<>(); 52 53 public TransientStateBackup(FXOMDocument fxomDocument) { 54 assert fxomDocument != null; 55 56 this.fxomDocument = fxomDocument; 57 58 final List<FXOMObject> candidates = new ArrayList<>(); 59 if (this.fxomDocument.getFxomRoot() != null) { 60 candidates.add(this.fxomDocument.getFxomRoot()); 61 } 62 63 while (candidates.isEmpty() == false) { 64 final FXOMObject candidate = candidates.get(0); 65 candidates.remove(0); 66 67 final Object sceneGraphObject = candidate.getSceneGraphObject(); 68 if (sceneGraphObject instanceof TabPane) { 69 final TabPane tabPane = (TabPane) sceneGraphObject; 70 final Tab currentTab = tabPane.getSelectionModel().getSelectedItem(); 71 if (currentTab != null) { 72 final FXOMObject tabObject 73 = candidate.searchWithSceneGraphObject(currentTab); 74 if (tabObject != null) { 75 tabPaneMap.put(candidate, tabObject); 76 } 77 } 78 } else if (sceneGraphObject instanceof Accordion) { 79 final Accordion accordion = (Accordion) sceneGraphObject; 80 final TitledPane currentTitledPane = accordion.getExpandedPane(); 81 if (currentTitledPane != null) { 82 final FXOMObject titledPaneObject 83 = candidate.searchWithSceneGraphObject(currentTitledPane); 84 if (titledPaneObject != null) { 85 accordionMap.put(candidate, titledPaneObject); 86 } 87 } 88 } 89 90 candidates.addAll(candidate.getChildObjects()); 91 } 92 } 93 94 public void restore() { 95 final List<FXOMObject> candidates = new ArrayList<>(); 96 if (this.fxomDocument.getFxomRoot() != null) { 97 candidates.add(this.fxomDocument.getFxomRoot()); 98 } 99 100 while (candidates.isEmpty() == false) { 101 final FXOMObject candidate = candidates.get(0); 102 candidates.remove(0); 103 104 final Object sceneGraphObject = candidate.getSceneGraphObject(); 105 if (sceneGraphObject instanceof TabPane) { 106 final TabPane tabPane = (TabPane) sceneGraphObject; 107 final FXOMObject tabObject = tabPaneMap.get(candidate); 108 if ((tabObject != null) && (tabObject.getParentObject() == candidate)) { 109 assert tabObject.getSceneGraphObject() instanceof Tab; 110 final Tab tab = (Tab) tabObject.getSceneGraphObject(); 111 assert tabPane.getTabs().contains(tab); 112 tabPane.getSelectionModel().select(tab); 113 } 114 } else if (sceneGraphObject instanceof Accordion) { 115 final Accordion accordion = (Accordion) sceneGraphObject; 116 final FXOMObject titlePaneObject = accordionMap.get(candidate); 117 if ((titlePaneObject != null) && (titlePaneObject.getParentObject() == candidate)) { 118 assert titlePaneObject.getSceneGraphObject() instanceof TitledPane; 119 final TitledPane titledPane = (TitledPane) titlePaneObject.getSceneGraphObject(); 120 assert accordion.getPanes().contains(titledPane); 121 accordion.setExpandedPane(titledPane); 122 } 123 } 124 125 candidates.addAll(candidate.getChildObjects()); 126 } 127 } 128 }