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()));
}
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());
}
}
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();
}
}
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;
}
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);
}
Aggregations