summaryrefslogtreecommitdiff
path: root/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2014-09-04 13:24:04 -0700
committerTor Norbye <tnorbye@google.com>2014-09-04 13:24:04 -0700
commitc3d3a90f6b4ead083d63e28e6b9fcea93d675678 (patch)
treefc0dcd722b6d445468dbe7dad13b4c11781b1cbe /python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format
parent1aa2e09bdbd413eacb677e9fa4b50630530d0656 (diff)
downloadidea-c3d3a90f6b4ead083d63e28e6b9fcea93d675678.tar.gz
Snapshot idea/138.1980 from git://git.jetbrains.org/idea/community.git
Change-Id: Ib567c9c152d770212a7a3db20fbf591c210920bd
Diffstat (limited to 'python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format')
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Course.java55
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Lesson.java45
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Task.java41
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskFile.java111
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskWindow.java133
5 files changed, 385 insertions, 0 deletions
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Course.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Course.java
new file mode 100644
index 000000000000..eb62d59cd9b1
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Course.java
@@ -0,0 +1,55 @@
+package org.jetbrains.plugins.coursecreator.format;
+
+import com.google.gson.annotations.Expose;
+import com.intellij.psi.PsiDirectory;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Course {
+ @Expose public List<Lesson> lessons = new ArrayList<Lesson>();
+ @Expose public String description;
+
+ @Expose public String name;
+ @Expose public String author;
+
+ public Map<String, Lesson> myLessonsMap = new HashMap<String, Lesson>();
+
+ public Map<String, Lesson> getLessonsMap() {
+ return myLessonsMap;
+ }
+
+ public Lesson getLesson(@NotNull final String name) {
+ return myLessonsMap.get(name);
+ }
+
+
+ public Course() {
+ }
+
+ public Course(@NotNull final String name, @NotNull final String author, @NotNull final String description) {
+ this.description = description;
+ this.name = name;
+ this.author = author;
+ }
+
+ public List<Lesson> getLessons() {
+ return lessons;
+ }
+
+ public void addLesson(@NotNull final Lesson lesson, @NotNull final PsiDirectory directory) {
+ lessons.add(lesson);
+ myLessonsMap.put(directory.getName(), lesson);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+}
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Lesson.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Lesson.java
new file mode 100644
index 000000000000..38720140caf1
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Lesson.java
@@ -0,0 +1,45 @@
+package org.jetbrains.plugins.coursecreator.format;
+
+import com.google.gson.annotations.Expose;
+import com.intellij.psi.PsiDirectory;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Lesson {
+ @Expose public String name;
+ @Expose public List<Task> task_list = new ArrayList<Task>();
+
+ public int myIndex;
+ public Map<String, Task> myTasksMap = new HashMap<String, Task>();
+
+ public Lesson() {}
+
+ public Lesson(@NotNull final String name) {
+ this.name = name;
+ }
+
+ public void addTask(@NotNull final Task task, PsiDirectory taskDirectory) {
+ myTasksMap.put(taskDirectory.getName(), task);
+ task_list.add(task);
+ }
+
+ public Task getTask(@NotNull final String name) {
+ return myTasksMap.get(name);
+ }
+
+ public List<Task> getTasklist() {
+ return task_list;
+ }
+
+ public void setIndex(int index) {
+ myIndex = index;
+ }
+
+ public int getIndex() {
+ return myIndex;
+ }
+}
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Task.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Task.java
new file mode 100644
index 000000000000..e6c085b5d6a1
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/Task.java
@@ -0,0 +1,41 @@
+package org.jetbrains.plugins.coursecreator.format;
+
+import com.google.gson.annotations.Expose;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Task {
+ @Expose public String name;
+ @Expose public Map<String, TaskFile> task_files = new HashMap<String, TaskFile>();
+ public int myIndex;
+
+ public Task() {}
+
+ public Task(@NotNull final String name) {
+ this.name = name;
+ }
+
+ public int getIndex() {
+ return myIndex;
+ }
+
+ public void addTaskFile(@NotNull final String name, int index) {
+ TaskFile taskFile = new TaskFile();
+ taskFile.setIndex(index);
+ task_files.put(name, taskFile);
+ }
+
+ public TaskFile getTaskFile(@NotNull final String name) {
+ return task_files.get(name);
+ }
+
+ public void setIndex(int index) {
+ myIndex = index;
+ }
+
+ public Map<String, TaskFile> getTaskFiles() {
+ return task_files;
+ }
+}
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskFile.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskFile.java
new file mode 100644
index 000000000000..85f0d91983f2
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskFile.java
@@ -0,0 +1,111 @@
+package org.jetbrains.plugins.coursecreator.format;
+
+import com.google.gson.annotations.Expose;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.LogicalPosition;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.plugins.coursecreator.CCProjectService;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TaskFile {
+ @Expose public List<TaskWindow> task_windows = new ArrayList<TaskWindow>();
+ public int myIndex;
+ public boolean myTrackChanges = true;
+
+ public boolean isTrackChanges() {
+ return myTrackChanges;
+ }
+
+ public void setTrackChanges(boolean trackChanges) {
+ myTrackChanges = trackChanges;
+ }
+
+ public TaskFile() {}
+
+ public void addTaskWindow(@NotNull final TaskWindow taskWindow, int index) {
+ taskWindow.setIndex(index);
+ task_windows.add(taskWindow);
+ }
+
+ public List<TaskWindow> getTaskWindows() {
+ return task_windows;
+ }
+
+ public void setIndex(int index) {
+ myIndex = index;
+ }
+
+
+ /**
+ * @param pos position in editor
+ * @return task window located in specified position or null if there is no task window in this position
+ */
+ @Nullable
+ public TaskWindow getTaskWindow(@NotNull final Document document, @NotNull final LogicalPosition pos) {
+ int line = pos.line;
+ if (line >= document.getLineCount()) {
+ return null;
+ }
+ int column = pos.column;
+ int offset = document.getLineStartOffset(line) + column;
+ for (TaskWindow tw : task_windows) {
+ if (tw.getLine() <= line) {
+ int twStartOffset = tw.getRealStartOffset(document);
+ final int length = tw.getReplacementLength() > 0 ? tw.getReplacementLength() : 0;
+ int twEndOffset = twStartOffset + length;
+ if (twStartOffset <= offset && offset <= twEndOffset) {
+ return tw;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Updates task window lines
+ *
+ * @param startLine lines greater than this line and including this line will be updated
+ * @param change change to be added to line numbers
+ */
+ public void incrementLines(int startLine, int change) {
+ for (TaskWindow taskTaskWindow : task_windows) {
+ if (taskTaskWindow.getLine() >= startLine) {
+ taskTaskWindow.setLine(taskTaskWindow.getLine() + change);
+ }
+ }
+ }
+
+ /**
+ * Updates windows in specific line
+ *
+ * @param lineChange change in line number
+ * @param line line to be updated
+ * @param newEndOffsetInLine distance from line start to end of inserted fragment
+ * @param oldEndOffsetInLine distance from line start to end of changed fragment
+ */
+ public void updateLine(int lineChange, int line, int newEndOffsetInLine, int oldEndOffsetInLine) {
+ for (TaskWindow w : task_windows) {
+ if ((w.getLine() == line) && (w.getStart() >= oldEndOffsetInLine)) {
+ int distance = w.getStart() - oldEndOffsetInLine;
+ boolean coveredByPrevTW = false;
+ int prevIndex = w.getIndex() - 1;
+ if (CCProjectService.indexIsValid(prevIndex, task_windows)) {
+ TaskWindow prevTW = task_windows.get(prevIndex);
+ if (prevTW.getLine() == line) {
+ int endOffset = prevTW.getStart() + prevTW.getLength();
+ if (endOffset >= newEndOffsetInLine) {
+ coveredByPrevTW = true;
+ }
+ }
+ }
+ if (lineChange != 0 || newEndOffsetInLine <= w.getStart() || coveredByPrevTW) {
+ w.setStart(distance + newEndOffsetInLine);
+ w.setLine(line + lineChange);
+ }
+ }
+ }
+ }
+}
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskWindow.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskWindow.java
new file mode 100644
index 000000000000..cb6418ec75d7
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/format/TaskWindow.java
@@ -0,0 +1,133 @@
+package org.jetbrains.plugins.coursecreator.format;
+
+import com.google.gson.annotations.Expose;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.editor.colors.EditorColors;
+import com.intellij.openapi.editor.colors.EditorColorsManager;
+import com.intellij.openapi.editor.markup.HighlighterLayer;
+import com.intellij.openapi.editor.markup.HighlighterTargetArea;
+import com.intellij.openapi.editor.markup.RangeHighlighter;
+import com.intellij.openapi.editor.markup.TextAttributes;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.coursecreator.CCProjectService;
+
+import java.io.File;
+
+public class TaskWindow implements Comparable{
+
+ @Expose public int line;
+ @Expose public int start;
+ @Expose public String hint;
+ @Expose public String possible_answer;
+ @Expose public int length;
+ public String myTaskText;
+ public int myReplacementLength;
+ public int myIndex;
+
+ public TaskWindow() {}
+
+ public TaskWindow(int line, int start, int length, String selectedText) {
+ this.line = line;
+ this.start = start;
+ myReplacementLength = length;
+ this.possible_answer = selectedText;
+ }
+
+ public void setTaskText(@NotNull final String taskText) {
+ myTaskText = taskText;
+ length = myTaskText.length();
+ }
+
+ public String getTaskText() {
+ return myTaskText;
+ }
+
+ public int getReplacementLength() {
+ return myReplacementLength;
+ }
+
+ public void setHint(String hint) {
+ this.hint = hint;
+ }
+
+ public String getHintName() {
+ return hint;
+ }
+
+ public void removeResources(@NotNull final Project project) {
+ if (hint != null) {
+ VirtualFile hints = project.getBaseDir().findChild("hints");
+ if (hints == null) {
+ return;
+ }
+ File hintFile = new File(hints.getPath(), hint);
+ CCProjectService.deleteProjectFile(hintFile, project);
+ }
+ }
+
+ public void drawHighlighter(@NotNull final Editor editor) {
+ int startOffset = editor.getDocument().getLineStartOffset(line) + start;
+ int endOffset = startOffset + myReplacementLength;
+ TextAttributes defaultTestAttributes =
+ EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.LIVE_TEMPLATE_ATTRIBUTES);
+ RangeHighlighter highlighter =
+ editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, HighlighterLayer.LAST + 1, defaultTestAttributes,
+ HighlighterTargetArea.EXACT_RANGE);
+ highlighter.setGreedyToLeft(true);
+ highlighter.setGreedyToRight(true);
+ }
+
+ public int getIndex() {
+ return myIndex;
+ }
+
+ public void setIndex(int index) {
+
+ myIndex = index;
+ }
+
+ public void setReplacementLength(int replacementLength) {
+ myReplacementLength = replacementLength;
+ }
+
+ public int getLine() {
+ return line;
+ }
+
+ public int getRealStartOffset(Document document) {
+ return document.getLineStartOffset(line) + start;
+ }
+
+ public void setLine(int line) {
+ this.line = line;
+ }
+
+ public int getStart() {
+ return start;
+ }
+
+ public void setStart(int start) {
+ this.start = start;
+ }
+
+ @Override
+ public int compareTo(Object o) {
+ TaskWindow taskWindow = (TaskWindow)o;
+ int lineDiff = line - taskWindow.line;
+ if (lineDiff == 0) {
+ return start - taskWindow.start;
+ }
+ return lineDiff;
+ }
+
+ public String getPossibleAnswer() {
+ return possible_answer;
+ }
+
+ public int getLength() {
+ return length;
+ }
+} \ No newline at end of file