Subversion Repositories svn.mios

Rev

Rev 557 | Rev 641 | 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.midi.MidiFilterDevice;
76
import org.midibox.midi.MidiMapDevice;
77
import org.midibox.midi.MidiRouterDevice;
78
import org.midibox.utils.gui.DialogOwner;
213 tk 79
import org.midibox.utils.gui.GuiUtils;
80
import org.midibox.utils.gui.ImageLoader;
628 adamjking 81
import org.midibox.utils.gui.SimpleTransferHandler;
213 tk 82
 
628 adamjking 83
public class MidiDeviceRoutingGUI extends JPanel implements MouseListener,
84
        ChangeListener, ListSelectionListener, ActionListener, Observer {
213 tk 85
 
86
    private MidiDeviceRouting midiDeviceRouting;
87
 
88
    private static Icon openIcon = ImageLoader.getImageIcon("midiPortOpen.png");
89
 
628 adamjking 90
    private static Icon closedIcon = ImageLoader
91
            .getImageIcon("midiPortClosed.png");
213 tk 92
 
93
    private Hashtable icons = new Hashtable();
94
 
95
    private DefaultListModel midiReadDevicesListModel;
96
 
97
    private JList midiReadDevicesList;
98
 
99
    private JScrollPane midiReadDevicesScroller;
100
 
101
    private DefaultListModel midiWriteDevicesListModel;
102
 
103
    private JList midiWriteDevicesList;
104
 
105
    private JScrollPane midiWriteDevicesScroller;
106
 
628 adamjking 107
    private JPopupMenu connectionMenu;
108
 
109
    private JButton connectButton;
110
 
111
    private JButton disconnectButton;
112
 
113
    private JButton disconnectallButton;
114
 
115
    private JButton midiDeviceManagerButton;
116
 
117
    private JButton midiRouterButton;
118
 
119
    private JButton filterButton;
120
 
121
    /*
122
     * private JButton mapButton;
123
     */
124
 
125
    private JToggleButton releaseButton;
126
 
127
    private JPanel wirePanel;
128
 
129
    private JDialog midiDeviceManagerDialog;
130
 
131
    private JDialog midiRouterManagerGUI;
132
 
133
    private JDialog midiFilterManagerDialog;
134
 
135
    /*
136
     * private JDialog midiMapManagerDialog;
137
     */
138
 
139
    private Hashtable midiDevicePropertiesDialogs;
140
 
213 tk 141
    public MidiDeviceRoutingGUI(MidiDeviceRouting midiDeviceRouting) {
142
        super(new BorderLayout());
143
 
144
        this.midiDeviceRouting = midiDeviceRouting;
145
        midiDeviceRouting.addObserver(this);
146
 
628 adamjking 147
        midiDevicePropertiesDialogs = new Hashtable();
148
 
149
        addMouseListener(this);
150
 
151
        addMidiDeviceIcon(MidiRouterDevice.class, ImageLoader
152
                .getImageIcon("virtualMidiDevice.png"));
153
        addMidiDeviceIcon(MidiFilterDevice.class, ImageLoader
154
                .getImageIcon("filter.png"));
155
        addMidiDeviceIcon(MidiMapDevice.class, ImageLoader
156
                .getImageIcon("map.png"));
157
 
213 tk 158
        midiReadDevicesListModel = new DefaultListModel();
159
        midiWriteDevicesListModel = new DefaultListModel();
160
 
161
        midiReadDevicesList = new JList(midiReadDevicesListModel);
628 adamjking 162
        midiReadDevicesList
163
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
213 tk 164
        midiReadDevicesList.setLayoutOrientation(JList.VERTICAL);
628 adamjking 165
        midiReadDevicesList.setVisibleRowCount(6);
213 tk 166
        midiReadDevicesList.setCellRenderer(new MyListCellRenderer());
628 adamjking 167
        midiReadDevicesList.addMouseListener(this);
213 tk 168
        midiReadDevicesList.setBackground(Color.WHITE);
169
        midiWriteDevicesList = new JList(midiWriteDevicesListModel);
628 adamjking 170
        midiWriteDevicesList
171
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
213 tk 172
        midiWriteDevicesList.setLayoutOrientation(JList.VERTICAL);
628 adamjking 173
        midiWriteDevicesList.setVisibleRowCount(6);
213 tk 174
        midiWriteDevicesList.setCellRenderer(new MyListCellRenderer());
628 adamjking 175
        midiWriteDevicesList.addMouseListener(this);
213 tk 176
        midiWriteDevicesList.setBackground(Color.WHITE);
177
 
178
        JPanel listPane = new JPanel(new GridBagLayout());
179
 
180
        GridBagConstraints gbc = new GridBagConstraints();
181
        gbc.gridx = 1;
182
        gbc.gridy = 1;
183
        gbc.gridwidth = 1;
184
        gbc.gridheight = 1;
185
        gbc.insets = new Insets(2, 0, 2, 0);
186
        gbc.fill = GridBagConstraints.HORIZONTAL;
187
        gbc.weightx = 1.0;
188
        gbc.weighty = 0.0;
189
 
190
        listPane.setOpaque(false);
191
 
628 adamjking 192
        listPane.add(new JLabel("MIDI Devices - Readable",
193
                SwingConstants.CENTER), gbc);
213 tk 194
        gbc.gridy++;
195
 
196
        midiReadDevicesScroller = new JScrollPane(midiReadDevicesList);
197
        midiReadDevicesScroller.getViewport().addChangeListener(this);
198
        midiReadDevicesScroller
199
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
200
        midiReadDevicesScroller
201
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
202
 
203
        gbc.fill = GridBagConstraints.BOTH;
204
        gbc.weighty = 1.0;
205
        gbc.weighty = 1.0;
206
 
207
        listPane.add(midiReadDevicesScroller, gbc);
208
        gbc.gridx++;
209
 
628 adamjking 210
        wirePanel = new JPanel() {
211
 
212
            public void paintComponent(Graphics g) {
213
                super.paintComponent(g);
214
                Graphics2D g2d = (Graphics2D) g;
215
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
216
                        RenderingHints.VALUE_ANTIALIAS_ON);
217
 
218
                Iterator it = getMidiDeviceRouting().getMidiReadDevices()
219
                        .iterator();
220
 
221
                int i = 0;
222
 
223
                while (it.hasNext()) {
224
 
225
                    MidiDevice transmittingDevice = (MidiDevice) it.next();
226
 
227
                    Iterator it2 = getMidiDeviceRouting().getMidiWriteDevices()
228
                            .iterator();
229
 
230
                    int o = 0;
231
                    while (it2.hasNext()) {
232
                        MidiDevice receivingDevice = (MidiDevice) it2.next();
233
 
234
                        int connected = getMidiDeviceRouting()
235
                                .devicesConnected(transmittingDevice,
236
                                        receivingDevice);
237
 
238
                        if (connected != MidiDeviceRouting.DISCONNECTED) {
239
 
240
                            Rectangle rec1 = midiReadDevicesList.getCellBounds(
241
                                    i, i);
242
                            Point p1 = new Point(0,
243
                                    ((rec1.height / 2) + rec1.y)
244
                                            + midiReadDevicesScroller.getY()
245
                                            - midiReadDevicesScroller
246
                                                    .getVerticalScrollBar()
247
                                                    .getValue()
248
                                            - wirePanel.getY());
249
 
250
                            Rectangle rec2 = midiWriteDevicesList
251
                                    .getCellBounds(o, o);
252
                            Point p2 = new Point(getWidth(),
253
                                    ((rec2.height / 2) + rec2.y)
254
                                            + midiReadDevicesScroller.getY()
255
                                            - midiWriteDevicesScroller
256
                                                    .getVerticalScrollBar()
257
                                                    .getValue()
258
                                            - wirePanel.getY());
259
 
260
                            if (connected == MidiDeviceRouting.LOGICALLY_CONNECTED) {
261
                                g2d.setStroke(new BasicStroke(2f,
262
                                        BasicStroke.CAP_BUTT,
263
                                        BasicStroke.JOIN_ROUND, 1f,
264
                                        new float[] { 3f, 6f }, 0f));
265
                            } else {
266
                                g2d.setStroke(new BasicStroke(2));
267
                            }
268
                            g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
269
                        }
270
                        o++;
271
                    }
272
                    i++;
273
                }
274
            }
275
        };
276
 
277
        wirePanel.setOpaque(false);
278
 
279
        listPane.add(wirePanel, gbc);
280
 
281
        GuiUtils.setComponentSize(wirePanel, 80, 80);
282
 
213 tk 283
        gbc.gridx++;
284
        gbc.gridy--;
285
 
286
        gbc.fill = GridBagConstraints.HORIZONTAL;
287
        gbc.weightx = 1.0;
288
        gbc.weighty = 0.0;
289
 
628 adamjking 290
        listPane.add(new JLabel("MIDI Devices - Writeable",
213 tk 291
                SwingConstants.CENTER), gbc);
292
        gbc.gridy++;
293
 
294
        midiWriteDevicesScroller = new JScrollPane(midiWriteDevicesList);
295
        midiWriteDevicesScroller
296
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
297
        midiWriteDevicesScroller
298
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
299
        midiWriteDevicesScroller.getViewport().addChangeListener(this);
300
 
301
        gbc.fill = GridBagConstraints.BOTH;
302
        gbc.weightx = 1.0;
303
        gbc.weighty = 1.0;
304
 
305
        listPane.add(midiWriteDevicesScroller, gbc);
306
 
307
        int maxPreferredWidth = Math.max(midiReadDevicesScroller
308
                .getPreferredSize().width, midiWriteDevicesScroller
309
                .getPreferredSize().width);
310
        int maxPreferredHeight = Math.max(midiReadDevicesScroller
311
                .getPreferredSize().height, midiWriteDevicesScroller
312
                .getPreferredSize().height);
313
 
314
        GuiUtils.setComponentSize(midiReadDevicesScroller, maxPreferredWidth,
315
                maxPreferredHeight);
316
        GuiUtils.setComponentSize(midiWriteDevicesScroller, maxPreferredWidth,
317
                maxPreferredHeight);
318
 
628 adamjking 319
        midiReadDevicesList.setDragEnabled(true);
320
        midiWriteDevicesList.setDragEnabled(true);
321
        midiReadDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
322
                false));
