Subversion Repositories svn.mios

Rev

Rev 846 | Rev 851 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)SysexSendReceiveGUI.java beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.midi.gui;
22
 
23
import java.awt.BorderLayout;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.ItemEvent;
33
import java.awt.event.ItemListener;
34
import java.io.File;
850 adamjking 35
import java.util.Iterator;
36
import java.util.LinkedList;
628 adamjking 37
import java.util.Observable;
38
import java.util.Observer;
845 tk 39
import java.util.StringTokenizer;
850 adamjking 40
import java.util.Vector;
628 adamjking 41
 
42
import javax.swing.BorderFactory;
43
import javax.swing.JButton;
850 adamjking 44
import javax.swing.JComponent;
628 adamjking 45
import javax.swing.JFileChooser;
46
import javax.swing.JLabel;
850 adamjking 47
import javax.swing.JMenuItem;
48
import javax.swing.JOptionPane;
628 adamjking 49
import javax.swing.JPanel;
850 adamjking 50
import javax.swing.JPopupMenu;
628 adamjking 51
import javax.swing.JScrollPane;
52
import javax.swing.JSpinner;
53
import javax.swing.JTextField;
54
import javax.swing.JTextPane;
55
import javax.swing.SpinnerNumberModel;
56
import javax.swing.event.ChangeEvent;
57
import javax.swing.event.ChangeListener;
850 adamjking 58
import javax.swing.event.PopupMenuEvent;
59
import javax.swing.event.PopupMenuListener;
60
import javax.swing.text.Segment;
628 adamjking 61
import javax.swing.text.Style;
62
import javax.swing.text.StyleConstants;
63
import javax.swing.text.StyleContext;
64
import javax.swing.text.StyledDocument;
65
 
66
import org.midibox.midi.MidiUtils;
67
import org.midibox.midi.SysexSendReceive;
68
import org.midibox.utils.gui.ImageLoader;
69
import org.midibox.utils.gui.SimpleFileChooserFilter;
850 adamjking 70
import org.midibox.utils.gui.SplitButton;
628 adamjking 71
 
