Search in sources :

Example 1 with IpnbFileEditor

use of org.jetbrains.plugins.ipnb.editor.IpnbFileEditor 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 2 with IpnbFileEditor

use of org.jetbrains.plugins.ipnb.editor.IpnbFileEditor in project intellij-community by JetBrains.

the class IpnbRunAllCellsAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    final DataContext context = event.getDataContext();
    final IpnbFileEditor ipnbEditor = IpnbFileEditor.DATA_KEY.getData(context);
    if (ipnbEditor != null) {
        final IpnbFilePanel ipnbFilePanel = ipnbEditor.getIpnbFilePanel();
        final List<IpnbEditablePanel> cells = ipnbFilePanel.getIpnbPanels();
        final Project project = ipnbFilePanel.getProject();
        final IpnbConnectionManager connectionManager = IpnbConnectionManager.getInstance(project);
        final VirtualFile virtualFile = ipnbEditor.getVirtualFile();
        final String path = virtualFile.getPath();
        if (!connectionManager.hasConnection(path)) {
            String url = IpnbSettings.getInstance(project).getURL();
            if (StringUtil.isEmptyOrSpaces(url)) {
                url = IpnbConnectionManager.showDialogUrl(url);
            }
            if (url == null)
                return;
            IpnbSettings.getInstance(project).setURL(url);
            final String finalUrl = url;
            ApplicationManager.getApplication().executeOnPooledThread(() -> {
                final boolean serverStarted = connectionManager.startIpythonServer(finalUrl, ipnbEditor);
                if (!serverStarted) {
                    return;
                }
                UIUtil.invokeLaterIfNeeded(() -> connectionManager.startConnection(null, path, finalUrl, false));
                runCells(cells, ipnbFilePanel);
            });
        } else {
            runCells(cells, ipnbFilePanel);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) IpnbConnectionManager(org.jetbrains.plugins.ipnb.configuration.IpnbConnectionManager) IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)

Example 3 with IpnbFileEditor

use of org.jetbrains.plugins.ipnb.editor.IpnbFileEditor in project intellij-community by JetBrains.

the class IpnbRunCellBaseAction method update.

@Override
public void update(AnActionEvent e) {
    final DataContext context = e.getDataContext();
    final IpnbFileEditor editor = IpnbFileEditor.DATA_KEY.getData(context);
    final Presentation presentation = e.getPresentation();
    presentation.setEnabledAndVisible(editor != null);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor) Presentation(com.intellij.openapi.actionSystem.Presentation)

Example 4 with IpnbFileEditor

use of org.jetbrains.plugins.ipnb.editor.IpnbFileEditor in project intellij-community by JetBrains.

the class IpnbRunCellInplaceAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    final DataContext context = event.getDataContext();
    final IpnbFileEditor ipnbEditor = IpnbFileEditor.DATA_KEY.getData(context);
    if (ipnbEditor != null) {
        final IpnbFilePanel component = ipnbEditor.getIpnbFilePanel();
        runCell(component, false);
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)

Example 5 with IpnbFileEditor

use of org.jetbrains.plugins.ipnb.editor.IpnbFileEditor in project intellij-community by JetBrains.

the class IpnbSaveAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    final DataContext context = event.getDataContext();
    final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
    if (editor instanceof IpnbFileEditor) {
        saveAndCheckpoint((IpnbFileEditor) editor);
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) FileEditor(com.intellij.openapi.fileEditor.FileEditor) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)

Aggregations

IpnbFileEditor (org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)27 DataContext (com.intellij.openapi.actionSystem.DataContext)23 IpnbFilePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel)19 FileEditor (com.intellij.openapi.fileEditor.FileEditor)14 IpnbEditablePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)3 Editor (com.intellij.openapi.editor.Editor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IpnbCodePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)2 IpnbCodeSourcePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodeSourcePanel)2 Disposable (com.intellij.openapi.Disposable)1 CustomShortcutSet (com.intellij.openapi.actionSystem.CustomShortcutSet)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 EditorMouseAdapter (com.intellij.openapi.editor.event.EditorMouseAdapter)1 EditorMouseEvent (com.intellij.openapi.editor.event.EditorMouseEvent)1 Project (com.intellij.openapi.project.Project)1 IpnbConnectionManager (org.jetbrains.plugins.ipnb.configuration.IpnbConnectionManager)1 IpnbConnection (org.jetbrains.plugins.ipnb.protocol.IpnbConnection)1