Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 46 additions & 15 deletions src/org/ilintar/study/MainScreenController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.ilintar.study;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
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;
Expand All @@ -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<String, QuestionFactory> factoryMap;

Expand All @@ -25,18 +26,42 @@ 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<String, Answer> collectedAnswers = new HashMap<>();

@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();
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(questionCounter,
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");
//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());
});
}

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<String> questionLines = new ArrayList<>();
boolean readingQuestions = false;
String questionType = null;
Expand All @@ -46,7 +71,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) {
Expand All @@ -59,7 +84,7 @@ private Node readQuestionFromFile(int i, InputStream resourceAsStream) {
throw new IllegalArgumentException("Invalid file format: StartQuestion type=<type>");
}
} else {
which++;
counter++;
}
} else {
if (readingQuestions) {
Expand All @@ -80,5 +105,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());
}

}
11 changes: 11 additions & 0 deletions src/org/ilintar/study/StudyDetails.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ First answer
A
Second answer
B
Third answer
C
Fourth answer
D
EndQuestion
StartQuestion type=radio
This is FUCKUP
First answer
A
Second answer
B
Third answer modified
C
Fourth answer
Expand Down
2 changes: 1 addition & 1 deletion src/org/ilintar/study/question/QuestionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface QuestionFactory {

public Node createQuestion(List<String> lines);
public Question createQuestion(List<String> lines);

}
73 changes: 73 additions & 0 deletions src/org/ilintar/study/question/RadioQuestion.java
Original file line number Diff line number Diff line change
@@ -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<QuestionAnsweredEventListener> 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;
}
}
13 changes: 9 additions & 4 deletions src/org/ilintar/study/question/RadioQuestionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lines) {
public Question createQuestion(List<String> lines) {
VBox questions = new VBox();
String question = lines.get(0);
questions.getChildren().add(new Label(question));
Expand All @@ -25,7 +25,12 @@ public Node createQuestion(List<String> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.ilintar.study.question.event;

public class QuestionAnsweredEventListener {

public interface QuestionAnsweredEventListener {
void handleQuestionAnsweredEvent(QuestionAnsweredEvent e);
}