Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
628 adamjking 2
 * @(#)MidiDeviceRoutingGUI.java    beta8   2006/04/23
213 tk 3
 *
628 adamjking 4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
213 tk 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
 
628 adamjking 23
import java.awt.BasicStroke;
213 tk 24
import java.awt.BorderLayout;
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.awt.Dimension;
628 adamjking 28
import java.awt.Graphics;
29
import java.awt.Graphics2D;
213 tk 30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.Insets;
628 adamjking 33
import java.awt.Point;
34
import java.awt.Rectangle;
35
import java.awt.RenderingHints;
36
import java.awt.datatransfer.DataFlavor;
37
import java.awt.datatransfer.Transferable;
213 tk 38
import java.awt.event.ActionEvent;
39
import java.awt.event.ActionListener;
628 adamjking 40
import java.awt.event.MouseEvent;
41
import java.awt.event.MouseListener;
42
import java.awt.event.WindowAdapter;
43
import java.awt.event.WindowEvent;
213 tk 44
import java.util.Hashtable;
45
import java.util.Iterator;
46
import java.util.Observable;
47
import java.util.Observer;
628 adamjking 48
import java.util.Vector;
213 tk 49
 
50
import javax.sound.midi.MidiDevice;
51
import javax.swing.BorderFactory;
52
import javax.swing.DefaultListCellRenderer;
53
import javax.swing.DefaultListModel;
54
import javax.swing.Icon;
628 adamjking 55
import javax.swing.JButton;
56
import javax.swing.JCheckBoxMenuItem;
57
import javax.swing.JComponent;
58
import javax.swing.JDialog;
213 tk 59
import javax.swing.JLabel;
60
import javax.swing.JList;
628 adamjking 61
import javax.swing.JMenu;
62
import javax.swing.JMenuItem;
213 tk 63
import javax.swing.JPanel;
628 adamjking 64
import javax.swing.JPopupMenu;
213 tk 65
import javax.swing.JScrollPane;
628 adamjking 66
import javax.swing.JToggleButton;
213 tk 67
import javax.swing.ListSelectionModel;
68
import javax.swing.SwingConstants;
69
import javax.swing.event.ChangeEvent;
70
import javax.swing.event.ChangeListener;
71
import javax.swing.event.ListSelectionEvent;
72
import javax.swing.event.ListSelectionListener;
73
 
74
import org.midibox.midi.MidiDeviceRouting;
628 adamjking 75
import org.midibox.utils.gui.DialogOwner;
213 tk 76
import org.midibox.utils.gui.GuiUtils;
77
import org.midibox.utils.gui.ImageLoader;
628 adamjking 78
import org.midibox.utils.gui.SimpleTransferHandler;
213 tk 79
 
628 adamjking 80
public class MidiDeviceRoutingGUI extends JPanel implements MouseListener,
81
        ChangeListener, ListSelectionListener, ActionListener, Observer {
213 tk 82
 
83
    private MidiDeviceRouting midiDeviceRouting;
84
 
85
    private static Icon openIcon = ImageLoader.getImageIcon("midiPortOpen.png");
86
 
628 adamjking 87
    private static Icon closedIcon = ImageLoader
88
            .getImageIcon("midiPortClosed.png");
213 tk 89
 
90
    private Hashtable icons = new Hashtable();
91
 
92
    private DefaultListModel midiReadDevicesListModel;
93
 
94
    private JList midiReadDevicesList;
95
 
96
    private JScrollPane midiReadDevicesScroller;
97
 
98
    private DefaultListModel midiWriteDevicesListModel;
99
 
100
    private JList midiWriteDevicesList;
101
 
102
    private JScrollPane midiWriteDevicesScroller;
103
 
628 adamjking 104
    private JPopupMenu connectionMenu;
105
 
106
    private JButton connectButton;
107
 
108
    private JButton disconnectButton;
109
 
110
    private JButton disconnectallButton;
111
 
112
    private JToggleButton releaseButton;
113
 
114
    private JPanel wirePanel;
115
 
116
    /*
117
     * private JDialog midiMapManagerDialog;
118
     */
119
 
120
    private Hashtable midiDevicePropertiesDialogs;
121
 
213 tk 122
    public MidiDeviceRoutingGUI(MidiDeviceRouting midiDeviceRouting) {
123
        super(new BorderLayout());
124
 
125
        this.midiDeviceRouting = midiDeviceRouting;
126
        midiDeviceRouting.addObserver(this);
127
 
628 adamjking 128
        midiDevicePropertiesDialogs = new Hashtable();
129
 
130
        addMouseListener(this);
131
 
213 tk 132
        midiReadDevicesListModel = new DefaultListModel();
133
        midiWriteDevicesListModel = new DefaultListModel();
134
 
135
        midiReadDevicesList = new JList(midiReadDevicesListModel);
628 adamjking 136
        midiReadDevicesList
137
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
213 tk 138
        midiReadDevicesList.setLayoutOrientation(JList.VERTICAL);
628 adamjking 139
        midiReadDevicesList.setVisibleRowCount(6);
213 tk 140
        midiReadDevicesList.setCellRenderer(new MyListCellRenderer());
628 adamjking 141
        midiReadDevicesList.addMouseListener(this);
213 tk 142
        midiReadDevicesList.setBackground(Color.WHITE);
143
        midiWriteDevicesList = new JList(midiWriteDevicesListModel);
628 adamjking 144
        midiWriteDevicesList
145
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
213 tk 146
        midiWriteDevicesList.setLayoutOrientation(JList.VERTICAL);
628 adamjking 147
        midiWriteDevicesList.setVisibleRowCount(6);
213 tk 148
        midiWriteDevicesList.setCellRenderer(new MyListCellRenderer());
628 adamjking 149
        midiWriteDevicesList.addMouseListener(this);
213 tk 150
        midiWriteDevicesList.setBackground(Color.WHITE);
151
 
152
        JPanel listPane = new JPanel(new GridBagLayout());
153
 
154
        GridBagConstraints gbc = new GridBagConstraints();
155
        gbc.gridx = 1;
156
        gbc.gridy = 1;
157
        gbc.gridwidth = 1;
158
        gbc.gridheight = 1;
159
        gbc.insets = new Insets(2, 0, 2, 0);
160
        gbc.fill = GridBagConstraints.HORIZONTAL;
161
        gbc.weightx = 1.0;
162
        gbc.weighty = 0.0;
163
 
164
        listPane.setOpaque(false);
165
 
628 adamjking 166
        listPane.add(new JLabel("MIDI Devices - Readable",
167
                SwingConstants.CENTER), gbc);
213 tk 168
        gbc.gridy++;
169
 
170
        midiReadDevicesScroller = new JScrollPane(midiReadDevicesList);
171
        midiReadDevicesScroller.getViewport().addChangeListener(this);
172
        midiReadDevicesScroller
173
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
174
        midiReadDevicesScroller
175
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
176
 
177
        gbc.fill = GridBagConstraints.BOTH;
178
        gbc.weighty = 1.0;
179
        gbc.weighty = 1.0;
180
 
181
        listPane.add(midiReadDevicesScroller, gbc);
182
        gbc.gridx++;
183
 
628 adamjking 184
        wirePanel = new JPanel() {
185
 
186
            public void paintComponent(Graphics g) {
187
                super.paintComponent(g);
188
                Graphics2D g2d = (Graphics2D) g;
189
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
190
                        RenderingHints.VALUE_ANTIALIAS_ON);
191
 
192
                Iterator it = getMidiDeviceRouting().getMidiReadDevices()
193
                        .iterator();
194
 
195
                int i = 0;
196
 
197
                while (it.hasNext()) {
198
 
199
                    MidiDevice transmittingDevice = (MidiDevice) it.next();
200
 
201
                    Iterator it2 = getMidiDeviceRouting().getMidiWriteDevices()
202
                            .iterator();
203
 
204
                    int o = 0;
205
                    while (it2.hasNext()) {
206
                        MidiDevice receivingDevice = (MidiDevice) it2.next();
207
 
208
                        int connected = getMidiDeviceRouting()
209
                                .devicesConnected(transmittingDevice,
210
                                        receivingDevice);
211
 
212
                        if (connected != MidiDeviceRouting.DISCONNECTED) {
213
 
214
                            Rectangle rec1 = midiReadDevicesList.getCellBounds(
215
                                    i, i);
216
                            Point p1 = new Point(0,
217
                                    ((rec1.height / 2) + rec1.y)
218
                                            + midiReadDevicesScroller.getY()
219
                                            - midiReadDevicesScroller
220
                                                    .getVerticalScrollBar()
221
                                                    .getValue()
222
                                            - wirePanel.getY());
223
 
224
                            Rectangle rec2 = midiWriteDevicesList
225
                                    .getCellBounds(o, o);
226
                            Point p2 = new Point(getWidth(),
227
                                    ((rec2.height / 2) + rec2.y)
228
                                            + midiReadDevicesScroller.getY()
229
                                            - midiWriteDevicesScroller
230
                                                    .getVerticalScrollBar()
231
                                                    .getValue()
232
                                            - wirePanel.getY());
233
 
234
                            if (connected == MidiDeviceRouting.LOGICALLY_CONNECTED) {
235
                                g2d.setStroke(new BasicStroke(2f,
236
                                        BasicStroke.CAP_BUTT,
237
                                        BasicStroke.JOIN_ROUND, 1f,
238
                                        new float[] { 3f, 6f }, 0f));
239
                            } else {
240
                                g2d.setStroke(new BasicStroke(2));
241
                            }
242
                            g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
243
                        }
244
                        o++;
245
                    }
246
                    i++;
247
                }
248
            }
249
        };
250
 
251
        wirePanel.setOpaque(false);
252
 
253
        listPane.add(wirePanel, gbc);
254
 
255
        GuiUtils.setComponentSize(wirePanel, 80, 80);
256
 
213 tk 257
        gbc.gridx++;
258
        gbc.gridy--;
259
 
260
        gbc.fill = GridBagConstraints.HORIZONTAL;
261
        gbc.weightx = 1.0;
262
        gbc.weighty = 0.0;
263
 
628 adamjking 264
        listPane.add(new JLabel("MIDI Devices - Writeable",
213 tk 265
                SwingConstants.CENTER), gbc);
266
        gbc.gridy++;
267
 
268
        midiWriteDevicesScroller = new JScrollPane(midiWriteDevicesList);
269
        midiWriteDevicesScroller
270
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
271
        midiWriteDevicesScroller
272
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
273
        midiWriteDevicesScroller.getViewport().addChangeListener(this);
274
 
275
        gbc.fill = GridBagConstraints.BOTH;
276
        gbc.weightx = 1.0;
277
        gbc.weighty = 1.0;
278
 
279
        listPane.add(midiWriteDevicesScroller, gbc);
280
 
281
        int maxPreferredWidth = Math.max(midiReadDevicesScroller
282
                .getPreferredSize().width, midiWriteDevicesScroller
283
                .getPreferredSize().width);
284
        int maxPreferredHeight = Math.max(midiReadDevicesScroller
285
                .getPreferredSize().height, midiWriteDevicesScroller
286
                .getPreferredSize().height);
287
 
288
        GuiUtils.setComponentSize(midiReadDevicesScroller, maxPreferredWidth,
289
                maxPreferredHeight);
290
        GuiUtils.setComponentSize(midiWriteDevicesScroller, maxPreferredWidth,
291
                maxPreferredHeight);
292
 
628 adamjking 293
        midiReadDevicesList.setDragEnabled(true);
294
        midiWriteDevicesList.setDragEnabled(true);
295
        midiReadDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
296
                false));
