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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
* RoombaRecorder --
*
* Copyright (c) 2006 Tod E. Kurt, tod@todbot.com, ThingM
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
*/
package com.hackingroomba.roombacomm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.filechooser.*;
import java.util.*;
import java.io.*;
/**
* A simple wrapper for multiple MacroRecorderPanels
*
*/
public class RoombaRecorder extends JFrame implements WindowListener,ActionListener {
static final String helpMsg = "<html>"+
//"<h2> </h2>"+
"<h2> Roomba Movement Keyboard Shortcuts </h2>"+
"<ul>"+
"<li> arrow keys -- move Roomba"+
"<li> space bar -- stop Roomba"+
"<li> L -- blink Roomba LEDs"+
"<li> V -- toggle Roomba vacuum"+
"<li> T -- test Roomba"+
"<li> R -- reset Roomba"+
"<li> 1,2,3,4 -- adjust speed"+
"</ul>"+
"<h2> Application Control Keyboard Shortcuts </h2>"+
"<ul>"+
"<li> cmd-T -- open new tab"+
"<li> cmd-W -- close current tab"+
"<li> cmd-arrow-left/right -- cycle through tabs"+
"<li> cmd-X/C/V -- cut/copy/paste events between tabs"+
"</ul>"+
"</html>";
JPanel contentPane;
JPanel recordPanel;
JTabbedPane tabbedPane;
JButton stopButton,recordButton,playButton;
JCheckBox loopButton;
JCheckBox playAllButton;
JFileChooser fc;
File file;
boolean hwhandshake = false;
int maxRoombas = 16;
int lastTab = 0;
HashMap clipboard;
RoombaRecorderPanel rrpanel; // just to make some of code lines shorter
boolean movingForward = false; // is roomba moving forward or not
boolean vacuuming = false; // is roomba vacuuming or not
int turnval = 0;
public static void main(String[] args) {
new RoombaRecorder(args);
}
public RoombaRecorder(String[] args) {
super("RoombaRecorder");
addWindowListener(this);
for(int i=0; i < args.length; i++) {
if(args[i].endsWith("hwhandshake"))
hwhandshake = true;
}
clipboard = new HashMap();
fc = new JFileChooser();
setResizable(false);
makeMenu();
makeRecordPanel();
// Create and set up the content pane.
tabbedPane = new JTabbedPane();
tabbedPane.setOpaque(true); //content panes must be opaque
openNewTab();
contentPane = new JPanel(new BorderLayout());
contentPane.add(recordPanel, BorderLayout.NORTH);
contentPane.add(tabbedPane, BorderLayout.CENTER);
//Container content = getContentPane();
setContentPane(contentPane);
addBindings();
bindKeys();
// Display the window.
pack(); //setSize(400, 400);
setVisible(true);
//setFocusableWindowState(false); // prevent focus
}
void makeMenu() {
JMenuBar menubar = new JMenuBar();
JMenuItem item;
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu help = new JMenu("Help");
file.setMnemonic(KeyEvent.VK_F);
edit.setMnemonic(KeyEvent.VK_E);
help.setMnemonic(KeyEvent.VK_H);
item = new JMenuItem("New Tab");
item.addActionListener(this);
file.add(item);
item = new JMenuItem("Close Tab");
item.addActionListener(this);
file.add(item);
item = new JMenuItem("Open Set");
item.addActionListener(this);
file.add(item);
item = new JMenuItem("Save Set");
item.addActionListener(this);
file.add(item);
item = new JMenuItem("Quit");
item.addActionListener(this);
file.add(item);
item = new JMenuItem("Cut");
item.addActionListener(this);
edit.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
edit.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
edit.add(item);
item = new JMenuItem("Help");
item.addActionListener(this);
help.add(item);
menubar.add(file);
menubar.add(edit);
menubar.add(help);
setJMenuBar(menubar);
}
void makeRecordPanel() {
recordPanel = new JPanel();
recordPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Action Record"), BorderFactory.createEmptyBorder())); //1,1,1,1)));
stopButton = new JButton(createImageIcon("images/but_transport_stop.png","stop"));
stopButton.setActionCommand("stop");
stopButton.addActionListener(this);
recordPanel.add(stopButton);
playButton = new JButton(createImageIcon("images/but_transport_play.png","play"));
playButton.setSelectedIcon(createImageIcon("images/but_transport_play_push.png","play"));
playButton.setActionCommand("play");
playButton.addActionListener(this);
recordPanel.add(playButton);
recordButton = new JButton(createImageIcon("images/but_transport_record.png","record"));
recordButton.setSelectedIcon(createImageIcon("images/but_transport_record_push.png","record"));
recordButton.setActionCommand("record");
recordButton.addActionListener(this);
recordPanel.add(recordButton);
loopButton = new JCheckBox("loop");
loopButton.setText("loop");
loopButton.setActionCommand("loop");
loopButton.addActionListener(this);
recordPanel.add(loopButton);
playAllButton = new JCheckBox("play all");
playAllButton.addActionListener(this);
recordPanel.add(playAllButton);
}
void addBindings() {
InputMap inputMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
KeyStroke key;
//key = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0);
//contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).remove(key);
key = KeyStroke.getKeyStroke('T',KeyEvent.CTRL_MASK);
inputMap.put(key, "new-tab");
key = KeyStroke.getKeyStroke('T',KeyEvent.META_MASK);
inputMap.put(key, "new-tab");
key = KeyStroke.getKeyStroke('N',KeyEvent.CTRL_MASK);
inputMap.put(key, "new-tab");
key = KeyStroke.getKeyStroke('N',KeyEvent.META_MASK);
inputMap.put(key, "new-tab");
key = KeyStroke.getKeyStroke('W',KeyEvent.CTRL_MASK);
inputMap.put(key, "close-tab");
key = KeyStroke.getKeyStroke('W',KeyEvent.META_MASK);
inputMap.put(key, "close-tab");
key = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,KeyEvent.CTRL_MASK);
inputMap.put(key, "right-tab");
key = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,KeyEvent.CTRL_MASK);
inputMap.put(key, "left-tab");
key = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,KeyEvent.META_MASK);
inputMap.put(key, "right-tab");
key = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,KeyEvent.META_MASK);
inputMap.put(key, "left-tab");
key = KeyStroke.getKeyStroke('X',KeyEvent.CTRL_MASK);
inputMap.put(key, "cut");
key = KeyStroke.getKeyStroke('X',KeyEvent.META_MASK);
inputMap.put(key, "cut");
key = KeyStroke.getKeyStroke('C',KeyEvent.CTRL_MASK);
inputMap.put(key, "copy");
key = KeyStroke.getKeyStroke('C',KeyEvent.META_MASK);
inputMap.put(key, "copy");
key = KeyStroke.getKeyStroke('V',KeyEvent.CTRL_MASK);
inputMap.put(key, "paste");
key = KeyStroke.getKeyStroke('V',KeyEvent.META_MASK);
inputMap.put(key, "paste");
ActionMap actionMap = contentPane.getActionMap();
actionMap.put("new-tab", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
openNewTab();
} });
actionMap.put("close-tab",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
closeCurrentTab();
} });
actionMap.put("left-tab",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
cycleTabLeft();
} });
actionMap.put("right-tab",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
cycleTabRight();
} });
actionMap.put("cut",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
clipboard = getCurrentTab().cut();
} });
actionMap.put("copy",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
clipboard = getCurrentTab().copy();
} });
actionMap.put("paste",new AbstractAction() {
public void actionPerformed(ActionEvent e) {
getCurrentTab().paste(clipboard);
} });
}
/**
* Bind keys to actions using a custom KeyEventPostProcessor
* (fixme: why can't this go in the panel?)
*/
void bindKeys() {
// ahh, the succinctness of java
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventDispatcher( new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
String action=null;
if(e.getID() != KeyEvent.KEY_PRESSED)
return false;
if(e.getModifiers() != 0)
return false;
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
action = "forward";
movingForward=true;
turnval = 0;
break;
case KeyEvent.VK_DOWN:
action="backward";
movingForward=false;
turnval = 0;
break;
case KeyEvent.VK_LEFT:
if(movingForward) {
turnval = (turnval==0) ? 100 : turnval-turnval/2;
if(turnval<10) { action="spinleft"; turnval=0; }
else action = "turn"+turnval;
}
else action = "spinleft";
//action = (movingForward) ? "turn100" : "spinleft";
break;
case KeyEvent.VK_RIGHT:
if(movingForward) {
turnval = (turnval==0) ? -100 : turnval-turnval/2;
if(turnval>-10) { action="spinright"; turnval=0; }
else action = "turn"+turnval;
}
else action = "spinright";
//action = (movingForward) ? "turn-100" : "spinright";
break;
case KeyEvent.VK_SPACE:
action="stop";
movingForward=false;
turnval = 0;
break;
case KeyEvent.VK_L:
action="blink-leds";
break;
case KeyEvent.VK_R:
action="reset";
break;
case KeyEvent.VK_T:
action="test";
break;
case KeyEvent.VK_V:
vacuuming = !vacuuming;
action = (vacuuming) ? "vacuum-on":"vacuum-off";
break;
case KeyEvent.VK_1:
action="50";
break;
case KeyEvent.VK_2:
action="100";
break;
case KeyEvent.VK_3:
action="150";
break;
case KeyEvent.VK_4:
action="250";
break;
case KeyEvent.VK_5:
action="400";
break;
case KeyEvent.VK_6:
action="500";
break;
}
if(action!=null)
getCurrentTab().actionPerformed(new ActionEvent(this,0,action));
//System.out.println("process '"+e.getKeyCode()+"' "+e);
return true;
}
});
}
/** Implement Actionlistener */
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
//System.out.println("action:"+action+", event: "+event);
if("stop".equals(action)) {
playButton.setSelected(false);
recordButton.setSelected(false);
for(int i=0; i<tabbedPane.getTabCount(); i++) {
rrpanel = (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
rrpanel.stop();
}
}
else if("play".equals(action)) {
playButton.setSelected(true);
recordButton.setSelected(false);
if( !playAllButton.isSelected() ) {
boolean playing = getCurrentTab().play(); // FIXME: hack
if(!playing) playButton.setSelected(false);
} else {
for(int i=0; i<tabbedPane.getTabCount(); i++) {
rrpanel= (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
rrpanel.play();
}
}
}
else if("record".equals(action)) {
playButton.setSelected(false);
recordButton.setSelected(true);
if( !playAllButton.isSelected() ) {
getCurrentTab().record();
} else {
int ir = tabbedPane.getSelectedIndex();
for(int i=0; i<tabbedPane.getTabCount(); i++) {
rrpanel= (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
if( i!=ir ) rrpanel.play();
else rrpanel.record();
}
}
}
else if("loop".equals(action)) {
for(int i=0; i<tabbedPane.getTabCount(); i++) {
rrpanel= (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
rrpanel.setLooping(loopButton.isSelected());
}
}
else if("New Tab".equals(action)) {
openNewTab();
}
else if("Close Tab".equals(action)) {
closeCurrentTab();
}
else if("Open Set".equals(action)) {
loadSet();
}
else if("Save Set".equals(action)) {
saveSet();
}
else if("Save Set As...".equals(action)) {
file = null;
saveSet();
}
else if("Quit".equals(action)) {
closeAllTabs();
windowClosing(null);
}
else if("Cut".equals(action)) {
clipboard = getCurrentTab().cut();
}
else if("Copy".equals(action)) {
clipboard = getCurrentTab().copy();
}
else if("Paste".equals(action)) {
getCurrentTab().paste(clipboard);
}
else if("Help".equals(action)) {
JOptionPane.showMessageDialog(null,new JLabel(helpMsg));
}
}
public RoombaRecorderPanel getCurrentTab() {
return (RoombaRecorderPanel) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
}
public void openNewTab() {
int i = tabbedPane.getTabCount();
if(i < maxRoombas) { // only 16 roombas supported
rrpanel = new RoombaRecorderPanel();
rrpanel.setShowHardwareHandhake(hwhandshake);
lastTab = lastTab+1;
tabbedPane.addTab("Roomba #"+lastTab, null, rrpanel);
tabbedPane.setSelectedIndex(lastTab-1);
}
}
public void closeAllTabs() {
int c = tabbedPane.getTabCount();
for( int i=0; i<c; i++)
closeCurrentTab();
lastTab = 0;
}
public void closeCurrentTab() {
int i = tabbedPane.getSelectedIndex();
rrpanel = (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
rrpanel.disconnect();
tabbedPane.remove(i);
}
public void cycleTabLeft() {
int i = tabbedPane.getSelectedIndex() - 1;
if(i<0) i = tabbedPane.getTabCount() - 1;
tabbedPane.setSelectedIndex(i);
loopButton.setSelected( getCurrentTab().getLooping() );
}
public void cycleTabRight() {
int i = tabbedPane.getSelectedIndex() + 1;
if(i==tabbedPane.getTabCount()) i = 0;
tabbedPane.setSelectedIndex(i);
}
/**
* Load a save set from a chosen file
*/
void loadSet() {
int returnVal = fc.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
System.out.println("Open command cancelled by user.");
return;
}
file = fc.getSelectedFile();
System.out.println("Opening: " + file.getName());
closeAllTabs();
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
//loopButton.setSelected( ois.readObject() );
ArrayList l = (ArrayList) ois.readObject();
System.out.println("read "+l.size()+" thingies");
ois.close();
for(int i=0; i<l.size(); i++) {
HashMap m = (HashMap) l.get(i);
openNewTab();
getCurrentTab().paste(m);
}
} catch(Exception e) {
System.out.println("Open error "+e);
}
}
/**
* Save the current set to a file.
* Will ask for a filename if one hasn't been chosen.
*/
void saveSet() {
if( file==null ) {
int returnVal = fc.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
System.out.println("Open command cancelled by user.");
return;
}
file = fc.getSelectedFile();
}
System.out.println("Saving to: " + file.getName());
try {
int count = tabbedPane.getTabCount();
ArrayList l = new ArrayList();
for(int i=0; i< count; i++) {
rrpanel= (RoombaRecorderPanel)tabbedPane.getComponentAt(i);
l.add(rrpanel.copy());
}
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//oos.writeObject(loopButton);
//oos.writeObject(
oos.writeObject(l);
oos.close();
} catch( Exception e ) {
System.out.println("Save error "+e);
}
}
/** implement windowlistener */
public void windowClosing(WindowEvent event) {
//rcPanel.disconnect();
System.exit(0);
}
/** implement windowlistener */
public void windowClosed(WindowEvent event) {
// do nothing
}
/** implement windowlistener */
public void windowOpened(WindowEvent event) {
// do nothing
}
/** implement windowlistener */
public void windowActivated(WindowEvent event) {
// do nothing
}
/** implement windowlistener */
public void windowDeactivated(WindowEvent event) {
// do nothing
}
/** implement windowlistener */
public void windowIconified(WindowEvent event) {
// do nothing
}
/** implement windowlistener */
public void windowDeiconified(WindowEvent event) {
// do nothing
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path,
String description) {
// yes, this is supposed to say "RoombaCommTest"
java.net.URL imgURL = RoombaCommPanel.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
|