323
        midiWriteDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
324
                true));
213 tk 325
        midiReadDevicesList.addListSelectionListener(this);
326
        midiWriteDevicesList.addListSelectionListener(this);
327
 
328
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
329
        listPane.setOpaque(false);
294 rutgerv 330
        add(listPane, BorderLayout.CENTER);
628 adamjking 331
 
332
        JPanel buttonPane = new JPanel(new GridBagLayout());
333
        gbc = new GridBagConstraints();
334
 
335
        gbc.gridx = 0;
336
        gbc.gridy = 0;
337
        gbc.gridwidth = 1;
338
        gbc.fill = GridBagConstraints.BOTH;
339
 
340
        gbc.insets = new Insets(5, 5, 5, 5);
341
 
342
        connectButton = new JButton("Connect");
343
        connectButton.setToolTipText("Connect Selected Devices");
344
        connectButton.addActionListener(this);
345
        buttonPane.add(connectButton, gbc);
346
        gbc.gridy++;
347
 
348
        gbc.insets = new Insets(5, 5, 10, 5);
349
 
350
        midiDeviceManagerButton = new JButton("Select Devices");
351
        midiDeviceManagerButton.setToolTipText("Select Hardware Devices");
352
        midiDeviceManagerButton.addActionListener(this);
353
        buttonPane.add(midiDeviceManagerButton, gbc);