297
        midiWriteDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
298
                true));
213 tk 299
        midiReadDevicesList.addListSelectionListener(this);
300
        midiWriteDevicesList.addListSelectionListener(this);
301
 
302
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
303
        listPane.setOpaque(false);
294 rutgerv 304
        add(listPane, BorderLayout.CENTER);
628 adamjking 305
 
306
        JPanel buttonPane = new JPanel(new GridBagLayout());
307
        gbc = new GridBagConstraints();
308
 
309
        gbc.gridx = 0;
310
        gbc.gridy = 0;
311
        gbc.gridwidth = 1;
312
        gbc.fill = GridBagConstraints.BOTH;
313
 
641 adamjking 314
        gbc.insets = new Insets(5, 5, 10, 5);
628 adamjking 315
 
316
        connectButton = new JButton("Connect");
317
        connectButton.setToolTipText("Connect Selected Devices");
318
        connectButton.addActionListener(this);
319
        buttonPane.add(connectButton, gbc);
320
        gbc.gridx++;
321
 
322
        disconnectButton = new JButton("Disconnect");
323
        disconnectButton.setToolTipText("Disconnect Selected Devices");
324
        disconnectButton.addActionListener(this);
325
        buttonPane.add(disconnectButton, gbc);
326
        gbc.gridx++;
