Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MidiFilterGUI.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;
741 adamjking 24
import java.awt.Component;
628 adamjking 25
import java.awt.Dimension;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
682 adamjking 28
import java.awt.Insets;
628 adamjking 29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
703 adamjking 31
import java.io.File;
753 adamjking 32
import java.util.Iterator;
628 adamjking 33
import java.util.Observable;
34
import java.util.Observer;
753 adamjking 35
import java.util.Vector;
628 adamjking 36
 
713 adamjking 37
import javax.sound.midi.ShortMessage;
741 adamjking 38
import javax.swing.AbstractCellEditor;
628 adamjking 39
import javax.swing.BorderFactory;
40
import javax.swing.Box;
682 adamjking 41
import javax.swing.JButton;
628 adamjking 42
import javax.swing.JCheckBox;
753 adamjking 43
import javax.swing.JComponent;
703 adamjking 44
import javax.swing.JFileChooser;
753 adamjking 45
import javax.swing.JMenuItem;
46
import javax.swing.JOptionPane;
628 adamjking 47
import javax.swing.JPanel;
753 adamjking 48
import javax.swing.JPopupMenu;
628 adamjking 49
import javax.swing.JScrollPane;
50
import javax.swing.JTable;
682 adamjking 51
import javax.swing.JToolBar;
753 adamjking 52
import javax.swing.event.PopupMenuEvent;
53
import javax.swing.event.PopupMenuListener;
741 adamjking 54
import javax.swing.table.DefaultTableCellRenderer;
628 adamjking 55
import javax.swing.table.DefaultTableModel;
741 adamjking 56
import javax.swing.table.TableCellEditor;
628 adamjking 57
 
58
import org.midibox.midi.MidiFilter;
59
import org.midibox.midi.MidiUtils;
709 adamjking 60
import org.midibox.midi.xml.MidiFilterXML;
682 adamjking 61
import org.midibox.utils.gui.ImageLoader;
703 adamjking 62
import org.midibox.utils.gui.SimpleFileChooserFilter;
753 adamjking 63
import org.midibox.utils.gui.SplitButton;
628 adamjking 64
 
