Commit c05dde3b authored by Josselin ENET's avatar Josselin ENET

Added 2 new classes #17

Aded 2 classes to handle removal and modification of existing travel
with an admin account.
parent 6cd21159
package fr.unantes.software.construction.ui;
import fr.unantes.software.construction.calendar.Travel;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;
import java.util.List;
import static javafx.scene.control.Alert.AlertType.ERROR;
import static javafx.scene.control.Alert.AlertType.INFORMATION;
public class AdminTravelModificationController extends SceneController {
public AdminTravelModificationController(GUI gui) { super(gui); }
@Override
public Scene createScene() {
gui.setTitle("Travel Modification");
GridPane root = new GridPane();
// users combo box
ComboBox travelsComboBox = new ComboBox();
updateComboBox(travelsComboBox);
Label lbTravelsComboBox = new Label("Travel:");
lbTravelsComboBox.setLabelFor(travelsComboBox);
Callback<ListView<Travel>, ListCell<Travel>> cellFactory = new Callback<ListView<Travel>, ListCell<Travel>>() {
@Override
public ListCell<Travel> call(ListView<Travel> l) {
return new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getFirstStep().getStartCity().getName()+" - "+item.getLastStep().getDestinationCity().getName());
}
}
} ;
}
};
travelsComboBox.setCellFactory(cellFactory);
ListCell<Travel> buttonCell = new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getFirstStep().getStartCity().getName()+" - "+item.getLastStep().getDestinationCity().getName());
} else {
setText(null);
}
}
};
travelsComboBox.setButtonCell(buttonCell);
// modify button
Button modifyButton = new Button();
modifyButton.setText("Modify");
modifyButton.setOnAction(event -> {
Travel selectedTravel = (Travel) travelsComboBox.getValue();
if (selectedTravel == null) {
AlertHandler.showAlert(ERROR, "No travel selected.");
}
else {
handleModify(selectedTravel);
updateComboBox(travelsComboBox);
}
});
// back button
Button backButton = new Button();
backButton.setText("Back");
backButton.setOnAction(event -> {
gui.setController(new AdminHomeController(gui));
});
root.add(lbTravelsComboBox,0,0);
root.add(travelsComboBox, 1, 0);
root.add(modifyButton, 0, 2);
root.add(backButton,0,4);
Scene result = new Scene(root,400,250);
return result;
}
private void updateComboBox(ComboBox usersComboBox) {
List<Travel> travelsList = gui.getTravelsDB().getAllTravel();
ObservableListImplementation<Travel> travelsObservableList = new ObservableListImplementation<>(travelsList);
usersComboBox.setItems(travelsObservableList);
}
public void handleModify(Travel travel) {
AlertHandler.showAlert(INFORMATION, "To do.");
}
}
package fr.unantes.software.construction.ui;
import fr.unantes.software.construction.calendar.Travel;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;
import java.util.List;
import static javafx.scene.control.Alert.AlertType.ERROR;
import static javafx.scene.control.Alert.AlertType.INFORMATION;
public class AdminTravelRemovalController extends SceneController {
public AdminTravelRemovalController(GUI gui) { super(gui); }
@Override
public Scene createScene() {
gui.setTitle("Travel Removal");
GridPane root = new GridPane();
// users combo box
ComboBox travelsComboBox = new ComboBox();
updateComboBox(travelsComboBox);
Label lbTravelsComboBox = new Label("Travel:");
lbTravelsComboBox.setLabelFor(travelsComboBox);
Callback<ListView<Travel>, ListCell<Travel>> cellFactory = new Callback<ListView<Travel>, ListCell<Travel>>() {
@Override
public ListCell<Travel> call(ListView<Travel> l) {
return new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getFirstStep().getStartCity().getName()+" - "+item.getLastStep().getDestinationCity().getName());
}
}
} ;
}
};
travelsComboBox.setCellFactory(cellFactory);
ListCell<Travel> buttonCell = new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getFirstStep().getStartCity().getName()+" - "+item.getLastStep().getDestinationCity().getName());
} else {
setText(null);
}
}
};
travelsComboBox.setButtonCell(buttonCell);
// remove button
Button removeButton = new Button();
removeButton.setText("Remove");
removeButton.setOnAction(event -> {
Travel selectedTravel = (Travel) travelsComboBox.getValue();
if (selectedTravel == null) {
AlertHandler.showAlert(ERROR, "No travel selected.");
}
else {
handleRemove(selectedTravel);
updateComboBox(travelsComboBox);
}
});
// back button
Button backButton = new Button();
backButton.setText("Back");
backButton.setOnAction(event -> {
gui.setController(new AdminHomeController(gui));
});
root.add(lbTravelsComboBox,0,0);
root.add(travelsComboBox, 1, 0);
root.add(removeButton, 0, 2);
root.add(backButton,0,4);
Scene result = new Scene(root,400,250);
return result;
}
private void updateComboBox(ComboBox usersComboBox) {
List<Travel> travelsList = gui.getTravelsDB().getAllTravel();
ObservableListImplementation<Travel> travelsObservableList = new ObservableListImplementation<>(travelsList);
usersComboBox.setItems(travelsObservableList);
}
public void handleRemove(Travel travel) {
if (gui.getTravelsDB().removeTravel(travel)) {
AlertHandler.showAlert(INFORMATION, "Travel deleted.");
} else {
AlertHandler.showAlert(ERROR, "Error during deletion.");
}
}
}
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