354
        gbc.gridx++;
355
        gbc.gridy--;
356
 
357
        gbc.insets = new Insets(5, 5, 5, 5);
358
 
359
        gbc.gridwidth = 2;
360
        disconnectButton = new JButton("Disconnect");
361
        disconnectButton.setToolTipText("Disconnect Selected Devices");
362
        disconnectButton.addActionListener(this);
363
        buttonPane.add(disconnectButton, gbc);
364
        gbc.gridy++;
365
 
366
        gbc.insets = new Insets(5, 5, 10, 5);
367
 
368
        gbc.gridwidth = 1;
369
        midiRouterButton = new JButton(ImageLoader
370
                .getImageIcon("virtualMidiDevice.png"));
371
        midiRouterButton.setToolTipText("Add/Remove Virtual Ports");
372
        midiRouterButton.addActionListener(this);
373
        buttonPane.add(midiRouterButton, gbc);
374
        gbc.gridx++;
375
 
376
        filterButton = new JButton(ImageLoader.getImageIcon("filter.png"));
377
        filterButton.setToolTipText("Add/Remove Filters");
378
        filterButton.addActionListener(this);
379
        buttonPane.add(filterButton, gbc);
380
        gbc.gridx++;
381
 
382
        /*
383
         * gbc.insets = new Insets(5, 4, 10, 5);
384
         *
385
         * mapButton = new JButton(ImageLoader.getImageIcon("map.png"));
386
         * mapButton.setToolTipText("Add/Remove Maps");
387
         * mapButton.addActionListener(this); mapButton.setMargin(new Insets(2,
388
         * 8, 2, 8)); buttonPane.add(mapButton, gbc); gbc.gridx++;
389
         */
