Subversion Repositories svn.mios

Rev

Rev 557 | Rev 641 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 557 Rev 628
Line 1... Line 1...
1
/*
1
/*
2
 * @(#)MidiDeviceRoutingGUI.java    beta7   2006/04/23
2
 * @(#)MidiDeviceRoutingGUI.java    beta8   2006/04/23
3
 *
3
 *
4
 * Copyright (C) 2006    Adam King (adamjking@optusnet.com.au)
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
5
 *
6
 * This application is free software; you can redistribute it and/or modify
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
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
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
9
 * (at your option) any later version.
Line 18... Line 18...
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
19
 */
20
20
21
package org.midibox.midi.gui;
21
package org.midibox.midi.gui;
22
22
-
 
23
import java.awt.BasicStroke;
23
import java.awt.BorderLayout;
24
import java.awt.BorderLayout;
24
import java.awt.Color;
25
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.Dimension;
-
 
28
import java.awt.Graphics;
-
 
29
import java.awt.Graphics2D;
27
import java.awt.GridBagConstraints;
30
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
31
import java.awt.GridBagLayout;
29
import java.awt.Insets;
32
import java.awt.Insets;
-
 
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;
30
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
39
import java.awt.event.ActionListener;
-
 
40
import java.awt.event.MouseEvent;
-
 
41
import java.awt.event.MouseListener;
-
 
42
import java.awt.event.WindowAdapter;
-
 
43
import java.awt.event.WindowEvent;
32
import java.util.Hashtable;
44
import java.util.Hashtable;
33
import java.util.Iterator;
45
import java.util.Iterator;
34
import java.util.Observable;
46
import java.util.Observable;
35
import java.util.Observer;
47
import java.util.Observer;
-
 
48
import java.util.Vector;
36
49
37
import javax.sound.midi.MidiDevice;
50
import javax.sound.midi.MidiDevice;
38
import javax.swing.BorderFactory;
51
import javax.swing.BorderFactory;
39
import javax.swing.DefaultListCellRenderer;
52
import javax.swing.DefaultListCellRenderer;
40
import javax.swing.DefaultListModel;
53
import javax.swing.DefaultListModel;
41
import javax.swing.Icon;
54
import javax.swing.Icon;
-
 
55
import javax.swing.JButton;
-
 
56
import javax.swing.JCheckBoxMenuItem;
-
 
57
import javax.swing.JComponent;
-
 
58
import javax.swing.JDialog;
42
import javax.swing.JLabel;
59
import javax.swing.JLabel;
43
import javax.swing.JList;
60
import javax.swing.JList;
-
 
61
import javax.swing.JMenu;
44
import javax.swing.JButton;
62
import javax.swing.JMenuItem;
45
import javax.swing.JPanel;
63
import javax.swing.JPanel;
-
 
64
import javax.swing.JPopupMenu;
46
import javax.swing.JScrollPane;
65
import javax.swing.JScrollPane;
-
 
66
import javax.swing.JToggleButton;
47
import javax.swing.ListSelectionModel;
67
import javax.swing.ListSelectionModel;
48
import javax.swing.SwingConstants;
68
import javax.swing.SwingConstants;
49
import javax.swing.event.ChangeEvent;
69
import javax.swing.event.ChangeEvent;
50
import javax.swing.event.ChangeListener;
70
import javax.swing.event.ChangeListener;
51
import javax.swing.event.ListSelectionEvent;
71
import javax.swing.event.ListSelectionEvent;
52
import javax.swing.event.ListSelectionListener;
72
import javax.swing.event.ListSelectionListener;
53
73
54
import org.midibox.midi.MidiDeviceRouting;
74
import org.midibox.midi.MidiDeviceRouting;
-
 
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;
55
import org.midibox.utils.gui.GuiUtils;
79
import org.midibox.utils.gui.GuiUtils;
56
import org.midibox.utils.gui.ImageLoader;
80
import org.midibox.utils.gui.ImageLoader;
-
 