709 adamjking 65
public class MidiFilterGUI extends JPanel implements Observer, ActionListener {
628 adamjking 66
 
67
    private MidiFilter midiFilter;
68
 
741 adamjking 69
    private JCheckBox voiceMessages;
628 adamjking 70
 
741 adamjking 71
    private JCheckBox systemCommonMessages;
628 adamjking 72
 
741 adamjking 73
    private JCheckBox systemRealtimeMessages;
74
 
75
    private JCheckBox sysexMessages;
76
 
77
    private JCheckBox metaMessages;
78
 
628 adamjking 79
    private JCheckBox activeSensing;
80
 
81
    private JCheckBox channelPressure;
82
 
83
    private JCheckBox continueMessage;
84
 
85
    private JCheckBox controlChange;
86
 
87
    private JCheckBox midiTimeCode;
88
 
89
    private JCheckBox noteOff;
90
 
91
    private JCheckBox noteOn;
92
 
93
    private JCheckBox pitchBend;
94
 
713 adamjking 95
    private JCheckBox polyPressure;
628 adamjking 96
 
97
    private JCheckBox programChange;
98
 
99
    private JCheckBox songPositionPointer;
100
 
101
    private JCheckBox songSelect;
102
 
103
    private JCheckBox start;
104
 
105
    private JCheckBox stop;
106
 
107
    private JCheckBox systemReset;
108
 
109
    private JCheckBox timingClock;
110
 
111
    private JCheckBox tuneRequest;
112
 
741 adamjking 113
    private JTable controlChangeTable;
628 adamjking 114
 
741 adamjking 115
    private DefaultTableModel controlChangeModel;
628 adamjking 116
 
741 adamjking 117
    private JTable channelsTable;
628 adamjking 118
 
119
    private DefaultTableModel channelsModel;
753 adamjking 120
 
121
    private JPopupMenu MRUPopupMenu;
122
 
123
    private SplitButton openMRUButton;
709 adamjking 124
 
753 adamjking 125
    private static int maxMRU = 10;
126
 
703 adamjking 127
    private static String currentDirectory = "";
753 adamjking 128
 
129
    private static Vector MRU = new Vector();
628 adamjking 130
 
703 adamjking 131
    private static JFileChooser fc = null;
132
 
628 adamjking 133
    public MidiFilterGUI(MidiFilter midiFilter) {
134
        super(new BorderLayout(5, 5));
135
        this.midiFilter = midiFilter;
136
 
713 adamjking 137
        midiFilter.addObserver(this);
714 adamjking 138
 
628 adamjking 139
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
140
 
141
        // voice messages
741 adamjking 142
        JPanel masterPanel = new JPanel(new GridBagLayout());
628 adamjking 143
        GridBagConstraints gbc = new GridBagConstraints();
741 adamjking 144
        gbc.gridx = 0;
628 adamjking 145
        gbc.gridy = 0;
146
        gbc.anchor = GridBagConstraints.NORTHWEST;
147
        gbc.fill = GridBagConstraints.BOTH;
148
        gbc.weightx = 1.0;
741 adamjking 149
        gbc.insets = new Insets(0, 2, 0, 2);
628 adamjking 150
 
741 adamjking 151
        masterPanel.setBorder(BorderFactory.createTitledBorder("Message Type"));
628 adamjking 152
 
741 adamjking 153
        voiceMessages = new JCheckBox("Voice", midiFilter.isVoiceMessages());
154
        voiceMessages.addActionListener(this);
155
 
156
        masterPanel.add(voiceMessages, gbc);
157
        gbc.gridy++;
158
 
159
        systemCommonMessages = new JCheckBox("System Common", midiFilter
160
                .isSystemCommonMessages());
161
        systemCommonMessages.addActionListener(this);
162
 
163
        masterPanel.add(systemCommonMessages, gbc);
164
        gbc.gridy++;
165
 
166
        systemRealtimeMessages = new JCheckBox("System Realtime", midiFilter
167
                .isSystemRealtimeMessages());
168
        systemRealtimeMessages.addActionListener(this);
169
 
170
        masterPanel.add(systemRealtimeMessages, gbc);
171
        gbc.gridy++;
172
 
173
        sysexMessages = new JCheckBox("SysEx", midiFilter.isSysexMessages());
174
        sysexMessages.addActionListener(this);
175
 
176
        masterPanel.add(sysexMessages, gbc);
177
        gbc.gridy++;
178
 
179
        metaMessages = new JCheckBox("Meta", midiFilter.isMetaMessages());
180
        metaMessages.addActionListener(this);
181
 
182
        masterPanel.add(metaMessages, gbc);
183
        gbc.gridy++;
184
 
185
        gbc.weighty = 1.0;
186
        masterPanel.add(Box.createVerticalGlue(), gbc);
187
 
188
        JPanel voicePanel = new JPanel(new GridBagLayout());
189
        gbc.gridy = 0;
190
        gbc.weighty = 0.0;
191
 
192
        voicePanel.setBorder(BorderFactory.createTitledBorder("Voice"));
193
 
714 adamjking 194
        noteOff = new JCheckBox("Note Off", midiFilter
195
                .getVoiceMessage(ShortMessage.NOTE_OFF));
628 adamjking 196
        noteOff.addActionListener(this);
197
        voicePanel.add(noteOff, gbc);
198
        gbc.gridy++;
199
 
714 adamjking 200
        noteOn = new JCheckBox("Note On", midiFilter
201
                .getVoiceMessage(ShortMessage.NOTE_ON));
628 adamjking 202
        noteOn.addActionListener(this);
203
        voicePanel.add(noteOn, gbc);
204
        gbc.gridy++;
205
 
714 adamjking 206
        polyPressure = new JCheckBox("Aftertouch", midiFilter
207
                .getVoiceMessage(ShortMessage.POLY_PRESSURE));
713 adamjking 208
        polyPressure.addActionListener(this);
209
        voicePanel.add(polyPressure, gbc);
628 adamjking 210
        gbc.gridy++;
211
 
714 adamjking 212
        controlChange = new JCheckBox("Control Change", midiFilter
213
                .getVoiceMessage(ShortMessage.CONTROL_CHANGE));
628 adamjking 214
        controlChange.addActionListener(this);
215
        voicePanel.add(controlChange, gbc);
216
        gbc.gridy++;
217
 
714 adamjking 218
        programChange = new JCheckBox("Program Change", midiFilter
219
                .getVoiceMessage(ShortMessage.PROGRAM_CHANGE));
628 adamjking 220
        programChange.addActionListener(this);
221
        voicePanel.add(programChange, gbc);
222
        gbc.gridy++;
223
 
714 adamjking 224
        channelPressure = new JCheckBox("Channel Pressure", midiFilter
225
                .getVoiceMessage(ShortMessage.CHANNEL_PRESSURE));
628 adamjking 226
        channelPressure.addActionListener(this);
227
        voicePanel.add(channelPressure, gbc);
228
        gbc.gridy++;
229
 
714 adamjking 230
        pitchBend = new JCheckBox("Pitch Bend", midiFilter
231
                .getVoiceMessage(ShortMessage.PITCH_BEND));
628 adamjking 232
        pitchBend.addActionListener(this);
233
        voicePanel.add(pitchBend, gbc);
234
        gbc.gridy++;
235
 
236
        gbc.weighty = 1.0;
237
        voicePanel.add(Box.createVerticalGlue(), gbc);
238
 
741 adamjking 239
        gbc.gridheight = 8;
240
        gbc.weighty = 0.0;
628 adamjking 241
        gbc.gridy = 0;
242
 
741 adamjking 243
        gbc.gridx++;
628 adamjking 244
 
741 adamjking 245
        controlChangeModel = new MidiFilterTableModel(
246
                new String[] { "Controller" }, 128);
247
        controlChangeTable = new JTable(controlChangeModel);
628 adamjking 248
 
741 adamjking 249
        JScrollPane ccScroll = new JScrollPane(controlChangeTable);
250
        ccScroll
251
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
252
        ccScroll.setPreferredSize(new Dimension(200, 200));
253
        controlChangeTable.setRowSelectionAllowed(false);
254
        controlChangeTable.setShowGrid(false);
255
        controlChangeTable.setDefaultRenderer(Boolean.class,
256
                new MidiFilterTableCellRenderer());
257
        controlChangeTable.setDefaultEditor(Boolean.class,
258
                new MidiFilterTableCellEditor());
259
 
260
        voicePanel.add(ccScroll, gbc);
261
 
262
        gbc.gridx++;
263
 
264
        channelsModel = new MidiFilterTableModel(new String[] { "Channel" }, 16);
265
        channelsTable = new JTable(channelsModel);
266
 
267
        JScrollPane channelsScroll = new JScrollPane(channelsTable);
268
        channelsScroll
269
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
270
        channelsScroll.setPreferredSize(new Dimension(150, 200));
271
        channelsTable.setCellSelectionEnabled(false);
272
        channelsTable.setShowGrid(false);
273
        channelsTable.setDefaultRenderer(Boolean.class,
274
                new MidiFilterTableCellRenderer());
275
        channelsTable.setDefaultEditor(Boolean.class,
276
                new MidiFilterTableCellEditor());
277
 
278
        voicePanel.add(channelsScroll, gbc);
279
 
628 adamjking 280
        // system common
741 adamjking 281
        JPanel systemCommonPanel = new JPanel(new GridBagLayout());
628 adamjking 282
        gbc.gridy = 0;
283
        gbc.weighty = 0.0;
741 adamjking 284
        gbc.gridheight = 1;
285
        systemCommonPanel.setBorder(BorderFactory
286
                .createTitledBorder("System Common"));
628 adamjking 287
 
716 adamjking 288
        midiTimeCode = new JCheckBox("MTC Quarter Frame", midiFilter
714 adamjking 289
                .getSystemCommonMessage(ShortMessage.MIDI_TIME_CODE));
628 adamjking 290
        midiTimeCode.addActionListener(this);
741 adamjking 291
        systemCommonPanel.add(midiTimeCode, gbc);
628 adamjking 292
        gbc.gridy++;
293
 
714 adamjking 294
        songPositionPointer = new JCheckBox("Song Position Pointer", midiFilter
295
                .getSystemCommonMessage(ShortMessage.SONG_POSITION_POINTER));
628 adamjking 296
        songPositionPointer.addActionListener(this);
741 adamjking 297
        systemCommonPanel.add(songPositionPointer, gbc);
628 adamjking 298
        gbc.gridy++;
299
 
714 adamjking 300
        songSelect = new JCheckBox("Song Select", midiFilter
301
                .getSystemCommonMessage(ShortMessage.SONG_SELECT));
628 adamjking 302
        songSelect.addActionListener(this);
741 adamjking 303
        systemCommonPanel.add(songSelect, gbc);
628 adamjking 304
        gbc.gridy++;
305
 
714 adamjking 306
        tuneRequest = new JCheckBox("Tune Request", midiFilter
307
                .getSystemCommonMessage(ShortMessage.TUNE_REQUEST));
628 adamjking 308
        tuneRequest.addActionListener(this);
741 adamjking 309
        systemCommonPanel.add(tuneRequest, gbc);
628 adamjking 310
        gbc.gridy++;
311
 
312
        gbc.weighty = 1.0;
741 adamjking 313
        systemCommonPanel.add(Box.createVerticalGlue(), gbc);
628 adamjking 314
 
315
        // system realtime
741 adamjking 316
        JPanel systemRealtimePanel = new JPanel(new GridBagLayout());
628 adamjking 317
        gbc.gridy = 0;
318
        gbc.weighty = 0.0;
741 adamjking 319
        systemRealtimePanel.setBorder(BorderFactory
320
                .createTitledBorder("System Realtime"));
628 adamjking 321
 
714 adamjking 322
        timingClock = new JCheckBox("Timing Clock", midiFilter
323
                .getSystemRealtimeMessage(ShortMessage.TIMING_CLOCK));
628 adamjking 324
        timingClock.addActionListener(this);
741 adamjking 325
        systemRealtimePanel.add(timingClock, gbc);
628 adamjking 326
        gbc.gridy++;
327
 
714 adamjking 328
        start = new JCheckBox("Start", midiFilter
329
                .getSystemRealtimeMessage(ShortMessage.START));
628 adamjking 330
        start.addActionListener(this);
741 adamjking 331
        systemRealtimePanel.add(start, gbc);
628 adamjking 332
        gbc.gridy++;
333
 
714 adamjking 334
        continueMessage = new JCheckBox("Continue", midiFilter
335
                .getSystemRealtimeMessage(ShortMessage.CONTINUE));
628 adamjking 336
        continueMessage.addActionListener(this);
741 adamjking 337
        systemRealtimePanel.add(continueMessage, gbc);
628 adamjking 338
        gbc.gridy++;
339
 
714 adamjking 340
        stop = new JCheckBox("Stop", midiFilter
341
                .getSystemRealtimeMessage(ShortMessage.STOP));
628 adamjking 342
        stop.addActionListener(this);
741 adamjking 343
        systemRealtimePanel.add(stop, gbc);
628 adamjking 344
        gbc.gridy++;
345
 
714 adamjking 346
        activeSensing = new JCheckBox("Active Sensing", midiFilter
347
                .getSystemRealtimeMessage(ShortMessage.ACTIVE_SENSING));
628 adamjking 348
        activeSensing.addActionListener(this);
741 adamjking 349
        systemRealtimePanel.add(activeSensing, gbc);
628 adamjking 350
        gbc.gridy++;
351
 
714 adamjking 352
        systemReset = new JCheckBox("System Reset", midiFilter
353
                .getSystemRealtimeMessage(ShortMessage.SYSTEM_RESET));
628 adamjking 354
        systemReset.addActionListener(this);
741 adamjking 355
        systemRealtimePanel.add(systemReset, gbc);
628 adamjking 356
        gbc.gridy++;
357
 
358
        gbc.weighty = 1.0;
741 adamjking 359
        systemRealtimePanel.add(Box.createVerticalGlue(), gbc);
628 adamjking 360
 
361
        JPanel mainPanel = new JPanel(new GridBagLayout());
362
        gbc.gridx = 0;
363
        gbc.gridy = 0;
364
 
741 adamjking 365
        mainPanel.add(masterPanel, gbc);
628 adamjking 366
        gbc.gridx++;
367
 
741 adamjking 368
        mainPanel.add(voicePanel, gbc);
628 adamjking 369
        gbc.gridx++;
370
 
741 adamjking 371
        mainPanel.add(systemCommonPanel, gbc);
628 adamjking 372
        gbc.gridx++;
373
 
741 adamjking 374
        mainPanel.add(systemRealtimePanel, gbc);
628 adamjking 375
 
682 adamjking 376
        add(mainPanel, BorderLayout.CENTER);
377
 
703 adamjking 378
        add(createToolBar(), BorderLayout.NORTH);
741 adamjking 379
 
380
        setButtonStates();
628 adamjking 381
    }
382
 
682 adamjking 383
    private JToolBar createToolBar() {
384
 
385
        JToolBar toolBar = new JToolBar();
386
 
387
        toolBar.setRollover(true);
388
 
389
        toolBar.setFloatable(false);
390
 
391
        JButton button = new JButton(ImageLoader.getImageIcon("open.png"));
392
 
393
        button.addActionListener(this);
394
 
395
        button.setMargin(new Insets(2, 2, 2, 2));
396
 
397
        button.setToolTipText("Load MIDI filter definition");
709 adamjking 398
 
703 adamjking 399
        button.setActionCommand("load");
682 adamjking 400
 
753 adamjking 401
        MRUPopupMenu = new JPopupMenu();
402
        MRUPopupMenu.addPopupMenuListener(new PopupMenuListener() {
682 adamjking 403
 
753 adamjking 404
            public void popupMenuCanceled(PopupMenuEvent e) {
405
 
406
            }
407
 
408
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
409
 
410
            }
411
 
412
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
413
 
414
                MidiFilterGUI.this.buildMRUMenu(MRUPopupMenu);
415
 
416
            }
417
        });
