refactoring of change course info action
authorliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 23 Mar 2016 14:53:36 +0000 (17:53 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 23 Mar 2016 14:53:36 +0000 (17:53 +0300)
remove usage of CCProjectService
use DialogBuilder instead of DialogWrapper

python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangeCourseInfo.java

index 28706dae9a72da9a16cce7d84e5f6199e19cba5f..3423c822aae87f9453710ffaee944990e898aff6 100644 (file)
@@ -2,41 +2,41 @@ package com.jetbrains.edu.coursecreator.actions;
 
 import com.intellij.ide.IdeView;
 import com.intellij.ide.projectView.ProjectView;
-import com.intellij.ide.util.DirectoryChooserUtil;
 import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.LangDataKeys;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.DialogWrapper;
+import com.intellij.openapi.ui.DialogBuilder;
 import com.intellij.psi.PsiDirectory;
-import com.jetbrains.edu.learning.courseFormat.Course;
-import com.jetbrains.edu.coursecreator.CCProjectService;
+import com.intellij.util.ui.JBUI;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.coursecreator.ui.CCNewProjectPanel;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
-import java.awt.*;
 
 public class CCChangeCourseInfo extends DumbAwareAction {
+
+  private static final String ACTION_TEXT = "Change Course Information";
+
   public CCChangeCourseInfo() {
-    super("Change Course Information", "Change Course Information", null);
+    super(ACTION_TEXT, ACTION_TEXT, null);
   }
 
   @Override
   public void update(@NotNull AnActionEvent event) {
-    if (!CCProjectService.setCCActionAvailable(event)) {
-      return;
-    }
+    final Project project = event.getProject();
     final Presentation presentation = event.getPresentation();
-    presentation.setVisible(false);
-    presentation.setEnabled(false);
-    final Project project = event.getData(CommonDataKeys.PROJECT);
     if (project == null) {
       return;
     }
+    presentation.setEnabledAndVisible(false);
+    if (!CCUtils.isCourseCreator(project)) {
+      return;
+    }
     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
     if (view == null) {
       return;
@@ -45,45 +45,24 @@ public class CCChangeCourseInfo extends DumbAwareAction {
     if (directories.length == 0) {
       return;
     }
-    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
-    if (directory != null && !project.getBaseDir().equals(directory.getVirtualFile())) {
-      return;
-    }
-    presentation.setVisible(true);
-    presentation.setEnabled(true);
-
+    presentation.setEnabledAndVisible(true);
   }
 
   @Override
   public void actionPerformed(@NotNull AnActionEvent e) {
-    if (!CCProjectService.setCCActionAvailable(e)) {
-      return;
-    }
-    final Project project = e.getProject();
+    Project project = e.getProject();
     if (project == null) {
       return;
     }
-    Course course = CCProjectService.getInstance(project).getCourse();
+    Course course = StudyTaskManager.getInstance(project).getCourse();
     if (course == null) {
       return;
     }
-    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
-    if (view == null) {
-      return;
-    }
-    final PsiDirectory[] directories = view.getDirectories();
-    if (directories.length == 0) {
-      return;
-    }
-    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
-    if (directory != null && !project.getBaseDir().equals(directory.getVirtualFile())) {
-      return;
-    }
-    CCNewProjectPanel panel = new CCNewProjectPanel(course.getName(), Course.getAuthorsString(course.getAuthors()), course.getDescription());
-    ChangeCourseInfoDialog changeCourseInfoDialog =
-      new ChangeCourseInfoDialog(project, panel);
-    changeCourseInfoDialog.show();
-    if (changeCourseInfoDialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
+
+    CCNewProjectPanel panel =
+      new CCNewProjectPanel(course.getName(), Course.getAuthorsString(course.getAuthors()), course.getDescription());
+    DialogBuilder builder = createChangeInfoDialog(project, panel);
+    if (builder.showAndGet()) {
       course.setAuthors(panel.getAuthors());
       course.setName(panel.getName());
       course.setDescription(panel.getDescription());
@@ -91,24 +70,14 @@ public class CCChangeCourseInfo extends DumbAwareAction {
     }
   }
 
-  static class ChangeCourseInfoDialog extends DialogWrapper {
+  private static DialogBuilder createChangeInfoDialog(Project project, @NotNull CCNewProjectPanel panel) {
+    DialogBuilder builder = new DialogBuilder(project);
 
-    private final JPanel myMainPanel;
+    builder.setTitle(ACTION_TEXT);
+    JPanel changeInfoPanel = panel.getMainPanel();
+    changeInfoPanel.setMinimumSize(JBUI.size(400, 300));
+    builder.setCenterPanel(changeInfoPanel);
 
-    public ChangeCourseInfoDialog(@Nullable Project project, CCNewProjectPanel panel) {
-      super(project);
-      setTitle("Change Course Information");
-      myMainPanel = panel.getMainPanel();
-      init();
-    }
-
-    @Nullable
-    @Override
-    protected JComponent createCenterPanel() {
-      myMainPanel.setPreferredSize(new Dimension(400, 300));
-      myMainPanel.setSize(new Dimension(400, 300));
-      myMainPanel.setMaximumSize(new Dimension(400, 300));
-      return myMainPanel;
-    }
+    return builder;
   }
 }