81
import org.midibox.utils.gui.SimpleTransferHandler;
57
82
58
public class MidiDeviceRoutingGUI extends JPanel implements ChangeListener, ListSelectionListener, ActionListener, Observer {
83
public class MidiDeviceRoutingGUI extends JPanel implements MouseListener,
-
 
84
        ChangeListener, ListSelectionListener, ActionListener, Observer {
59
85
60
    private MidiDeviceRouting midiDeviceRouting;
86
    private MidiDeviceRouting midiDeviceRouting;
61
87
62
    private static Icon openIcon = ImageLoader.getImageIcon("midiPortOpen.png");
88
    private static Icon openIcon = ImageLoader.getImageIcon("midiPortOpen.png");
63
89
64
    private static Icon closedIcon = ImageLoader.getImageIcon("midiPortClosed.png");
90
    private static Icon closedIcon = ImageLoader
-
 
91
            .getImageIcon("midiPortClosed.png");
65
92
66
    private Hashtable icons = new Hashtable();
93
    private Hashtable icons = new Hashtable();
67
94
68
    private DefaultListModel midiReadDevicesListModel;
95
    private DefaultListModel midiReadDevicesListModel;
69
96
Line 75... Line 102...
75
102
76
    private JList midiWriteDevicesList;
103
    private JList midiWriteDevicesList;
77
104
78
    private JScrollPane midiWriteDevicesScroller;
105
    private JScrollPane midiWriteDevicesScroller;
79
   
106
-
 
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
80
    private JButton rescan;
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;
81
140
82
    public MidiDeviceRoutingGUI(MidiDeviceRouting midiDeviceRouting) {
141
    public MidiDeviceRoutingGUI(MidiDeviceRouting midiDeviceRouting) {
83
        super(new BorderLayout());
142
        super(new BorderLayout());
84
143
85
        this.midiDeviceRouting = midiDeviceRouting;
144
        this.midiDeviceRouting = midiDeviceRouting;
86
        midiDeviceRouting.addObserver(this);
145
        midiDeviceRouting.addObserver(this);
-
 
146
-
 
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"));
87
157
88
        midiReadDevicesListModel = new DefaultListModel();
158
        midiReadDevicesListModel = new DefaultListModel();
89
        midiWriteDevicesListModel = new DefaultListModel();
159
        midiWriteDevicesListModel = new DefaultListModel();
90
160
91
        midiReadDevicesList = new JList(midiReadDevicesListModel);
161
        midiReadDevicesList = new JList(midiReadDevicesListModel);
-
 
162
        midiReadDevicesList
92
        midiReadDevicesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
163
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
93
        midiReadDevicesList.setLayoutOrientation(JList.VERTICAL);
164
        midiReadDevicesList.setLayoutOrientation(JList.VERTICAL);
94
        midiReadDevicesList.setVisibleRowCount(10);
165
        midiReadDevicesList.setVisibleRowCount(6);
95
        midiReadDevicesList.setCellRenderer(new MyListCellRenderer());
166
        midiReadDevicesList.setCellRenderer(new MyListCellRenderer());
-
 
167
        midiReadDevicesList.addMouseListener(this);
96
        midiReadDevicesList.setBackground(Color.WHITE);
168
        midiReadDevicesList.setBackground(Color.WHITE);
97
        midiWriteDevicesList = new JList(midiWriteDevicesListModel);
169
        midiWriteDevicesList = new JList(midiWriteDevicesListModel);
-
 
170
        midiWriteDevicesList
98
        midiWriteDevicesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
171
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
99
        midiWriteDevicesList.setLayoutOrientation(JList.VERTICAL);
172
        midiWriteDevicesList.setLayoutOrientation(JList.VERTICAL);
100
        midiWriteDevicesList.setVisibleRowCount(10);
173
        midiWriteDevicesList.setVisibleRowCount(6);
101
        midiWriteDevicesList.setCellRenderer(new MyListCellRenderer());
174
        midiWriteDevicesList.setCellRenderer(new MyListCellRenderer());
-
 
175
        midiWriteDevicesList.addMouseListener(this);
102
        midiWriteDevicesList.setBackground(Color.WHITE);
176
        midiWriteDevicesList.setBackground(Color.WHITE);
103
177
104
        JPanel listPane = new JPanel(new GridBagLayout());
178
        JPanel listPane = new JPanel(new GridBagLayout());
105
179
106
        GridBagConstraints gbc = new GridBagConstraints();
180
        GridBagConstraints gbc = new GridBagConstraints();
Line 113... Line 187...
113
        gbc.weightx = 1.0;
187
        gbc.weightx = 1.0;
114
        gbc.weighty = 0.0;
188
        gbc.weighty = 0.0;
115
189
116
        listPane.setOpaque(false);
190
        listPane.setOpaque(false);
117
191
118
        listPane.add(new JLabel("MIDI Input devices",SwingConstants.CENTER), gbc);
192
        listPane.add(new JLabel("MIDI Devices - Readable",
-
 
193
                SwingConstants.CENTER), gbc);
119
        gbc.gridy++;
194
        gbc.gridy++;
120
195
121
        midiReadDevicesScroller = new JScrollPane(midiReadDevicesList);
196
        midiReadDevicesScroller = new JScrollPane(midiReadDevicesList);
122
        midiReadDevicesScroller.getViewport().addChangeListener(this);
197
        midiReadDevicesScroller.getViewport().addChangeListener(this);
123
        midiReadDevicesScroller
198
        midiReadDevicesScroller
Line 129... Line 204...
129
        gbc.weighty = 1.0;
204
        gbc.weighty = 1.0;
130
        gbc.weighty = 1.0;
205
        gbc.weighty = 1.0;
131
206
132
        listPane.add(midiReadDevicesScroller, gbc);
207
        listPane.add(midiReadDevicesScroller, gbc);
133
        gbc.gridx++;
208
        gbc.gridx++;
-
 
209
-
 
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);
134
282
135
        gbc.gridx++;
283
        gbc.gridx++;
136
        gbc.gridy--;
284
        gbc.gridy--;
137
285
138
        gbc.fill = GridBagConstraints.HORIZONTAL;
286
        gbc.fill = GridBagConstraints.HORIZONTAL;
139
        gbc.weightx = 1.0;
287
        gbc.weightx = 1.0;
140
        gbc.weighty = 0.0;
288
        gbc.weighty = 0.0;
141
289
142
        listPane.add(new JLabel("MIDI Output devices",
290
        listPane.add(new JLabel("MIDI Devices - Writeable",
143
                SwingConstants.CENTER), gbc);
291
                SwingConstants.CENTER), gbc);
144
        gbc.gridy++;
292
        gbc.gridy++;
145
293
146
        midiWriteDevicesScroller = new JScrollPane(midiWriteDevicesList);
294
        midiWriteDevicesScroller = new JScrollPane(midiWriteDevicesList);
147
        midiWriteDevicesScroller
295
        midiWriteDevicesScroller
Line 166... Line 314...
166
        GuiUtils.setComponentSize(midiReadDevicesScroller, maxPreferredWidth,
314
        GuiUtils.setComponentSize(midiReadDevicesScroller, maxPreferredWidth,
167
                maxPreferredHeight);
315
                maxPreferredHeight);
168
        GuiUtils.setComponentSize(midiWriteDevicesScroller, maxPreferredWidth,
316
        GuiUtils.setComponentSize(midiWriteDevicesScroller, maxPreferredWidth,
169
                maxPreferredHeight);
317
                maxPreferredHeight);
170
318
-
 
319
        midiReadDevicesList.setDragEnabled(true);
-
 
320
        midiWriteDevicesList.setDragEnabled(true);
-
 
321
        midiReadDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
-
 
322
                false));
-
 
323
        midiWriteDevicesList.setTransferHandler(new MidiDevicesTransferHandler(
-
 
324
                true));
171
        midiReadDevicesList.addListSelectionListener(this);
325
        midiReadDevicesList.addListSelectionListener(this);
172
        midiWriteDevicesList.addListSelectionListener(this);
326
        midiWriteDevicesList.addListSelectionListener(this);
173
327
174
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
328
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
175
        listPane.setOpaque(false);
329
        listPane.setOpaque(false);
176
        add(listPane, BorderLayout.CENTER);
330
        add(listPane, BorderLayout.CENTER);
-
 
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
177
        rescan = new JButton("Rescan");
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");
178
        rescan.addActionListener(this);
406
        releaseButton.addActionListener(this);
-
 
407
        buttonPane.add(releaseButton, gbc);
-
 
408
-
 
409
        buttonPane.setOpaque(false);
179
        add(rescan, BorderLayout.SOUTH);
410
        add(buttonPane, BorderLayout.SOUTH);
180
        populateTrees();
411
        populateTrees();
181
    }