327
 
328
        disconnectallButton = new JButton("Disconnect All");
329
        disconnectallButton.setToolTipText("Disconnect All Devices");
330
        disconnectallButton.addActionListener(this);
331
        buttonPane.add(disconnectallButton, gbc);
641 adamjking 332
        gbc.gridx++;
628 adamjking 333
 
334
        releaseButton = new JToggleButton("Release Ports", midiDeviceRouting
721 adamjking 335
                .isPortsReleased());
628 adamjking 336
        releaseButton.setToolTipText("Release MIDI Ports");
337
        releaseButton.addActionListener(this);
338
        buttonPane.add(releaseButton, gbc);
339
 
340
        buttonPane.setOpaque(false);
341
        add(buttonPane, BorderLayout.SOUTH);
213 tk 342
        populateTrees();
343
    }
344
 
345
    public MidiDeviceRouting getMidiDeviceRouting() {
346
        return midiDeviceRouting;
347
    }
348
 
628 adamjking 349
    public void addMidiDeviceIcon(Object object, Icon icon) {
350
        icons.put(object, icon);
351
    }
352
 
213 tk 353
    public Icon getMidiDeviceIcon(MidiDevice md) {
354
        if (icons.containsKey(md)) {
355
            return (Icon) icons.get(md);
356
        }
357
 
358
        if (icons.containsKey(md.getClass())) {
359
            return (Icon) icons.get(md.getClass());
360
        }
361
 
362
        try {
363
            if (md.isOpen()) {
364
                return openIcon;
365
            }
366
 
367
            return closedIcon;
368
 
369
        } catch (Exception e) {
370
            return closedIcon;
371
        }
372
    }
