Skip to content

Commit

Permalink
Merge pull request #92 from StepicOrg/feature/active_step
Browse files Browse the repository at this point in the history
The StepProjectManager manage a study node selection
#IDEA-209 Fixed
  • Loading branch information
meanmail authored Mar 21, 2017
2 parents 83b0c78 + 5a6165f commit 13e6984
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.jetbrains.tmp.learning.serialization.StudySerializationUtils;
import com.jetbrains.tmp.learning.serialization.StudyUnrecognizedFormatException;
import com.jetbrains.tmp.learning.serialization.SupportedLanguagesConverter;
import com.jetbrains.tmp.learning.ui.StudyToolWindow;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
Expand Down Expand Up @@ -59,8 +60,8 @@
import java.util.UUID;

import static com.jetbrains.tmp.learning.SupportedLanguages.INVALID;
import static org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcDirectory;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcDirectory;

@State(name = "StepikStudySettings", storages = @Storage("stepik_study_project.xml"))
public class StepikProjectManager implements PersistentStateComponent<Element>, DumbAware {
Expand All @@ -79,6 +80,7 @@ public class StepikProjectManager implements PersistentStateComponent<Element>,
@XStreamOmitField
private final Project project;
private StudyNode<?, ?> root;
private StudyNode<?, ?> selected;
private boolean showHint = false;
private long createdBy;
private SupportedLanguages defaultLang = INVALID;
Expand Down Expand Up @@ -176,6 +178,59 @@ private static Element toElement(ByteArrayOutputStream out)
}
}

public static void setSelected(@NotNull Project project, @Nullable StudyNode studyNode, boolean force) {
StepikProjectManager instance = getInstance(project);
if (instance != null) {
instance.setSelected(studyNode, force);
}
}

public static void setSelected(@NotNull Project project, @Nullable StudyNode studyNode) {
setSelected(project, studyNode, false);
}

public static void updateSelection(@NotNull Project project) {
StepikProjectManager instance = getInstance(project);
if (instance != null) {
instance.updateSelection();
}
}

@Nullable
public static StudyNode<?, ?> getSelected(@Nullable Project project) {
if (project == null) {
return null;
}
StepikProjectManager instance = getInstance(project);
if (instance != null) {
return instance.getSelected();
}

return null;
}

public StudyNode<?, ?> getSelected() {
return selected;
}

public void setSelected(StudyNode<?, ?> selected) {
setSelected(selected, false);
}

public void setSelected(StudyNode<?, ?> selected, boolean force) {
this.selected = selected;
if (project != null) {
StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
if (toolWindow != null) {
ApplicationManager.getApplication().invokeLater(() -> toolWindow.setStepNode(selected, force));
}
}
}

public void updateSelection() {
setSelected(selected, true);
}

