aboutsummaryrefslogtreecommitdiff
path: root/src/com.example.portfolio2/com/example/portfolio2/Window.java
blob: 93d522b7831c2b5f835165dab3872182765d3126 (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. @Override
  46. public void start(final Stage stage) throws IOException {
  47. // clear old insertions into participation table
  48. con.initialize();
  49. // define list of columns based on their names
  50. List<ActivityColumn> columns = List.of(
  51. new ActivityColumn(GUI.Section.PROGRAM),
  52. new ActivityColumn(GUI.Section.SUBJECT1),
  53. new ActivityColumn(GUI.Section.SUBJECT2),
  54. new ActivityColumn(GUI.Section.ELECTIVE)
  55. );
  56. // define button functionality for each activity column
  57. for (ActivityColumn col : columns) {
  58. col.nameLabel.setStyle(LABEL_STYLE);
  59. col.ectsLabel.setStyle(LABEL_STYLE);
  60. col.categoryCombo.setPrefSize(
  61. LIST_WIDTH, LIST_HEIGHT);
  62. col.activitySelect.setPrefSize(
  63. LIST_WIDTH, LIST_HEIGHT);
  64. col.area.setPrefWidth(LIST_WIDTH);
  65. // all boxes share same activity logic
  66. col.activitySelect.setOnAction(event -> {
  67. con.onActivitySelected(
  68. col.categoryCombo,
  69. col.activitySelect,
  70. col.area);
  71. con.updateEcts(
  72. col.ectsLabel,
  73. col.categoryCombo);
  74. });
  75. // handle each category box
  76. switch (col.section) {
  77. case GUI.Section.PROGRAM -> {
  78. col.categoryCombo.getItems().addAll(
  79. "HumTek", "NatBach");
  80. col.categoryCombo.setOnAction(event -> {
  81. con.onCategorySelected(
  82. col.categoryCombo.getValue(),
  83. col.activitySelect,
  84. col.area);
  85. });
  86. }
  87. // TODO: use the list for filling the box
  88. case GUI.Section.SUBJECT1 -> {
  89. col.categoryCombo.getItems().addAll(
  90. "Computer Science",
  91. "Informatik",
  92. "Astrology");
  93. col.categoryCombo.setOnAction(event -> {
  94. con.onSubjectModuleSelected(
  95. col.categoryCombo,
  96. columns.get(
  97. GUI.Section.SUBJECT2.column)
  98. .categoryCombo);
  99. con.onCategorySelected(
  100. col.categoryCombo.getValue(),
  101. col.activitySelect,
  102. col.area);
  103. });
  104. }
  105. case GUI.Section.SUBJECT2 -> {
  106. col.categoryCombo.getItems().addAll(
  107. "Computer Science",
  108. "Informatik",
  109. "Astrology");
  110. // TODO: figure out a better way...
  111. col.categoryCombo.setOnAction(event -> {
  112. con.onSubjectModuleSelected(
  113. col.categoryCombo,
  114. columns.get(
  115. GUI.Section.SUBJECT1.column)
  116. .categoryCombo);
  117. con.onCategorySelected(
  118. col.categoryCombo.getValue(),
  119. col.activitySelect,
  120. col.area);
  121. });
  122. }
  123. case GUI.Section.ELECTIVE -> {
  124. // hide useless box
  125. col.categoryCombo.setVisible(false);
  126. con.fillElective(col.activitySelect);
  127. }
  128. }
  129. }
  130. // define HBox and scene for columns
  131. HBox root = new HBox(
  132. columns.get(GUI.Section.PROGRAM.column)
  133. .asVBox(),
  134. columns.get(GUI.Section.SUBJECT1.column)
  135. .asVBox(),
  136. columns.get(GUI.Section.SUBJECT2.column)
  137. .asVBox(),
  138. columns.get(GUI.Section.ELECTIVE.column)
  139. .asVBox());
  140. Scene scene = new Scene(
  141. root, WINDOW_WIDTH, WINDOW_HEIGHT);
  142. stage.setTitle(
  143. "Course Selector RUC: Ultimate Deluxe Edition");
  144. stage.setScene(scene);
  145. stage.show();
  146. }
  147. /// JVM entry point
  148. ///
  149. /// @param args command-line arguments
  150. public static void main(final String[] args) {
  151. launch();
  152. }
  153. /// column of activities
  154. ///
  155. /// @param section structural section for column
  156. /// @param nameLabel display text
  157. /// @param categoryCombo dropdown list for categories
  158. /// @param activitySelect dropdown list for activities
  159. /// @param area description of chosen activities
  160. /// @param ectsLabel text to display ECTS points
  161. private record ActivityColumn(
  162. GUI.Section section,
  163. Label nameLabel,
  164. ComboBox<String> categoryCombo,
  165. ComboBox<String> activitySelect,
  166. TextArea area,
  167. Label ectsLabel
  168. ) {
  169. /// column of activities
  170. ///
  171. /// @param section structural section for column
  172. public ActivityColumn(GUI.Section section) {
  173. this(
  174. section,
  175. new Label(section.label),
  176. new ComboBox<>(),
  177. new ComboBox<>(),
  178. new TextArea(),
  179. new Label()
  180. );
  181. }
  182. /// activity columne as VBox
  183. ///
  184. /// @return column of activities as VBox
  185. VBox asVBox() {
  186. return new VBox(
  187. nameLabel,
  188. categoryCombo,
  189. activitySelect,
  190. area,
  191. ectsLabel);
  192. }
  193. }
  194. }