Search in sources :

Example 6 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class StudyAnswerPlaceholderExtendWordHandler method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    AnswerPlaceholder placeholder = getAnswerPlaceholder(e, cursorOffset);
    assert placeholder != null;
    final Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(placeholder, editor.getDocument());
    return Collections.singletonList(new TextRange(offsets.getFirst(), offsets.getSecond()));
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) TextRange(com.intellij.openapi.util.TextRange)

Example 7 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class CCEditAnswerPlaceholder method performAnswerPlaceholderAction.

@Override
protected void performAnswerPlaceholderAction(@NotNull CCState state) {
    final Project project = state.getProject();
    PsiFile file = state.getFile();
    final PsiDirectory taskDir = file.getContainingDirectory();
    final PsiDirectory lessonDir = taskDir.getParent();
    if (lessonDir == null)
        return;
    AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
    if (answerPlaceholder == null) {
        return;
    }
    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder.getTaskText(), answerPlaceholder.getHints());
    dlg.setTitle("Edit Answer Placeholder");
    if (dlg.showAndGet()) {
        final String answerPlaceholderText = dlg.getTaskText();
        answerPlaceholder.setTaskText(answerPlaceholderText);
        answerPlaceholder.setLength(answerPlaceholder.getActiveSubtaskInfo().isNeedInsertText() ? 0 : StringUtil.notNullize(answerPlaceholderText).length());
        answerPlaceholder.setHints(dlg.getHints());
    }
}
Also used : Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 8 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class CCShowPreview method showPreviewDialog.

private static void showPreviewDialog(@NotNull Project project, @NotNull VirtualFile userFile, @NotNull TaskFile taskFile) {
    final FrameWrapper showPreviewFrame = new FrameWrapper(project);
    showPreviewFrame.setTitle(userFile.getName());
    LabeledEditor labeledEditor = new LabeledEditor(null);
    final EditorFactory factory = EditorFactory.getInstance();
    Document document = FileDocumentManager.getInstance().getDocument(userFile);
    if (document == null) {
        return;
    }
    final EditorEx createdEditor = (EditorEx) factory.createEditor(document, project, userFile, true);
    Disposer.register(project, new Disposable() {

        public void dispose() {
            factory.releaseEditor(createdEditor);
        }
    });
    for (AnswerPlaceholder answerPlaceholder : taskFile.getActivePlaceholders()) {
        if (answerPlaceholder.getActiveSubtaskInfo().isNeedInsertText()) {
            answerPlaceholder.setLength(answerPlaceholder.getTaskText().length());
        }
        Integer minIndex = Collections.min(answerPlaceholder.getSubtaskInfos().keySet());
        answerPlaceholder.setUseLength(minIndex >= answerPlaceholder.getActiveSubtaskIndex());
        EduAnswerPlaceholderPainter.drawAnswerPlaceholder(createdEditor, answerPlaceholder, JBColor.BLUE);
    }
    JPanel header = new JPanel();
    header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
    header.setBorder(new EmptyBorder(10, 10, 10, 10));
    header.add(new JLabel("Read-only preview."));
    String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
    header.add(new JLabel(String.format("Created %s.", timeStamp)));
    JComponent editorComponent = createdEditor.getComponent();
    labeledEditor.setComponent(editorComponent, header);
    createdEditor.setCaretVisible(false);
    createdEditor.setCaretEnabled(false);
    showPreviewFrame.setComponent(labeledEditor);
    showPreviewFrame.setSize(new Dimension(500, 500));
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        showPreviewFrame.show();
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) EditorFactory(com.intellij.openapi.editor.EditorFactory) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) EditorEx(com.intellij.openapi.editor.ex.EditorEx) FrameWrapper(com.intellij.openapi.ui.FrameWrapper) Document(com.intellij.openapi.editor.Document) LabeledEditor(com.intellij.openapi.diff.impl.util.LabeledEditor) EmptyBorder(javax.swing.border.EmptyBorder) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class CCAddAnswerPlaceholder method arePlaceholdersIntersect.

private static boolean arePlaceholdersIntersect(@NotNull final TaskFile taskFile, int start, int end) {
    List<AnswerPlaceholder> answerPlaceholders = taskFile.getActivePlaceholders();
    for (AnswerPlaceholder existingAnswerPlaceholder : answerPlaceholders) {
        int twStart = existingAnswerPlaceholder.getOffset();
        int twEnd = existingAnswerPlaceholder.getPossibleAnswerLength() + twStart;
        if ((start >= twStart && start < twEnd) || (end > twStart && end <= twEnd) || (twStart >= start && twStart < end) || (twEnd > start && twEnd <= end)) {
            return true;
        }
    }
    return false;
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)

Example 10 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class CCAnswerPlaceholderAction method getState.

@Nullable
protected CCState getState(@NotNull AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null || !CCUtils.isCourseCreator(project)) {
        return null;
    }
    final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
    if (psiFile == null) {
        return null;
    }
    VirtualFile virtualFile = psiFile.getVirtualFile();
    if (virtualFile == null) {
        return null;
    }
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (editor == null) {
        return null;
    }
    TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
    if (taskFile == null) {
        return null;
    }
    AnswerPlaceholder answerPlaceholder = StudyUtils.getAnswerPlaceholder(editor.getCaretModel().getOffset(), getPlaceholders(taskFile));
    return new CCState(taskFile, answerPlaceholder, psiFile, editor, project);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)25 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)12 Document (com.intellij.openapi.editor.Document)9 Editor (com.intellij.openapi.editor.Editor)7 Project (com.intellij.openapi.project.Project)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)4 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiFile (com.intellij.psi.PsiFile)3 File (java.io.File)3 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)2 StudyState (com.jetbrains.edu.learning.StudyState)2 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)2 Course (com.jetbrains.edu.learning.courseFormat.Course)2 StudyEditor (com.jetbrains.edu.learning.editor.StudyEditor)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 Gson (com.google.gson.Gson)1