418
 
419
        openMRUButton = new SplitButton(button, MRUPopupMenu);
420
        openMRUButton.setRollover(true);
421
 
422
        toolBar.add(openMRUButton);
423
 
682 adamjking 424
        button = new JButton(ImageLoader.getImageIcon("save.png"));
425
 
426
        button.setToolTipText("Save MIDI filter definition");
709 adamjking 427
 
703 adamjking 428
        button.setActionCommand("save");
682 adamjking 429
 
430
        button.addActionListener(this);
431
 
432
        button.setMargin(new Insets(2, 2, 2, 2));
433
 
434
        toolBar.add(button);
435
 
436
        return toolBar;
437
    }
709 adamjking 438
 
703 adamjking 439
    protected void loadFilterDefinition() {
709 adamjking 440
 
703 adamjking 441
        if (fc == null) {
442
            fc = new JFileChooser(currentDirectory);
443
            SimpleFileChooserFilter fileFilter = new SimpleFileChooserFilter(
444
                    "XML files", "xml", "XML Filter Definition");
445
            fc.addChoosableFileFilter(fileFilter);
446
            fc.setAcceptAllFileFilterUsed(false);
447
        }
682 adamjking 448
 
703 adamjking 449
        File noFile = new File("");
450
        File noFiles[] = { noFile };
451
        fc.setSelectedFile(noFile);
452
        fc.setSelectedFiles(noFiles);
453
 
454
        int nRetVal = fc.showOpenDialog(this);
455
 
456
        if (nRetVal == JFileChooser.APPROVE_OPTION) {
457
            File file = fc.getSelectedFile();
709 adamjking 458
 
753 adamjking 459
            loadFilterDefinition(file);
460
 
461
            setButtonStates();
462
 
463
            currentDirectory = fc.getCurrentDirectory().toString();
464
        }
465
    }
