extracted course initialization
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Fri, 13 Feb 2015 16:15:51 +0000 (19:15 +0300)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Fri, 13 Feb 2015 16:15:51 +0000 (19:15 +0300)
python/edu/interactive-learning-python/src/com/jetbrains/edu/learning/PyStudyDirectoryProjectGenerator.java
python/educational/interactive-learning/src/com/jetbrains/edu/learning/StudyTaskManager.java
python/educational/interactive-learning/src/com/jetbrains/edu/learning/courseGeneration/StudyGenerator.java [moved from python/educational/interactive-learning/src/com/jetbrains/edu/learning/StudyGenerator.java with 71% similarity]
python/educational/interactive-learning/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java [moved from python/educational/interactive-learning/src/com/jetbrains/edu/learning/StudyProjectGenerator.java with 97% similarity]
python/educational/interactive-learning/src/com/jetbrains/edu/learning/ui/StudyNewProjectPanel.java
python/educational/src/com/jetbrains/edu/courseFormat/AnswerPlaceholder.java
python/educational/src/com/jetbrains/edu/courseFormat/Course.java
python/educational/src/com/jetbrains/edu/courseFormat/Lesson.java
python/educational/src/com/jetbrains/edu/courseFormat/Task.java
python/educational/src/com/jetbrains/edu/courseFormat/TaskFile.java

index 89d1b91f282691598bdc5b2ef828414383322168..2372cff24165f327f7ef54b046c3d72e50ff968b 100644 (file)
@@ -8,6 +8,7 @@ import com.intellij.openapi.progress.ProcessCanceledException;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.platform.DirectoryProjectGenerator;
+import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
 import com.jetbrains.edu.learning.ui.StudyNewProjectPanel;
 import com.jetbrains.python.newProject.PythonProjectGenerator;
 import icons.InteractiveLearningPythonIcons;
index 3bd234de9735a39cc90cb34aff0f09515a237c01..e893a66b4bc89ea126c506ede253a2218420d476 100644 (file)
@@ -33,6 +33,7 @@ import com.jetbrains.edu.courseFormat.Lesson;
 import com.jetbrains.edu.courseFormat.Task;
 import com.jetbrains.edu.courseFormat.TaskFile;
 import com.jetbrains.edu.learning.actions.*;
+import com.jetbrains.edu.learning.courseGeneration.StudyGenerator;
 import com.jetbrains.edu.learning.ui.StudyCondition;
 import com.jetbrains.edu.learning.ui.StudyToolWindowFactory;
 import org.jdom.Element;
@@ -99,7 +100,7 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
   public void loadState(Element el) {
     myCourse = XmlSerializer.deserialize(el.getChild(COURSE_ELEMENT), Course.class);
     if (myCourse != null) {
-      myCourse.init(true);
+      StudyGenerator.initCourse(myCourse, true);
     }
   }
 
@@ -366,7 +367,7 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
               if (StudyUtils.indexIsValid(taskIndex, tasks)) {
                 final Task task = tasks.get(taskIndex);
                 final TaskFile taskFile = new TaskFile();
-                taskFile.init(task, false);
+                StudyGenerator.initTaskFile(taskFile, task, false);
                 taskFile.setUserCreated(true);
                 task.getTaskFiles().put(createdFile.getName(), taskFile);
               }
similarity index 71%
rename from python/educational/interactive-learning/src/com/jetbrains/edu/learning/StudyGenerator.java
rename to python/educational/interactive-learning/src/com/jetbrains/edu/learning/courseGeneration/StudyGenerator.java
index 62628525f6c76cb013d6d75b8f03238d526633a4..9edf03edfdbf2052bba636007f8eb44b426d1f89 100644 (file)
@@ -1,4 +1,4 @@
-package com.jetbrains.edu.learning;
+package com.jetbrains.edu.learning.courseGeneration;
 
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
@@ -6,15 +6,15 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.edu.StudyNames;
-import com.jetbrains.edu.courseFormat.Course;
-import com.jetbrains.edu.courseFormat.Lesson;
-import com.jetbrains.edu.courseFormat.Task;
-import com.jetbrains.edu.courseFormat.TaskFile;
+import com.jetbrains.edu.courseFormat.*;
+import com.jetbrains.edu.courseFormat.info.LessonInfo;
+import com.jetbrains.edu.learning.StudyUtils;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -135,4 +135,57 @@ public class StudyGenerator {
         }
       });
   }