72
public class SysexSendReceiveGUI extends JPanel implements ActionListener,
73
        ItemListener, ChangeListener, Observer {
74
 
850 adamjking 75
    private JPopupMenu MRUPopupMenu;
76
 
77
    private SplitButton openMRUButton;
78
 
79
    private static int maxMRU = 10;
80
 
800 adamjking 81
    private static String currentDirectory = "";
628 adamjking 82
 
850 adamjking 83
    private static Vector MRU = new Vector();
84
 
628 adamjking 85
    private static JFileChooser fc = null;
86
 
87
    private SysexSendReceive sysexSendReceive;
88
 
89
    // GUI components
90
 
846 tk 91
    private JButton sendBrowseButton;
92
    private JTextField sendFileName;
93
    private JButton sendStartButton;
94
    private JButton sendStopButton;
95
    private JButton sendClearButton;
850 adamjking 96
    private JButton sendSaveButton;
628 adamjking 97
 
846 tk 98
    private JButton receiveStartButton;
99
    private JButton receiveStopButton;
100
    private JButton receiveClearButton;
850 adamjking 101
    private JButton receiveSaveButton;
628 adamjking 102
 
103
    private JTextPane sysexSendTextArea;
104
 
105
    private JTextPane sysexReceiveTextArea;
106
 
845 tk 107
    private JSpinner sendDelayTimeSpinner;
628 adamjking 108
 
109
    public SysexSendReceiveGUI(SysexSendReceive sysexSendReceive) {
850 adamjking 110
 
628 adamjking 111
        super(new BorderLayout());
112
 
113
        this.sysexSendReceive = sysexSendReceive;
850 adamjking 114
 
628 adamjking 115
        sysexSendReceive.addObserver(this);
116
 
850 adamjking 117
        sendBrowseButton = new JButton("SysEx File", ImageLoader
800 adamjking 118
                .getImageIcon("open.png"));
846 tk 119
        sendFileName = new JTextField();
120
        sendFileName.setEditable(false);
121
        sendBrowseButton.setActionCommand("sendBrowse");
122
        sendBrowseButton.addActionListener(this);
628 adamjking 123
 
846 tk 124
        sendStartButton = new JButton("Start");
125
        sendStartButton.setActionCommand("sendStart");
126
        sendStartButton.addActionListener(this);
127
        sendStartButton.setAlignmentX(Component.LEFT_ALIGNMENT);
128
        sendStopButton = new JButton("Stop");
129
        sendStopButton.addActionListener(this);
130
        sendStopButton.setActionCommand("sendStop");
131
        sendStopButton.setAlignmentX(Component.LEFT_ALIGNMENT);
132
        sendClearButton = new JButton("Clear");
133
        sendClearButton.addActionListener(this);
134
        sendClearButton.setActionCommand("sendClear");
135
        sendClearButton.setAlignmentX(Component.LEFT_ALIGNMENT);
628 adamjking 136
 
850 adamjking 137
        sendSaveButton = new JButton("", ImageLoader.getImageIcon("save.png"));
138
        sendSaveButton.setActionCommand("sendSave");
139
        sendSaveButton.addActionListener(this);
628 adamjking 140
 
846 tk 141
        receiveStartButton = new JButton("Start");
142
        receiveStartButton.setActionCommand("receiveStart");
143
        receiveStartButton.addActionListener(this);
144
        receiveStartButton.setAlignmentX(Component.LEFT_ALIGNMENT);
145
        receiveStopButton = new JButton("Stop");
146
        receiveStopButton.addActionListener(this);
147
        receiveStopButton.setActionCommand("receiveStop");
148
        receiveStopButton.setAlignmentX(Component.LEFT_ALIGNMENT);
149
        receiveClearButton = new JButton("Clear");
150
        receiveClearButton.addActionListener(this);
151
        receiveClearButton.setActionCommand("receiveClear");
152
        receiveClearButton.setAlignmentX(Component.LEFT_ALIGNMENT);
850 adamjking 153
        receiveSaveButton = new JButton("", ImageLoader
154
                .getImageIcon("save.png"));
155
        receiveSaveButton.setActionCommand("receiveSave");
156
        receiveSaveButton.addActionListener(this);
628 adamjking 157
 
850 adamjking 158
        JPanel sendFileButtonsPanel = new JPanel(new BorderLayout(2, 2));
846 tk 159
 
850 adamjking 160
        MRUPopupMenu = new JPopupMenu();
161
        MRUPopupMenu.addPopupMenuListener(new PopupMenuListener() {
846 tk 162
 
850 adamjking 163
            public void popupMenuCanceled(PopupMenuEvent e) {
164
 
165
            }
166
 
167
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
168
 
169
            }
170
 
171
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
172
 
173
                SysexSendReceiveGUI.this.buildMRUMenu(MRUPopupMenu);
174
 
175
            }
176
        });
177
 
178
        openMRUButton = new SplitButton(sendBrowseButton, MRUPopupMenu);
179
        openMRUButton.setRollover(true);
180
 
181
        sendFileButtonsPanel.add(openMRUButton, BorderLayout.WEST);
846 tk 182
        sendFileButtonsPanel.add(sendFileName, BorderLayout.CENTER);
850 adamjking 183
        sendFileButtonsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
184
                5));
846 tk 185
 
186
        JPanel sendControlButtonsPanel = new JPanel(new GridBagLayout());
628 adamjking 187
        GridBagConstraints gbc = new GridBagConstraints();
188
        gbc.gridx = 0;
189
        gbc.gridy = 0;
190
        gbc.gridwidth = 1;
191
        gbc.gridheight = 1;
192
 
193
        gbc.insets = new Insets(5, 5, 5, 5);
194
 
846 tk 195
        sendControlButtonsPanel.add(sendStartButton, gbc);
628 adamjking 196
        gbc.gridx++;
197
 
198
        gbc.insets = new Insets(5, 13, 5, 5);
199
 
846 tk 200
        sendControlButtonsPanel.add(sendStopButton, gbc);
628 adamjking 201
        gbc.gridx++;
202
 
846 tk 203
        gbc.insets = new Insets(5, 13, 5, 5);