390
 
391
        gbc.gridy--;
392
 
393
        gbc.insets = new Insets(5, 5, 5, 5);
394
 
395
        disconnectallButton = new JButton("Disconnect All");
396
        disconnectallButton.setToolTipText("Disconnect All Devices");
397
        disconnectallButton.addActionListener(this);
398
        buttonPane.add(disconnectallButton, gbc);
399
        gbc.gridy++;
400
 
401
        gbc.insets = new Insets(5, 5, 10, 5);
402
 
403
        releaseButton = new JToggleButton("Release Ports", midiDeviceRouting
404
                .getPortsReleased());
405
        releaseButton.setToolTipText("Release MIDI Ports");
406
        releaseButton.addActionListener(this);
407
        buttonPane.add(releaseButton, gbc);
408
 
409
        buttonPane.setOpaque(false);
410
        add(buttonPane, BorderLayout.SOUTH);
213 tk 411
        populateTrees();
412
    }
413
 
414
    public MidiDeviceRouting getMidiDeviceRouting() {
415
        return midiDeviceRouting;
416
    }
417
 
628 adamjking 418
    public void addMidiDeviceIcon(Object object, Icon icon) {
419
        icons.put(object, icon);
420
    }
421
 
213 tk 422
    public Icon getMidiDeviceIcon(MidiDevice md) {
423
        if (icons.containsKey(md)) {
424
            return (Icon) icons.get(md);
425
        }
426
 
427
        if (icons.containsKey(md.getClass())) {
428
            return (Icon) icons.get(md.getClass());
429
        }
430
 
431
        try {
432
            if (md.isOpen()) {
433
                return openIcon;
434
            }
435
 
436
            return closedIcon;
437
 
438
        } catch (Exception e) {
439
            return closedIcon;
440
        }
441
    }
442
 
443
    public void redrawAll() {
444
        midiReadDevicesList.repaint();
628 adamjking 445
        midiWriteDevicesList.repaint();
446
        wirePanel.repaint();
447
        setButtonStates();
213 tk 448
    }
449
 
628 adamjking 450
    private void buildConnectionMenu(boolean output) {
451
 
452
        final MidiDevice sourceDevice = (MidiDevice) ((output) ? midiDeviceRouting
453
                .getMidiWriteDevices()
454
                : midiDeviceRouting.getMidiReadDevices())
455
                .elementAt(((output) ? midiWriteDevicesList
456
                        : midiReadDevicesList).getSelectedIndex());
457
        final Vector targetDevices = ((output) ? midiDeviceRouting
458
                .getMidiReadDevices() : midiDeviceRouting.getMidiWriteDevices());
459
 
460
        connectionMenu = new JPopupMenu(sourceDevice.getDeviceInfo().getName());
461
 
462
        JMenu connectMenu = new JMenu("Connect to ...");
463
 
464
        connectionMenu.add(connectMenu);
465
 
466
        connectionMenu.setLabel(sourceDevice.getDeviceInfo().getName());
467
 
468
        Iterator it = targetDevices.iterator();
469
 
470
        while (it.hasNext()) {
471
 
472
            MidiDevice targetDevice = (MidiDevice) it.next();
473
 
474
            final MidiDevice transmittingDevice = ((output) ? targetDevice
475
                    : sourceDevice);
476
            final MidiDevice receivingDevice = ((output) ? sourceDevice
477
                    : targetDevice);
478
            final JCheckBoxMenuItem connect = new JCheckBoxMenuItem(
479
                    targetDevice.getDeviceInfo().getName(),
480
                    midiDeviceRouting.devicesConnected(transmittingDevice,
481
                            receivingDevice) != MidiDeviceRouting.DISCONNECTED);
482
 
483
            connect.addActionListener(new ActionListener() {
484
                public void actionPerformed(ActionEvent ae) {
485
                    if (connect.isSelected()) {
486
                        midiDeviceRouting.connectDevices(transmittingDevice,
487
                                receivingDevice);
488
                    } else {
489
                        midiDeviceRouting.disconnectDevices(transmittingDevice,
490
                                receivingDevice);
491
                    }
492
                }
493
            });
494
            connectMenu.add(connect);
495
        }
496
 
497
        JMenuItem properties = new JMenuItem("MIDI Device Properties");
498
        properties.addActionListener(new ActionListener() {
499
            public void actionPerformed(ActionEvent ae) {
500
                showMidiDevicePropertyDialog(sourceDevice);
501
            }
502
        });
503
        connectionMenu.add(properties);
504
    }