373
 
374
    public void redrawAll() {
375
        midiReadDevicesList.repaint();
628 adamjking 376
        midiWriteDevicesList.repaint();
377
        wirePanel.repaint();
378
        setButtonStates();
213 tk 379
    }
380
 
628 adamjking 381
    private void buildConnectionMenu(boolean output) {
382
 
383
        final MidiDevice sourceDevice = (MidiDevice) ((output) ? midiDeviceRouting
384
                .getMidiWriteDevices()
385
                : midiDeviceRouting.getMidiReadDevices())
386
                .elementAt(((output) ? midiWriteDevicesList
387
                        : midiReadDevicesList).getSelectedIndex());
388
        final Vector targetDevices = ((output) ? midiDeviceRouting
389
                .getMidiReadDevices() : midiDeviceRouting.getMidiWriteDevices());
390
 
391
        connectionMenu = new JPopupMenu(sourceDevice.getDeviceInfo().getName());
392
 
393
        JMenu connectMenu = new JMenu("Connect to ...");
394
 
395
        connectionMenu.add(connectMenu);
396
 
397
        connectionMenu.setLabel(sourceDevice.getDeviceInfo().getName());
398
 
399
        Iterator it = targetDevices.iterator();
400
 
401
        while (it.hasNext()) {
402
 
403
            MidiDevice targetDevice = (MidiDevice) it.next();
404
 
405
            final MidiDevice transmittingDevice = ((output) ? targetDevice
406
                    : sourceDevice);
407
            final MidiDevice receivingDevice = ((output) ? sourceDevice
408
                    : targetDevice);
409
            final JCheckBoxMenuItem connect = new JCheckBoxMenuItem(
410
                    targetDevice.getDeviceInfo().getName(),
411
                    midiDeviceRouting.devicesConnected(transmittingDevice,
412
                            receivingDevice) != MidiDeviceRouting.DISCONNECTED);
413
 
414
            connect.addActionListener(new ActionListener() {
415
                public void actionPerformed(ActionEvent ae) {
416
                    if (connect.isSelected()) {
417
                        midiDeviceRouting.connectDevices(transmittingDevice,
418
                                receivingDevice);
419
                    } else {
420
                        midiDeviceRouting.disconnectDevices(transmittingDevice,
421
                                receivingDevice);
422
                    }
423
                }
424
            });
425
            connectMenu.add(connect);
426
        }
427
 
428
        JMenuItem properties = new JMenuItem("MIDI Device Properties");
429
        properties.addActionListener(new ActionListener() {
430
            public void actionPerformed(ActionEvent ae) {
431
                showMidiDevicePropertyDialog(sourceDevice);
432
            }
433
        });
434
        connectionMenu.add(properties);
435
    }
