Commit b9efa28f authored by legoc's avatar legoc
Browse files

Added add component menu

parent e62210ee
......@@ -4,6 +4,7 @@ import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Optional;
......@@ -20,6 +21,7 @@ import fr.ill.ics.n3d.model.Component;
import fr.ill.ics.n3d.model.ConfigParams;
import fr.ill.ics.n3d.model.ReferenceAxis;
import fr.ill.ics.n3d.model.ReferenceAxisList;
import fr.ill.ics.n3d.operations.NewComponentOperation;
import fr.ill.ics.n3d.operations.Operations;
import fr.ill.ics.n3d.operations.OperationsSerializer;
import fr.ill.ics.n3d.operations.SetAxisOperation;
......@@ -30,7 +32,6 @@ import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
......@@ -47,14 +48,11 @@ import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TreeView;
......@@ -63,13 +61,13 @@ import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.text.TextAlignment;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
/**
* Menu bar of the Nomad3DEditor.getInstance().
......@@ -729,6 +727,7 @@ public class EditorMenuBar extends MenuBar {
valueThread.interrupt();
addStage.close();
});
Button cancelButton = new Button("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(cancelEvent -> {
......@@ -790,6 +789,8 @@ public class EditorMenuBar extends MenuBar {
addStage.show();
});
///////////////////////////////////////////////////////////////////////////////////////
// Axis properties menu
MenuItem displayAxisItem = new MenuItem("Axis Properties");
displayAxisItem.setOnAction(event -> {
if (Nomad3DEditor.getInstance().getModel() == null || Nomad3DEditor.getInstance().getRoot() == null) {
......@@ -836,7 +837,9 @@ public class EditorMenuBar extends MenuBar {
displayStage.show();
});
///////////////////////////////////////////////////////////////////////////////////////
// Material menu
MenuItem editMaterialItem = new MenuItem("Edit Material");
editMaterialItem.setAccelerator(new KeyCodeCombination(KeyCode.M, KeyCombination.CONTROL_DOWN));
editMaterialItem.setOnAction(new EventHandler<ActionEvent>() {
......@@ -915,6 +918,7 @@ public class EditorMenuBar extends MenuBar {
modifyStage.close();
}
});
Button cancelButton = new Button("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
......@@ -928,6 +932,128 @@ public class EditorMenuBar extends MenuBar {
modifyStage.showAndWait();
}
});
///////////////////////////////////////////////////////////////////////////////////////
// Add component menu
MenuItem addComponentItem = new MenuItem("Add Component");
addComponentItem.setOnAction(event -> {
if (!modelLoaded()) {
return;
}
///////////////////////////////////////////////////////////////////////////////////
// Name
Label nameLabel = new Label("Name : ");
TextField nameField = new TextField("");
nameField.setAlignment(Pos.TOP_LEFT);
nameField.setPrefColumnCount(25);
///////////////////////////////////////////////////////////////////////////////////
// Parent
Label parentLabel = new Label("Parent : ");
ObservableList<String> assemblyComponents = FXCollections.observableArrayList(getAssemblyComponentNames());
ComboBox<String> parentBox = new ComboBox<String>(assemblyComponents);
parentBox.getSelectionModel().select(0);
///////////////////////////////////////////////////////////////////////////////////
// Layout
GridPane scenePane = new GridPane();
scenePane.setAlignment(Pos.CENTER);
Scene scene = new Scene(scenePane);
Stage stage = new Stage(StageStyle.UTILITY);
stage.setTitle("Add Component");
stage.setScene(scene);
stage.initOwner(Nomad3DEditor.getInstance().getStage());
GridPane mainPane = new GridPane();
mainPane.setVgap(5);
mainPane.setHgap(10);
mainPane.setAlignment(Pos.CENTER);
scenePane.addRow(0, mainPane);
GridPane.setMargin(mainPane, new Insets(10));
GridPane addPane = new GridPane();
addPane.setVgap(5);
addPane.setHgap(10);
addPane.setAlignment(Pos.CENTER);
scenePane.addRow(0, addPane);
GridPane.setMargin(addPane, new Insets(10));
addPane.addRow(0, nameLabel, nameField);
addPane.addRow(1, parentLabel, parentBox);
Button okButton = new Button("OK");
okButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Get the names.
String newComponentName = nameField.getText();
String parentComponentName = parentBox.getSelectionModel().getSelectedItem();
boolean error = false;
String errorMessage = null;
// Check the name.
if (nameExists(newComponentName)) {
error = true;
errorMessage = "The component name already exists.";
}
if (newComponentName.isEmpty()) {
error = true;
errorMessage = "The name is empty.";
}
// Check the name.
if (error) {
Alert emptyAlert = new Alert(AlertType.WARNING);
emptyAlert.setTitle("Bad name");
emptyAlert.setContentText(errorMessage);
emptyAlert.showAndWait();
return;
}
Component parent = Nomad3DEditor.getInstance().getModel().findComponent(parentComponentName);
parent.addComponent(newComponentName);
// Update the scene.
updateScene();
// Save the operation.
Operations.getInstance().addOperation(new NewComponentOperation(newComponentName, parentComponentName));
// Change the title.
updateModified(true);
// Close the stage.
stage.close();
}
});
Button cancelButton = new Button("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
stage.close();
}
});
GridPane buttonPane = new GridPane();
buttonPane.setHgap(10);
buttonPane.setAlignment(Pos.CENTER);
buttonPane.addRow(0, okButton, cancelButton);
mainPane.addRow(0, addPane);
mainPane.addRow(1, buttonPane);
stage.showAndWait();
});
MenuItem regroupComponentsItem = new MenuItem("Regroup Components");
regroupComponentsItem.setOnAction(event -> {
if (!modelLoaded()) {
......@@ -1483,7 +1609,7 @@ public class EditorMenuBar extends MenuBar {
Menu componentsMenu = new Menu("Components");
componentsMenu.getItems().addAll(regroupComponentsItem, subdivideComponentItem, insertComponentItem, removeComponentsItem);
this.edit.getItems().addAll(undoItem, new SeparatorMenuItem(), editAxisItem, displayAxisItem, editMaterialItem, new SeparatorMenuItem(), componentsMenu);
this.edit.getItems().addAll(undoItem, new SeparatorMenuItem(), editAxisItem, displayAxisItem, editMaterialItem, new SeparatorMenuItem(), componentsMenu, addComponentItem);
this.view = new Menu("View");
Menu cameraMenu = new Menu("Camera");
......@@ -1904,6 +2030,65 @@ public class EditorMenuBar extends MenuBar {
}
return modifiedComponent;
}
/**
* Gets the assembly component names recursively.
* @param result The list of names.
* @param component The component.
*/
private void getAssemblyComponentNames(ArrayList<String> result, Component component) {
if (!component.isLeaf()) {
result.add(component.getName());
}
for (Component child : component.getChildren()) {
getAssemblyComponentNames(result, child);
}
}
/**
* Gets the assembly component names.
* @return The list of assembly names.
*/
private ArrayList<String> getAssemblyComponentNames() {
ArrayList<String> result = new ArrayList<String>();
getAssemblyComponentNames(result, Nomad3DEditor.getInstance().getModel().getRoot());
return result;
}
/**
* Returns true if the name exists.
* @param name The name to find.
* @param component The component checked.
* @return True if found.
*/
private boolean nameExists(String name, Component component) {
if (component.getName().equals(name)) {
return true;
}
for (Component child : component.getChildren()) {
if (nameExists(name, child)) {
return true;
}
}
return false;
}
/**
* Returns true if the name exists.
* @param name The name to find.
* @return True if found.
*/
private boolean nameExists(String name) {
return nameExists(name, Nomad3DEditor.getInstance().getModel().getRoot());
}
private boolean modelLoaded() {
return !(Nomad3DEditor.getInstance().getModel() == null || Nomad3DEditor.getInstance().getModel().getRoot() == null);
......
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