2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.edu.coursecreator.actions;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.CommonDataKeys;
21 import com.intellij.openapi.actionSystem.LangDataKeys;
22 import com.intellij.openapi.actionSystem.Presentation;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.diff.impl.util.LabeledEditor;
25 import com.intellij.openapi.editor.Document;
26 import com.intellij.openapi.editor.EditorFactory;
27 import com.intellij.openapi.editor.ex.EditorEx;
28 import com.intellij.openapi.fileEditor.FileDocumentManager;
29 import com.intellij.openapi.module.Module;
30 import com.intellij.openapi.project.DumbAwareAction;
31 import com.intellij.openapi.project.Project;
32 import com.intellij.openapi.ui.FrameWrapper;
33 import com.intellij.openapi.ui.Messages;
34 import com.intellij.openapi.util.Disposer;
35 import com.intellij.openapi.util.Pair;
36 import com.intellij.openapi.vfs.VirtualFile;
37 import com.intellij.psi.PsiDirectory;
38 import com.intellij.psi.PsiFile;
39 import com.intellij.ui.JBColor;
40 import com.jetbrains.edu.coursecreator.CCUtils;
41 import com.jetbrains.edu.learning.StudyTaskManager;
42 import com.jetbrains.edu.learning.StudyUtils;
43 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
44 import com.jetbrains.edu.learning.core.EduUtils;
45 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
46 import com.jetbrains.edu.learning.courseFormat.Course;
47 import com.jetbrains.edu.learning.courseFormat.TaskFile;
48 import org.jetbrains.annotations.NotNull;
51 import javax.swing.border.EmptyBorder;
53 import java.text.SimpleDateFormat;
54 import java.util.Calendar;
55 import java.util.Collections;
57 public class CCShowPreview extends DumbAwareAction {
58 public static final String SHOW_PREVIEW = "Show Preview";
59 public static final String NO_PREVIEW_MESSAGE = "Preview is available for task files with answer placeholders only";
61 public CCShowPreview() {
62 super(SHOW_PREVIEW, SHOW_PREVIEW, null);
66 public void update(@NotNull AnActionEvent e) {
67 Presentation presentation = e.getPresentation();
68 presentation.setEnabledAndVisible(false);
69 Project project = e.getProject();
70 if (project == null) {
73 if (!CCUtils.isCourseCreator(project)) {
76 final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
77 if (file != null && StudyUtils.getTaskFile(project, file.getVirtualFile()) != null) {
78 presentation.setEnabledAndVisible(true);
83 public void actionPerformed(@NotNull AnActionEvent e) {
84 final Project project = e.getProject();
85 Module module = LangDataKeys.MODULE.getData(e.getDataContext());
86 if (project == null || module == null) {
89 final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
93 Course course = StudyTaskManager.getInstance(project).getCourse();
97 VirtualFile virtualFile = file.getVirtualFile();
98 TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
99 if (taskFile == null) {
102 final PsiDirectory taskDir = file.getContainingDirectory();
103 if (taskDir == null) {
106 PsiDirectory lessonDir = taskDir.getParentDirectory();
107 if (lessonDir == null) {
112 if (taskFile.getActivePlaceholders().isEmpty()) {
113 Messages.showInfoMessage("Preview is available for task files with answer placeholders only", "No Preview for This File");
117 VirtualFile generatedFilesFolder = CCUtils.getGeneratedFilesFolder(project, module);
119 if (generatedFilesFolder == null) {
123 ApplicationManager.getApplication().runWriteAction(new Runnable() {
126 Pair<VirtualFile, TaskFile> pair =
127 EduUtils.createStudentFile(this, project, virtualFile, generatedFilesFolder, null, taskFile.getTask().getActiveSubtaskIndex());
129 showPreviewDialog(project, pair.getFirst(), pair.getSecond());
135 private static void showPreviewDialog(@NotNull Project project, @NotNull VirtualFile userFile, @NotNull TaskFile taskFile) {
136 final FrameWrapper showPreviewFrame = new FrameWrapper(project);
137 showPreviewFrame.setTitle(userFile.getName());
138 LabeledEditor labeledEditor = new LabeledEditor(null);
139 final EditorFactory factory = EditorFactory.getInstance();
140 Document document = FileDocumentManager.getInstance().getDocument(userFile);
141 if (document == null) {
144 final EditorEx createdEditor = (EditorEx)factory.createEditor(document, project, userFile, true);
145 Disposer.register(project, new Disposable() {
146 public void dispose() {
147 factory.releaseEditor(createdEditor);
150 for (AnswerPlaceholder answerPlaceholder : taskFile.getActivePlaceholders()) {
151 if (answerPlaceholder.getActiveSubtaskInfo().isNeedInsertText()) {
152 answerPlaceholder.setLength(answerPlaceholder.getTaskText().length());
154 Integer minIndex = Collections.min(answerPlaceholder.getSubtaskInfos().keySet());
155 answerPlaceholder.setUseLength(minIndex >= answerPlaceholder.getActiveSubtaskIndex());
156 EduAnswerPlaceholderPainter.drawAnswerPlaceholder(createdEditor, answerPlaceholder, JBColor.BLUE);
158 JPanel header = new JPanel();
159 header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
160 header.setBorder(new EmptyBorder(10, 10, 10, 10));
161 header.add(new JLabel("Read-only preview."));
162 String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
163 header.add(new JLabel(String.format("Created %s.", timeStamp)));
164 JComponent editorComponent = createdEditor.getComponent();
165 labeledEditor.setComponent(editorComponent, header);
166 createdEditor.setCaretVisible(false);
167 createdEditor.setCaretEnabled(false);
168 showPreviewFrame.setComponent(labeledEditor);
169 showPreviewFrame.setSize(new Dimension(500, 500));
170 if (!ApplicationManager.getApplication().isUnitTestMode()) {
171 showPreviewFrame.show();