Skip to content

Commit efaba6d

Browse files
committed
add menus
1 parent eb76861 commit efaba6d

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/main/java/org/beryx/swing/handler/Demo.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.beryx.swing.handler;
1717

1818
import org.beryx.textio.TextIO;
19-
import org.beryx.textio.swing.SwingTextTerminal;
2019

2120
public class Demo {
2221
private static class Product {
@@ -36,7 +35,7 @@ public String toString() {
3635

3736

3837
public static void main(String[] args) {
39-
SwingTextTerminal terminal = new SwingTextTerminal();
38+
MarsTerminal terminal = new MarsTerminal();
4039
terminal.init();
4140
TextIO textIO = new TextIO(terminal);
4241

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.beryx.swing.handler;
2+
3+
import org.beryx.textio.swing.SwingTextTerminal;
4+
5+
import javax.swing.*;
6+
import java.awt.event.*;
7+
import java.util.logging.Logger;
8+
9+
public class MarsTerminal extends SwingTextTerminal {
10+
private static Logger logger = Logger.getLogger(MarsTerminal.class.getName());
11+
12+
private final JPopupMenu popup = new JPopupMenu();
13+
14+
private static class PopupListener extends MouseAdapter {
15+
private final JPopupMenu popup;
16+
17+
public PopupListener(JPopupMenu popup) {
18+
this.popup = popup;
19+
}
20+
21+
public void mousePressed(MouseEvent e) {
22+
maybeShowPopup(e);
23+
}
24+
25+
public void mouseReleased(MouseEvent e) {
26+
maybeShowPopup(e);
27+
}
28+
29+
private void maybeShowPopup(MouseEvent e) {
30+
if (e.isPopupTrigger()) {
31+
popup.show(e.getComponent(), e.getX(), e.getY());
32+
}
33+
}
34+
}
35+
36+
public MarsTerminal() {
37+
configureMainMenu();
38+
39+
JTextPane textPane = getTextPane();
40+
addAction("ctrl C", "Copy", () -> textPane.copy());
41+
addAction("ctrl V", "Paste", () -> textPane.paste());
42+
MouseListener popupListener = new PopupListener(popup);
43+
textPane.addMouseListener(popupListener);
44+
}
45+
46+
private void configureMainMenu() {
47+
JFrame frame = getFrame();
48+
frame.setTitle("Mars Simulation");
49+
JMenuBar menuBar = new JMenuBar();
50+
JMenu menu = new JMenu("Help");
51+
menu.setMnemonic(KeyEvent.VK_H);
52+
53+
JMenuItem menuItem = new JMenuItem("About", KeyEvent.VK_A);
54+
menuItem.addActionListener(e -> JOptionPane.showMessageDialog(frame, "The Mars Simulation Project"));
55+
menu.add(menuItem);
56+
57+
menuBar.add(menu);
58+
frame.setJMenuBar(menuBar);
59+
}
60+
61+
private boolean addAction(String keyStroke, String menuText, Runnable action) {
62+
KeyStroke ks = KeyStroke.getKeyStroke(keyStroke);
63+
if(ks == null) {
64+
logger.warning("Invalid keyStroke: " + keyStroke);
65+
return false;
66+
}
67+
JMenuItem menuItem = new JMenuItem(menuText);
68+
menuItem.addActionListener(e -> action.run());
69+
popup.add(menuItem);
70+
71+
JTextPane textPane = getTextPane();
72+
String actionKey = "MarsTerminal." + keyStroke.replaceAll("\\s", "-");
73+
textPane.getInputMap().put(ks, actionKey);
74+
textPane.getActionMap().put(actionKey, new AbstractAction() {
75+
@Override
76+
public void actionPerformed(ActionEvent e) {
77+
action.run();
78+
}
79+
});
80+
return true;
81+
}
82+
}

0 commit comments

Comments
 (0)