466
 
467
    protected void loadFilterDefinition(File file) {
468
 
469
        if (file.exists()) {
470
 
722 adamjking 471
            MidiFilterXML midiFilterXML = new MidiFilterXML(midiFilter,
472
                    MidiFilterXML.TAG_ROOT_ELEMENT);
473
 
721 adamjking 474
            midiFilterXML.loadXML(file);
753 adamjking 475
 
476
            saveMRU(file.getPath());
477
 
478
        } else {
479
            JOptionPane.showMessageDialog(this,
480
                    "MIDI filter definition no longer exists",
481
                    "File does not exist", JOptionPane.ERROR_MESSAGE);
482
        }
483
    }
484
 
485
    private void buildMRUMenu(JComponent menu) {
709 adamjking 486
 
753 adamjking 487
        menu.removeAll();
709 adamjking 488
 
753 adamjking 489
        Iterator it = MRU.iterator();
490
 
491
        while (it.hasNext()) {
492
 
493
            final JMenuItem menuItem = new JMenuItem((String) it.next());
494
 
495
            menuItem.addActionListener(new ActionListener() {
496
 
497
                public void actionPerformed(ActionEvent ae) {
498
 
499
                    File file = new File(menuItem.getText());
500
 
501
                    loadFilterDefinition(file);
502
                }
503
            });
504
 
505
            menu.add(menuItem, 0);
703 adamjking 506
        }
507
    }