436
 
437
    private void showMidiDevicePropertyDialog(MidiDevice midiDeviceIn) {
438
 
439
        final MidiDevice midiDevice = midiDeviceIn;
440
 
441
        if (!midiDevicePropertiesDialogs.containsKey(midiDevice)) {
442
 
443
            final MidiDeviceProperties midiDeviceProperties = new MidiDeviceProperties(
444
                    midiDevice);
445
 
446
            JDialog midiDevicePropertiesDialog = new JDialog(DialogOwner
447
                    .getFrame(), midiDevice.getDeviceInfo().getName(), false);
448
            midiDevicePropertiesDialog.setContentPane(midiDeviceProperties);
449
            midiDevicePropertiesDialog.pack();
450
            midiDevicePropertiesDialog.setLocationRelativeTo(this);
451
            midiDevicePropertiesDialog.setVisible(true);
452
 
453
            midiDevicePropertiesDialogs.put(midiDevice,
454
                    midiDevicePropertiesDialog);
455
 
456
            midiDevicePropertiesDialog.addWindowListener(new WindowAdapter() {
457
                public void windowClosing(WindowEvent we) {
458
                    midiDevicePropertiesDialogs.remove(midiDevice);
459
                }
460
            });
461
        }
462
        ((JDialog) midiDevicePropertiesDialogs.get(midiDevice)).requestFocus();
463
    }
464
 
213 tk 465
    private void populateTrees() {
628 adamjking 466
 
213 tk 467
        midiReadDevicesListModel.removeAllElements();
628 adamjking 468
 
213 tk 469
        Iterator it = midiDeviceRouting.getMidiReadDevices().iterator();
470
 
471
        while (it.hasNext()) {
472
            midiReadDevicesListModel.addElement(((MidiDevice) it.next())
473
                    .getDeviceInfo().getName());
474
        }
628 adamjking 475
 
213 tk 476
        midiReadDevicesList.setSelectedIndex(0);
628 adamjking 477
 
213 tk 478
        midiWriteDevicesListModel.removeAllElements();
628 adamjking 479
 
213 tk 480
        it = midiDeviceRouting.getMidiWriteDevices().iterator();
628 adamjking 481
 
213 tk 482
        while (it.hasNext()) {
483
            midiWriteDevicesListModel.addElement(((MidiDevice) it.next())
484
                    .getDeviceInfo().getName());
485
        }
628 adamjking 486
 
213 tk 487
        midiWriteDevicesList.setSelectedIndex(0);
628 adamjking 488
 
213 tk 489
        updateUI();
490
    }
491
 
