Skip to content

Commit cc7264e

Browse files
committed
PlotView: Move keybindings to parent frame
1 parent a4c6670 commit cc7264e

2 files changed

Lines changed: 34 additions & 33 deletions

File tree

src/main/java/com/babai/ssplot/ui/MainFrame.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.awt.Color;
2727
import java.awt.Desktop;
2828
import java.awt.Dimension;
29+
import java.awt.event.ActionEvent;
2930
import java.awt.event.ComponentAdapter;
3031
import java.awt.event.ComponentEvent;
3132
import java.awt.image.BufferedImage;
@@ -38,12 +39,14 @@
3839
import java.util.Properties;
3940

4041
import javax.imageio.ImageIO;
42+
import javax.swing.AbstractAction;
4143
import javax.swing.ButtonGroup;
4244
import javax.swing.ImageIcon;
4345
import javax.swing.JButton;
4446
import javax.swing.JCheckBoxMenuItem;
4547
import javax.swing.JColorChooser;
4648
import javax.swing.JComboBox;
49+
import javax.swing.JComponent;
4750
import javax.swing.JDesktopPane;
4851
import javax.swing.JFileChooser;
4952
import javax.swing.JFrame;
@@ -343,6 +346,25 @@ public void componentResized(ComponentEvent ce) {
343346
}
344347
});
345348

349+
// Setting Keybinding for movement
350+
// Keybindings for movement and actions
351+
bindAction(ifrmPlot, "left", "LEFT", pv::moveLeft);
352+
bindAction(ifrmPlot, "right", "RIGHT", pv::moveRight);
353+
bindAction(ifrmPlot, "up", "UP", pv::moveUp);
354+
bindAction(ifrmPlot, "down", "DOWN", pv::moveDown);
355+
356+
bindAction(ifrmPlot, "plus", "J", pv::zoomIn);
357+
bindAction(ifrmPlot, "minus", "F", pv::zoomOut);
358+
bindAction(ifrmPlot, "splus", "H", pv::smallZoomIn);
359+
bindAction(ifrmPlot, "sminus", "G", pv::smallZoomOut);
360+
361+
bindAction(ifrmPlot, "rotAp", "Q", pv::rotateXPlus);
362+
bindAction(ifrmPlot, "rotAm", "A", pv::rotateXMinus);
363+
bindAction(ifrmPlot, "rotBp", "W", pv::rotateYPlus);
364+
bindAction(ifrmPlot, "rotBm", "S", pv::rotateYMinus);
365+
bindAction(ifrmPlot, "rotCp", "E", pv::rotateZPlus);
366+
bindAction(ifrmPlot, "rotCm", "D", pv::rotateZMinus);
367+
346368
JToolBar toolbar = new JToolBar();
347369

348370
// --- Zoom Section ---
@@ -488,6 +510,17 @@ public void componentResized(ComponentEvent ce) {
488510
setExtendedState(JFrame.MAXIMIZED_BOTH);
489511
}
490512

513+
/* Actions */
514+
private void bindAction(JComponent control, String actionName, String hotkey, Runnable action) {
515+
control.getInputMap().put(KeyStroke.getKeyStroke(hotkey), actionName);
516+
control.getActionMap().put(actionName, new AbstractAction() {
517+
@Override
518+
public void actionPerformed(ActionEvent arg0) {
519+
action.run();
520+
}
521+
});
522+
}
523+
491524
/* Menu Actions */
492525
public void saveImage() {
493526
var files = new JFileChooser();

src/main/java/com/babai/ssplot/ui/PlotView.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.awt.Color;
2929
import java.awt.Graphics;
30-
import java.awt.event.ActionEvent;
3130
import java.awt.event.MouseEvent;
3231
import java.awt.event.MouseListener;
3332
import java.awt.event.MouseMotionListener;
@@ -36,9 +35,7 @@
3635
import java.util.Optional;
3736
import java.util.Vector;
3837

39-
import javax.swing.AbstractAction;
4038
import javax.swing.JLabel;
41-
import javax.swing.KeyStroke;
4239
import javax.swing.Timer;
4340

4441
import com.babai.ssplot.math.plot.PlotData;
@@ -78,25 +75,7 @@ public PlotView(StatLogger logger, Plotter plt) {
7875
// Mouse Listener
7976
addMouseListener(this);
8077
addMouseMotionListener(this);
81-
82-
// Setting Keybinding for movement
83-
// Keybindings for movement and actions
84-
bindAction("left", "LEFT", this::moveLeft);
85-
bindAction("right", "RIGHT", this::moveRight);
86-
bindAction("up", "UP", this::moveUp);
87-
bindAction("down", "DOWN", this::moveDown);
88-
89-
bindAction("plus", "J", this::zoomIn);
90-
bindAction("minus", "F", this::zoomOut);
91-
bindAction("splus", "H", this::smallZoomIn);
92-
bindAction("sminus", "G", this::smallZoomOut);
93-
94-
bindAction("rotAp", "Q", this::rotateXPlus);
95-
bindAction("rotAm", "A", this::rotateXMinus);
96-
bindAction("rotBp", "W", this::rotateYPlus);
97-
bindAction("rotBm", "S", this::rotateYMinus);
98-
bindAction("rotCp", "E", this::rotateZPlus);
99-
bindAction("rotCm", "D", this::rotateZMinus);
78+
setFocusable(true);
10079
}
10180

10281
@Override
@@ -214,17 +193,6 @@ public void resize(int w, int h) {
214193
plt.initPlot(w, h);
215194
repaint();
216195
}
217-
218-
/* Actions */
219-
private void bindAction(String actionName, String hotkey, Runnable action) {
220-
getInputMap().put(KeyStroke.getKeyStroke(hotkey), actionName);
221-
getActionMap().put(actionName, new AbstractAction() {
222-
@Override
223-
public void actionPerformed(ActionEvent arg0) {
224-
action.run();
225-
}
226-
});
227-
}
228196

229197
public void zoomIn() {
230198
plt.zoomIn(0,0);

0 commit comments

Comments
 (0)