753 adamjking 508
 
509
    public static void saveMRU(String file) {
709 adamjking 510
 
753 adamjking 511
        MRU.remove(file);
512
 
513
        MRU.add(file);
514
 
515
        for (int i = MRU.size() - maxMRU; i > 0; i--) {
516
 
517
            MRU.removeElementAt(i - 1);
518
        }
519
    }
520
 
521
    public static Vector getMRU() {
522
        return MRU;
523
    }
524
 
703 adamjking 525
    protected void saveFilterDefinition() {
709 adamjking 526
 
527
        if (fc == null) {
528
            fc = new JFileChooser(currentDirectory);
529
            SimpleFileChooserFilter fileFilter = new SimpleFileChooserFilter(
530
                    "XML files", "xml", "XML Filter Definition");
531
            fc.addChoosableFileFilter(fileFilter);
532
            fc.setAcceptAllFileFilterUsed(false);
533
        }
534
 
535
        File noFile = new File("");
536
        File noFiles[] = { noFile };
537
        fc.setSelectedFile(noFile);
538
        fc.setSelectedFiles(noFiles);
539
 
540
        int nRetVal = fc.showSaveDialog(this);
541
 
542
        if (nRetVal == JFileChooser.APPROVE_OPTION) {
543
            File file = fc.getSelectedFile();
544
 
722 adamjking 545
            MidiFilterXML midiFilterXML = new MidiFilterXML(midiFilter,
546
                    MidiFilterXML.TAG_ROOT_ELEMENT);
547
 
721 adamjking 548
            midiFilterXML.saveXML(file);
709 adamjking 549
 
753 adamjking 550
            saveMRU(file.getPath());
551
 
709 adamjking 552
            currentDirectory = fc.getCurrentDirectory().toString();
553
        }
703 adamjking 554
    }
709 adamjking 555
 
722 adamjking 556
    public static String getCurrentDirectory() {
557
        return currentDirectory;
558
    }
559
 
560
    public static void setCurrentDirectory(String currentDirectory) {
561
        MidiFilterGUI.currentDirectory = currentDirectory;
562
    }