+
+  /**
+   * Initializes state of course
+   */
+  public static void initCourse(@NotNull final Course course, boolean isRestarted) {
+    for (Lesson lesson : course.lessons) {
+      initLesson(lesson, course, isRestarted);
+    }
+  }
+
+  public static void initLesson(@NotNull final Lesson lesson, final Course course, boolean isRestarted) {
+    lesson.setCourse(course);
+    final LessonInfo info = lesson.getLessonInfo();
+    final List<Task> taskList = lesson.getTaskList();
+    info.setTaskNum(taskList.size());
+    info.setTaskUnchecked(taskList.size());
+    for (Task task : taskList) {
+      initTask(task, lesson, isRestarted);
+    }
+  }
+
+  /**
+   * Initializes state of task file
+   *
+   * @param lesson lesson which task belongs to
+   */
+  public static void initTask(@NotNull final Task task, final Lesson lesson, boolean isRestarted) {
+    task.setLesson(lesson);
+    for (TaskFile taskFile : task.getTaskFiles().values()) {
+      initTaskFile(taskFile, task, isRestarted);
+    }
+  }
+
+  public static void initTaskFile(@NotNull final TaskFile taskFile, final Task task, boolean isRestarted) {
+    taskFile.setTask(task);
+    final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
+    for (AnswerPlaceholder answerPlaceholder : answerPlaceholders) {
+      initAnswerPlaceholder(answerPlaceholder, taskFile, isRestarted);
+    }
+    Collections.sort(answerPlaceholders);
+    for (int i = 0; i < answerPlaceholders.size(); i++) {
+      answerPlaceholders.get(i).setIndex(i);
+    }
+  }
+
+  public static void initAnswerPlaceholder(@NotNull final AnswerPlaceholder placeholder, final TaskFile file, boolean isRestarted) {
+    if (!isRestarted) {
+      placeholder.setInitialState(new AnswerPlaceholder.MyInitialState(placeholder.getLine(), placeholder.getLength(),
+                                                                       placeholder.getStart()));
+    }
+    placeholder.setTaskFile(file);
+  }
+
 }
similarity index 97%
rename from python/educational/interactive-learning/src/com/jetbrains/edu/learning/StudyProjectGenerator.java
rename to python/educational/interactive-learning/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java
index f9d657046da2d142a206f2d95005757112c727f0..69d9d499e2343cbfaa35c2ee72db98936c5ab8fa 100644 (file)
@@ -1,4 +1,4 @@
-package com.jetbrains.edu.learning;
+package com.jetbrains.edu.learning.courseGeneration;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
@@ -28,6 +28,10 @@ import com.jetbrains.edu.courseFormat.Course;
 import com.jetbrains.edu.courseFormat.Lesson;
 import com.jetbrains.edu.courseFormat.Task;
 import com.jetbrains.edu.courseFormat.TaskFile;
+import com.jetbrains.edu.learning.CourseInfo;
+import com.jetbrains.edu.learning.StudyLanguageManager;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.stepic.StudyStepicConnector;
 import org.jetbrains.annotations.NotNull;
 
