Commit e62210ee authored by legoc's avatar legoc
Browse files

Implemented model modification and confirmation to save the model

parent 17f3b80e
package fr.ill.ics.n3d;
import java.io.File;
import java.util.Optional;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
......@@ -19,6 +20,7 @@ import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.geometry.Point3D;
......@@ -32,8 +34,11 @@ import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.MenuBar;
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;
......@@ -42,6 +47,7 @@ import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
* Main class of the Nomad 3D Editor.
......@@ -97,6 +103,9 @@ public class Nomad3DEditor extends Application {
/** Mouse previous cursor. */
private Cursor mouseOldCursor = null;
/** The editor menu bar. */
private EditorMenuBar menuBar;
/** Title of the editor's window. */
public final static String TITLE = "Nomad 3D editor";
......@@ -183,6 +192,31 @@ public class Nomad3DEditor extends Application {
// ClassLoader classLoader = getClass().getClassLoader();
// scene.getStylesheets().add(classLoader.getResource("Style.css").toExternalForm());
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
if (menuBar.isModified()) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Model Modified");
alert.setHeaderText("Save your current modifications?");
Optional<ButtonType> option = alert.showAndWait();
if (option.get() == null) {
return;
}
else if (option.get() == ButtonType.OK) {
// Serialize the operations.
OperationsSerializer serializer = new OperationsSerializer();
serializer.write(Nomad3DEditor.getInstance().getModel());
}
}
}
});
this.stage = stage;
stage.setTitle(TITLE);
......@@ -372,7 +406,7 @@ public class Nomad3DEditor extends Application {
* @return The menu bar
*/
private MenuBar buildMenuBar() {
MenuBar menuBar = new EditorMenuBar();
menuBar = new EditorMenuBar();
return menuBar;
}
......
......@@ -109,8 +109,12 @@ public class EditorMenuBar extends MenuBar {
/** Currently displayed configuration. */
private String activeConfiguration;
/** The active level of detail. */
private int activeLod;
/** True if there are some operations not saved. */
private boolean modified = false;
private class SubdividedChild {
SimpleStringProperty name = new SimpleStringProperty();
SimpleStringProperty path = new SimpleStringProperty();
......@@ -125,9 +129,6 @@ public class EditorMenuBar extends MenuBar {
/**
* Constructor.
*
* @param Nomad3DEditor.getInstance()
* Editor owning the menu bar
*/
public EditorMenuBar() {
this.showAxes = true;
......@@ -144,6 +145,32 @@ public class EditorMenuBar extends MenuBar {
openItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Check if the there are current modifications.
if (modified) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Model Modified");
alert.setHeaderText("Save your current modifications?");
Optional<ButtonType> option = alert.showAndWait();
if (option.get() == null) {
return;
}
else if (option.get() == ButtonType.OK) {
// Serialize the operations.
OperationsSerializer serializer = new OperationsSerializer();
serializer.write(Nomad3DEditor.getInstance().getModel());
}
}
// Reset modified to false.
modified = false;
// Reset the operations.
Operations.getInstance().reset();
// Load the scene.
Scene scene = Nomad3DEditor.getInstance().loadScene();
if (scene != null) {
Nomad3DEditor.getInstance().getStage().setScene(scene);
......@@ -162,6 +189,9 @@ public class EditorMenuBar extends MenuBar {
// Serialize the operations.
OperationsSerializer serializer = new OperationsSerializer();
serializer.write(Nomad3DEditor.getInstance().getModel());
// Change the title.
updateModified(false);
}
}
});
......@@ -175,7 +205,10 @@ public class EditorMenuBar extends MenuBar {
// Undo the last operation.
Operations.getInstance().undoLastOperation(Nomad3DEditor.getInstance().getModel());
// Updates the scene.
// Change the title.
updateModified(true);
// Update the scene.
updateScene();
});
......@@ -682,6 +715,9 @@ public class EditorMenuBar extends MenuBar {
reversedDirection2Check.isSelected(),
selectedAxis.getZeroValue(),
controllerName));
// Change the title.
updateModified(true);
}
else {
System.out.println("Axis NOT modified");
......@@ -870,6 +906,9 @@ public class EditorMenuBar extends MenuBar {
// Save the operation.
Operations.getInstance().addOperation(new SetMaterialOperation(newMaterial, selectedComp.getName()));
// Change the title.
updateModified(true);
}
Nomad3DEditor.getInstance().getModel().clearSelection();
......@@ -1721,6 +1760,29 @@ public class EditorMenuBar extends MenuBar {
this.getMenus().addAll(this.file, this.edit, this.view, this.selection, this.help);
}
/**
* Sets the modified flag. Updates the title with a modification or not.
*/
private void updateModified(boolean modified) {
this.modified = modified;
if (modified) {
Nomad3DEditor.getInstance().getStage().setTitle(Nomad3DEditor.getInstance().getModel().getName() + "* - " + Nomad3DEditor.TITLE);
}
else {
Nomad3DEditor.getInstance().getStage().setTitle(Nomad3DEditor.getInstance().getModel().getName() + " - " + Nomad3DEditor.TITLE);
}
}
/**
* Returns true if the model is modified.
* @return True if modified.
*/
public boolean isModified() {
return modified;
}
/**
* Calculates the zero value of an axis from its reference axes.
......@@ -1850,6 +1912,7 @@ public class EditorMenuBar extends MenuBar {
/**
* Updates the menu bar and the Nomad3DEditor.getInstance() accordingly.
*/
private void update() {
updateLodMenu();
updateConfigMenus();
......@@ -1983,4 +2046,5 @@ public class EditorMenuBar extends MenuBar {
updateSystem();
updateFocus();
}
}
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