563
 
703 adamjking 564
    public void update(Observable observable, Object object) {
709 adamjking 565
 
713 adamjking 566
        setButtonStates();
703 adamjking 567
    }
568
 
628 adamjking 569
    public void actionPerformed(ActionEvent ae) {
570
 
571
        Object source = ae.getSource();
714 adamjking 572
 
741 adamjking 573
        if (source == voiceMessages)
574
            midiFilter.setVoiceMessages(voiceMessages.isSelected());
575
 
576
        if (source == systemCommonMessages)
577
            midiFilter.setSystemCommonMessages(systemCommonMessages
578
                    .isSelected());
579
 
580
        if (source == systemRealtimeMessages)
581
            midiFilter.setSystemRealtimeMessages(systemRealtimeMessages
582
                    .isSelected());
583
 
584
        if (source == sysexMessages)
585
            midiFilter.setSysexMessages(sysexMessages.isSelected());
586
 
587
        if (source == metaMessages)
588
            midiFilter.setMetaMessages(metaMessages.isSelected());
589
 
628 adamjking 590
        if (source == noteOff)
714 adamjking 591
            midiFilter.setVoiceMessage(ShortMessage.NOTE_OFF, noteOff
592
                    .isSelected());
628 adamjking 593
 
594
        if (source == noteOn)
714 adamjking 595
            midiFilter.setVoiceMessage(ShortMessage.NOTE_ON, noteOn
596
                    .isSelected());
597
 
713 adamjking 598
        if (source == polyPressure)
714 adamjking 599
            midiFilter.setVoiceMessage(ShortMessage.POLY_PRESSURE, polyPressure
600
                    .isSelected());
628 adamjking 601
 
713 adamjking 602
        if (source == controlChange)
714 adamjking 603
            midiFilter.setVoiceMessage(ShortMessage.CONTROL_CHANGE,
604
                    controlChange.isSelected());
605
 
713 adamjking 606
        if (source == programChange)
714 adamjking 607
            midiFilter.setVoiceMessage(ShortMessage.PROGRAM_CHANGE,
608
                    programChange.isSelected());
609
 
713 adamjking 610
        if (source == channelPressure)
714 adamjking 611
            midiFilter.setVoiceMessage(ShortMessage.CHANNEL_PRESSURE,
612
                    channelPressure.isSelected());
613
 
628 adamjking 614
        if (source == pitchBend)
714 adamjking 615
            midiFilter.setVoiceMessage(ShortMessage.PITCH_BEND, pitchBend
616
                    .isSelected());
617
 
713 adamjking 618
        if (source == midiTimeCode)
714 adamjking 619
            midiFilter.setSystemCommonMessage(ShortMessage.MIDI_TIME_CODE,
620
                    midiTimeCode.isSelected());
621
 
628 adamjking 622
        if (source == songPositionPointer)
714 adamjking 623
            midiFilter.setSystemCommonMessage(
624
                    ShortMessage.SONG_POSITION_POINTER, songPositionPointer
625
                            .isSelected());
628 adamjking 626
 
627
        if (source == songSelect)
714 adamjking 628
            midiFilter.setSystemCommonMessage(ShortMessage.SONG_SELECT,
629
                    songSelect.isSelected());
630
 
713 adamjking 631
        if (source == tuneRequest)
714 adamjking 632
            midiFilter.setSystemCommonMessage(ShortMessage.TUNE_REQUEST,
633
                    tuneRequest.isSelected());
634
 
713 adamjking 635
        if (source == timingClock)
714 adamjking 636
            midiFilter.setSystemRealtimeMessage(ShortMessage.TIMING_CLOCK,
637
                    timingClock.isSelected());
638
 
628 adamjking 639
        if (source == start)
714 adamjking 640
            midiFilter.setSystemRealtimeMessage(ShortMessage.START, start
641
                    .isSelected());
628 adamjking 642
 
713 adamjking 643
        if (source == continueMessage)
714 adamjking 644
            midiFilter.setSystemRealtimeMessage(ShortMessage.CONTINUE,
645
                    continueMessage.isSelected());
713 adamjking 646
 
628 adamjking 647
        if (source == stop)
714 adamjking 648
            midiFilter.setSystemRealtimeMessage(ShortMessage.STOP, stop
649
                    .isSelected());
650
 
713 adamjking 651
        if (source == activeSensing)
714 adamjking 652
            midiFilter.setSystemRealtimeMessage(ShortMessage.ACTIVE_SENSING,
653
                    activeSensing.isSelected());
628 adamjking 654
 
655
        if (source == systemReset)
714 adamjking 656
            midiFilter.setSystemRealtimeMessage(ShortMessage.SYSTEM_RESET,
657
                    systemReset.isSelected());
628 adamjking 658
 
703 adamjking 659
        if (ae.getActionCommand() == "load")
660
            loadFilterDefinition();
709 adamjking 661
 
662
        if (ae.getActionCommand() == "save")
703 adamjking 663
            saveFilterDefinition();
628 adamjking 664
    }