204
 
205
        sendControlButtonsPanel.add(sendClearButton, gbc);
206
        gbc.gridx++;
207
 
850 adamjking 208
        sendControlButtonsPanel.add(sendSaveButton, gbc);
209
        gbc.gridx++;
210
 
628 adamjking 211
        gbc.insets = new Insets(2, 2, 2, 2);
212
 
213
        gbc.weightx = 1.0;
846 tk 214
        sendControlButtonsPanel.add(new JPanel(), gbc);
628 adamjking 215
        gbc.gridx++;
216
        gbc.weightx = 0.0;
217
 
218
        gbc.fill = GridBagConstraints.NONE;
219
 
846 tk 220
        JLabel label = new JLabel("Delay between SysEx messages (ms): ");
221
        sendControlButtonsPanel.add(label, gbc);
628 adamjking 222
        gbc.gridx++;
223
 
845 tk 224
        sendDelayTimeSpinner = new JSpinner(new SpinnerNumberModel(
225
                sysexSendReceive.getSendDelayTime(), 1, 1000, 1));
226
        sendDelayTimeSpinner.addChangeListener(this);
846 tk 227
        sendControlButtonsPanel.add(sendDelayTimeSpinner, gbc);
628 adamjking 228
 
846 tk 229
        JPanel receiveControlButtonsPanel = new JPanel(new GridBagLayout());
230
        gbc.gridx = 0;
231
        gbc.gridy = 0;
232
        gbc.gridwidth = 1;
233
        gbc.gridheight = 1;
234
 
235
        gbc.insets = new Insets(5, 5, 5, 5);
236
 
237
        receiveControlButtonsPanel.add(receiveStartButton, gbc);
238
        gbc.gridx++;
239
        gbc.insets = new Insets(5, 13, 5, 5);
240
        receiveControlButtonsPanel.add(receiveStopButton, gbc);
241
        gbc.gridx++;
242
        receiveControlButtonsPanel.add(receiveClearButton, gbc);
850 adamjking 243
        gbc.gridx++;
244
        receiveControlButtonsPanel.add(receiveSaveButton, gbc);
846 tk 245
 
628 adamjking 246
        JPanel sendPanel = new JPanel(new GridBagLayout());
247
 
248
        gbc.fill = GridBagConstraints.BOTH;
249
        gbc.weightx = 1.0;
250
 
251
        sendPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
252
                .createEmptyBorder(2, 2, 2, 2), BorderFactory
253
                .createTitledBorder("Send")));
254
 
255
        sysexSendTextArea = new JTextPane();
256
        sysexSendTextArea.setBackground(Color.WHITE);
845 tk 257
        sysexSendTextArea.setEditable(true);
628 adamjking 258
 
259
        StyledDocument doc = sysexSendTextArea.getStyledDocument();
260
 
261
        Style def = StyleContext.getDefaultStyleContext().getStyle(
262
                StyleContext.DEFAULT_STYLE);
263
 
264
        doc.addStyle("regular", def);
265
        StyleConstants.setFontFamily(def, "Monospaced");
266
 
850 adamjking 267
        sysexSendTextArea.setLogicalStyle(def);
268
 
269
        if (sysexSendReceive.getSendFile() != null) {
270
 
271
            loadSysexSendFile();
272
        }
273
 
628 adamjking 274
        JScrollPane sysexSendTextAreaScrollPane = new JScrollPane(
275
                sysexSendTextArea);
276
        sysexSendTextAreaScrollPane
277
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
278
 
279
        // Make preferred width smaller than the other panel's minimum width.
280
        sysexSendTextAreaScrollPane.setPreferredSize(new Dimension(300, 100));
281
 
846 tk 282
        gbc.insets = new Insets(5, 5, 5, 5);
283
        gbc.gridy = 0;
284
        gbc.gridx = 0;
285
        sendPanel.add(sendFileButtonsPanel, gbc);
628 adamjking 286
        gbc.gridy++;
287
        gbc.weighty = 1.0;
846 tk 288
        sendPanel.add(sendControlButtonsPanel, gbc);
289
        gbc.gridy++;
290
        gbc.weighty = 1.0;
