Skip to content

Commit 7c37f95

Browse files
Merge pull request #99 from UCSDOalads/addShortcutsByXYG
add shortcuts
2 parents e145417 + 2853d95 commit 7c37f95

3 files changed

Lines changed: 107 additions & 6 deletions

File tree

src/painttools/toolbar/ToolBar.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
import javax.swing.JButton;
99
import javax.swing.JPanel;
1010

11-
import painttools.tools.*;
12-
13-
11+
import painttools.tools.AddClassTool;
12+
import painttools.tools.AddInputBoxTool;
13+
import painttools.tools.AddOutputBoxTool;
14+
import painttools.tools.DotTool;
15+
import painttools.tools.LineTool;
16+
import painttools.tools.PaintTool;
17+
import painttools.tools.SelectTool;
1418
import ui.PaintPanel;
19+
import ui.ShortcutHandler;
1520

1621
public class ToolBar extends JPanel {
1722

@@ -39,7 +44,8 @@ public ToolBar(PaintPanel panel) {
3944
addTool(new AddOutputBoxTool(panel));
4045

4146
addTool(new LineTool());
42-
47+
48+
this.addKeyListener(new ShortcutHandler(panel));
4349
}
4450

4551
/**

src/ui/PaintPanel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum State {
3636

3737
private SelectTool selectTool;
3838
private KeyHandler keyHandler;
39-
39+
private ShortcutHandler shortcutHandler;
40+
4041
/**
4142
* @return the tempComponent
4243
*/
@@ -90,6 +91,7 @@ public PaintPanel() {
9091

9192
this.components = new ArrayList<>();
9293
this.keyHandler = new KeyHandler(this);
94+
this.shortcutHandler = new ShortcutHandler(this);
9395
this.addMouseListener(new MouseListener() {
9496

9597
@Override
@@ -156,7 +158,7 @@ public void keyReleased(KeyEvent e) {
156158
@Override
157159
public void keyPressed(KeyEvent e) {
158160
// TODO Auto-generated method stub
159-
161+
shortcutHandler.keyPressed(e);
160162
}
161163
});
162164
}

src/ui/ShortcutHandler.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)