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