628 adamjking 291
        sendPanel.add(sysexSendTextAreaScrollPane, gbc);
292
 
293
        JPanel receivePanel = new JPanel(new GridBagLayout());
294
        receivePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
295
                .createEmptyBorder(2, 2, 2, 2), BorderFactory
296
                .createTitledBorder("Receive")));
297
 
298
        sysexReceiveTextArea = new JTextPane();
299
        sysexReceiveTextArea.setBackground(Color.WHITE);
300
 
845 tk 301
        sysexReceiveTextArea.setEditable(true);
850 adamjking 302
 
303
        doc = sysexReceiveTextArea.getStyledDocument();
304
 
305
        def = StyleContext.getDefaultStyleContext().getStyle(
306
                StyleContext.DEFAULT_STYLE);
307
 
308
        doc.addStyle("regular", def);
309
        StyleConstants.setFontFamily(def, "Monospaced");
310
 
311
        sysexReceiveTextArea.setLogicalStyle(def);
312
 
628 adamjking 313
        JScrollPane sysexReceiveTextAreaScrollPane = new JScrollPane(
314
                sysexReceiveTextArea);
315
        sysexReceiveTextAreaScrollPane
316
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
317
 
318
        // Make preferred width smaller than the other panel's minimum width.
319
        sysexReceiveTextAreaScrollPane
320
                .setPreferredSize(new Dimension(300, 100));
321
 
846 tk 322
        gbc.insets = new Insets(5, 5, 5, 5);
628 adamjking 323
        gbc.gridy = 0;
846 tk 324
        gbc.gridx = 0;
325
        gbc.weighty = 1.0;
326
        gbc.fill = GridBagConstraints.NONE;
327
        gbc.anchor = GridBagConstraints.WEST;
328
        receivePanel.add(receiveControlButtonsPanel, gbc);
329
        gbc.anchor = GridBagConstraints.CENTER;
330
        gbc.fill = GridBagConstraints.BOTH;
331
        gbc.gridy++;
332
        gbc.weighty = 1.0;
628 adamjking 333
        receivePanel.add(sysexReceiveTextAreaScrollPane, gbc);
334
 
335
        JPanel mainPanel = new JPanel(new GridBagLayout());
336
 
337
        gbc.gridx = 0;
338
        gbc.gridy = 0;
339
        gbc.insets = new Insets(4, 4, 4, 4);
340
 
341
        gbc.weighty = 1.0;
342
        mainPanel.add(sendPanel, gbc);
343
        gbc.gridy++;
344
        mainPanel.add(receivePanel, gbc);
345
 
800 adamjking 346
        add(mainPanel, BorderLayout.CENTER);
628 adamjking 347
 
348
        UIUpdate();
349
    }
350
 
351
    public void UIUpdate() {
850 adamjking 352
        boolean bUploading = (!sysexSendReceive.isSendCancelled() && !sysexSendReceive
353
                .isSendDone());
846 tk 354
        boolean bReceiving = !sysexSendReceive.isReceiveStopped();
628 adamjking 355
 
846 tk 356
        sendBrowseButton.setEnabled(!bUploading);
357
        sendStartButton.setEnabled(!bUploading);
358
        sendStopButton.setEnabled(bUploading);
359
        sendClearButton.setEnabled(!bUploading);
845 tk 360
        sendDelayTimeSpinner.setEnabled(!bUploading);
361
        sysexSendTextArea.setEnabled(!bUploading);
846 tk 362
 
850 adamjking 363
        receiveSaveButton.setEnabled(!bReceiving);
846 tk 364
        receiveStartButton.setEnabled(!bReceiving);
365
        receiveStopButton.setEnabled(bReceiving);
850 adamjking 366
        // receiveClearButton.setEnabled(!bReceiving); // better to allow clear
367
        // while receiving
846 tk 368
        sysexReceiveTextArea.setEnabled(!bReceiving);
628 adamjking 369
    }
370
 