505
 
506
    private void showMidiDevicePropertyDialog(MidiDevice midiDeviceIn) {
507
 
508
        final MidiDevice midiDevice = midiDeviceIn;
509
 
510
        if (!midiDevicePropertiesDialogs.containsKey(midiDevice)) {
511
 
512
            final MidiDeviceProperties midiDeviceProperties = new MidiDeviceProperties(
513
                    midiDevice);
514
 
515
            JDialog midiDevicePropertiesDialog = new JDialog(DialogOwner
516
                    .getFrame(), midiDevice.getDeviceInfo().getName(), false);
517
            midiDevicePropertiesDialog.setContentPane(midiDeviceProperties);
518
            midiDevicePropertiesDialog.pack();
519
            midiDevicePropertiesDialog.setLocationRelativeTo(this);
520
            midiDevicePropertiesDialog.setVisible(true);
521
 
522
            midiDevicePropertiesDialogs.put(midiDevice,
523
                    midiDevicePropertiesDialog);
524
 
525
            midiDevicePropertiesDialog.addWindowListener(new WindowAdapter() {
526
                public void windowClosing(WindowEvent we) {
527
                    midiDevicePropertiesDialogs.remove(midiDevice);
528
                }
529
            });
530
        }
531
        ((JDialog) midiDevicePropertiesDialogs.get(midiDevice)).requestFocus();
532
    }
533
 
534
    private void showMidiDeviceManagerDialog() {
535
        if (midiDeviceManagerDialog == null) {
536
            final MidiDeviceManagerGUI midiDeviceManagerGUI = new MidiDeviceManagerGUI(
537
                    midiDeviceRouting.getMidiDeviceManager());
538
 
539
            midiDeviceManagerDialog = new JDialog(DialogOwner.getFrame(),
540
                    "MIDI Devices", false);
541
            midiDeviceManagerDialog.setContentPane(midiDeviceManagerGUI);
542
            midiDeviceManagerDialog.pack();
543
            midiDeviceManagerDialog.setLocationRelativeTo(this);
544
            midiDeviceManagerDialog.setVisible(true);
545
 
546
            midiDeviceManagerDialog.addWindowListener(new WindowAdapter() {
547
                public void windowClosing(WindowEvent we) {
548
                    midiDeviceRouting.getMidiDeviceManager().deleteObserver(
549
                            midiDeviceManagerGUI);
550
                    midiDeviceManagerDialog = null;
551
                }
552
            });
553
        }
554
        midiDeviceManagerDialog.requestFocus();
555
    }
556
 
557
    private void showMidiRouterManagerDialog() {
558
        if (midiRouterManagerGUI == null) {
559
            final MidiRouterDeviceManagerGUI midiRouterDeviceManagerGUI = new MidiRouterDeviceManagerGUI(
560
                    midiDeviceRouting.getMidiRouterDeviceManager());
561
 
562
            midiRouterManagerGUI = new JDialog(DialogOwner.getFrame(),
563
                    "Virtual Ports", false);
564
            midiRouterManagerGUI.setContentPane(midiRouterDeviceManagerGUI);
565
            midiRouterManagerGUI.pack();
566
            midiRouterManagerGUI.setLocationRelativeTo(this);
567
            midiRouterManagerGUI.setVisible(true);
568
 
569
            midiRouterManagerGUI.addWindowListener(new WindowAdapter() {
570
                public void windowClosing(WindowEvent we) {
571
                    midiDeviceRouting.getMidiRouterDeviceManager()
572
                            .deleteObserver(midiRouterDeviceManagerGUI);
573
                    midiRouterManagerGUI = null;
574
                }
575
            });
576
        }
577
        midiRouterManagerGUI.requestFocus();
578
    }
