EDU-141 rename lesson and task actions

This commit is contained in:
Liana Bakradze
2014-09-08 17:27:17 +04:00
parent 60a9e99e96
commit 1d3540541d
11 changed files with 258 additions and 17 deletions
@@ -58,6 +58,12 @@
<action id="ShowPreview" class="org.jetbrains.plugins.coursecreator.actions.CCShowPreview">
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
</action>
<action id="RenameLesson" class="org.jetbrains.plugins.coursecreator.actions.CCRenameLesson">
<add-to-group group-id="ProjectViewPopupMenu" anchor="before" relative-to-action="CutCopyPasteGroup"/>
</action>
<action id="RenameTask" class="org.jetbrains.plugins.coursecreator.actions.CCRenameTask">
<add-to-group group-id="ProjectViewPopupMenu" anchor="before" relative-to-action="CutCopyPasteGroup"/>
</action>
<action id="PackCourse" class="org.jetbrains.plugins.coursecreator.actions.CreateCourseArchive">
<add-to-group group-id="MainToolBar" anchor="last" />
</action>
@@ -151,7 +151,7 @@ public class CCProjectComponent implements ProjectComponent {
if (task == null) {
return;
}
lesson.getTasklist().remove(task);
lesson.getTaskList().remove(task);
lesson.getTasksMap().remove(removedFile.getName());
}
@@ -71,6 +71,9 @@ public class CCProjectService implements PersistentStateComponent<Element> {
@Override
public void loadState(Element el) {
myCourse = XmlSerializer.deserialize(el.getChild(COURSE_ELEMENT), Course.class);
if (myCourse != null) {
myCourse.init();
}
}
public static CCProjectService getInstance(@NotNull Project project) {
@@ -155,7 +158,7 @@ public class CCProjectService implements PersistentStateComponent<Element> {
}
Lesson lesson = lessons.get(lessonIndex);
int taskIndex = getIndex(taskDirName, "task");
List<Task> tasks = lesson.getTasklist();
List<Task> tasks = lesson.getTaskList();
if (!indexIsValid(taskIndex, tasks)) {
return false;
}
@@ -228,8 +228,8 @@ public class CCRunTests extends AnAction {
.createChildDirectory(project, taskDir.getName());
if (CCProjectService.indexIsValid(lessonIndex, course.getLessons())) {
Lesson lesson = course.getLessons().get(lessonIndex);
if (CCProjectService.indexIsValid(index, lesson.getTasklist())) {
Task task = lesson.getTasklist().get(index);
if (CCProjectService.indexIsValid(index, lesson.getTaskList())) {
Task task = lesson.getTaskList().get(index);
HashMap<TaskFile, TaskFile> taskFilesCopy = new HashMap<TaskFile, TaskFile>();
for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
CreateCourseArchive.createUserFile(project, taskFilesCopy, taskResourceDir, taskDir, entry);
@@ -0,0 +1,95 @@
/*
* Copyright 2000-2014 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.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.psi.PsiDirectory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.coursecreator.CCProjectService;
import org.jetbrains.plugins.coursecreator.format.Course;
import javax.swing.*;
public abstract class CCRename extends DumbAwareAction {
public CCRename(String text, String description, Icon icon) {
super(text, description, icon);
}
@Override
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
final Project project = event.getData(CommonDataKeys.PROJECT);
if (project == null) {
presentation.setVisible(false);
presentation.setEnabled(false);
return;
}
final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
if (view == null) {
presentation.setVisible(false);
presentation.setEnabled(false);
return;
}
final PsiDirectory[] directories = view.getDirectories();
if (directories.length == 0) {
presentation.setVisible(false);
presentation.setEnabled(false);
return;
}
final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
if (directory == null || !directory.getName().contains(getFolderName())) {
presentation.setEnabled(false);
presentation.setVisible(false);
return;
}
presentation.setVisible(true);
presentation.setEnabled(true);
}
public abstract String getFolderName();
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
final Project project = e.getData(CommonDataKeys.PROJECT);
if (view == null || project == null) {
return;
}
final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
if (directory == null || !directory.getName().contains("lesson")) {
return;
}
Course course = CCProjectService.getInstance(project).getCourse();
if (course == null) {
return;
}
if (!processRename(project, directory, course)) return;
ProjectView.getInstance(project).refresh();
}
public abstract boolean processRename(Project project, PsiDirectory directory, Course course);
}
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2014 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.actions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import org.jetbrains.plugins.coursecreator.format.Course;
import org.jetbrains.plugins.coursecreator.format.Lesson;
public class CCRenameLesson extends CCRename {
public CCRenameLesson() {
super("Rename Lesson", "Rename Lesson", null);
}
@Override
public String getFolderName() {
return "lesson";
}
@Override
public boolean processRename(Project project, PsiDirectory directory, Course course) {
Lesson lesson = course.getLesson(directory.getName());
if (lesson == null) {
return false;
}
String newName = Messages.showInputDialog(project, "Enter new name", "Rename " + getFolderName(), null);
if (newName == null) {
return false;
}
lesson.setName(newName);
return true;
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2014 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.actions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import org.jetbrains.plugins.coursecreator.format.Course;
import org.jetbrains.plugins.coursecreator.format.Lesson;
import org.jetbrains.plugins.coursecreator.format.Task;
public class CCRenameTask extends CCRename {
public CCRenameTask() {
super("Rename Task", "Rename Task", null);
}
@Override
public String getFolderName() {
return "task";
}
@Override
public boolean processRename(Project project, PsiDirectory directory, Course course) {
PsiDirectory lessonDir = directory.getParent();
if (lessonDir == null || !lessonDir.getName().contains("lesson")) {
return false;
}
Lesson lesson = course.getLesson(lessonDir.getName());
if (lesson == null) {
return false;
}
Task task = lesson.getTask(directory.getName());
if (task == null) {
return false;
}
String newName = Messages.showInputDialog(project, "Enter new name", "Rename " + getFolderName(), null);
if (newName == null) {
return false;
}
task.setName(newName);
return true;
}
}
@@ -18,6 +18,7 @@ import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.coursecreator.CCProjectService;
import org.jetbrains.plugins.coursecreator.format.Course;
import org.jetbrains.plugins.coursecreator.format.Lesson;
@@ -42,7 +43,7 @@ public class CreateTask extends DumbAwareAction {
final CCProjectService service = CCProjectService.getInstance(project);
final Course course = service.getCourse();
final Lesson lesson = course.getLesson(directory.getName());
final int size = lesson.getTasklist().size();
final int size = lesson.getTaskList().size();
final String taskName = Messages.showInputDialog("Name:", "Task Name", null, "task" + (size + 1), null);
if (taskName == null) return;
@@ -83,7 +84,7 @@ public class CreateTask extends DumbAwareAction {
}
@Override
public void update(AnActionEvent event) {
public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
final Project project = event.getData(CommonDataKeys.PROJECT);
if (project == null) {
@@ -4,10 +4,7 @@ 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;
import java.util.*;
public class Course {
@Expose public List<Lesson> lessons = new ArrayList<Lesson>();
@@ -52,4 +49,13 @@ public class Course {
public String getDescription() {
return description;
}
public void init() {
lessons.clear();
for (Lesson lesson: myLessonsMap.values()) {
lessons.add(lesson);
lesson.init();
}
Collections.sort(lessons);
}
}
@@ -4,12 +4,9 @@ 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;
import java.util.*;
public class Lesson {
public class Lesson implements Comparable{
@Expose public String name;
@Expose public List<Task> task_list = new ArrayList<Task>();
@@ -27,11 +24,15 @@ public class Lesson {
task_list.add(task);
}
public void setName(String name) {
this.name = name;
}
public Task getTask(@NotNull final String name) {
return myTasksMap.get(name);
}
public List<Task> getTasklist() {
public List<Task> getTaskList() {
return task_list;
}
@@ -46,4 +47,18 @@ public class Lesson {
public Map<String, Task> getTasksMap() {
return myTasksMap;
}
public void init() {
task_list.clear();
for (Task task : myTasksMap.values()) {
task_list.add(task);
}
Collections.sort(task_list);
}
@Override
public int compareTo(@NotNull Object o) {
Lesson lesson = (Lesson) o;
return myIndex - lesson.getIndex();
}
}
@@ -7,7 +7,7 @@ import org.jetbrains.plugins.coursecreator.CCProjectService;
import java.util.HashMap;
import java.util.Map;
public class Task {
public class Task implements Comparable{
@Expose public String name;
@Expose public Map<String, TaskFile> task_files = new HashMap<String, TaskFile>();
public int myIndex;
@@ -44,4 +44,14 @@ public class Task {
public boolean isTaskFile(String name) {
return task_files.get(name) != null;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(@NotNull Object o) {
Task task = (Task) o;
return myIndex - task.getIndex();
}
}