371
    public void actionPerformed(ActionEvent e) {
846 tk 372
        if (e.getActionCommand() == "sendStart") {
845 tk 373
            byte[] sysexData = new byte[sysexSendReceive.getSendBufferSize()];
374
            int sysexDataOffset = 0;
628 adamjking 375
 
845 tk 376
            StyledDocument doc = sysexSendTextArea.getStyledDocument();
377
            int len = doc.getLength();
378
 
850 adamjking 379
            if (len > 0) {
845 tk 380
                int nleft = len;
381
                Segment text = new Segment();
382
                int offs = 0;
850 adamjking 383
                text.setPartialReturn(true);
845 tk 384
                while (nleft > 0) {
385
                    try {
386
                        doc.getText(offs, nleft, text);
387
                    } catch (Exception ex) {
850 adamjking 388
                        JOptionPane.showMessageDialog(null,
389
                                "Error while parsing hex digits!",
390
                                "Syntax Error", JOptionPane.ERROR_MESSAGE);
845 tk 391
                        return;
392
                    }
393
 
394
                    StringTokenizer st = new StringTokenizer(text.toString());
395
                    while (st.hasMoreTokens()) {
396
                        int value;
397
 
850 adamjking 398
                        try {
399
                            value = (int) java.lang.Integer.parseInt(st
400
                                    .nextToken(), 16);
401
                            sysexData[sysexDataOffset++] = (byte) value;
402
                        } catch (Exception ex) {
403
                            JOptionPane.showMessageDialog(null,
404
                                    "Found invalid hexadecimal digit!",
405
                                    "Syntax Error", JOptionPane.ERROR_MESSAGE);
845 tk 406
                            return;
407
                        }
408
                    }
409
 
410
                    nleft -= text.count;
411
                    offs += text.count;
412
                }
413
 
850 adamjking 414
                if (sysexDataOffset > 0) {
845 tk 415
                    final byte[] finalSysexData = sysexData;
416
                    final int finalSysexDataOffset = sysexDataOffset;
417
 
418
                    Thread t = new Thread() {
850 adamjking 419
                        public void run() {
420
                            sysexSendReceive.startSend(finalSysexData,
421
                                    finalSysexDataOffset);
422
                        }
423
                    };
424
 
845 tk 425
                    t.start();
426
                }
427
            }
428
 
846 tk 429
        } else if (e.getActionCommand() == "sendStop") {
430
            sysexSendReceive.sendCancel();
628 adamjking 431
 
846 tk 432
        } else if (e.getActionCommand() == "sendClear") {
433
            sysexSendTextArea.setText("");
434
 
435
        } else if (e.getActionCommand() == "sendBrowse") {
628 adamjking 436
            onOpenSysexFile();
846 tk 437
 
438
        } else if (e.getActionCommand() == "receiveStart") {
439
            sysexSendReceive.setReceiveStopped(false);
440
 
441
        } else if (e.getActionCommand() == "receiveStop") {
442
            sysexSendReceive.setReceiveStopped(true);
443
 
444
        } else if (e.getActionCommand() == "receiveClear") {
445
            sysexReceiveTextArea.setText("");
446
 
850 adamjking 447
        } else if (e.getActionCommand() == "receiveSave") {
448
            onSaveSysexFile(sysexReceiveTextArea.getStyledDocument());
449
 
450
        } else if (e.getActionCommand() == "sendSave") {
451
            onSaveSysexFile(sysexSendTextArea.getStyledDocument());
628 adamjking 452
        }
845 tk 453
 
454
        UIUpdate();
628 adamjking 455
    }
456
 
457
    protected void onOpenSysexFile() {
458
 
459
        if (fc == null) {
460
            fc = new JFileChooser(currentDirectory);
461
            fc.addChoosableFileFilter(new SimpleFileChooserFilter(
846 tk 462
                    "SysEx files (*.syx)", "syx", "SysEx File"));
628 adamjking 463
        }
464
 
465
        File noFile = new File("");
466
        File noFiles[] = { noFile };
467
        fc.setSelectedFile(noFile);
468
        fc.setSelectedFiles(noFiles);
469
 
470
        int nRetVal = fc.showOpenDialog(this);
471
 
472
        if (nRetVal == JFileChooser.APPROVE_OPTION) {
850 adamjking 473
 
628 adamjking 474
            File file = fc.getSelectedFile();
850 adamjking 475
 
476
            openSysexSendFile(file);
477
 
628 adamjking 478
            currentDirectory = fc.getCurrentDirectory().toString();
479
        }
480
    }
