aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Window.java
blob: d3e2b068c6186bf7f9d82d27fabbf01f5f87c03d (plain)
  1. // SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  2. // SPDX-FileCopyrightText: 2025 <Alexander Marthin Klemensen stud-marthin@ruc.dk>
  3. // SPDX-FileCopyrightText: 2025 <Ian Valentin Christensen stud-ianc@ruc.dk>
  4. // SPDX-FileCopyrightText: 2025 <Zahed Noos zahed@ruc.dk>
  5. // SPDX-License-Identifier: GPL-3.0-or-later
  6. package com.example.portfolio2;
  7. import javafx.application.Application;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.ComboBox;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.control.TextArea;
  12. import javafx.scene.layout.HBox;
  13. import javafx.scene.layout.VBox;
  14. import javafx.stage.Stage;
  15. import java.io.IOException;
  16. import java.util.List;
  17. /// Bachelorizer - JavaFX Window view
  18. // Class is final to forbid subclassing,
  19. // because object is passed to controller during instantiation
  20. public final class Window extends Application {
  21. /// Default constructor
  22. // (declared explicitly only to silence javadoc)
  23. public Window() { }
  24. /// window width
  25. private static final int WINDOW_WIDTH = 1000;
  26. /// window height
  27. private static final int WINDOW_HEIGHT = 500;
  28. /// box width
  29. private static final int LIST_WIDTH = 250;
  30. /// box height
  31. private static final int LIST_HEIGHT = 35;
  32. /// Label styling
  33. private static final String LABEL_STYLE =
  34. "-fx-font-weight: bold;"
  35. + "-fx-font-size: 18;"
  36. + "-fx-padding: 10";
  37. /// Storage model
  38. private Database store = new Database();
  39. /// UI model
  40. ///
  41. /// Must be subclass GUI to cover columns.
  42. private GUI ui = new GUI();
  43. /// Application controller
  44. private Controller con = new Controller(store, ui, this);
  45. /// column state data as List of ActivityColumn objects
  46. private List<ActivityColumn> columns;
  47. @Override
  48. public void start(final Stage stage) throws IOException {
  49. // clear old insertions into participation table
  50. con.initialize();
  51. // define list of columns based on their names
  52. columns = List.of(
  53. new ActivityColumn(GUI.Section.PROGRAM),
  54. new ActivityColumn(GUI.Section.SUBJECT1),
  55. new ActivityColumn(GUI.Section.SUBJECT2),
  56. new ActivityColumn(GUI.Section.ELECTIVE)
  57. );
  58. // define button functionality for each activity column
  59. for (ActivityColumn col : columns) {
  60. col.nameLabel.setStyle(LABEL_STYLE);
  61. col.ectsLabel.setStyle(LABEL_STYLE);
  62. col.categoryCombo.setPrefSize(
  63. LIST_WIDTH, LIST_HEIGHT);
  64. col.activitySelect.setPrefSize(
  65. LIST_WIDTH, LIST_HEIGHT);
  66. col.area.setPrefWidth(LIST_WIDTH);
  67. // all boxes share same activity logic
  68. col.activitySelect.setOnAction(event -> {
  69. con.onActivitySelected(
  70. col.categoryCombo,
  71. col.activitySelect,
  72. col.area);
  73. con.updateEcts(
  74. col.ectsLabel,
  75. col.categoryCombo);
  76. });
  77. // handle each category box
  78. switch (col.section) {
  79. case GUI.Section.PROGRAM -> {
  80. col.categoryCombo.getItems().addAll(
  81. "HumTek", "NatBach");
  82. col.categoryCombo.setOnAction(event -> {
  83. con.onCategorySelected(
  84. col.categoryCombo.getValue(),
  85. col.activitySelect,
  86. col.area);
  87. });
  88. }
  89. // TODO: use the list for filling the box
  90. case GUI.Section.SUBJECT1 -> {
  91. col.categoryCombo.getItems().addAll(
  92. "Computer Science",
  93. "Informatik",
  94. "Astrology");
  95. col.categoryCombo.setOnAction(event -> {
  96. con.onSubjectModuleSelected(
  97. col.categoryCombo,
  98. columns.get(
  99. GUI.Section.SUBJECT2.column)
  100. .categoryCombo);
  101. con.onCategorySelected(
  102. col.categoryCombo.getValue(),
  103. col.activitySelect,
  104. col.area);
  105. });
  106. }
  107. case GUI.Section.SUBJECT2 -> {
  108. col.categoryCombo.getItems().addAll(
  109. "Computer Science",
  110. "Informatik",
  111. "Astrology");
  112. // TODO: figure out a better way...
  113. col.categoryCombo.setOnAction(event -> {
  114. con.onSubjectModuleSelected(
  115. col.categoryCombo,
  116. columns.get(
  117. GUI.Section.SUBJECT1.column)
  118. .categoryCombo);
  119. con.onCategorySelected(
  120. col.categoryCombo.getValue(),
  121. col.activitySelect,
  122. col.area);
  123. });
  124. }
  125. case GUI.Section.ELECTIVE -> {
  126. // hide useless box
  127. col.categoryCombo.setVisible(false);
  128. con.fillElective(col.activitySelect);
  129. }
  130. }
  131. }
  132. // define HBox and scene for columns
  133. HBox root = new HBox(
  134. columns.get(GUI.Section.PROGRAM.column)
  135. .asVBox(),
  136. columns.get(GUI.Section.SUBJECT1.column)
  137. .asVBox(),
  138. columns.get(GUI.Section.SUBJECT2.column)
  139. .asVBox(),
  140. columns.get(GUI.Section.ELECTIVE.column)
  141. .asVBox());
  142. Scene scene = new Scene(
  143. root, WINDOW_WIDTH, WINDOW_HEIGHT);
  144. stage.setTitle(
  145. "Course Selector RUC: Ultimate Deluxe Edition");
  146. stage.setScene(scene);
  147. stage.show();
  148. }
  149. /// JVM entry point
  150. ///
  151. /// @param args command-line arguments
  152. public static void main(final String[] args) {
  153. launch();
  154. }
  155. /// column of activities
  156. ///
  157. /// @param section structural section for column
  158. /// @param nameLabel display text
  159. /// @param categoryCombo dropdown list for categories
  160. /// @param activitySelect dropdown list for activities
  161. /// @param area description of chosen activities
  162. /// @param ectsLabel text to display ECTS points
  163. private record ActivityColumn(
  164. GUI.Section section,
  165. Label nameLabel,
  166. ComboBox<String> categoryCombo,
  167. ComboBox<String> activitySelect,
  168. TextArea area,
  169. Label ectsLabel
  170. ) {
  171. /// column of activities
  172. ///
  173. /// @param section structural section for column
  174. public ActivityColumn(GUI.Section section) {
  175. this(
  176. section,
  177. new Label(section.label),
  178. new ComboBox<>(),
  179. new ComboBox<>(),
  180. new TextArea(),
  181. new Label()
  182. );
  183. }
  184. /// activity columne as VBox
  185. ///
  186. /// @return column of activities as VBox
  187. VBox asVBox() {
  188. return new VBox(
  189. nameLabel,
  190. categoryCombo,
  191. activitySelect,
  192. area,
  193. ectsLabel);
  194. }
  195. }
  196. }