665
 
709 adamjking 666
    protected void setButtonStates() {
628 adamjking 667
 
741 adamjking 668
        voiceMessages.setSelected(midiFilter.isVoiceMessages());
669
 
670
        systemCommonMessages.setSelected(midiFilter.isSystemCommonMessages());
671
 
672
        systemRealtimeMessages.setSelected(midiFilter
673
                .isSystemRealtimeMessages());
674
 
675
        sysexMessages.setSelected(midiFilter.isSysexMessages());
676
 
677
        metaMessages.setSelected(midiFilter.isMetaMessages());
678
 
714 adamjking 679
        noteOff.setSelected(midiFilter.getVoiceMessage(ShortMessage.NOTE_OFF));
741 adamjking 680
        noteOff.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 681
 
682
        noteOn.setSelected(midiFilter.getVoiceMessage(ShortMessage.NOTE_ON));
741 adamjking 683
        noteOn.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 684
 
685
        polyPressure.setSelected(midiFilter
686
                .getVoiceMessage(ShortMessage.POLY_PRESSURE));
741 adamjking 687
        polyPressure.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 688
 
689
        controlChange.setSelected(midiFilter
690
                .getVoiceMessage(ShortMessage.CONTROL_CHANGE));
741 adamjking 691
        controlChange.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 692
 
693
        programChange.setSelected(midiFilter
694
                .getVoiceMessage(ShortMessage.PROGRAM_CHANGE));
741 adamjking 695
        programChange.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 696
 
697
        channelPressure.setSelected(midiFilter
698
                .getVoiceMessage(ShortMessage.CHANNEL_PRESSURE));
741 adamjking 699
        channelPressure.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 700
 
701
        pitchBend.setSelected(midiFilter
702
                .getVoiceMessage(ShortMessage.PITCH_BEND));
741 adamjking 703
        pitchBend.setEnabled(midiFilter.isVoiceMessages());
714 adamjking 704
 
741 adamjking 705
        controlChangeModel.fireTableDataChanged();
706
 
707
        channelsModel.fireTableDataChanged();
708
 
714 adamjking 709
        midiTimeCode.setSelected(midiFilter
710
                .getSystemCommonMessage(ShortMessage.MIDI_TIME_CODE));
741 adamjking 711
        midiTimeCode.setEnabled(midiFilter.isSystemCommonMessages());
714 adamjking 712
 
713
        songPositionPointer.setSelected(midiFilter
714
                .getSystemCommonMessage(ShortMessage.SONG_POSITION_POINTER));
741 adamjking 715
        songPositionPointer.setEnabled(midiFilter.isSystemCommonMessages());
714 adamjking 716
 
717
        songSelect.setSelected(midiFilter
718
                .getSystemCommonMessage(ShortMessage.SONG_SELECT));
741 adamjking 719
        songSelect.setEnabled(midiFilter.isSystemCommonMessages());
714 adamjking 720
 
721
        tuneRequest.setSelected(midiFilter
722
                .getSystemCommonMessage(ShortMessage.TUNE_REQUEST));
741 adamjking 723
        tuneRequest.setEnabled(midiFilter.isSystemCommonMessages());
714 adamjking 724
 
725
        timingClock.setSelected(midiFilter
726
                .getSystemRealtimeMessage(ShortMessage.TIMING_CLOCK));
741 adamjking 727
        timingClock.setEnabled(midiFilter.isSystemRealtimeMessages());
714 adamjking 728
 
729
        start.setSelected(midiFilter
730
                .getSystemRealtimeMessage(ShortMessage.START));
741 adamjking 731
        start.setEnabled(midiFilter.isSystemRealtimeMessages());
714 adamjking 732
 
733
        continueMessage.setSelected(midiFilter
734
                .getSystemRealtimeMessage(ShortMessage.CONTINUE));
741 adamjking 735
        continueMessage.setEnabled(midiFilter.isSystemRealtimeMessages());
714 adamjking 736
 
737
        stop
738
                .setSelected(midiFilter
739
                        .getSystemRealtimeMessage(ShortMessage.STOP));
741 adamjking 740
        stop.setEnabled(midiFilter.isSystemRealtimeMessages());
714 adamjking 741
 
742
        activeSensing.setSelected(midiFilter
743
                .getSystemRealtimeMessage(ShortMessage.ACTIVE_SENSING));
741 adamjking 744
        activeSensing.setEnabled(midiFilter.isSystemRealtimeMessages());
714 adamjking 745
 
746
        systemReset.setSelected(midiFilter
747
                .getSystemRealtimeMessage(ShortMessage.SYSTEM_RESET));
741 adamjking 748
        systemReset.setEnabled(midiFilter.isSystemRealtimeMessages());
749
    }