579
 
580
    private void showFilterManagerDialog() {
581
        if (midiFilterManagerDialog == null) {
582
            final MidiFilterDeviceManagerGUI midiFilterManagerGUI = new MidiFilterDeviceManagerGUI(
583
                    midiDeviceRouting.getMidiFilterManager());
584
 
585
            midiFilterManagerDialog = new JDialog(DialogOwner.getFrame(),
586
                    "Filters", false);
587
            midiFilterManagerDialog.setContentPane(midiFilterManagerGUI);
588
            midiFilterManagerDialog.pack();
589
            midiFilterManagerDialog.setLocationRelativeTo(this);
590
            midiFilterManagerDialog.setVisible(true);
591
 
592
            midiFilterManagerDialog.addWindowListener(new WindowAdapter() {
593
                public void windowClosing(WindowEvent we) {
594
                    midiDeviceRouting.getMidiFilterManager().deleteObserver(
595
                            midiFilterManagerGUI);
596
                    midiFilterManagerDialog = null;
597
                }
598
            });
599
        }
600
        midiFilterManagerDialog.requestFocus();
601
    }
602
 
603
    /*
604
     * private void showMapManagerDialog() { if (midiMapManagerDialog == null) {
605
     * final MidiMapDeviceManagerGUI midiMapManagerGUI = new
606
     * MidiMapDeviceManagerGUI( midiDeviceRouting.getMidiMapManager());
607
     *
608
     * midiMapManagerDialog = new JDialog();
609
     * midiMapManagerDialog.setModal(false);
610
     * midiMapManagerDialog.setTitle("Maps");
611
     * midiMapManagerDialog.setContentPane(midiMapManagerGUI);
612
     * midiMapManagerDialog.pack();
613
     * midiMapManagerDialog.setLocationRelativeTo(this);
614
     * midiMapManagerDialog.setVisible(true);
615
     *
616
     * midiMapManagerDialog.addWindowListener(new WindowAdapter() { public void
617
     * windowClosing(WindowEvent we) {
618
     * midiDeviceRouting.getMidiMapManager().deleteObserver( midiMapManagerGUI);
619
     * midiMapManagerDialog = null; } }); } midiMapManagerDialog.requestFocus();
620
     * }
621
     */
622
 
213 tk 623
    private void populateTrees() {
628 adamjking 624
 
213 tk 625
        midiReadDevicesListModel.removeAllElements();
628 adamjking 626
 
213 tk 627
        Iterator it = midiDeviceRouting.getMidiReadDevices().iterator();
628
 
629
        while (it.hasNext()) {
630
            midiReadDevicesListModel.addElement(((MidiDevice) it.next())
631
                    .getDeviceInfo().getName());
632
        }
628 adamjking 633
 
213 tk 634
        midiReadDevicesList.setSelectedIndex(0);
628 adamjking 635
 
213 tk 636
        midiWriteDevicesListModel.removeAllElements();
628 adamjking 637
 
213 tk 638
        it = midiDeviceRouting.getMidiWriteDevices().iterator();
628 adamjking 639
 
213 tk 640
        while (it.hasNext()) {
641
            midiWriteDevicesListModel.addElement(((MidiDevice) it.next())
642
                    .getDeviceInfo().getName());
643
        }
628 adamjking 644
 
213 tk 645
        midiWriteDevicesList.setSelectedIndex(0);
628 adamjking 646
 
213 tk 647
        updateUI();
648
    }
649
 
