1 package com.jetbrains.edu.learning.courseFormat;
3 import com.google.gson.annotations.Expose;
4 import com.google.gson.annotations.SerializedName;
5 import com.intellij.util.xmlb.annotations.Transient;
6 import org.jetbrains.annotations.NotNull;
7 import org.jetbrains.annotations.Nullable;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
14 * Implementation of task file which contains task windows for student to type in and
15 * which is visible to student in project view
18 public class TaskFile {
19 @Expose public String name;
20 @Expose public String text;
21 private int myIndex = -1;
22 private boolean myUserCreated = false;
23 private boolean myTrackChanges = true;
24 private boolean myHighlightErrors = false;
25 @Expose @SerializedName("placeholders") private List<AnswerPlaceholder> myAnswerPlaceholders = new ArrayList<AnswerPlaceholder>();
26 @Transient private Task myTask;
31 public void initTaskFile(final Task task, boolean isRestarted) {
33 final List<AnswerPlaceholder> answerPlaceholders = getAnswerPlaceholders();
34 for (AnswerPlaceholder answerPlaceholder : answerPlaceholders) {
35 answerPlaceholder.initAnswerPlaceholder(this, isRestarted);
37 Collections.sort(answerPlaceholders, new AnswerPlaceholderComparator());
38 for (int i = 0; i < answerPlaceholders.size(); i++) {
39 answerPlaceholders.get(i).setIndex(i);
43 public List<AnswerPlaceholder> getAnswerPlaceholders() {
44 return myAnswerPlaceholders;
47 public void setAnswerPlaceholders(List<AnswerPlaceholder> answerPlaceholders) {
48 this.myAnswerPlaceholders = answerPlaceholders;
51 public void addAnswerPlaceholder(AnswerPlaceholder answerPlaceholder) {
52 myAnswerPlaceholders.add(answerPlaceholder);
55 public int getIndex() {
59 public void setIndex(int index) {
64 public Task getTask() {
69 public void setTask(Task task) {
74 * @param offset position in editor
75 * @return answer placeholder located in specified position or null if there is no task window in this position
78 public AnswerPlaceholder getAnswerPlaceholder(int offset) {
79 for (AnswerPlaceholder placeholder : myAnswerPlaceholders) {
80 int placeholderStart = placeholder.getOffset();
81 int placeholderEnd = placeholderStart + placeholder.getRealLength();
82 if (placeholderStart <= offset && offset <= placeholderEnd) {
90 public static void copy(@NotNull final TaskFile source, @NotNull final TaskFile target) {
91 List<AnswerPlaceholder> sourceAnswerPlaceholders = source.getAnswerPlaceholders();
92 List<AnswerPlaceholder> answerPlaceholdersCopy = new ArrayList<AnswerPlaceholder>(sourceAnswerPlaceholders.size());
93 for (AnswerPlaceholder answerPlaceholder : sourceAnswerPlaceholders) {
94 AnswerPlaceholder answerPlaceholderCopy = new AnswerPlaceholder();
95 answerPlaceholderCopy.setTaskText(answerPlaceholder.getTaskText());
96 answerPlaceholderCopy.setOffset(answerPlaceholder.getOffset());
97 answerPlaceholderCopy.setLength(answerPlaceholder.getLength());
98 answerPlaceholderCopy.setPossibleAnswer(answerPlaceholder.getPossibleAnswer());
99 answerPlaceholderCopy.setIndex(answerPlaceholder.getIndex());
100 answerPlaceholderCopy.setHint(answerPlaceholder.getHint());
101 answerPlaceholdersCopy.add(answerPlaceholderCopy);
103 target.name = source.name;
104 target.setAnswerPlaceholders(answerPlaceholdersCopy);
107 public void setUserCreated(boolean userCreated) {
108 myUserCreated = userCreated;
111 public boolean isUserCreated() {
112 return myUserCreated;
115 public boolean isTrackChanges() {
116 return myTrackChanges;
119 public void setTrackChanges(boolean trackChanges) {
120 myTrackChanges = trackChanges;
123 public boolean isHighlightErrors() {
124 return myHighlightErrors;
127 public void setHighlightErrors(boolean highlightErrors) {
128 myHighlightErrors = highlightErrors;
131 public void sortAnswerPlaceholders() {
132 Collections.sort(myAnswerPlaceholders, new AnswerPlaceholderComparator());
133 for (int i = 0; i < myAnswerPlaceholders.size(); i++) {
134 myAnswerPlaceholders.get(i).setIndex(i + 1);
138 public boolean hasFailedPlaceholders() {
139 for (AnswerPlaceholder placeholder : myAnswerPlaceholders) {
140 if (placeholder.getStatus() == StudyStatus.Failed) {