Search in sources :

Example 6 with Disposable

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;
}
Also used : Disposable(com.intellij.openapi.Disposable) EditorMouseEvent(com.intellij.openapi.editor.event.EditorMouseEvent) EditorMouseAdapter(com.intellij.openapi.editor.event.EditorMouseAdapter) CustomShortcutSet(com.intellij.openapi.actionSystem.CustomShortcutSet) IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)

Example 7 with Disposable

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();
    }
}
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 8 with Disposable

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());
        }
    });
}
Also used : Disposable(com.intellij.openapi.Disposable)

Example 9 with Disposable

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;
}
Also used : Disposable(com.intellij.openapi.Disposable)

Example 10 with 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;
}
Also used : Disposable(com.intellij.openapi.Disposable) ActionEvent(java.awt.event.ActionEvent) Project(com.intellij.openapi.project.Project) SimpleActionGroup(com.intellij.tools.SimpleActionGroup) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) LocalChangeList(com.intellij.openapi.vcs.changes.LocalChangeList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Disposable (com.intellij.openapi.Disposable)282 NotNull (org.jetbrains.annotations.NotNull)52 Test (org.junit.Test)25 Project (com.intellij.openapi.project.Project)18 TestOnly (org.jetbrains.annotations.TestOnly)17 ArrayList (java.util.ArrayList)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 RelativePoint (com.intellij.ui.awt.RelativePoint)11 Nullable (org.jetbrains.annotations.Nullable)10 Document (com.intellij.openapi.editor.Document)9 File (java.io.File)9 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Editor (com.intellij.openapi.editor.Editor)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)6 Content (com.intellij.ui.content.Content)6 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)5 Alarm (com.intellij.util.Alarm)5 ActionEvent (java.awt.event.ActionEvent)5 ListSelectionEvent (javax.swing.event.ListSelectionEvent)5