481
 
850 adamjking 482
    protected void onSaveSysexFile(StyledDocument doc) {
846 tk 483
 
484
        if (fc == null) {
485
            fc = new JFileChooser(currentDirectory);
486
            fc.addChoosableFileFilter(new SimpleFileChooserFilter(
487
                    "SysEx files (*.syx)", "syx", "SysEx File"));
488
        }
489
 
490
        File noFile = new File("");
491
        File noFiles[] = { noFile };
492
        fc.setSelectedFile(noFile);
493
        fc.setSelectedFiles(noFiles);
494
 
495
        int nRetVal = fc.showSaveDialog(this);
496
 
497
        if (nRetVal == JFileChooser.APPROVE_OPTION) {
498
            File file = fc.getSelectedFile();
499
            sysexSendReceive.setReceiveFile(file);
500
            currentDirectory = fc.getCurrentDirectory().toString();
501
 
850 adamjking 502
            int maxSysexDataLen = 200000; // TODO: find better method... linked
503
                                            // list?
846 tk 504
            byte[] sysexData = new byte[maxSysexDataLen];
505
            int sysexDataOffset = 0;
506
 
507
            int len = doc.getLength();
508
 
850 adamjking 509
            if (len > 0) {
846 tk 510
                int nleft = len;
511
                Segment text = new Segment();
512
                int offs = 0;
513
                text.setPartialReturn(true);
514
                while (nleft > 0) {
515
                    try {
516
                        doc.getText(offs, nleft, text);
517
                    } catch (Exception ex) {
850 adamjking 518
                        JOptionPane.showMessageDialog(null,
519
                                "Error while parsing hex digits!",
520
                                "Syntax Error", JOptionPane.ERROR_MESSAGE);
846 tk 521
                        return;
522
                    }
523
 
524
                    StringTokenizer st = new StringTokenizer(text.toString());
525
                    while (st.hasMoreTokens()) {
526
                        int value;
527
 
850 adamjking 528
                        if (sysexDataOffset >= maxSysexDataLen) {
529
                            JOptionPane.showMessageDialog(null,
530
                                    "Too many SysEx bytes!", "Parsing Error",
531
                                    JOptionPane.ERROR_MESSAGE);
846 tk 532
                            return;
533
                        }
534
 
850 adamjking 535
                        try {
536
                            value = (int) java.lang.Integer.parseInt(st
537
                                    .nextToken(), 16);
538
                            sysexData[sysexDataOffset++] = (byte) value;
539
                        } catch (Exception ex) {
540
                            JOptionPane.showMessageDialog(null,
541
                                    "Found invalid hexadecimal digit!",
542
                                    "Syntax Error", JOptionPane.ERROR_MESSAGE);
846 tk 543
                            return;
544
                        }
545
                    }
546
 
547
                    nleft -= text.count;
548
                    offs += text.count;
549
                }
550
 
850 adamjking 551
                if (sysexDataOffset > 0) {
552
                    if (!sysexSendReceive.writeSysexFile(file, sysexData,
553
                            sysexDataOffset)) {
554
                        JOptionPane.showMessageDialog(null,
555
                                "Error while writing file!",
556
                                "File Access Error", JOptionPane.ERROR_MESSAGE);
846 tk 557
                        return;
558
                    }
559
                }
560
            }
561
        }
562
    }
563
 
628 adamjking 564
    public void itemStateChanged(ItemEvent e) {
565
        UIUpdate();
566
    }
567
 
568
    public void stateChanged(ChangeEvent e) {
569
 
845 tk 570
        Object source = e.getSource();
571
 
572
        if (source == sendDelayTimeSpinner) {
850 adamjking 573
            sysexSendReceive.setSendDelayTime(((Integer) sendDelayTimeSpinner
574
                    .getValue()).intValue());
845 tk 575
        }
576
 
628 adamjking 577
        UIUpdate();
578
    }
579
 
