Commit b5b71cb8 authored by legoc's avatar legoc
Browse files

Manage the expanded state of the tree items

parent 5958a67e
package fr.ill.ics.n3d;
import java.io.File;
import java.util.HashMap;
import java.util.Optional;
import java.util.logging.FileHandler;
import java.util.logging.Level;
......@@ -36,10 +37,11 @@ import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
......@@ -379,6 +381,7 @@ public class Nomad3DEditor extends Application {
buildCamera();
TreeView<Component> hierarchyView = buildHierarchyView();
this.pane = new BorderPane();
pane.setTop(buildMenuBar());
......@@ -415,10 +418,20 @@ public class Nomad3DEditor extends Application {
/**
* Builds the hierarchy view.
* @return The trre view.
* @return The tree view.
*/
private TreeView<Component> buildHierarchyView() {
TreeView<Component> hierarchyView = (model != null && model.getRoot() != null) ? new TreeView<Component>(model.getRoot().getTreeItem()) : new TreeView<Component>();
TreeView<Component> hierarchyView = null;
if ((model != null && model.getRoot() != null)) {
hierarchyView = new TreeView<Component>(model.getRoot().getTreeItem());
expandTreeView(model.getRoot().getTreeItem());
}
else {
hierarchyView = new TreeView<Component>();
}
hierarchyView.setEditable(true);
hierarchyView.setMinWidth(100);
hierarchyView.setPrefWidth(300);
......@@ -584,4 +597,73 @@ public class Nomad3DEditor extends Application {
}
});
}
/**
* Expands all the tree item hierarchy.
* @param item The item to begin.
*/
public void expandTreeView(TreeItem<Component> item) {
if (item != null && !item.isLeaf()) {
item.setExpanded(true);
for (TreeItem<Component> child : item.getChildren()) {
expandTreeView(child);
}
}
}
/**
* Gets the current expanded state of the tree item hierarchy.
* @param item The item to begin.
* @param states The map to save the states.
*/
private void getTreeViewExpandedStates(TreeItem<Component> item, HashMap<String, Boolean> states) {
if (item != null && !item.isLeaf()) {
states.put(item.getValue().getName(), item.expandedProperty().getValue());
for (TreeItem<Component> child : item.getChildren()) {
getTreeViewExpandedStates(child, states);
}
}
}
/**
* Gets the current expanded state of the tree item hierarchy.
* @param item The item to begin.
* @return The map of the states.
*/
public HashMap<String, Boolean> getTreeViewExpandedStates(TreeItem<Component> item) {
HashMap<String, Boolean> states = new HashMap<>();
getTreeViewExpandedStates(item, states);
return states;
}
/**
* Expands all the tree item hierarchy following the initial states map.
* @param item The item to begin.
* @param states The map of the states.
*/
public void expandTreeView(TreeItem<Component> item, HashMap<String, Boolean> states) {
if (item != null && !item.isLeaf()) {
String componentName = item.getValue().getName();
if (states.containsKey(componentName)) {
item.setExpanded(states.get(componentName));
}
else {
item.setExpanded(true);
}
for (TreeItem<Component> child : item.getChildren()) {
expandTreeView(child, states);
}
}
}
}
......@@ -5,6 +5,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Optional;
......@@ -702,8 +703,6 @@ public class EditorMenuBar extends MenuBar {
// Do not save the operation if the axis is the same.
if (!selectedAxis.equals(initialAxis)) {
System.out.println("Axis modified");
// Stop the build.
Nomad3DEditor.getInstance().getAxisBuilder().stop(selectedComponent);
......@@ -745,9 +744,6 @@ public class EditorMenuBar extends MenuBar {
// Change the title.
updateModified(true);
}
else {
System.out.println("Axis NOT modified");
}
Nomad3DEditor.getInstance().getModel().clearSelection();
updateScene();
......@@ -2415,12 +2411,19 @@ public class EditorMenuBar extends MenuBar {
if (!modelLoaded()) {
return;
}
// Save the expanded state of the tree items.
HashMap<String, Boolean> itemStates = Nomad3DEditor.getInstance().getTreeViewExpandedStates(Nomad3DEditor.getInstance().getModel().getRoot().getTreeItem());
Nomad3DEditor.getInstance().getModel().clearSelection();
Nomad3DEditor.getInstance().getRoot().getChildren().remove(Nomad3DEditor.getInstance().getModel().getRoot().getSceneNode());
Nomad3DEditor.getInstance().getModel().computeSceneHierarchies(activeLod);
Nomad3DEditor.getInstance().getRoot().getChildren().add(Nomad3DEditor.getInstance().getModel().getRoot().getSceneNode());
((TreeView<Component>) Nomad3DEditor.getInstance().getPane().getLeft()).setRoot(Nomad3DEditor.getInstance().getModel().getRoot().getTreeItem());
// Restore the expanded state of the tree items.
Nomad3DEditor.getInstance().expandTreeView(Nomad3DEditor.getInstance().getModel().getRoot().getTreeItem(), itemStates);
updateAxes();
updateWalls();
updateSystem();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment