use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class IpnbCodeSourcePanel method createViewPanel.
@Override
protected JComponent createViewPanel() {
final JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(UIUtil.isUnderDarcula() ? IpnbEditorUtil.getBackground() : Gray._247);
myEditor = IpnbEditorUtil.createPythonCodeEditor(myProject, this);
Disposer.register(myParent.getFileEditor(), new Disposable() {
@Override
public void dispose() {
EditorFactory.getInstance().releaseEditor(myEditor);
}
});
final JComponent component = myEditor.getComponent();
final JComponent contentComponent = myEditor.getContentComponent();
new IpnbRunCellAction().registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke("shift ENTER")), contentComponent);
new IpnbRunCellInplaceAction().registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke("ctrl ENTER")), contentComponent);
contentComponent.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
final int keyCode = e.getKeyCode();
final Container parent = myParent.getParent();
if (keyCode == KeyEvent.VK_ESCAPE && parent instanceof IpnbFilePanel) {
getIpnbCodePanel().setEditing(false);
parent.repaint();
UIUtil.requestFocus(getIpnbCodePanel().getFileEditor().getIpnbFilePanel());
}
}
private void updateVisibleArea(boolean up) {
final IpnbFileEditor fileEditor = myParent.getFileEditor();
final IpnbFilePanel ipnbPanel = fileEditor.getIpnbFilePanel();
final Rectangle rect = ipnbPanel.getVisibleRect();
final Rectangle cellBounds = IpnbCodeSourcePanel.this.getIpnbCodePanel().getBounds();
final JScrollPane scrollPane = fileEditor.getScrollPane();
final int y = cellBounds.y + myEditor.visualPositionToXY(myEditor.getCaretModel().getVisualPosition()).y;
int delta = myEditor.getLineHeight() * 2;
if (y <= rect.getY() && up) {
scrollPane.getVerticalScrollBar().setValue(y);
}
if (y + delta > rect.getY() + rect.getHeight() && !up) {
scrollPane.getVerticalScrollBar().setValue(y - rect.height + delta);
}
}
@Override
public void keyReleased(KeyEvent e) {
final int keyCode = e.getKeyCode();
final Container parent = myParent.getParent();
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1) + 10;
contentComponent.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
myParent.revalidate();
myParent.repaint();
panel.revalidate();
panel.repaint();
if (parent instanceof IpnbFilePanel) {
IpnbFilePanel ipnbFilePanel = (IpnbFilePanel) parent;
ipnbFilePanel.revalidate();
ipnbFilePanel.repaint();
if (keyCode == KeyEvent.VK_ENTER && InputEvent.CTRL_MASK == e.getModifiers()) {
IpnbRunCellBaseAction.runCell(ipnbFilePanel, false);
} else if (keyCode == KeyEvent.VK_ENTER && InputEvent.SHIFT_DOWN_MASK == e.getModifiersEx()) {
IpnbRunCellBaseAction.runCell(ipnbFilePanel, true);
} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_PAGE_DOWN || keyCode == KeyEvent.VK_PAGE_UP) {
updateVisibleArea(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP);
}
}
}
});
contentComponent.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (InputEvent.CTRL_DOWN_MASK == e.getModifiersEx())
return;
final Container ipnbFilePanel = myParent.getParent();
if (ipnbFilePanel instanceof IpnbFilePanel) {
((IpnbFilePanel) ipnbFilePanel).setSelectedCell(myParent, true);
myParent.switchToEditing();
}
UIUtil.requestFocus(contentComponent);
}
});
panel.add(component);
contentComponent.addHierarchyListener(new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
final Container parent = myParent.getParent();
if (parent != null) {
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1) + 10;
contentComponent.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
}
}
});
contentComponent.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
@Override
public void ancestorResized(HierarchyEvent e) {
final Container parent = myParent.getParent();
final Component component = e.getChanged();
if (parent != null && component instanceof IpnbFilePanel) {
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1) + 10;
contentComponent.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
panel.revalidate();
panel.repaint();
parent.repaint();
}
}
});
return panel;
}
use of com.intellij.openapi.Disposable 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.intellij.openapi.Disposable in project kotlin by JetBrains.
the class KtParsingTestCase method registerExtensionPoint.
@Override
protected <T> void registerExtensionPoint(final ExtensionPointName<T> extensionPointName, Class<T> aClass) {
super.registerExtensionPoint(extensionPointName, aClass);
Disposer.register(myProject, new Disposable() {
@Override
public void dispose() {
Extensions.getRootArea().unregisterExtensionPoint(extensionPointName.getName());
}
});
}
use of com.intellij.openapi.Disposable in project kotlin by JetBrains.
the class AbstractCompileKotlinAgainstKotlinTest method createDisposable.
private Disposable createDisposable(String debugName) {
Disposable disposable = Disposer.newDisposable("CompileDisposable" + debugName);
Disposer.register(getTestRootDisposable(), disposable);
return disposable;
}
use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.
the class SwitchTaskAction method createPopup.
private static ListPopupImpl createPopup(final DataContext dataContext, @Nullable final Runnable onDispose, boolean withTitle) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final Ref<Boolean> shiftPressed = Ref.create(false);
final Ref<JComponent> componentRef = Ref.create();
List<TaskListItem> items = project == null ? Collections.<TaskListItem>emptyList() : createPopupActionGroup(project, shiftPressed, PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext));
final String title = withTitle ? "Switch to Task" : null;
ListPopupStep<TaskListItem> step = new MultiSelectionListPopupStep<TaskListItem>(title, items) {
@Override
public PopupStep<?> onChosen(List<TaskListItem> selectedValues, boolean finalChoice) {
if (finalChoice) {
selectedValues.get(0).select();
return FINAL_CHOICE;
}
ActionGroup group = createActionsStep(selectedValues, project, shiftPressed);
return JBPopupFactory.getInstance().createActionsStep(group, DataManager.getInstance().getDataContext(componentRef.get()), false, false, null, null, true);
}
@Override
public Icon getIconFor(TaskListItem aValue) {
return aValue.getIcon();
}
@NotNull
@Override
public String getTextFor(TaskListItem value) {
return value.getText();
}
@Nullable
@Override
public ListSeparator getSeparatorAbove(TaskListItem value) {
return value.getSeparator() == null ? null : new ListSeparator(value.getSeparator());
}
@Override
public boolean hasSubstep(List<TaskListItem> selectedValues) {
return selectedValues.size() > 1 || selectedValues.get(0).getTask() != null;
}
};
final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createListPopup(step);
if (onDispose != null) {
Disposer.register(popup, new Disposable() {
@Override
public void dispose() {
onDispose.run();
}
});
}
componentRef.set(popup.getComponent());
if (items.size() <= 2) {
return popup;
}
popup.setAdText("Press SHIFT to merge with current context");
popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
shiftPressed.set(true);
popup.setCaption("Merge with Current Context");
}
});
popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
shiftPressed.set(false);
popup.setCaption("Switch to Task");
}
});
popup.registerAction("invoke", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
popup.handleSelect(true);
}
});
return popup;
}
Aggregations