Skip to content

Commit

Permalink
Merge pull request #6 from Maestro-ESEO/Project
Browse files Browse the repository at this point in the history
Project
  • Loading branch information
maximeduby authored Jan 22, 2024
2 parents 43d0d6e + 74bb531 commit f8b206c
Show file tree
Hide file tree
Showing 25 changed files with 1,746 additions and 79 deletions.
63 changes: 46 additions & 17 deletions src/main/java/com/maestro/desktop/controllers/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.sql.SQLException;
Expand Down Expand Up @@ -52,6 +61,10 @@ public static AppController getInstance() {
return INSTANCE;
}

public User getUser() { return this.user; }

public NavigableView getAllProjects() { return allProjects; }

/**
* initialize - Sets the user and displays the items from the sidebar.
* @param user - User logged in.
Expand All @@ -66,6 +79,9 @@ public void initialize(User user) throws SQLException {
System.out.println("Password: "+this.user.getPassword());
System.out.println("Picture: "+this.user.getProfilePhotoPath());
this.profileBtn.setText(this.user.getName());
Circle clipShape = new Circle(15, 15, 15);
this.profileBtn.getGraphic().setClip(clipShape);
((ImageView) this.profileBtn.getGraphic()).setImage(new Image(this.user.getProfilePhotoPath()));
AppController.INSTANCE = this;
this.dashboard = new NavigableView(this.user, NavigableView.FxmlView.DASHBOARD, dashboardButton);
this.allProjects = new NavigableView(this.user.getProjects(), NavigableView.FxmlView.ALL_PROJECTS, allProjectsButton);
Expand Down Expand Up @@ -149,7 +165,12 @@ public void navigateWithData(Object data) {

// Check if already in recent Navigable Views
for (NavigableView nav : recents) {
if (nav.getData() == data) {
if ((data instanceof Project
&& nav.getData() instanceof Project
&& ((Project) data).getId() == ((Project) nav.getData()).getId())
|| (data instanceof Task
&& nav.getData() instanceof Task
&& ((Task) data).getId() == ((Task) nav.getData()).getId()) ) {
AppController.getInstance().updateView(nav);
return;
}
Expand Down Expand Up @@ -195,32 +216,40 @@ public void navigateWithData(Object data) {
* @param event - ActionEvent raised when clicking on the "New project" button.
*/
public void createNewProject(ActionEvent event) {
Project project = new Project(
420,
"Test",
"Delete Later",
new Date(),
new Date(),
new Date(),
new Date(),
this.user
);
try {
DatabaseConnection.getInstance().insertProject(project);
DatabaseConnection.getInstance().updateAllProjects(this.user);
System.out.println("Projects updated: "+this.user.getProjects());
this.navigateWithData(project);
} catch (SQLException e) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/dialogs/new-project-dialog.fxml"));
DialogPane pane = loader.load();
NewProjectDialogController controller = loader.getController();
Stage stage = new Stage();
stage.initOwner(((Node) event.getSource()).getScene().getWindow());
stage.initModality(Modality.APPLICATION_MODAL);
controller.initialize(stage);
stage.setTitle("New Project");
stage.setScene(new Scene(pane));
stage.setResizable(false);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

public void updateRecentContainer() {
for (NavigableView nav : this.recents) {
((Button) nav.getNavSource()).setText(nav.getData().toString());
}
((VBox) this.recentContainer.getChildren().getLast()).getChildren().setAll(this.recents.stream().map(NavigableView::getNavSource).toList());
}

public void deleteRecent(Object data) {
NavigableView nav = this.recents.stream().filter(obj -> obj.getData() == data).toList().getFirst();
this.recents.remove(nav);
}

/**
* getAccount - Getter for the account member of the AppController class.
* @return NavigableView - The account member of the class.
*/
public NavigableView getAccount(){
return this.account;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.maestro.desktop.controllers;

import com.maestro.desktop.App;
import com.maestro.desktop.models.Project;
import com.maestro.desktop.models.User;
import com.maestro.desktop.utils.DatabaseConnection;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EditProjectDialogController {

private List<User> collaborators;
private List<User> newCollaborators;
private Stage stage;
private Project project;

@FXML
private TextField name;
@FXML
private TextArea description;
@FXML
private DatePicker startDate;
@FXML
private DatePicker endDate;
@FXML
private TextField findCollaborators;
@FXML
private TilePane collaboratorPane;
@FXML
private Button updateButton;
@FXML
private Button cancelButton;
@FXML
private Button deleteButton;

public void initialize(Stage stage, Project project) {
this.stage = stage;
this.project = project;
this.updateButton.setDisable(true);
this.updateButton.setOnAction(event -> this.updateProject());
this.cancelButton.setOnAction(event -> this.stage.close());
this.deleteButton.setOnAction(event -> this.deleteProject());
this.name.textProperty().addListener((observable, oldValue, newValue) -> { this.updateButton.setDisable(this.name.getText().isBlank()); });
this.findCollaborators.setOnAction(event -> this.checkUser());
this.findCollaborators.textProperty().addListener((observable, oldValue, newValue) -> { this.findCollaborators.getStyleClass().setAll("text-field"); });
this.collaborators = new ArrayList<>(this.project.getUsers());
this.newCollaborators = new ArrayList<>();
for (User user : this.collaborators) {
Label collaborator = new Label(user.getName());
Button deleteBtn = new Button();
deleteBtn.setId("delete-collaborator");
deleteBtn.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
deleteBtn.setPrefSize(12, 12);

HBox hBox = new HBox(5, deleteBtn, collaborator);
hBox.getStyleClass().setAll("collaborator");
hBox.setAlignment(Pos.CENTER_LEFT);
hBox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
this.collaboratorPane.getChildren().add(hBox);
deleteBtn.setOnAction(event -> {
this.collaborators.remove(user);
this.collaboratorPane.getChildren().remove(hBox);
this.stage.sizeToScene();
});
this.stage.sizeToScene();
}
this.name.setText(this.project.getName());
this.description.setText(this.project.getDescription());
this.startDate.setValue(new java.sql.Date(this.project.getStartDate().getTime()).toLocalDate());
this.endDate.setValue(new java.sql.Date(this.project.getEndDate().getTime()).toLocalDate());
}

public void updateProject() {
this.project.setName(this.name.getText());
this.project.setDescription(this.description.getText());
this.project.setStartDate(java.sql.Date.valueOf(this.startDate.getValue()));
this.project.setEndDate(java.sql.Date.valueOf(this.endDate.getValue()));
this.project.setUpdatedAt(new Date());
this.project.setUsers(this.collaborators);
try {
DatabaseConnection.getInstance().updateProject(this.project, this.newCollaborators);
DatabaseConnection.getInstance().updateAllProjects(AppController.getInstance().getUser());
AppController.getInstance().navigateWithData(this.project);
AppController.getInstance().updateRecentContainer();
} catch (SQLException e) {
e.printStackTrace();
}
this.stage.close();
}

public void checkUser() {
String email = this.findCollaborators.getText().toLowerCase().strip();
try {
User fetchedUser = DatabaseConnection.getInstance().fetchUserFromEmail(email);
if (fetchedUser == null) {
this.findCollaborators.getStyleClass().setAll("error-container");
return;
} else if (collaborators.stream().anyMatch(user -> user.getId() == fetchedUser.getId())) {
return;
} else {
Label collaborator = new Label(fetchedUser.getName());
Button deleteBtn = new Button();
deleteBtn.setId("delete-collaborator");
deleteBtn.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
deleteBtn.setPrefSize(12, 12);

HBox hBox = new HBox(5, deleteBtn, collaborator);
hBox.getStyleClass().setAll("collaborator");
hBox.setAlignment(Pos.CENTER_LEFT);
hBox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
this.collaboratorPane.getChildren().add(hBox);
deleteBtn.setOnAction(event -> {
this.collaborators.remove(fetchedUser);
this.newCollaborators.remove(fetchedUser);
this.collaboratorPane.getChildren().remove(hBox);
this.stage.sizeToScene();
});
this.collaborators.add(fetchedUser);
this.newCollaborators.add(fetchedUser);
this.stage.sizeToScene();
this.findCollaborators.clear();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public void deleteProject() {
try {
DatabaseConnection.getInstance().deleteProject(this.project);
AppController.getInstance().getUser().removeProject(this.project);
DatabaseConnection.getInstance().updateAllProjects(AppController.getInstance().getUser());
AppController.getInstance().updateView(AppController.getInstance().getAllProjects());
AppController.getInstance().deleteRecent(this.project);
AppController.getInstance().updateRecentContainer();
} catch (SQLException e) {
e.printStackTrace();
}
this.stage.close();
}
}
Loading

0 comments on commit f8b206c

Please sign in to comment.