628 adamjking 492
    private void setButtonStates() {
493
 
494
        int[] selectedReadIndices = midiReadDevicesList.getSelectedIndices();
495
        int[] selectedWriteIndices = midiWriteDevicesList.getSelectedIndices();
496
 
497
        connectButton.setEnabled(false);
498
        disconnectButton.setEnabled(false);
721 adamjking 499
        releaseButton.setSelected(midiDeviceRouting.isPortsReleased());
628 adamjking 500
 
501
        if (selectedReadIndices.length > 0 && selectedWriteIndices.length > 0) {
502
 
503
            for (int r = 0; r < selectedReadIndices.length; r++) {
504
 
505
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
506
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
507
 
508
                for (int w = 0; w < selectedWriteIndices.length; w++) {
509
 
510
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
511
                            .getMidiWriteDevices().elementAt(
512
                                    selectedWriteIndices[w]);
513
 
514
                    if (midiDeviceRouting.devicesConnected(transmittingDevice,
515
                            receivingDevice) != MidiDeviceRouting.DISCONNECTED) {
516
                        disconnectButton.setEnabled(true);
517
                    } else {
518
                        connectButton.setEnabled(true);
519
                    }
520
                }
521
            }
522
        }
523
    }
524
 
213 tk 525
    public void valueChanged(ListSelectionEvent lse) {
628 adamjking 526
        setButtonStates();
527
    }
528
 
529
    public void mouseClicked(MouseEvent me) {
530
 
531
    }
532
 
533
    public void mouseReleased(MouseEvent me) {
534
        Object source = me.getSource();
535
 
536
        if (source == midiReadDevicesList || source == midiWriteDevicesList) {
537
            JList source2 = (JList) source;
538
            if (me.getButton() == MouseEvent.BUTTON3) {
539
                int index = source2.locationToIndex(me.getPoint());
540
                source2.setSelectedIndex(index);
541
                buildConnectionMenu((source == midiWriteDevicesList) ? true
542
                        : false);
543
                connectionMenu.show(source2, me.getX(), me.getY());
213 tk 544
            }
628 adamjking 545
        }
213 tk 546
    }
547
 
628 adamjking 548
    public void mousePressed(MouseEvent me) {
549
 
550
    }
551
 
552
    public void mouseEntered(MouseEvent me) {
553
 
554
    }
555
 
556
    public void mouseExited(MouseEvent me) {
557
 
558
    }
559
 
213 tk 560
    public void stateChanged(ChangeEvent ce) {
628 adamjking 561
        wirePanel.repaint();
213 tk 562
    }
563
 
564
    public Dimension getMinimumSize() {
565
        return getPreferredSize();
566
    }
567
 
568
    public void update(Observable observable, Object object) {
569
        if (observable == midiDeviceRouting) {
628 adamjking 570
            if (object == midiDeviceRouting.getMidiReadDevices()
571
                    || object == midiDeviceRouting.getMidiWriteDevices()) {
213 tk 572
                populateTrees();
573
            }
557 rutgerv 574
            redrawAll();
213 tk 575
        }
576
    }
577
 
578
    public void actionPerformed(ActionEvent ae) {
628 adamjking 579
        Object source = ae.getSource();
580
 
581
        if (source == connectButton) {
582
 
583
            int[] selectedReadIndices = midiReadDevicesList
584
                    .getSelectedIndices();
585
            int[] selectedWriteIndices = midiWriteDevicesList
586
                    .getSelectedIndices();
587
 
588
            for (int r = 0; r < selectedReadIndices.length; r++) {
589
 
590
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
591
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
592
 
593
                for (int w = 0; w < selectedWriteIndices.length; w++) {
594
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
595
                            .getMidiWriteDevices().elementAt(
596
                                    selectedWriteIndices[w]);
597
 
598
                    midiDeviceRouting.connectDevices(transmittingDevice,
599
                            receivingDevice);
600
                }
601
 
602
            }
603
        } else if (source == disconnectButton) {
604
 
605
            int[] selectedReadIndices = midiReadDevicesList
606
                    .getSelectedIndices();
607
            int[] selectedWriteIndices = midiWriteDevicesList
608
                    .getSelectedIndices();
609
 
610
            for (int r = 0; r < selectedReadIndices.length; r++) {
611
 
612
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
613
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
614
 
615
                for (int w = 0; w < selectedWriteIndices.length; w++) {
616
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
617
                            .getMidiWriteDevices().elementAt(
618
                                    selectedWriteIndices[w]);
619
 
620
                    midiDeviceRouting.disconnectDevices(transmittingDevice,
621
                            receivingDevice);
622
                }
623
            }
624
        } else if (source == disconnectallButton) {
625
            midiDeviceRouting.disconnectAll();
641 adamjking 626
        } else if (source == releaseButton) {
628 adamjking 627
            midiDeviceRouting.setPortsReleased(releaseButton.isSelected());
294 rutgerv 628
        }
213 tk 629
    }