628 adamjking 650
    private void setButtonStates() {
651
 
652
        int[] selectedReadIndices = midiReadDevicesList.getSelectedIndices();
653
        int[] selectedWriteIndices = midiWriteDevicesList.getSelectedIndices();
654
 
655
        connectButton.setEnabled(false);
656
        disconnectButton.setEnabled(false);
657
        releaseButton.setSelected(midiDeviceRouting.getPortsReleased());
658
 
659
        if (selectedReadIndices.length > 0 && selectedWriteIndices.length > 0) {
660
 
661
            for (int r = 0; r < selectedReadIndices.length; r++) {
662
 
663
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
664
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
665
 
666
                for (int w = 0; w < selectedWriteIndices.length; w++) {
667
 
668
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
669
                            .getMidiWriteDevices().elementAt(
670
                                    selectedWriteIndices[w]);
671
 
672
                    if (midiDeviceRouting.devicesConnected(transmittingDevice,
673
                            receivingDevice) != MidiDeviceRouting.DISCONNECTED) {
674
                        disconnectButton.setEnabled(true);
675
                    } else {
676
                        connectButton.setEnabled(true);
677
                    }
678
                }
679
            }
680
        }
681
    }
682
 
213 tk 683
    public void valueChanged(ListSelectionEvent lse) {
628 adamjking 684
        setButtonStates();
685
    }
686
 
687
    public void mouseClicked(MouseEvent me) {
688
 
689
    }
690
 
691
    public void mouseReleased(MouseEvent me) {
692
        Object source = me.getSource();
693
 
694
        if (source == midiReadDevicesList || source == midiWriteDevicesList) {
695
            JList source2 = (JList) source;
696
            if (me.getButton() == MouseEvent.BUTTON3) {
697
                int index = source2.locationToIndex(me.getPoint());
698
                source2.setSelectedIndex(index);
699
                buildConnectionMenu((source == midiWriteDevicesList) ? true
700
                        : false);
701
                connectionMenu.show(source2, me.getX(), me.getY());
213 tk 702
            }
628 adamjking 703
        }
213 tk 704
    }
705
 
628 adamjking 706
    public void mousePressed(MouseEvent me) {
707
 
708
    }
709
 
710
    public void mouseEntered(MouseEvent me) {
711
 
712
    }
713
 
714
    public void mouseExited(MouseEvent me) {
715
 
716
    }
717
 
213 tk 718
    public void stateChanged(ChangeEvent ce) {
628 adamjking 719
        wirePanel.repaint();
213 tk 720
    }
721
 
722
    public Dimension getMinimumSize() {
723
        return getPreferredSize();
724
    }
725
 
726
    public void update(Observable observable, Object object) {
727
        if (observable == midiDeviceRouting) {
628 adamjking 728
            if (object == midiDeviceRouting.getMidiReadDevices()
729
                    || object == midiDeviceRouting.getMidiWriteDevices()) {
213 tk 730
                populateTrees();
731
            }
557 rutgerv 732
            redrawAll();
213 tk 733
        }
734
    }
735
 
736
    public void actionPerformed(ActionEvent ae) {
628 adamjking 737
        Object source = ae.getSource();
738
 
739
        if (source == connectButton) {
740
 
741
            int[] selectedReadIndices = midiReadDevicesList
742
                    .getSelectedIndices();
743
            int[] selectedWriteIndices = midiWriteDevicesList
744
                    .getSelectedIndices();
745
 
746
            for (int r = 0; r < selectedReadIndices.length; r++) {
747
 
748
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
749
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
750
 
751
                for (int w = 0; w < selectedWriteIndices.length; w++) {
752
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
753
                            .getMidiWriteDevices().elementAt(
754
                                    selectedWriteIndices[w]);
755
 
756
                    midiDeviceRouting.connectDevices(transmittingDevice,
757
                            receivingDevice);
758
                }
759
 
760
            }
761
        } else if (source == disconnectButton) {
762
 
763
            int[] selectedReadIndices = midiReadDevicesList
764
                    .getSelectedIndices();
765
            int[] selectedWriteIndices = midiWriteDevicesList
766
                    .getSelectedIndices();
767
 
768
            for (int r = 0; r < selectedReadIndices.length; r++) {
769
 
770
                MidiDevice transmittingDevice = (MidiDevice) midiDeviceRouting
771
                        .getMidiReadDevices().elementAt(selectedReadIndices[r]);
772
 
773
                for (int w = 0; w < selectedWriteIndices.length; w++) {
774
                    MidiDevice receivingDevice = (MidiDevice) midiDeviceRouting
775
                            .getMidiWriteDevices().elementAt(
776
                                    selectedWriteIndices[w]);
777
 
778
                    midiDeviceRouting.disconnectDevices(transmittingDevice,
779
                            receivingDevice);
780
                }
781
            }
782
        } else if (source == disconnectallButton) {
783
            midiDeviceRouting.disconnectAll();
784
        } else if (source == midiDeviceManagerButton) {
785
            showMidiDeviceManagerDialog();
786
        } else if (source == midiRouterButton) {
787
            showMidiRouterManagerDialog();
788
        } else if (source == filterButton) {
789
            showFilterManagerDialog();
790
        } /*
791
         * else if (source == mapButton) { showMapManagerDialog(); }
792
         */
793
        else if (source == releaseButton) {
794
            midiDeviceRouting.setPortsReleased(releaseButton.isSelected());
294 rutgerv 795
        }
213 tk 796
    }
