diff --git a/python/educational-core/src/com/jetbrains/edu/learning/StudyProjectComponent.java b/python/educational-core/src/com/jetbrains/edu/learning/StudyProjectComponent.java index e2e84ae3ea67..16aa194a1ba4 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/StudyProjectComponent.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/StudyProjectComponent.java @@ -21,6 +21,7 @@ import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.DumbAwareRunnable; import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.startup.StartupManager; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; @@ -56,13 +57,14 @@ import java.util.List; import java.util.Map; import static com.jetbrains.edu.learning.StudyUtils.execCancelable; +import static com.jetbrains.edu.learning.StudyUtils.navigateToStep; public class StudyProjectComponent implements ProjectComponent { private static final Logger LOG = Logger.getInstance(StudyProjectComponent.class.getName()); private final Project myProject; private FileCreatedByUserListener myListener; - private Map>> myDeletedShortcuts = new HashMap<>(); + private final Map>> myDeletedShortcuts = new HashMap<>(); private MessageBusConnection myBusConnection; private StudyProjectComponent(@NotNull final Project project) { @@ -103,6 +105,29 @@ public class StudyProjectComponent implements ProjectComponent { } } }); + + selectStep(); + } + + private void selectStep() { + StartupManager.getInstance(myProject).runWhenProjectIsInitialized(() -> { + Project defaultProject = ProjectManager.getInstance().getDefaultProject(); + if (myProject == defaultProject) { + return; + } + StudyTaskManager defaultTaskManager = StudyTaskManager.getInstance(defaultProject); + int stepId = defaultTaskManager.getStepId(); + + if (stepId != 0) { + StudyTaskManager taskManager = StudyTaskManager.getInstance(myProject); + Course course = taskManager.getCourse(); + if (course != null) { + + navigateToStep(myProject, course, stepId); + defaultTaskManager.setStepId(0); + } + } + }); } private void updateAvailable(Course course) { @@ -122,7 +147,6 @@ public class StudyProjectComponent implements ProjectComponent { }, "Updating Course", true, myProject); EduUtils.synchronize(); course.setUpdated(); - } }); notification.notify(myProject); @@ -272,7 +296,7 @@ public class StudyProjectComponent implements ProjectComponent { AnAction[] newGroupActions = ((ActionGroup)ActionManager.getInstance().getAction("NewGroup")).getChildren(null); for (AnAction newAction : newGroupActions) { if (newAction == action) { - myListener = new FileCreatedByUserListener(); + myListener = new FileCreatedByUserListener(); VirtualFileManager.getInstance().addVirtualFileListener(myListener); break; } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/StudyTaskManager.java b/python/educational-core/src/com/jetbrains/edu/learning/StudyTaskManager.java index 519a56396258..210dd11c6a87 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/StudyTaskManager.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/StudyTaskManager.java @@ -55,6 +55,7 @@ public class StudyTaskManager implements PersistentStateComponent, Dumb private boolean myTurnEditingMode = false; @Transient private final Project myProject; + @Transient private int myStepId; public StudyTaskManager(Project project) { myProject = project; @@ -255,4 +256,12 @@ public class StudyTaskManager implements PersistentStateComponent, Dumb public void setTurnEditingMode(boolean turnEditingMode) { myTurnEditingMode = turnEditingMode; } + + public void setStepId(int stepId) { + myStepId = stepId; + } + + public int getStepId() { + return myStepId; + } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/StudyUtils.java b/python/educational-core/src/com/jetbrains/edu/learning/StudyUtils.java index 46d3ca1b1fc2..0b1b6bcf557e 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/StudyUtils.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/StudyUtils.java @@ -84,6 +84,8 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import static com.jetbrains.edu.learning.navigation.StudyNavigator.navigateToTask; + public class StudyUtils { private StudyUtils() { } @@ -821,4 +823,25 @@ public class StudyUtils { } } } + + public static void navigateToStep(@NotNull Project project, @NotNull Course course, int stepId) { + if (stepId == 0 || course.isAdaptive()) { + return; + } + Task task = getTask(course, stepId); + if (task != null) { + navigateToTask(project, task); + } + } + + @Nullable + private static Task getTask(@NotNull Course course, int stepId) { + for (Lesson lesson : course.getLessons()) { + Task task = lesson.getTask(stepId); + if (task != null) { + return task; + } + } + return null; + } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/builtInServer/EduBuiltInServerUtils.java b/python/educational-core/src/com/jetbrains/edu/learning/builtInServer/EduBuiltInServerUtils.java index d3b912b6e6ee..32408c5262ae 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/builtInServer/EduBuiltInServerUtils.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/builtInServer/EduBuiltInServerUtils.java @@ -24,11 +24,8 @@ import com.intellij.openapi.project.ProjectManager; import com.intellij.util.xmlb.XmlSerializationException; import com.jetbrains.edu.learning.StudyTaskManager; import com.jetbrains.edu.learning.courseFormat.Course; -import com.jetbrains.edu.learning.courseFormat.Lesson; import com.jetbrains.edu.learning.courseFormat.RemoteCourse; -import com.jetbrains.edu.learning.courseFormat.tasks.Task; import com.jetbrains.edu.learning.newproject.ui.EduCreateNewProjectDialog; -import com.jetbrains.edu.learning.newproject.ui.EduCreateNewProjectListener; import com.jetbrains.edu.learning.newproject.ui.EduCreateNewStepikProjectDialog; import org.jdom.Document; import org.jdom.Element; @@ -41,8 +38,8 @@ import java.io.File; import java.io.IOException; import java.util.List; +import static com.jetbrains.edu.learning.StudyUtils.navigateToStep; import static com.jetbrains.edu.learning.core.EduNames.STUDY_PROJECT_XML_PATH; -import static com.jetbrains.edu.learning.navigation.StudyNavigator.navigateToTask; public class EduBuiltInServerUtils { public static boolean focusOpenProject(int courseId, int stepId) { @@ -96,8 +93,6 @@ public class EduBuiltInServerUtils { List recentPaths = state.recentPaths; - Project defaultProject = ProjectManager.getInstance().getDefaultProject(); - StudyTaskManager taskManager = new StudyTaskManager(defaultProject); SAXBuilder parser = new SAXBuilder(); for (String projectPath : recentPaths) { @@ -105,17 +100,13 @@ public class EduBuiltInServerUtils { if (component == null) { continue; } + StudyTaskManager taskManager = getDefaultTaskManager(); int courseId = getCourseId(taskManager, component); if (courseId == targetCourseId) { + taskManager.setStepId(stepId); Project project = openProject(projectPath); if (project != null) { - Course course = taskManager.getCourse(); - if (course != null) { - ApplicationManager.getApplication().invokeLater(() -> - navigateToStep(project, course, stepId) - ); - } return true; } } @@ -123,6 +114,7 @@ public class EduBuiltInServerUtils { return false; } + @Nullable private static Element readComponent(@NotNull SAXBuilder parser, @NotNull String projectPath) { Element component = null; @@ -154,44 +146,16 @@ public class EduBuiltInServerUtils { public static boolean createProject(int courseId, int stepId) { ApplicationManager.getApplication().invokeLater(() -> { + getDefaultTaskManager().setStepId(stepId); EduCreateNewProjectDialog createNewProjectDlg = new EduCreateNewStepikProjectDialog(courseId); - EduCreateNewProjectListener listener = new EduCreateNewProjectListener() { - @Override - public void created(@NotNull Project project) { - ApplicationManager.getApplication().invokeLater(() -> { - StudyTaskManager taskManager = StudyTaskManager.getInstance(project); - Course targetCourse = taskManager.getCourse(); - if (targetCourse != null) { - navigateToStep(project, targetCourse, stepId); - } - }); - } - }; - createNewProjectDlg.addListener(listener); createNewProjectDlg.show(); }); return true; } - - private static void navigateToStep(@NotNull Project project, @NotNull Course course, int stepId) { - if (stepId == 0 || course.isAdaptive()) { - return; - } - Task task = getTask(course, stepId); - if (task != null) { - navigateToTask(project, task); - } - } - - @Nullable - private static Task getTask(@NotNull Course course, int stepId) { - for (Lesson lesson : course.getLessons()) { - Task task = lesson.getTask(stepId); - if (task != null) { - return task; - } - } - return null; + @NotNull + private static StudyTaskManager getDefaultTaskManager() { + Project defaultProject = ProjectManager.getInstance().getDefaultProject(); + return StudyTaskManager.getInstance(defaultProject); } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectDialog.java b/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectDialog.java index a80f170f5ebf..97e6937dae2a 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectDialog.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectDialog.java @@ -43,15 +43,12 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.io.File; -import java.util.ArrayList; import java.util.EnumSet; -import java.util.List; public class EduCreateNewProjectDialog extends DialogWrapper { private static final Logger LOG = Logger.getInstance(EduCreateNewProjectDialog.class); - private Project myProject; - private Course myCourse; - private final List myListeners = new ArrayList<>(); + protected Project myProject; + protected Course myCourse; private final EduCreateNewProjectPanel myPanel; public EduCreateNewProjectDialog() { @@ -69,12 +66,8 @@ public class EduCreateNewProjectDialog extends DialogWrapper { return myPanel; } - public void addListener(@NotNull EduCreateNewProjectListener listener) { - myListeners.add(listener); - } - public void setCourse(@Nullable Course course) { - this.myCourse = course; + myCourse = course; } @Override @@ -107,7 +100,6 @@ public class EduCreateNewProjectDialog extends DialogWrapper { myPanel.setError("Project did't created"); return; } - notifyCreated(); super.doOKAction(); } @@ -180,8 +172,4 @@ public class EduCreateNewProjectDialog extends DialogWrapper { myProject = PlatformProjectOpenProcessor.doOpenProject(baseDir, null, -1, callback, options); return null; } - - private void notifyCreated() { - myListeners.forEach(listener -> listener.created(myProject)); - } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectListener.java b/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectListener.java deleted file mode 100644 index 3c7b8e122f0c..000000000000 --- a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewProjectListener.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2000-2017 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 com.jetbrains.edu.learning.newproject.ui; - -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; - -public interface EduCreateNewProjectListener { - void created(@NotNull Project project); -} diff --git a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewStepikProjectDialog.java b/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewStepikProjectDialog.java index 0c7079b58281..7db74ffde68d 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewStepikProjectDialog.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/newproject/ui/EduCreateNewStepikProjectDialog.java @@ -62,4 +62,13 @@ public class EduCreateNewStepikProjectDialog extends EduCreateNewProjectDialog { }, "Getting Available Courses", true, defaultProject) ); } + + @Override + public void show() { + if (myCourse != null) { + super.show(); + } else { + doCancelAction(); + } + } }