summaryrefslogtreecommitdiff
path: root/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCProjectService.java
blob: 1e38bab865cb5204476619f4ca05f5e6361925a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.coursecreator;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.coursecreator.format.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@State(name = "CCProjectService",
       storages = {
         @Storage(file = "$PROJECT_CONFIG_DIR$/course_service.xml")
       }
)
public class CCProjectService implements PersistentStateComponent<Element> {

  private static final Logger LOG = Logger.getInstance(CCProjectService.class.getName());
  public Course myCourse;
  public static final String COURSE_ELEMENT = "course";
  private static final Map<Document, StudyDocumentListener> myDocumentListeners = new HashMap<Document, StudyDocumentListener>();

  public void setCourse(@NotNull final Course course) {
    myCourse = course;
  }

  public Course getCourse() {
    return myCourse;
  }

  @Override
  public Element getState() {
    final Element el = new Element("CCProjectService");
    if (myCourse != null) {
      Element courseElement = new Element(COURSE_ELEMENT);
      XmlSerializer.serializeInto(myCourse, courseElement);
      el.addContent(courseElement);
    }
    return el;
  }

  @Override
  public void loadState(Element el) {
    myCourse = XmlSerializer.deserialize(el.getChild(COURSE_ELEMENT), Course.class);
  }

  public static CCProjectService getInstance(@NotNull Project project) {
    return ServiceManager.getService(project, CCProjectService.class);
  }

  public static void deleteProjectFile(File file, @NotNull final Project project) {
    if (!file.delete()) {
      LOG.info("Failed to delete file " + file.getPath());
    }
    VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
    ProjectView.getInstance(project).refresh();
  }

  public static void drawTaskWindows(@NotNull final VirtualFile virtualFile, @NotNull final Editor editor, @NotNull final Course course) {
    VirtualFile taskDir = virtualFile.getParent();
    if (taskDir == null) {
      return;
    }
    String taskDirName = taskDir.getName();
    if (!taskDirName.contains("task")) {
      return;
    }
    VirtualFile lessonDir = taskDir.getParent();
    if (lessonDir == null) {
      return;
    }
    String lessonDirName = lessonDir.getName();
    if (!lessonDirName.contains("lesson")) {
      return;
    }
    Lesson lesson = course.getLessonsMap().get(lessonDirName);
    if (lesson == null) {
      return;
    }
    Task task = lesson.getTask(taskDirName);
    if (task == null) {
      return;
    }
    TaskFile taskFile = task.getTaskFile(virtualFile.getName());
    if (taskFile == null) {
      return;
    }
    List<TaskWindow> taskWindows = taskFile.getTaskWindows();
    for (TaskWindow taskWindow : taskWindows) {
      taskWindow.drawHighlighter(editor);
    }
  }

  public static void addDocumentListener(Document document, StudyDocumentListener listener) {
    myDocumentListeners.put(document, listener);
  }

  public static StudyDocumentListener getListener(Document document) {
    return myDocumentListeners.get(document);
  }

  public static void removeListener(Document document) {
    myDocumentListeners.remove(document);
  }

  public static boolean indexIsValid(int index, List<TaskWindow> collection) {
    int size = collection.size();
    return index >= 0 && index < size;
  }
}