remove redundant Expose annotations
[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       answerPlaceholdersCopy.add(answerPlaceholderCopy);
102     }
103     target.name = source.name;
104     target.setAnswerPlaceholders(answerPlaceholdersCopy);
105   }
106
107   public void setUserCreated(boolean userCreated) {
108     myUserCreated = userCreated;
109   }
110
111   public boolean isUserCreated() {
112     return myUserCreated;
113   }
114
115   public boolean isTrackChanges() {
116     return myTrackChanges;
117   }
118
119   public void setTrackChanges(boolean trackChanges) {
120     myTrackChanges = trackChanges;
121   }
122
123   public boolean isHighlightErrors() {
124     return myHighlightErrors;
125   }
126
127   public void setHighlightErrors(boolean highlightErrors) {
128     myHighlightErrors = highlightErrors;
129   }
130
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);
135     }
136   }
137
138   public boolean hasFailedPlaceholders() {
139     for (AnswerPlaceholder placeholder : myAnswerPlaceholders) {
140       if (placeholder.getStatus() == StudyStatus.Failed) {
141         return true;
142       }
143     }
144     return false;
145   }
146 }