public void setRootNode(@Nullable StudyNode root) {
this.root = root;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.jetbrains.tmp.learning;

import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerAdapter;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.tmp.learning.courseFormat.StudyNode;
import com.jetbrains.tmp.learning.ui.StudyToolWindow;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
Expand All @@ -34,23 +33,20 @@ public Map<String, JPanel> getAdditionalPanels(Project project) {

@NotNull
@Override
public FileEditorManagerListener getFileEditorManagerListener(
@NotNull Project project,
@NotNull StudyToolWindow toolWindow) {

return new FileEditorManagerListener() {
public FileEditorManagerListener getFileEditorManagerListener(@NotNull Project project) {
return new FileEditorManagerAdapter() {
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
}
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
VirtualFile file = event.getNewFile();
if (file == null) {
return;
}

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
}
StudyNode stepNode = StudyUtils.getStudyNode(project, file);

@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
StudyNode stepNode = StudyUtils.getSelectedNode(event.getManager().getProject());
toolWindow.setStepNode(stepNode);
if (stepNode != null) {
StepikProjectManager.setSelected(project, stepNode);
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.jetbrains.tmp.learning.ui.StudyToolWindow;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
Expand All @@ -28,9 +27,7 @@ public interface StudyPluginConfigurator {
Map<String, JPanel> getAdditionalPanels(Project project);

@NotNull
FileEditorManagerListener getFileEditorManagerListener(
@NotNull final Project project,
@NotNull final StudyToolWindow toolWindow);
FileEditorManagerListener getFileEditorManagerListener(@NotNull final Project project);

boolean accept(@NotNull final Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public void projectOpened() {
if (!StepikProjectManager.isStepikProject(project)) {
return;
}
// Check if user has javafx lib in his JDK. Now bundled JDK doesn't have this lib inside.
if (StudyUtils.hasJavaFx()) {
Platform.setImplicitExit(false);
}

Platform.setImplicitExit(false);

registerStudyToolWindow();
ApplicationManager.getApplication().invokeLater(
Expand Down
50 changes: 0 additions & 50 deletions core/src/main/java/com/jetbrains/tmp/learning/StudyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.intellij.ide.projectView.impl.AbstractProjectViewPane;
import com.intellij.ide.ui.LafManager;
import com.intellij.ide.ui.laf.darcula.DarculaLookAndFeelInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
Expand Down Expand Up @@ -59,11 +57,6 @@ public class StudyUtils {
private StudyUtils() {
}

public static void updateToolWindows(@NotNull final Project project) {
StudyNode stepNode = getSelectedNode(project);
setStudyNode(project, stepNode, true);
}

static void initToolWindows(@NotNull final Project project) {
final ToolWindowManager windowManager = ToolWindowManager.getInstance(project);
windowManager.getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW)
Expand All @@ -89,17 +82,6 @@ static StudyToolWindow getStudyToolWindow(@NotNull final Project project) {
return null;
}

public static void setStudyNode(@NotNull final Project project, @Nullable StudyNode studyNode) {
setStudyNode(project, studyNode, false);
}

public static void setStudyNode(@NotNull final Project project, @Nullable StudyNode studyNode, boolean force) {
StudyToolWindow toolWindow = getStudyToolWindow(project);
if (toolWindow != null) {
ApplicationManager.getApplication().invokeLater(() -> toolWindow.setStepNode(studyNode, force));
}
}

@NotNull
public static String getVideoStepText(
@NotNull VideoStepNodeHelper videoStepNode,
Expand Down Expand Up @@ -268,29 +250,6 @@ private static String getRelativePath(@NotNull Project project, @NotNull Virtual
return ProjectFilesUtils.getRelativePath(basePath, path);
}

@Nullable
public static StepNode getSelectedStep(@NotNull Project project) {
VirtualFile[] files = FileEditorManager.getInstance(project).getSelectedFiles();
if (files.length == 0) {
return null;
}

StudyNode studyNode = getStudyNode(project, files[0]);

return studyNode instanceof StepNode ? (StepNode) studyNode : null;
}

@Nullable
static StudyNode getSelectedNode(@NotNull Project project) {
StudyNode studyNode = getSelectedStep(project);

if (studyNode == null) {
studyNode = getSelectedNodeInTree(project);
}

return studyNode;
}

@Nullable
public static StudyNode getSelectedNodeInTree(@NotNull Project project) {
PsiElement element = getSelectedPsiElement(project);
Expand Down Expand Up @@ -340,15 +299,6 @@ private static PsiElement getSelectedPsiElement(@NotNull Project project) {
}
}

public static boolean hasJavaFx() {
try {
Class.forName("javafx.application.Platform");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

@Nullable
public static StudyNode getStudyNode(@NotNull Project project, @NotNull VirtualFile nodeVF) {
String path = getRelativePath(project, nodeVF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class Node<
private StudyNode getLastNode() {
List<C> children = getChildren();

for (int i = children.size() - 1; i > 0; i--) {
for (int i = children.size() - 1; i >= 0; i--) {
C child = children.get(i);
if (!child.getWasDeleted()) {
return child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void handleEvent(Event event) {
boolean locked = elements.isLocked();
if (!locked) {
getAttempt(stepNode);
StudyUtils.setStudyNode(project, node, true);
StepikProjectManager.updateSelection(project);
}
break;
case "active":
Expand Down Expand Up @@ -210,7 +210,7 @@ public void run(@NotNull ProgressIndicator indicator) {
}
} catch (StepikClientException e) {
logger.warn("Failed send step from browser", e);
StudyUtils.updateToolWindows(project);
StepikProjectManager.updateSelection(project);
}
}
});
Expand Down
Loading

0 comments on commit 13e6984

Please sign in to comment.