compute initial state of placeholder correctly
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / courseFormat / TaskFile.java
1 package com.jetbrains.edu.learning.courseFormat;
2
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;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
12
13 /**
14  * Implementation of task file which contains task windows for student to type in and
15  * which is visible to student in project view
16  */
17
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;
27
28   public TaskFile() {
29   }
30
31   public void initTaskFile(final Task task, boolean isRestarted) {
32     setTask(task);
33     final List<AnswerPlaceholder> answerPlaceholders = getAnswerPlaceholders();
34     for (AnswerPlaceholder answerPlaceholder : answerPlaceholders) {
35       answerPlaceholder.initAnswerPlaceholder(this, isRestarted);
36     }
37     Collections.sort(answerPlaceholders, new AnswerPlaceholderComparator());
38     for (int i = 0; i < answerPlaceholders.size(); i++) {
39       answerPlaceholders.get(i).setIndex(i);
40     }
41   }
42
43   public List<AnswerPlaceholder> getAnswerPlaceholders() {
44     return myAnswerPlaceholders;
45   }
46
47   public void setAnswerPlaceholders(List<AnswerPlaceholder> answerPlaceholders) {
48     this.myAnswerPlaceholders = answerPlaceholders;
49   }
50
51   public void addAnswerPlaceholder(AnswerPlaceholder answerPlaceholder) {
52     myAnswerPlaceholders.add(answerPlaceholder);
53   }
54
55   public int getIndex() {
56     return myIndex;
57   }
58
59   public void setIndex(int index) {
60     myIndex = index;
61   }
62
63   @Transient
64   public Task getTask() {
65     return myTask;
66   }
67
68   @Transient
69   public void setTask(Task task) {
70     myTask = task;
71   }
72
73   /**
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
76    */
77   @Nullable
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) {
83         return placeholder;
84       }
85     }
86     return null;
87   }
88
89
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       final AnswerPlaceholder.MyInitialState state = answerPlaceholder.getInitialState();
102       if (state != null) {
103         answerPlaceholderCopy.setInitialState(new AnswerPlaceholder.MyInitialState(state.getOffset(), state.getLength()));
104       }
105       answerPlaceholdersCopy.add(answerPlaceholderCopy);
106     }
107     target.name = source.name;
108     target.setAnswerPlaceholders(answerPlaceholdersCopy);
109   }
110
111   public void setUserCreated(boolean userCreated) {
112     myUserCreated = userCreated;
113   }
114
115   public boolean isUserCreated() {
116     return myUserCreated;
117   }
118
119   public boolean isTrackChanges() {
120     return myTrackChanges;
121   }
122
123   public void setTrackChanges(boolean trackChanges) {
124     myTrackChanges = trackChanges;
125   }
126
127   public boolean isHighlightErrors() {
128     return myHighlightErrors;
129   }
130
131   public void setHighlightErrors(boolean highlightErrors) {
132     myHighlightErrors = highlightErrors;
133   }
134
135   public void sortAnswerPlaceholders() {
136     Collections.sort(myAnswerPlaceholders, new AnswerPlaceholderComparator());
137     for (int i = 0; i < myAnswerPlaceholders.size(); i++) {
138       myAnswerPlaceholders.get(i).setIndex(i + 1);
139     }
140   }
141
142   public boolean hasFailedPlaceholders() {
143     for (AnswerPlaceholder placeholder : myAnswerPlaceholders) {
144       if (placeholder.getStatus() == StudyStatus.Failed) {
145         return true;
146       }
147     }
148     return false;
149   }
150 }