Skip to content

Commit

Permalink
Add right click copy and paste menu
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 19, 2023
1 parent 9d79d88 commit 40bd4b8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions convex-gui/src/main/java/convex/gui/components/RightCopyMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package convex.gui.components;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.text.JTextComponent;

@SuppressWarnings("serial")
public class RightCopyMenu extends JPopupMenu implements ActionListener {

JMenuItem copyMenuItem = new JMenuItem("Copy");
JMenuItem cutMenuItem = new JMenuItem("Cut");
JMenuItem pasteMenuItem = new JMenuItem("Paste");

public RightCopyMenu(Component invoker) {
copyMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);

add(copyMenuItem);
add(cutMenuItem);
add(pasteMenuItem);

setInvoker(invoker);
}

public static void addTo(JTextComponent tf) {
tf.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
switch(e.getButton()) {
case MouseEvent.BUTTON3: {
Component comp=e.getComponent();
RightCopyMenu menu=new RightCopyMenu(comp);
menu.show(comp, e.getX(), e.getY());
break;
}
}
}
});
}

@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
Component invoker=getInvoker();
if (invoker instanceof JTextComponent) {
JTextComponent tf = (JTextComponent)invoker;
if (source == cutMenuItem) {
tf.cut();
} else if (source == copyMenuItem) {
tf.copy();
} else if (source == pasteMenuItem) {
tf.paste();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import convex.core.transactions.Invoke;
import convex.gui.components.AccountChooserPanel;
import convex.gui.components.ActionPanel;
import convex.gui.components.RightCopyMenu;

@SuppressWarnings("serial")
public class REPLPanel extends JPanel {
Expand Down Expand Up @@ -112,6 +113,7 @@ public REPLPanel(Convex convex) {
outputArea.setEditable(false);
outputArea.setLineWrap(true);
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 16));
RightCopyMenu.addTo(outputArea);
//outputArea.setForeground(Color.GREEN);
//DefaultCaret caret = (DefaultCaret)(outputArea.getCaret());
//caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Expand All @@ -122,6 +124,7 @@ public REPLPanel(Convex convex) {
inputArea.setFont(new Font("Monospaced", Font.PLAIN, 20));
inputArea.getDocument().addDocumentListener(inputListener);
inputArea.addKeyListener(inputListener);
RightCopyMenu.addTo(inputArea);
//inputArea.setForeground(Color.GREEN);

splitPane.setRightComponent(new JScrollPane(inputArea));
Expand Down

0 comments on commit 40bd4b8

Please sign in to comment.