630
 
631
    public class MyListCellRenderer extends DefaultListCellRenderer {
632
        public Component getListCellRendererComponent(JList list, Object value,
633
                int index, boolean isSelected, boolean hasFocus) {
634
 
635
            super.getListCellRendererComponent(list, value, index, isSelected,
636
                    hasFocus);
637
 
638
            setIcon(getMidiDeviceIcon(((MidiDevice) ((list == midiWriteDevicesList) ? midiDeviceRouting
639
                    .getMidiWriteDevices()
640
                    : midiDeviceRouting.getMidiReadDevices()).elementAt(index))));
641
 
642
            return this;
643
        }
628 adamjking 644
    }
645
 
646
    public class MidiDevicesTransferHandler extends SimpleTransferHandler {
647
 
648
        private boolean output;
649
 
650
        public MidiDevicesTransferHandler(boolean output) {
651
            super();
652
            this.output = output;
653
            dropOnSelf = false;
654
            supportedDataFlavors = new DataFlavor[] { MidiDeviceTransferable.LOCAL_JVM_MIDI_DEVICE_FLAVOR };
655
        }
656
 
657
        public boolean importData(JComponent c, Transferable t) {
658
 
659
            if (canImport(c, supportedDataFlavors)) {
660
 
661
                int[] indices = ((output) ? midiWriteDevicesList
662
                        : midiReadDevicesList).getSelectedIndices();
663
 
664
                for (int d = 0; d < indices.length; d++) {
665
 
666
                    MidiDevice localDevice = (MidiDevice) ((output) ? midiDeviceRouting
667
                            .getMidiWriteDevices()
668
                            : midiDeviceRouting.getMidiReadDevices())
669
                            .elementAt(indices[d]);
670
 
671
                    try {
672
                        MidiDevice[] remoteDevices = (MidiDevice[]) t
673
                                .getTransferData(MidiDeviceTransferable.LOCAL_JVM_MIDI_DEVICE_FLAVOR);
674
 
675
                        for (int d2 = 0; d2 < remoteDevices.length; d2++) {
676
 
677
                            MidiDevice remoteDevice = remoteDevices[d2];
678
 
679
                            if (output) {
680
                                midiDeviceRouting.connectDevices(remoteDevice,
681
                                        localDevice);
682
                            } else {
683
                                midiDeviceRouting.connectDevices(localDevice,
684
                                        remoteDevice);
685
                            }
686
                        }
687
                    } catch (Exception e) {
688
                        return false;
689
                    }
690
                }
691
                return true;
692
            }
693
            return false;
694
        }
695
 
696
        protected Transferable createTransferable(JComponent c) {
697
            justExported = true;
698
            int[] indices = ((output) ? midiWriteDevicesList
699
                    : midiReadDevicesList).getSelectedIndices();
700
 
701
            MidiDevice[] midiDevices = new MidiDevice[indices.length];
702
 
703
            for (int d = 0; d < midiDevices.length; d++) {
704
                midiDevices[d] = (MidiDevice) ((output) ? midiDeviceRouting
705
                        .getMidiWriteDevices() : midiDeviceRouting
706
                        .getMidiReadDevices()).elementAt(indices[d]);
707
            }
708
            return new MidiDeviceTransferable(midiDevices);
709
        }
710
    }
213 tk 711
}