PY-19960 UI change: use right-click pop-up menu to merge and split cells
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 14 Oct 2016 15:54:49 +0000 (18:54 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Mon, 24 Oct 2016 17:09:46 +0000 (20:09 +0300)
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/IpnbFileEditor.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbMergeCellAboveAction.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbMergeCellBelowAction.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbSplitCellAction.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbEditablePanel.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/IpnbMarkdownPanel.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodePanel.java
python/ipnb/src/org/jetbrains/plugins/ipnb/editor/panels/code/IpnbCodeSourcePanel.java

index b50a9c446d57b133b0eac57194b94c70a718b77e..92ed37a2e62485f95423dd3cb5c9c9aca337b9e7 100644 (file)
@@ -122,8 +122,6 @@ public class IpnbFileEditor extends UserDataHolderBase implements FileEditor {
     addCutButton(editorPanel);
     addCopyButton(editorPanel);
     addPasteButton(editorPanel);
-    addMergeButton(editorPanel);
-    addSplitButton(editorPanel);
     controlPanel.add(editorPanel);
 
     final JPanel runPanel = new JPanel(new GridBagLayout());
@@ -205,24 +203,6 @@ public class IpnbFileEditor extends UserDataHolderBase implements FileEditor {
       }
     }, AllIcons.Actions.Menu_cut, "Cut Cell");
   }