580
    public void update(Observable observable, Object object) {
850 adamjking 581
 
628 adamjking 582
        if (observable == sysexSendReceive) {
850 adamjking 583
 
584
            if (object == sysexSendReceive.WORKER) {
585
 
586
            } else if (object == sysexSendReceive.SEND_SYSEX_FILE) {
587
 
588
                loadSysexSendFile();
589
 
590
            } else if (object == sysexSendReceive.RECEIVE_SYSEX_FILE) {
591
 
846 tk 592
                StyledDocument doc = sysexReceiveTextArea.getStyledDocument();
628 adamjking 593
 
846 tk 594
                LinkedList l = sysexSendReceive.getReceivedBytes();
850 adamjking 595
 
846 tk 596
                int i;
850 adamjking 597
 
598
                for (i = 0; i < l.size(); ++i) {
599
                    byte[] data = (byte[]) l.get(i);
846 tk 600
                    try {
850 adamjking 601
                        if (doc.getLength() > 0)
602
                            doc.insertString(doc.getLength(), "\n", doc
603
                                    .getStyle("regular"));
604
                        doc.insertString(doc.getLength(), "F0 ", doc
605
                                .getStyle("regular"));
606
                        doc.insertString(doc.getLength(), MidiUtils
607
                                .getHexString(data), doc.getStyle("regular"));
846 tk 608
                    } catch (Exception e) {
609
                    }
610
                }
850 adamjking 611
 
846 tk 612
                sysexSendReceive.clearReceivedBytes();
628 adamjking 613
            }
845 tk 614
 
615
            UIUpdate();
628 adamjking 616
        }
617
    }
618
 
850 adamjking 619
    private void buildMRUMenu(JComponent menu) {
620
 
621
        menu.removeAll();
622
 
623
        Iterator it = MRU.iterator();
624
 
625
        while (it.hasNext()) {
626
 
627
            final JMenuItem menuItem = new JMenuItem((String) it.next());
628
 
629
            menuItem.addActionListener(new ActionListener() {
630
 
631
                public void actionPerformed(ActionEvent ae) {
632
 
633
                    File file = new File(menuItem.getText());
634
 
635
                    openSysexSendFile(file);
636
                }
637
            });
638
 
639
            menu.add(menuItem, 0);
640
        }
641
    }
642
 
643
    protected void openSysexSendFile(File file) {
644
 
645
        if (file.exists()) {
646
 
647
            sysexSendReceive.setSendFile(file);
648
 
649
            saveMRU(file.getPath());
650
 
651
        } else {
652
            JOptionPane.showMessageDialog(this, "Hex file no longer exists",
653
                    "File does not exist", JOptionPane.ERROR_MESSAGE);
654
        }
655
    }
656
 
657
    public static void saveMRU(String file) {
658
 
659
        MRU.remove(file);
660
 
661
        MRU.add(file);
662
 
663
        for (int i = MRU.size() - maxMRU; i > 0; i--) {
664
 
665
            MRU.removeElementAt(i - 1);
666
        }
667
    }
668
 
669
    public static Vector getMRU() {
670
        return MRU;
671
    }
672
 
628 adamjking 673
    public SysexSendReceive getSysexSendReceive() {
674
        return sysexSendReceive;
675
    }
800 adamjking 676
 
677
    public static String getCurrentDirectory() {
678
        return currentDirectory;
679
    }
680
 
681
    public static void setCurrentDirectory(String currentDirectory) {
682
        SysexSendReceiveGUI.currentDirectory = currentDirectory;
683
    }
808 adamjking 684
 
850 adamjking 685
    private void loadSysexSendFile() {
686
 
687
        sendFileName.setText(sysexSendReceive.getSendFile().getPath());
688
 
689
        sysexSendTextArea.setText("");
690
 
691
        StyledDocument doc = sysexSendTextArea.getStyledDocument();
692
 
693
        try {
694
            doc.insertString(0, MidiUtils.getHexString(sysexSendReceive
695
                    .getSysexFile().getData()), doc.getStyle("regular"));
696
        } catch (Exception e) {
697
 
698
        }
699
    }
628 adamjking 700
}