412
    }
182
413
183
    public MidiDeviceRouting getMidiDeviceRouting() {
414
    public MidiDeviceRouting getMidiDeviceRouting() {
184
        return midiDeviceRouting;
415
        return midiDeviceRouting;
-
 
416
    }
-
 
417
-
 
418
    public void addMidiDeviceIcon(Object object, Icon icon) {
-
 
419
        icons.put(object, icon);
185
    }
420
    }
186
421
187
    public Icon getMidiDeviceIcon(MidiDevice md) {
422
    public Icon getMidiDeviceIcon(MidiDevice md) {
188
        if (icons.containsKey(md)) {
423
        if (icons.containsKey(md)) {
189
            return (Icon) icons.get(md);
424
            return (Icon) icons.get(md);
Line 206... Line 441...
206
    }
441
    }
207
442
208
    public void redrawAll() {
443
    public void redrawAll() {
209
        midiReadDevicesList.repaint();
444
        midiReadDevicesList.repaint();
210
        midiWriteDevicesList.repaint();
445
        midiWriteDevicesList.repaint();
-
 
446
        wirePanel.repaint();
-
 
447
        setButtonStates();
211
    }
448
    }
-
 
449
-
 
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
     */
212
622
213
    private void populateTrees() {
623
    private void populateTrees() {
-
 
624
214
        midiReadDevicesListModel.removeAllElements();
625
        midiReadDevicesListModel.removeAllElements();
-
 
626
215
        Iterator it = midiDeviceRouting.getMidiReadDevices().iterator();
627
        Iterator it = midiDeviceRouting.getMidiReadDevices().iterator();
216
628
217
        while (it.hasNext()) {
629
        while (it.hasNext()) {
218
            midiReadDevicesListModel.addElement(((MidiDevice) it.next())
630
            midiReadDevicesListModel.addElement(((MidiDevice) it.next())
219
                    .getDeviceInfo().getName());
631
                    .getDeviceInfo().getName());
220
        }
632
        }
-
 
633
221
        midiReadDevicesList.setSelectedIndex(0);
634
        midiReadDevicesList.setSelectedIndex(0);
-
 
635
222
        midiWriteDevicesListModel.removeAllElements();
636
        midiWriteDevicesListModel.removeAllElements();
-
 
637
223
        it = midiDeviceRouting.getMidiWriteDevices().iterator();
638
        it = midiDeviceRouting.getMidiWriteDevices().iterator();
-
 
639
224
        while (it.hasNext()) {
640
        while (it.hasNext()) {
225
            midiWriteDevicesListModel.addElement(((MidiDevice) it.next())
641
            midiWriteDevicesListModel.addElement(((MidiDevice) it.next())
226
                    .getDeviceInfo().getName());
642
                    .getDeviceInfo().getName());
227
        }
643
        }
-
 
644
228
        midiWriteDevicesList.setSelectedIndex(0);
645
        midiWriteDevicesList.setSelectedIndex(0);
-
 
646
229
        updateUI();
647
        updateUI();
-
 
648
    }
-
 
649
-
 
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
        }
230
    }
681
    }