-  
-  private void addMergeButton(@NotNull final JPanel controlPanel) {
-    addButton(controlPanel, new ActionListener() {
-      @Override
-      public void actionPerformed(ActionEvent e) {
-        IpnbMergeCellAboveAction.mergeCell(myIpnbFilePanel);
-      }
-    }, AllIcons.Modules.Merge, "Merge Cell Above");
-  }
-  
-  private void addSplitButton(@NotNull final JPanel controlPanel) {
-    addButton(controlPanel, new ActionListener() {
-      @Override
-      public void actionPerformed(ActionEvent e) {
-        IpnbSplitCellAction.splitCell(myIpnbFilePanel);
-      }
-    }, AllIcons.Modules.Split, "Split Cellхо");
-  }
 
   private void addCopyButton(@NotNull final JPanel controlPanel) {
     addButton(controlPanel, new ActionListener() {
index 5d7078be6565e4fc9a86e7c6e91e6abd288c45a0..8b5d7bbc8535250efa885de7e4776df99b77a517 100644 (file)
@@ -10,6 +10,11 @@ import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
 
 public class IpnbMergeCellAboveAction extends AnAction {
+
+  public IpnbMergeCellAboveAction() {
+    super("Merge Cell Above");
+  }
+
   @Override
   public void actionPerformed(AnActionEvent e) {
     final DataContext context = e.getDataContext();
index 2a5c4edbc0c4d32bef62de6ab3cce14c4d007f54..b9b6d71a173e12bb9986845f9f411d4ab6f7b138 100644 (file)
@@ -10,6 +10,11 @@ import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
 
 public class IpnbMergeCellBelowAction extends AnAction {
+
+  public IpnbMergeCellBelowAction() {
+    super("Merge Cell Below");
+  }
+
   @Override
   public void actionPerformed(AnActionEvent e) {
     final DataContext context = e.getDataContext();
index cc87c0b60bf2907c2f1910f653b13dc2d07784f4..e378864934a4dcded70bc70763c2297a19b0df17 100644 (file)
@@ -6,10 +6,14 @@ import com.intellij.openapi.actionSystem.DataContext;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.command.CommandProcessor;
 import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
-import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
 
 public class IpnbSplitCellAction extends AnAction {
+
+  public IpnbSplitCellAction() {
+    super("Split Cell");
+  }
+
   @Override
   public void actionPerformed(AnActionEvent e) {
     final DataContext context = e.getDataContext();
@@ -24,16 +28,4 @@ public class IpnbSplitCellAction extends AnAction {
     CommandProcessor.getInstance().executeCommand(filePanel.getProject(), () -> ApplicationManager.getApplication().runWriteAction(
       () -> filePanel.splitCell()), "Ipnb.splitCell", new Object());
   }
-
-  @Override
-  public void update(AnActionEvent e) {
-    final DataContext context = e.getDataContext();
-    final IpnbFileEditor ipnbEditor = IpnbFileEditor.DATA_KEY.getData(context);
-    if (ipnbEditor != null) {
-      final IpnbFilePanel ipnbFilePanel = ipnbEditor.getIpnbFilePanel();
-      final IpnbEditablePanel selectedCellPanel = ipnbFilePanel.getSelectedCell();
-      final boolean isEnabled = selectedCellPanel != null && selectedCellPanel.isEditing();
-      e.getPresentation().setEnabled(isEnabled);
-    }
-  }
 }
index 560f1c0ed852a8d286a2922b3b8cac209b61f4e9..fd4fdb887157a920d7fa0b53b20d096b64c81308 100644 (file)
@@ -1,14 +1,23 @@
 package org.jetbrains.plugins.ipnb.editor.panels;
 
+import com.intellij.ide.DataManager;
+import com.intellij.openapi.actionSystem.DataContext;
+import com.intellij.openapi.actionSystem.DefaultActionGroup;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.ui.popup.JBPopupFactory;
+import com.intellij.openapi.ui.popup.ListPopup;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.ui.JBColor;
+import com.intellij.ui.awt.RelativePoint;
 import com.intellij.ui.OnePixelSplitter;
 import com.intellij.util.ui.JBUI;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellAboveAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellBelowAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbSplitCellAction;
 import org.jetbrains.plugins.ipnb.format.cells.IpnbEditableCell;
 
 import javax.swing.*;
@@ -48,6 +57,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
     mySplitter.setSecondComponent(null);
     setBackground(IpnbEditorUtil.getBackground());
     add(mySplitter);
+    addRightClickMenu();
   }
 
   private void addEditablePanel() {
@@ -201,7 +211,7 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
 
   public void updateCellView() { // TODO: make abstract
   }
-  
+
   public int getCaretPosition() {
     if (myEditing) {
       if (myEditablePanel != null) {
@@ -210,7 +220,28 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
     }
     return -1;
   }
-  
+
+  public void addRightClickMenu() {
+    myViewPanel.addMouseListener(new MouseAdapter() {
+      @Override
+      public void mousePressed(MouseEvent e) {
+        if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) {
+          final DefaultActionGroup group =
+            new DefaultActionGroup(new IpnbMergeCellAboveAction(), new IpnbMergeCellBelowAction(), new IpnbSplitCellAction());
+          createClickMenu(e.getLocationOnScreen(), group);
+        }
+      }
+    });
+  }
+
+  protected void createClickMenu(@NotNull Point point, @NotNull DefaultActionGroup group) {
+    final DataContext context = DataManager.getInstance().getDataContext(this);
+    final ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(null, group, context,
+                                                                                JBPopupFactory.ActionSelectionAid.MNEMONICS,
+                                                                                true);
+    popup.show(RelativePoint.fromScreen(point));
+  }
+
   @Nullable
   public String getText(int from, int to) {
     if (myEditing && myEditablePanel != null) {
index c462a6a2b24b9e660a692daf189a9c93242b00b8..4f890405e8aae691f4fe0fda0c3004e2a9df4ab7 100644 (file)
@@ -1,10 +1,16 @@
 package org.jetbrains.plugins.ipnb.editor.panels;
 
+import com.intellij.openapi.actionSystem.DefaultActionGroup;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.plugins.ipnb.IpnbUtils;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellAboveAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellBelowAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbSplitCellAction;
 import org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell;
 
 import javax.swing.*;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
 
 public class IpnbMarkdownPanel extends IpnbEditablePanel<JComponent, IpnbMarkdownCell> {
 
@@ -33,6 +39,28 @@ public class IpnbMarkdownPanel extends IpnbEditablePanel<JComponent, IpnbMarkdow
     initPanel();
   }
 
+  @Override
+  public void addRightClickMenu() {
+    myViewPanel.addMouseListener(new MouseAdapter() {
+      @Override
+      public void mousePressed(MouseEvent e) {
+        if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) {
+          final DefaultActionGroup group = new DefaultActionGroup(new IpnbMergeCellAboveAction(), new IpnbMergeCellBelowAction());
+          createClickMenu(e.getLocationOnScreen(), group);
+        }
+      }
+    });
+    myEditablePanel.addMouseListener(new MouseAdapter() {
+      @Override
+      public void mousePressed(MouseEvent e) {
+        if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) {
+          final DefaultActionGroup group = new DefaultActionGroup(new IpnbSplitCellAction());
+          createClickMenu(e.getLocationOnScreen(), group);
+        }
+      }
+    });
+  }
+
   @SuppressWarnings("CloneDoesntCallSuperClone")
   @Override
   protected Object clone() {
index 146299f5631ad1e48eaa6329f5af1da0d8829cd8..f71f0b4419c13b71ee8058d08698deba8840b704 100644 (file)
@@ -2,11 +2,14 @@ package org.jetbrains.plugins.ipnb.editor.panels.code;
 
 import com.google.common.collect.Lists;
 import com.intellij.icons.AllIcons;
+import com.intellij.openapi.actionSystem.DefaultActionGroup;
 import com.intellij.openapi.application.Application;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.application.ModalityState;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.editor.event.EditorMouseAdapter;
+import com.intellij.openapi.editor.event.EditorMouseEvent;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.VerticalFlowLayout;
 import com.intellij.openapi.util.TextRange;
@@ -18,6 +21,9 @@ import org.jetbrains.annotations.Nullable;
 import org.jetbrains.plugins.ipnb.configuration.IpnbConnectionManager;
 import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
 import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellAboveAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbMergeCellBelowAction;
+import org.jetbrains.plugins.ipnb.editor.actions.IpnbSplitCellAction;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
 import org.jetbrains.plugins.ipnb.editor.panels.IpnbPanel;
@@ -49,6 +55,7 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
 
     myViewPanel = createViewPanel();
     add(myViewPanel);
+    addRightClickMenu();
   }
 
   @NotNull
@@ -79,6 +86,20 @@ public class IpnbCodePanel extends IpnbEditablePanel<JComponent, IpnbCodeCell> {
     return panel;
   }
 
+  @Override
+  public void addRightClickMenu() {
+    myCodeSourcePanel.addMouseListener(new EditorMouseAdapter() {
+      @Override
+      public void mousePressed(EditorMouseEvent e) {
+        final MouseEvent mouseEvent = e.getMouseEvent();
+        if (SwingUtilities.isRightMouseButton(mouseEvent) && mouseEvent.getClickCount() == 1) {
+          createClickMenu(mouseEvent.getLocationOnScreen(),
+                          new DefaultActionGroup(new IpnbMergeCellAboveAction(), new IpnbMergeCellBelowAction(), new IpnbSplitCellAction()));
+        }
+      }
+    });
+  }
+
   @NotNull
   private JPanel createCodeComponent() {
     myCodeSourcePanel = new IpnbCodeSourcePanel(myProject, this, myCell);
index 32031ceebf3386830271de118cad8b1cab5490d9..b283678cd3f7e62c169a661c81a611b44f88e9b1 100644 (file)
@@ -19,6 +19,7 @@ import com.intellij.openapi.Disposable;
 import com.intellij.openapi.actionSystem.CustomShortcutSet;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.EditorFactory;
+import com.intellij.openapi.editor.event.EditorMouseListener;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.ui.Gray;
@@ -56,6 +57,10 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
     final JComponent panel = createViewPanel();
     add(panel);
   }
+  
+  public void addMouseListener(@NotNull final EditorMouseListener listener) {
+    myEditor.addEditorMouseListener(listener);
+  }
 
   @NotNull
   public IpnbCodePanel getIpnbCodePanel() {