@@ -57,7 +61,7 @@ public class StudyProjectGenerator {
     final Course course = StudyStepicConnector.getCourse(mySelectedCourseInfo);
     if (course == null) return;
     flushCourse(course);
-    course.init(false);
+    StudyGenerator.initCourse(course, false);
     final File courseDirectory = new File(myCoursesDir, course.getName());
     StudyGenerator.createCourse(course, baseDir, courseDirectory, project);
     course.setCourseDirectory(new File(myCoursesDir, mySelectedCourseInfo.getName()).getAbsolutePath());
index 117e9e91b5539c4c88fcba4b6133f1ca6545a96d..225ba643135a62fde6f2b9330f89f9f472ac71e8 100644 (file)
@@ -4,8 +4,8 @@ import com.intellij.facet.ui.FacetValidatorsManager;
 import com.intellij.facet.ui.ValidationResult;
 import com.intellij.icons.AllIcons;
 import com.jetbrains.edu.learning.CourseInfo;
-import com.jetbrains.edu.learning.StudyProjectGenerator;
 import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
index 8b1dcff642e552911c83d74336460a3086caccee..7e9c8742784d21e56ba19501843ba1fcb9b60c81 100644 (file)
@@ -4,6 +4,7 @@ import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;
 import com.intellij.openapi.editor.Document;
 import com.intellij.ui.JBColor;
+import com.intellij.util.xmlb.annotations.Transient;
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -24,14 +25,7 @@ public class AnswerPlaceholder implements Comparable, StudyStateful {
   private MyInitialState myInitialState;
 
 
-  private TaskFile myTaskFile;
-
-  public void init(final TaskFile file, boolean isRestarted) {
-    if (!isRestarted) {
-      myInitialState = new MyInitialState(line, length, start);
-    }
-    myTaskFile = file;
-  }
+  @Transient private TaskFile myTaskFile;
 
   public StudyStatus getStatus() {
     return myStatus;
@@ -97,10 +91,16 @@ public class AnswerPlaceholder implements Comparable, StudyStateful {
     myInitialState = initialState;
   }
 
+  @Transient
   public TaskFile getTaskFile() {
     return myTaskFile;
   }
 
+  @Transient
+  public void setTaskFile(TaskFile taskFile) {
+    myTaskFile = taskFile;
+  }
+
   public JBColor getColor() {
     if (myStatus == StudyStatus.Solved) {
       return JBColor.GREEN;
@@ -146,7 +146,7 @@ public class AnswerPlaceholder implements Comparable, StudyStateful {
     length = myInitialState.myLength;
   }
 
-  private static class MyInitialState {
+  public static class MyInitialState {
     public int myLine = -1;
     public int myLength = -1;
     public int myStart = -1;
index ad732c24d4cc1015543010dd57c01ca5a643cc0e..9bf78703364c25b515aecfcf6b3d672ef8f24ba5 100644 (file)
@@ -16,15 +16,6 @@ public class Course {
   private boolean myUpToDate;
   private String myLanguage;
 
-  /**
-   * Initializes state of course
-   */
-  public void init(boolean isRestarted) {
-    for (Lesson lesson : lessons) {
-      lesson.init(this, isRestarted);
-    }
-  }
-
   public List<Lesson> getLessons() {
     return lessons;
   }
index 5db440fb1cfc892854c08822a4b98f732270765c..d62b544ded55212b10bdddfbd60b6c6573621376 100644 (file)
@@ -20,20 +20,13 @@ public class Lesson implements StudyStateful {
   private String name;
 
   public List<Task> taskList = new ArrayList<Task>();
+
+  @Transient
   private Course myCourse = null;
   private int myIndex = -1;
   private LessonInfo myLessonInfo = new LessonInfo();
 
 
-  public void init(final Course course, boolean isRestarted) {
-    myCourse = course;
-    myLessonInfo.setTaskNum(taskList.size());
-    myLessonInfo.setTaskUnchecked(taskList.size());
-    for (Task task : taskList) {
-      task.init(this, isRestarted);
-    }
-  }
-
   public String getName() {
     return name;
   }
@@ -76,7 +69,13 @@ public class Lesson implements StudyStateful {
     return taskList;
   }
 
+  @Transient
   public Course getCourse() {
     return myCourse;
   }
+
+  @Transient
+  public void setCourse(Course course) {
+    myCourse = course;
+  }
 }
index c0345bde49a1e60c1a93d5c31314e598067fff74..87f664db6f7af94d3239db0108475962f5e3388e 100644 (file)
@@ -24,22 +24,10 @@ public class Task implements StudyStateful {
   private String text;
   private String testsText;
 
-  private Lesson myLesson;
+  @Transient private Lesson myLesson;
   private List<UserTest> userTests = new ArrayList<UserTest>();
 
 
-  /**
-   * Initializes state of task file
-   *
-   * @param lesson lesson which task belongs to
-   */
-  public void init(final Lesson lesson, boolean isRestarted) {
-    myLesson = lesson;
-    for (TaskFile taskFile : taskFiles.values()) {
-      taskFile.init(this, isRestarted);
-    }
-  }
-
   public String getName() {
     return name;
   }
@@ -120,10 +108,15 @@ public class Task implements StudyStateful {
     return taskFiles.get(fileName);
   }
 
+  @Transient
   public Lesson getLesson() {
     return myLesson;
   }
 
+  @Transient
+  public void setLesson(Lesson lesson) {
+    myLesson = lesson;
+  }
 
   @Nullable
   public VirtualFile getTaskDir(Project project) {
index 8820a747b0a9dd2ea7bbda5d6d74a4c712902437..c7e5302af6490487625d839bf977f7e714d4a0fa 100644 (file)
@@ -8,7 +8,6 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 /**
@@ -24,24 +23,13 @@ public class TaskFile implements StudyStateful {
 
   public String name;
   public String text;
-  private Task myTask;
+  @Transient private Task myTask;
   @Transient
   private AnswerPlaceholder mySelectedAnswerPlaceholder = null;
   private boolean myUserCreated = false;
   private boolean myTrackChanges = true;
   private boolean myHighlightErrors = false;
 
-  public void init(final Task task, boolean isRestarted) {
-    myTask = task;
-    for (AnswerPlaceholder answerPlaceholder : myAnswerPlaceholders) {
-      answerPlaceholder.init(this, isRestarted);
-    }
-    Collections.sort(myAnswerPlaceholders);
-    for (int i = 0; i < myAnswerPlaceholders.size(); i++) {
-      myAnswerPlaceholders.get(i).setIndex(i);
-    }
-  }
-
   /**
    * @return if all the windows in task file are marked as resolved
    */
@@ -96,10 +84,16 @@ public class TaskFile implements StudyStateful {
     myIndex = index;
   }
 
+  @Transient
   public Task getTask() {
     return myTask;
   }
 
+  @Transient
+  public void setTask(Task task) {
+    myTask = task;
+  }
+
   /**
    * @param pos position in editor
    * @return task window located in specified position or null if there is no task window in this position