Commits

beholdmyglory committed dafaaeb

Added keyboard shortcut to select cell type

Comments (0)

Files changed (1)

src/se/kth/csc/inda11/remains/gui/MainWindow.java

 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.awt.event.KeyEvent;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
+import javax.swing.AbstractAction;
 import javax.swing.BorderFactory;
 import javax.swing.Box;
 import javax.swing.BoxLayout;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JColorChooser;
+import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JSlider;
 import javax.swing.JToggleButton;
 import javax.swing.JToolBar;
+import javax.swing.KeyStroke;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
             toolbar.add(this.cellChooserButtons[i]);
         }
         this.cellChooserButtons[0].setSelected(true);
-        this.cellChooserButtons[0].setToolTipText("Empty cell");
-        this.cellChooserButtons[1].setToolTipText("Conductor");
-        this.cellChooserButtons[2].setToolTipText("Electron head");
-        this.cellChooserButtons[3].setToolTipText("Electron tail");
+        this.cellChooserButtons[0].setToolTipText("Empty cell (1)");
+        this.cellChooserButtons[1].setToolTipText("Conductor (2)");
+        this.cellChooserButtons[2].setToolTipText("Electron head (3)");
+        this.cellChooserButtons[3].setToolTipText("Electron tail (4)");
         
         toolbar.add(new JToolBar.Separator());
         
         });
         speedChanger.setToolTipText("Adjust simulation speed");
         toolbar.add(speedChanger);
+
+        this.addCellChooserShortcut(KeyEvent.VK_1, Simulator.Cell.EMPTY);
+        this.addCellChooserShortcut(KeyEvent.VK_2, Simulator.Cell.CONDUCTOR);
+        this.addCellChooserShortcut(KeyEvent.VK_3, Simulator.Cell.HEAD);
+        this.addCellChooserShortcut(KeyEvent.VK_4, Simulator.Cell.TAIL);
         
         this.add(toolbar, BorderLayout.NORTH);
         
         }
         return new ImageIcon(square);
     }
+    
+    private void addCellChooserShortcut(int keycode, final Simulator.Cell cellType) {
+        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
+                KeyStroke.getKeyStroke(keycode, 0), keycode);
+        this.getRootPane().getActionMap().put(keycode, new AbstractAction() {
+            private static final long serialVersionUID = -4689396260113479961L;
+
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                MainWindow.this.cellChooserButtons[cellType.ordinal()].doClick();
+            }
+        });
+    }
 
 }