797
 
798
    public class MyListCellRenderer extends DefaultListCellRenderer {
799
        public Component getListCellRendererComponent(JList list, Object value,
800
                int index, boolean isSelected, boolean hasFocus) {
801
 
802
            super.getListCellRendererComponent(list, value, index, isSelected,
803
                    hasFocus);
804
 
805
            setIcon(getMidiDeviceIcon(((MidiDevice) ((list == midiWriteDevicesList) ? midiDeviceRouting
806
                    .getMidiWriteDevices()
807
                    : midiDeviceRouting.getMidiReadDevices()).elementAt(index))));
808
 
809
            return this;
810
        }
628 adamjking 811
    }
812
 
813
    public class MidiDevicesTransferHandler extends SimpleTransferHandler {
814
 
815
        private boolean output;
816
 
817
        public MidiDevicesTransferHandler(boolean output) {
818
            super();
819
            this.output = output;
820
            dropOnSelf = false;
821
            supportedDataFlavors = new DataFlavor[] { MidiDeviceTransferable.LOCAL_JVM_MIDI_DEVICE_FLAVOR };
822
        }
823
 
824
        public boolean importData(JComponent c, Transferable t) {
825
 
826
            if (canImport(c, supportedDataFlavors)) {
827
 
828
                int[] indices = ((output) ? midiWriteDevicesList
829
                        : midiReadDevicesList).getSelectedIndices();
830
 
831
                for (int d = 0; d < indices.length; d++) {
832
 
833
                    MidiDevice localDevice = (MidiDevice) ((output) ? midiDeviceRouting
834
                            .getMidiWriteDevices()
835
                            : midiDeviceRouting.getMidiReadDevices())
836
                            .elementAt(indices[d]);
837
 
838
                    try {
839
                        MidiDevice[] remoteDevices = (MidiDevice[]) t
840
                                .getTransferData(MidiDeviceTransferable.LOCAL_JVM_MIDI_DEVICE_FLAVOR);
841
 
842
                        for (int d2 = 0; d2 < remoteDevices.length; d2++) {
843
 
844
                            MidiDevice remoteDevice = remoteDevices[d2];
845
 
846
                            if (output) {
847
                                midiDeviceRouting.connectDevices(remoteDevice,
848
                                        localDevice);
849
                            } else {
850
                                midiDeviceRouting.connectDevices(localDevice,
851
                                        remoteDevice);
852
                            }
853
                        }
854
                    } catch (Exception e) {
855
                        return false;
856
                    }
857
                }
858
                return true;
859
            }
860
            return false;
861
        }
862
 
863
        protected Transferable createTransferable(JComponent c) {
864
            justExported = true;
865
            int[] indices = ((output) ? midiWriteDevicesList
866
                    : midiReadDevicesList).getSelectedIndices();
867
 
868
            MidiDevice[] midiDevices = new MidiDevice[indices.length];
869
 
870
            for (int d = 0; d < midiDevices.length; d++) {
871
                midiDevices[d] = (MidiDevice) ((output) ? midiDeviceRouting
872
                        .getMidiWriteDevices() : midiDeviceRouting
873
                        .getMidiReadDevices()).elementAt(indices[d]);
874
            }
875
            return new MidiDeviceTransferable(midiDevices);
876
        }
877
    }
213 tk 878
}