use correct length
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCShowPreview.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.jetbrains.edu.coursecreator.actions;
17
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.Presentation;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.diagnostic.Logger;
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.project.DumbAwareAction;
30 import com.intellij.openapi.project.DumbModePermission;
31 import com.intellij.openapi.project.DumbService;
32 import com.intellij.openapi.project.Project;
33 import com.intellij.openapi.ui.FrameWrapper;
34 import com.intellij.openapi.util.Disposer;
35 import com.intellij.openapi.vfs.VirtualFile;
36 import com.intellij.psi.PsiDirectory;
37 import com.intellij.psi.PsiFile;
38 import com.intellij.ui.JBColor;
39 import com.jetbrains.edu.EduAnswerPlaceholderPainter;
40 import com.jetbrains.edu.EduUtils;
41 import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
42 import com.jetbrains.edu.courseFormat.Course;
43 import com.jetbrains.edu.courseFormat.TaskFile;
44 import com.jetbrains.edu.coursecreator.CCProjectService;
45 import org.jetbrains.annotations.NotNull;
46
47 import javax.swing.*;
48 import javax.swing.border.EmptyBorder;
49 import java.awt.*;
50 import java.text.SimpleDateFormat;
51 import java.util.Calendar;
52
53 public class CCShowPreview extends DumbAwareAction {
54   private static final Logger LOG = Logger.getInstance(CCShowPreview.class.getName());
55
56   public CCShowPreview() {
57     super("Show Preview","Show preview", null);
58   }
59
60   @Override
61   public void update(@NotNull AnActionEvent e) {
62     if (!CCProjectService.setCCActionAvailable(e)) {
63       return;
64     }
65     Presentation presentation = e.getPresentation();
66     presentation.setEnabled(false);
67     presentation.setVisible(false);
68     final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
69     if (file != null && file.getName().contains(".answer")) {
70       presentation.setEnabled(true);
71       presentation.setVisible(true);
72     }
73   }
74
75   @Override
76   public void actionPerformed(@NotNull AnActionEvent e) {
77     final Project project = e.getProject();
78     if (project == null) {
79       return;
80     }
81     final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
82     if (file == null || !file.getName().contains(".answer")) {
83       return;
84     }
85     final PsiDirectory taskDir = file.getContainingDirectory();
86     if (taskDir == null) {
87       return;
88     }
89     PsiDirectory lessonDir = taskDir.getParentDirectory();
90     if (lessonDir == null) {
91       return;
92     }
93     final CCProjectService service = CCProjectService.getInstance(project);
94     Course course = service.getCourse();
95     if (course == null) {
96       return;
97     }
98     TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
99     if (taskFile == null) {
100       return;
101     }
102     final TaskFile taskFileCopy = new TaskFile();
103     TaskFile.copy(taskFile, taskFileCopy);
104     final String taskFileName = CCProjectService.getRealTaskFileName(file.getVirtualFile().getName());
105     ApplicationManager.getApplication().runWriteAction(new Runnable() {
106         @Override
107         public void run() {
108           EduUtils.createStudentFileFromAnswer(project, taskDir.getVirtualFile(), taskDir.getVirtualFile(), taskFileName, taskFileCopy);
109       }
110     });
111
112     String userFileName = CCProjectService.getRealTaskFileName(file.getName());
113     if (userFileName == null) {
114       return;
115     }
116     VirtualFile userFile = taskDir.getVirtualFile().findChild(userFileName);
117     if (userFile == null) {
118       LOG.info("Generated file " + userFileName + "was not found");
119       return;
120     }
121     final FrameWrapper showPreviewFrame = new FrameWrapper(project);
122     showPreviewFrame.setTitle(userFileName);
123     LabeledEditor labeledEditor = new LabeledEditor(null);
124     final EditorFactory factory = EditorFactory.getInstance();
125     Document document = FileDocumentManager.getInstance().getDocument(userFile);
126     if (document == null) {
127       return;
128     }
129     final EditorEx createdEditor = (EditorEx)factory.createEditor(document, project, userFile, true);
130     Disposer.register(project, new Disposable() {
131       public void dispose() {
132         factory.releaseEditor(createdEditor);
133       }
134     });
135     for (AnswerPlaceholder answerPlaceholder : taskFileCopy.getAnswerPlaceholders()) {
136       EduAnswerPlaceholderPainter.drawAnswerPlaceholder(createdEditor, answerPlaceholder, true, JBColor.BLUE);
137     }
138     JPanel header = new JPanel();
139     header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
140     header.setBorder(new EmptyBorder(10, 10, 10, 10));
141     header.add(new JLabel("Read-only preview."));
142     String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
143     header.add(new JLabel(String.format("Created %s.", timeStamp)));
144     JComponent editorComponent = createdEditor.getComponent();
145     labeledEditor.setComponent(editorComponent, header);
146     createdEditor.setCaretVisible(false);
147     createdEditor.setCaretEnabled(false);
148     showPreviewFrame.setComponent(labeledEditor);
149     showPreviewFrame.setSize(new Dimension(500, 500));
150     DumbService.allowStartingDumbModeInside(DumbModePermission.MAY_START_BACKGROUND, new Runnable() {
151       @Override
152       public void run() {
153         showPreviewFrame.show();
154       }
155     });
156   }
157 }