714 adamjking 750
 
741 adamjking 751
    class MidiFilterTableCellEditor extends AbstractCellEditor implements
752
            TableCellEditor {
714 adamjking 753
 
741 adamjking 754
        private JCheckBox checkBox;
714 adamjking 755
 
741 adamjking 756
        public Component getTableCellEditorComponent(JTable table,
757
                Object value, boolean isSelected, int row, int column) {
628 adamjking 758
 
741 adamjking 759
            if (table.getRowCount() == 128) {
714 adamjking 760
 
741 adamjking 761
                checkBox = new JCheckBox(row + ": " + MidiUtils.ccNames[row],
762
                        midiFilter.getControlChange(row));
763
 
764
                checkBox.setEnabled(midiFilter.isVoiceMessages()
765
                        && midiFilter
766
                                .getVoiceMessage(ShortMessage.CONTROL_CHANGE));
767
 
768
            } else {
769
 
770
                checkBox = new JCheckBox("Channel " + (row + 1), midiFilter
771
                        .getChannel(row));
772
            }
773
 
774
            checkBox.setOpaque(false);
775
 
776
            checkBox.addActionListener(new ActionListener() {
777
                public void actionPerformed(ActionEvent ae) {
778
                    fireEditingStopped();
779
                }
780
            });
781
 
782
            return checkBox;
783
        }
784
 
785
        public Object getCellEditorValue() {
786
 
787
            return new Boolean(checkBox.isSelected());
788
        }
628 adamjking 789
    }
790
 
741 adamjking 791
    class MidiFilterTableCellRenderer extends DefaultTableCellRenderer {
628 adamjking 792
 
741 adamjking 793
        public Component getTableCellRendererComponent(JTable table,
794
                Object value, boolean isSelected, boolean hasFocus, int row,
795
                int column) {
796
 
797
            JCheckBox checkBox;
798
 
799
            if (table.getRowCount() == 128) {
800
 
801
                checkBox = new JCheckBox(row + ": " + MidiUtils.ccNames[row],
802
                        midiFilter.getControlChange(row));
803
 
804
                checkBox.setEnabled(midiFilter.isVoiceMessages()
805
                        && midiFilter
806
                                .getVoiceMessage(ShortMessage.CONTROL_CHANGE));
807
 
808
            } else {
809
 
810
                checkBox = new JCheckBox("Channel " + (row + 1), midiFilter
811
                        .getChannel(row));
812
 
813
                checkBox.setEnabled(midiFilter.isVoiceMessages());
814
            }
815
 
816
            checkBox.setOpaque(false);
817
 
818
            return checkBox;
819
        }
820
    }
821
 
822
    class MidiFilterTableModel extends DefaultTableModel {
823
 
824
        public MidiFilterTableModel(Object[] a, int b) {
628 adamjking 825
            super(a, b);
826
        }
709 adamjking 827
 
628 adamjking 828
        public Class getColumnClass(int c) {
829
            return getValueAt(0, c).getClass();
830
        }
831
 
703 adamjking 832
        public Object getValueAt(int row, int column) {
709 adamjking 833
 
834
            if (column == 0) {
835
 
836
                if (getRowCount() == 128) {
837
 
713 adamjking 838
                    return new Boolean(midiFilter.getControlChange(row));
709 adamjking 839
                } else {
840
 
724 adamjking 841
                    return new Boolean(midiFilter.getChannel(row));
709 adamjking 842
                }
843
            }
844
 
845
            return super.getValueAt(row, column);
846
        }
847
 
848
        public void setValueAt(Object value, int row, int column) {
714 adamjking 849
 
703 adamjking 850
            if (column == 0) {
709 adamjking 851
 
852
                if (getRowCount() == 128) {
853
 
714 adamjking 854
                    midiFilter.setControlChange(row, ((Boolean) value)
855
                            .booleanValue());
709 adamjking 856
                } else {
857
 
738 adamjking 858
                    midiFilter
859
                            .setChannel(row, ((Boolean) value).booleanValue());
709 adamjking 860
                }
703 adamjking 861
            } else {
714 adamjking 862
 
709 adamjking 863
                super.setValueAt(value, row, column);
703 adamjking 864
            }
865
        }
866
 
628 adamjking 867
        public boolean isCellEditable(int row, int col) {
714 adamjking 868
 
709 adamjking 869
            if (col == 0) {
714 adamjking 870
                return true;
871
            }
709 adamjking 872
            return false;
628 adamjking 873
        }
874
    }
875
}