Interface.java
9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Interface extends JFrame implements ActionListener, FocusListener {
private static final long serialVersionUID = 1L;
private static final Font titleFont = new Font(new JLabel().getFont().getName(), Font.BOLD, 14);
private static final Font defaultFont = new Font(new JLabel().getFont().getName(), Font.PLAIN, new JLabel().getFont().getSize());
private static final Border defaultJTextFieldBorder = new JTextField().getBorder();
private JTextArea logs;
private JButton[][] moveButtons = new JButton[3][3];
private JTextField stepsField;
private JButton waitButton;
private JButton nextButton;
private JButton revertButton;
private JButton exportButton;
private JButton importButton;
private boolean keyboardWait = false;
private ActionList actionList;
public Interface(ActionList actionList) {
super("PercTeacher");
this.actionList = actionList;
setResizable(false);
populateWindow();
pack();
setLocationRelativeTo(null); // center window
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new CustomDispatcher());
getContentPane().requestFocusInWindow();
}
private void populateWindow() {
JPanel mainPanel = new JPanel(new BorderLayout());
addLogPanel(mainPanel);
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
addMoveCommandsPanel(centerPanel);
JPanel optionCommandsPanel = new JPanel();
addOptionCommandsPanel(centerPanel, optionCommandsPanel);
addExportImportPanel(optionCommandsPanel);
mainPanel.add(centerPanel, BorderLayout.CENTER);
add(mainPanel);
updateLogs();
}
private void addLogPanel(JPanel parent) {
JPanel logPanel = new JPanel(new BorderLayout());
logPanel.setBorder(BorderFactory.createMatteBorder(2, 0, 0, 0, Color.LIGHT_GRAY));
JLabel loglabel = new JLabel("Actions");
loglabel.setFont(titleFont);
logPanel.add(loglabel, BorderLayout.NORTH);
logs = new JTextArea(6, 0);
logs.setEditable(false);
JScrollPane logScroll = new JScrollPane(logs, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
logPanel.add(logScroll, BorderLayout.CENTER);
parent.add(logPanel, BorderLayout.SOUTH);
}
private void addMoveCommandsPanel(JPanel parent) {
JPanel moveCommandsPanel = new JPanel(new GridLayout(3, 3));
moveCommandsPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.LIGHT_GRAY));
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i == 1 && j == 1) { // central component is an image of the robot
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/icons/robot.png")));
moveButtons[i][j] = null;
moveCommandsPanel.add(label);
} else {
JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/move" + String.valueOf(j) + String.valueOf(i) + ".png")));
button.addActionListener(this);
moveButtons[i][j] = button;
moveCommandsPanel.add(button);
}
}
}
parent.add(moveCommandsPanel, BorderLayout.CENTER);
}
private void addOptionCommandsPanel(JPanel parent, JPanel optionCommandsPanel) {
optionCommandsPanel.setLayout(new BoxLayout(optionCommandsPanel, BoxLayout.PAGE_AXIS));
// title
JLabel title = new JLabel("Options");
title.setFont(titleFont);
title.setAlignmentX(Component.LEFT_ALIGNMENT);
optionCommandsPanel.add(title);
// steps
JPanel stepPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
stepPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel stepLabel = new JLabel("Steps : ");
stepLabel.setFont(defaultFont);
stepPanel.add(stepLabel);
stepsField = new JTextField(10);
stepsField.setText("10");
stepsField.addFocusListener(this);
stepPanel.add(stepsField);
optionCommandsPanel.add(stepPanel);
//buttons
JPanel buttonsPanel = new JPanel(new GridLayout(0, 1));
buttonsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
// wait button
waitButton = new JButton("Add a pause");
waitButton.addActionListener(this);
buttonsPanel.add(waitButton);
// next action button
nextButton = new JButton("Next action");
nextButton.addActionListener(this);
buttonsPanel.add(nextButton);
// revert action button
revertButton = new JButton("Revert last action");
revertButton.addActionListener(this);
buttonsPanel.add(revertButton);
optionCommandsPanel.add(buttonsPanel);
// separator
JSeparator sep = new JSeparator();
sep.setForeground(Color.LIGHT_GRAY);
sep.setBackground(Color.LIGHT_GRAY);
optionCommandsPanel.add(sep);
parent.add(optionCommandsPanel, BorderLayout.EAST);
}
private void addExportImportPanel(JPanel parent) {
JPanel exportImportPanel = new JPanel(new GridLayout(0, 1));
exportImportPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
// export
exportButton = new JButton("Export actions");
exportButton.addActionListener(this);
exportImportPanel.add(exportButton);
// import
importButton = new JButton("Import actions");
importButton.addActionListener(this);
exportImportPanel.add(importButton);
parent.add(exportImportPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
keyboardWait = true;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(e.getSource() == moveButtons[i][j]) {
String str = stepsField.getText();
try {
if(str != null && !str.equals("")) {
long step = Long.parseLong(str);
actionList.addToActionList(i, j, step);
}
} catch (NumberFormatException ignored) {
stepsField.setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
}
}
if(e.getSource() == waitButton) {
String str = JOptionPane.showInputDialog(null, "Pause duration (in miliseconds) :");
try {
if(str != null && !str.equals("")) {
long delai = Long.parseLong(str) * 1000;
actionList.completeActionAndAddPause(delai);
}
} catch (NumberFormatException ignored) {}
} else if(e.getSource() == nextButton) {
actionList.completeAction();
} else if(e.getSource() == revertButton) {
actionList.revertLastAction();
} else if(e.getSource() == exportButton) {
ImportExport.exportWithDialog(actionList);
} else if(e.getSource() == importButton) {
ActionList newActionList = ImportExport.importsWithDialog();
if(newActionList != null)
actionList = newActionList;
}
updateLogs();
keyboardWait = false;
}
private void updateLogs() {
if(actionList != null)
logs.setText(actionList.toString());
}
@Override
public void focusGained(FocusEvent e) {
keyboardWait = true;
if(e.getSource() == stepsField) {
stepsField.setBorder(defaultJTextFieldBorder);
stepsField.setCaretPosition(stepsField.getDocument().getLength());
}
}
@Override
public void focusLost(FocusEvent e) {
keyboardWait = false;
}
private class CustomDispatcher implements KeyEventDispatcher {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(keyboardWait && Interface.this.getFocusOwner() == stepsField && e.getKeyCode() == KeyEvent.VK_ENTER) {
Interface.this.getContentPane().requestFocusInWindow();
return true;
}
if(e.getID() != KeyEvent.KEY_RELEASED || keyboardWait)
return false;
if(e.getKeyCode() == KeyEvent.VK_NUMPAD1)
actionPerformed(new ActionEvent(moveButtons[2][0], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD2)
actionPerformed(new ActionEvent(moveButtons[2][1], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD3)
actionPerformed(new ActionEvent(moveButtons[2][2], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD4)
actionPerformed(new ActionEvent(moveButtons[1][0], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD6)
actionPerformed(new ActionEvent(moveButtons[1][2], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD7)
actionPerformed(new ActionEvent(moveButtons[0][0], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD8)
actionPerformed(new ActionEvent(moveButtons[0][1], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD9)
actionPerformed(new ActionEvent(moveButtons[0][2], 0, null));
else if(e.getKeyCode() == KeyEvent.VK_SPACE)
actionPerformed(new ActionEvent(nextButton, 0, null));
else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
actionPerformed(new ActionEvent(revertButton, 0, null));
else if(e.getKeyCode() == KeyEvent.VK_NUMPAD0)
actionPerformed(new ActionEvent(waitButton, 0, null));
return true;
}
}
}