|
| 1 | +package ui; |
| 2 | + |
| 3 | +import java.awt.event.KeyAdapter; |
| 4 | +import java.awt.event.KeyEvent; |
| 5 | + |
| 6 | +import actions.AddDataDisplayBoxAction; |
| 7 | +import actions.AddDataInputBoxAction; |
| 8 | +import actions.AddLazyJavaClassAction; |
| 9 | +import actions.FileSaveAs; |
| 10 | +import actions.RemovePaintComponent; |
| 11 | + |
| 12 | +/** |
| 13 | + * Shortcuts for some actions |
| 14 | + * |
| 15 | + * Ctrl + s: save |
| 16 | + * Ctrl + r: remove |
| 17 | + * Ctrl + i: add input box |
| 18 | + * Ctrl + o: add output display box |
| 19 | + * Ctrl + c: add a class |
| 20 | + * @author Xiangyi Gong |
| 21 | + */ |
| 22 | +public class ShortcutHandler extends KeyAdapter { |
| 23 | + |
| 24 | + private PaintPanel panel; |
| 25 | + |
| 26 | + public ShortcutHandler(PaintPanel panel) { |
| 27 | + this.panel = panel; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void keyPressed(KeyEvent e) { |
| 32 | + // CTRL is not being pressed, return |
| 33 | + if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + switch (e.getKeyCode()) { |
| 38 | + case KeyEvent.VK_S: |
| 39 | + save(); |
| 40 | + break; |
| 41 | + case KeyEvent.VK_C: |
| 42 | + addClass(); |
| 43 | + break; |
| 44 | + case KeyEvent.VK_R: |
| 45 | + remove(); |
| 46 | + break; |
| 47 | + case KeyEvent.VK_I: |
| 48 | + addInput(); |
| 49 | + break; |
| 50 | + case KeyEvent.VK_O: |
| 51 | + addOutput(); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + private void save() { |
| 59 | + FileSaveAs action = new FileSaveAs(panel); |
| 60 | + if (action.canPerformAction()) { |
| 61 | + action.performAction(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void remove() { |
| 66 | + RemovePaintComponent action = new RemovePaintComponent(panel); |
| 67 | + if (action.canPerformAction()) { |
| 68 | + action.performAction(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void addInput() { |
| 73 | + AddDataInputBoxAction action = new AddDataInputBoxAction(panel); |
| 74 | + if (action.canPerformAction()) { |
| 75 | + action.performAction(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void addOutput() { |
| 80 | + AddDataDisplayBoxAction action = new AddDataDisplayBoxAction(panel); |
| 81 | + if (action.canPerformAction()) { |
| 82 | + action.performAction(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void addClass() { |
| 87 | + AddLazyJavaClassAction action = new AddLazyJavaClassAction(panel); |
| 88 | + if (action.canPerformAction()) { |
| 89 | + action.performAction(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments