From f60b3c223a9af2f98cf3d7d9d1b8b7eb04099be4 Mon Sep 17 00:00:00 2001 From: RJ Date: Thu, 15 Dec 2016 23:57:37 +0100 Subject: [PATCH 1/2] Done most of what was aimed at. Event emission on click does NOT work, though. Coding in progress. --- .idea/vcs.xml | 6 ++ .../ilintar/study/MainScreenController.java | 50 +++++++++---- src/org/ilintar/study/StudyDetails.sqf | 11 +++ .../study/question/QuestionFactory.java | 2 +- .../ilintar/study/question/RadioQuestion.java | 73 +++++++++++++++++++ .../study/question/RadioQuestionFactory.java | 13 +++- .../question/event/QuestionAnsweredEvent.java | 3 + .../event/QuestionAnsweredEventListener.java | 4 +- 8 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/org/ilintar/study/question/RadioQuestion.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/org/ilintar/study/MainScreenController.java b/src/org/ilintar/study/MainScreenController.java index 3a473fd..41e27bb 100644 --- a/src/org/ilintar/study/MainScreenController.java +++ b/src/org/ilintar/study/MainScreenController.java @@ -1,5 +1,13 @@ package org.ilintar.study; +import javafx.fxml.FXML; +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.layout.AnchorPane; +import org.ilintar.study.question.*; +import org.ilintar.study.question.event.QuestionAnsweredEvent; +import org.ilintar.study.question.event.QuestionAnsweredEventListener; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -9,14 +17,7 @@ import java.util.List; import java.util.Map; -import org.ilintar.study.question.QuestionFactory; -import org.ilintar.study.question.RadioQuestionFactory; - -import javafx.fxml.FXML; -import javafx.scene.Node; -import javafx.scene.layout.AnchorPane; - -public class MainScreenController { +public class MainScreenController implements QuestionAnsweredEventListener { protected static Map factoryMap; @@ -25,18 +26,31 @@ public class MainScreenController { factoryMap.put("radio", new RadioQuestionFactory()); } - @FXML AnchorPane mainStudy; + //Storage of already answered & gathered questions from current study; + //questions are stored as their IDs to save memory; answers are stored as Answer objects. + private Map collectedAnswers = new HashMap<>(); + + @FXML AnchorPane mainStudy; @FXML public void startStudy() { + //Clear the pane: mainStudy.getChildren().clear(); - Node questionComponent = readQuestionFromFile(0, getClass().getResourceAsStream("StudyDetails.sqf")); - mainStudy.getChildren().add(questionComponent); + //Create a new Question object basing on the input file: + RadioQuestion createdQuestion = (RadioQuestion) readQuestionFromFile(0, getClass().getResourceAsStream("StudyDetails.sqf")); + //Add the question's graphical component to the pane: + mainStudy.getChildren().add(createdQuestion.getRenderedQuestion()); + //Create new button for question ending: + Button questionAnsweredButton = new Button("Zakończ pytanie"); + questionAnsweredButton.onMouseClickedProperty(); + //Add the button to the pane: + mainStudy.getChildren().add(questionAnsweredButton); } - private Node readQuestionFromFile(int i, InputStream resourceAsStream) { + //The .sqf parser: + private Question readQuestionFromFile(int questionCounter, InputStream resourceAsStream) { BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream)); String currentLine; - int which = 0; + int counter = 0; List questionLines = new ArrayList<>(); boolean readingQuestions = false; String questionType = null; @@ -46,7 +60,7 @@ private Node readQuestionFromFile(int i, InputStream resourceAsStream) { if (readingQuestions) { throw new IllegalArgumentException("Invalid file format: StartQuestion without EndQuestion"); } - if (which == i) { + if (counter == questionCounter) { readingQuestions = true; String[] split = currentLine.split(" "); if (split.length > 1) { @@ -59,7 +73,7 @@ private Node readQuestionFromFile(int i, InputStream resourceAsStream) { throw new IllegalArgumentException("Invalid file format: StartQuestion type="); } } else { - which++; + counter++; } } else { if (readingQuestions) { @@ -80,5 +94,11 @@ private Node readQuestionFromFile(int i, InputStream resourceAsStream) { } return null; } + + //Method from the QuestionAnsweredListener interface. + //Puts the current question & its answer into the collectedAnswers HashMap and reads the next question. + public void handleQuestionAnsweredEvent(QuestionAnsweredEvent e) { + this.collectedAnswers.put(e.getQuestion().getId(), e.getAnswer()); + } } diff --git a/src/org/ilintar/study/StudyDetails.sqf b/src/org/ilintar/study/StudyDetails.sqf index b3f0c76..d2eefc2 100644 --- a/src/org/ilintar/study/StudyDetails.sqf +++ b/src/org/ilintar/study/StudyDetails.sqf @@ -8,4 +8,15 @@ Third answer modified C Fourth answer D +EndQuestion +StartQuestion type=radio +This is FUCKUP +First answer +A +Second answer +B +Third answer modified +C +Fourth answer +D EndQuestion \ No newline at end of file diff --git a/src/org/ilintar/study/question/QuestionFactory.java b/src/org/ilintar/study/question/QuestionFactory.java index 954d7a0..0743b52 100644 --- a/src/org/ilintar/study/question/QuestionFactory.java +++ b/src/org/ilintar/study/question/QuestionFactory.java @@ -6,6 +6,6 @@ public interface QuestionFactory { - public Node createQuestion(List lines); + public Question createQuestion(List lines); } diff --git a/src/org/ilintar/study/question/RadioQuestion.java b/src/org/ilintar/study/question/RadioQuestion.java new file mode 100644 index 0000000..9f4c33b --- /dev/null +++ b/src/org/ilintar/study/question/RadioQuestion.java @@ -0,0 +1,73 @@ +package org.ilintar.study.question; + +import javafx.event.Event; +import javafx.scene.Node; +import javafx.scene.layout.VBox; +import org.ilintar.study.question.event.QuestionAnsweredEvent; +import org.ilintar.study.question.event.QuestionAnsweredEventListener; + +import java.util.ArrayList; + +/** + * Created by RJ on 2016-12-01. + */ +//Class for standard radio questions. +public class RadioQuestion implements Question { + //Visible part of the question (javafx Node); + private VBox empiricalPart; + //QuestionID, specified at the constructor method: + private String questionID; + //Question's answer, initialised here & added later on: + private Answer answer; + //List of active listeners: + private ArrayList listeners; + + //Constructor method: + public RadioQuestion() { + this.listeners = new ArrayList<>(); + } + + //Adds new listener to the list: + @Override + public void addQuestionAnsweredListener(QuestionAnsweredEventListener listener) { + listeners.add(listener); + } + + //Removes the listener from the list: + @Override + public void removeQuestionAnsweredListener(QuestionAnsweredEventListener listener) { + listeners.remove(listener); + } + + //Create new event carrying the question itself & its answer; then return the event: + public QuestionAnsweredEvent emitEvent() { + return new QuestionAnsweredEvent(this, this.answer); + } + + //Returns the question wrapped in a Node object (for further use by the controller); + @Override + public Node getRenderedQuestion(){ + return this.getEmpiricalPart(); + } + + //Getter for the question's ID: + @Override + public String getId() { + return questionID; + } + + //Setter for the question's answer; + public void setAnswer(Answer answer) { + this.answer = answer; + } + + //Getter fot the graphical component: + public VBox getEmpiricalPart() { + return this.empiricalPart; + } + + //Setter for the graphical component: + public void setEmpiricalPart(VBox empiricalPart) { + this.empiricalPart = empiricalPart; + } +} diff --git a/src/org/ilintar/study/question/RadioQuestionFactory.java b/src/org/ilintar/study/question/RadioQuestionFactory.java index 749b67c..7c52af8 100644 --- a/src/org/ilintar/study/question/RadioQuestionFactory.java +++ b/src/org/ilintar/study/question/RadioQuestionFactory.java @@ -2,16 +2,16 @@ import java.util.List; -import javafx.scene.Node; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; public class RadioQuestionFactory implements QuestionFactory { - +//Creates Question objects basing on parsed .sqf; +//At the first step, creates visual component of the question (a VBox object). @Override - public Node createQuestion(List lines) { + public Question createQuestion(List lines) { VBox questions = new VBox(); String question = lines.get(0); questions.getChildren().add(new Label(question)); @@ -25,7 +25,12 @@ public Node createQuestion(List lines) { questions.getChildren().add(button); } questions.onContextMenuRequestedProperty(); - return questions; + //Create new RadioQuestion object: + RadioQuestion createdQuestion = new RadioQuestion(); + //Add the created VBox to the question (later available by getEmpiricalComponent()): + createdQuestion.setEmpiricalPart(questions); + //Return the whole question as a RadioQuestion object: + return createdQuestion; } } diff --git a/src/org/ilintar/study/question/event/QuestionAnsweredEvent.java b/src/org/ilintar/study/question/event/QuestionAnsweredEvent.java index 6813104..3df9e20 100644 --- a/src/org/ilintar/study/question/event/QuestionAnsweredEvent.java +++ b/src/org/ilintar/study/question/event/QuestionAnsweredEvent.java @@ -1,5 +1,8 @@ package org.ilintar.study.question.event; +import javafx.beans.NamedArg; +import javafx.event.Event; +import javafx.event.EventType; import org.ilintar.study.question.Answer; import org.ilintar.study.question.Question; diff --git a/src/org/ilintar/study/question/event/QuestionAnsweredEventListener.java b/src/org/ilintar/study/question/event/QuestionAnsweredEventListener.java index 8fdec03..859561d 100644 --- a/src/org/ilintar/study/question/event/QuestionAnsweredEventListener.java +++ b/src/org/ilintar/study/question/event/QuestionAnsweredEventListener.java @@ -1,5 +1,5 @@ package org.ilintar.study.question.event; -public class QuestionAnsweredEventListener { - +public interface QuestionAnsweredEventListener { + void handleQuestionAnsweredEvent(QuestionAnsweredEvent e); } From 8cba3200342a7b7f908aee81f045ac4ecd5a2cb6 Mon Sep 17 00:00:00 2001 From: RJ Date: Fri, 16 Dec 2016 00:46:48 +0100 Subject: [PATCH 2/2] A desperate attempt to fix the controller. Event triggered now & the questions change, but obviously just screwed up the notifier-listener logic. --- .../ilintar/study/MainScreenController.java | 19 +++++++++++++++---- src/org/ilintar/study/StudyDetails.sqf | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/org/ilintar/study/MainScreenController.java b/src/org/ilintar/study/MainScreenController.java index 41e27bb..70854a5 100644 --- a/src/org/ilintar/study/MainScreenController.java +++ b/src/org/ilintar/study/MainScreenController.java @@ -1,7 +1,7 @@ package org.ilintar.study; +import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.geometry.Pos; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import org.ilintar.study.question.*; @@ -32,18 +32,29 @@ public class MainScreenController implements QuestionAnsweredEventListener { @FXML AnchorPane mainStudy; + //Just a self-explanatory dummy variable for startStudy(): + int questionCounter = 0; + //AND NOW FOR STH COMPLETELY DIFFERENT: @FXML public void startStudy() { //Clear the pane: mainStudy.getChildren().clear(); //Create a new Question object basing on the input file: - RadioQuestion createdQuestion = (RadioQuestion) readQuestionFromFile(0, getClass().getResourceAsStream("StudyDetails.sqf")); + RadioQuestion createdQuestion = (RadioQuestion) readQuestionFromFile(questionCounter, + getClass().getResourceAsStream("StudyDetails.sqf")); //Add the question's graphical component to the pane: - mainStudy.getChildren().add(createdQuestion.getRenderedQuestion()); + mainStudy.getChildren().add(createdQuestion.getRenderedQuestion()); //Create new button for question ending: Button questionAnsweredButton = new Button("Zakończ pytanie"); - questionAnsweredButton.onMouseClickedProperty(); //Add the button to the pane: mainStudy.getChildren().add(questionAnsweredButton); + //Set button's reaction: + questionAnsweredButton.setOnAction((event) -> { + mainStudy.getChildren().remove(createdQuestion); + questionCounter++; + RadioQuestion tempQuestion = (RadioQuestion) readQuestionFromFile(questionCounter, + getClass().getResourceAsStream("StudyDetails.sqf")); + mainStudy.getChildren().add(tempQuestion.getRenderedQuestion()); + }); } //The .sqf parser: diff --git a/src/org/ilintar/study/StudyDetails.sqf b/src/org/ilintar/study/StudyDetails.sqf index d2eefc2..f7aee64 100644 --- a/src/org/ilintar/study/StudyDetails.sqf +++ b/src/org/ilintar/study/StudyDetails.sqf @@ -4,7 +4,7 @@ First answer A Second answer B -Third answer modified +Third answer C Fourth answer D