Subversion Repositories svn.mios

Rev

Rev 721 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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