231
682
232
    public void valueChanged(ListSelectionEvent lse) {
683
    public void valueChanged(ListSelectionEvent lse) {
233
        if (lse.getSource()==midiReadDevicesList) {
684
        setButtonStates();
234
            if (midiReadDevicesList.getSelectedIndex() > 0) {
-
 
235
                midiDeviceRouting.setInputDevice(midiReadDevicesList.getSelectedIndex());
-
 
236
            }
685
    }
-
 
686
237
        } else if (lse.getSource()==midiWriteDevicesList) {
687
    public void mouseClicked(MouseEvent me) {
238
            if (midiWriteDevicesList.getSelectedIndex() > 0) {
-
 
239
                midiDeviceRouting.setOutputDevice(midiWriteDevicesList.getSelectedIndex());    
-
 
-
 
688
240
            }
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());
241
        }  
702
            }
-
 
703
        }
-
 
704
    }
-
 
705
-
 
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
242
    }
716
    }
243
717
244
    public void stateChanged(ChangeEvent ce) {
718
    public void stateChanged(ChangeEvent ce) {
-
 
719
        wirePanel.repaint();
245
    }
720
    }
246
721
247
    public Dimension getMinimumSize() {
722
    public Dimension getMinimumSize() {
248
        return getPreferredSize();
723
        return getPreferredSize();
249
    }
724
    }
250
725
251
    public void update(Observable observable, Object object) {
726
    public void update(Observable observable, Object object) {
252
        if (observable == midiDeviceRouting) {
727
        if (observable == midiDeviceRouting) {
-
 
728
            if (object == midiDeviceRouting.getMidiReadDevices()
253
            if (object == midiDeviceRouting.getMidiReadDevices() || object == midiDeviceRouting.getMidiWriteDevices()) {
729
                    || object == midiDeviceRouting.getMidiWriteDevices()) {
254
                populateTrees();
730
                populateTrees();
255
            }
731
            }
256
            midiReadDevicesList.setSelectedIndex(midiDeviceRouting.getInputDeviceIndex());
-
 
257
            midiWriteDevicesList.setSelectedIndex(midiDeviceRouting.getOutputDeviceIndex());
-
 
258
            redrawAll();
732
            redrawAll();
259
        }
733
        }
260
       
-
 
261
    }
734
    }
262
735
263
    public void actionPerformed(ActionEvent ae) {
736
    public void actionPerformed(ActionEvent ae) {
-
 
737
        Object source = ae.getSource();
-
 
738
264
        if (ae.getSource()==rescan) {
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) {
265
            midiDeviceRouting.rescanDevices();
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());
266
        }
795
        }
267
    }
796
    }
268
797
269
    public class MyListCellRenderer extends DefaultListCellRenderer {
798
    public class MyListCellRenderer extends DefaultListCellRenderer {
270
        public Component getListCellRendererComponent(JList list, Object value,
799
        public Component getListCellRendererComponent(JList list, Object value,
Line 276... Line 805...
276
            setIcon(getMidiDeviceIcon(((MidiDevice) ((list == midiWriteDevicesList) ? midiDeviceRouting
805
            setIcon(getMidiDeviceIcon(((MidiDevice) ((list == midiWriteDevicesList) ? midiDeviceRouting
277
                    .getMidiWriteDevices()
806
                    .getMidiWriteDevices()
278
                    : midiDeviceRouting.getMidiReadDevices()).elementAt(index))));
807
                    : midiDeviceRouting.getMidiReadDevices()).elementAt(index))));
279
808
280
            return this;
809
            return this;
-
 
810
        }
-
 
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);
281
        }
876
        }
282
    }  
877
    }
283
}
878
}