Subversion Repositories svn.mios

Rev

Rev 628 | Rev 642 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 628 Rev 641
1
/*
1
/*
2
 * @(#)MidiDeviceRouting.java   beta8   2006/04/23
2
 * @(#)MidiDeviceRouting.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;
21
package org.midibox.midi;
22
22
23
import java.util.ArrayList;
23
import java.util.ArrayList;
24
import java.util.Collection;
24
import java.util.Collection;
25
import java.util.Iterator;
25
import java.util.Iterator;
26
import java.util.List;
26
import java.util.List;
27
import java.util.Observable;
27
import java.util.Observable;
28
import java.util.Observer;
28
import java.util.Observer;
29
import java.util.Vector;
29
import java.util.Vector;
30
30
31
import javax.sound.midi.MidiDevice;
31
import javax.sound.midi.MidiDevice;
32
import javax.sound.midi.MidiUnavailableException;
32
import javax.sound.midi.MidiUnavailableException;
33
import javax.sound.midi.Receiver;
33
import javax.sound.midi.Receiver;
34
import javax.sound.midi.Transmitter;
34
import javax.sound.midi.Transmitter;
35
35
36
public class MidiDeviceRouting extends Observable implements Observer {
36
public class MidiDeviceRouting extends Observable {
37
37
38
    public static int DISCONNECTED = 0;
38
    public static int DISCONNECTED = 0;
39
39
40
    public static int LOGICALLY_CONNECTED = 1;
40
    public static int LOGICALLY_CONNECTED = 1;
41
41
42
    public static int PHYSICALLY_CONNECTED = 2;
42
    public static int PHYSICALLY_CONNECTED = 2;
43
43
44
    private Vector midiReadDevices;
44
    private Vector midiReadDevices;
45
45
46
    private Vector midiWriteDevices;
46
    private Vector midiWriteDevices;
47
47
48
    private Vector logicalConnections;
48
    private Vector logicalConnections;
49
-
 
50
    private MidiDeviceManager midiDeviceManager;
-
 
51
-
 
52
    private MidiRouterDeviceManager midiRouterDeviceManager;
-
 
53
-
 
54
    private MidiFilterDeviceManager midiFilterManager;
-
 
55
-
 
56
    /*
-
 
57
     * private MidiMapDeviceManager midiMapManager;
-
 
58
     */
-
 
59
49
60
    private boolean portsReleased;
50
    private boolean portsReleased;
61
51
62
    public MidiDeviceRouting() {
52
    public MidiDeviceRouting() {
63
        midiReadDevices = new Vector();
53
        midiReadDevices = new Vector();
64
        midiWriteDevices = new Vector();
54
        midiWriteDevices = new Vector();
65
55
66
        logicalConnections = new Vector();
56
        logicalConnections = new Vector();
67
-
 
68
        midiDeviceManager = new MidiDeviceManager();
-
 
69
        midiDeviceManager.addObserver(this);
-
 
70
-
 
71
        midiRouterDeviceManager = new MidiRouterDeviceManager();
-
 
72
        midiRouterDeviceManager.addObserver(this);
-
 
73
-
 
74
        midiFilterManager = new MidiFilterDeviceManager();
-
 
75
        midiFilterManager.addObserver(this);
-
 
76
-
 
77
        /*
-
 
78
         * midiMapManager = new MidiMapDeviceManager();
-
 
79
         * midiMapManager.addObserver(this);
-
 
80
         */
-
 
81
    }
57
    }
82
58
83
    public Vector getMidiReadDevices() {
59
    public Vector getMidiReadDevices() {
84
        return midiReadDevices;
60
        return midiReadDevices;
85
    }
61
    }
86
62
87
    public Vector getMidiWriteDevices() {
63
    public Vector getMidiWriteDevices() {
88
        return midiWriteDevices;
64
        return midiWriteDevices;
89
    }
65
    }
90
66
91
    public void addMidiReadDevice(MidiDevice midiDevice) {
67
    public void addMidiReadDevice(MidiDevice midiDevice) {
92
        midiReadDevices.add(midiDevice);
68
        midiReadDevices.add(midiDevice);
93
        setChanged();
69
        setChanged();
94
        notifyObservers(midiReadDevices);
70
        notifyObservers(midiReadDevices);
95
        clearChanged();
71
        clearChanged();
96
    }
72
    }
97
73
98
    public void addMidiWriteDevice(MidiDevice midiDevice) {
74
    public void addMidiWriteDevice(MidiDevice midiDevice) {
99
        midiWriteDevices.add(midiDevice);
75
        midiWriteDevices.add(midiDevice);
100
        setChanged();
76
        setChanged();
101
        notifyObservers(midiReadDevices);
77
        notifyObservers(midiReadDevices);
102
        clearChanged();
78
        clearChanged();
103
    }
79
    }
104
80
105
    public void addMidiReadDevices(Collection midiDevices) {
81
    public void addMidiReadDevices(Collection midiDevices) {
106
        Iterator it = midiDevices.iterator();
82
        Iterator it = midiDevices.iterator();
107
83
108
        while (it.hasNext()) {
84
        while (it.hasNext()) {
109
            MidiDevice midiDevice = (MidiDevice) it.next();
85
            MidiDevice midiDevice = (MidiDevice) it.next();
110
            addMidiReadDevice(midiDevice);
86
            addMidiReadDevice(midiDevice);
111
        }
87
        }
112
    }
88
    }
113
89
114
    public void addMidiWriteDevices(Collection midiDevices) {
90
    public void addMidiWriteDevices(Collection midiDevices) {
115
        Iterator it = midiDevices.iterator();
91
        Iterator it = midiDevices.iterator();
116
92
117
        while (it.hasNext()) {
93
        while (it.hasNext()) {
118
            MidiDevice midiDevice = (MidiDevice) it.next();
94
            MidiDevice midiDevice = (MidiDevice) it.next();
119
            addMidiWriteDevice(midiDevice);
95
            addMidiWriteDevice(midiDevice);
120
        }
96
        }
121
    }
97
    }
122
98
123
    public void removeMidiReadDevice(MidiDevice midiDevice) {
99
    public void removeMidiReadDevice(MidiDevice midiDevice) {
124
        midiReadDevices.remove(midiDevice);
100
        midiReadDevices.remove(midiDevice);
125
        setChanged();
101
        setChanged();
126
        notifyObservers(midiReadDevices);
102
        notifyObservers(midiReadDevices);
127
        clearChanged();
103
        clearChanged();
128
    }
104
    }
129
105
130
    public void removeMidiWriteDevice(MidiDevice midiDevice) {
106
    public void removeMidiWriteDevice(MidiDevice midiDevice) {
131
        midiWriteDevices.remove(midiDevice);
107
        midiWriteDevices.remove(midiDevice);
132
        setChanged();
108
        setChanged();
133
        notifyObservers(midiWriteDevices);
109
        notifyObservers(midiWriteDevices);
134
        clearChanged();
110
        clearChanged();
135
    }
111
    }
136
112
137
    public void removeMidiReadDevices(Collection midiDevices) {
113
    public void removeMidiReadDevices(Collection midiDevices) {
138
        Iterator it = midiDevices.iterator();
114
        Iterator it = midiDevices.iterator();
139
115
140
        while (it.hasNext()) {
116
        while (it.hasNext()) {
141
            MidiDevice midiDevice = (MidiDevice) it.next();
117
            MidiDevice midiDevice = (MidiDevice) it.next();
142
            removeMidiReadDevice(midiDevice);
118
            removeMidiReadDevice(midiDevice);
143
        }
119
        }
144
    }
120
    }
145
121
146
    public void removeMidiWriteDevices(Collection midiDevices) {
122
    public void removeMidiWriteDevices(Collection midiDevices) {
147
        Iterator it = midiDevices.iterator();
123
        Iterator it = midiDevices.iterator();
148
124
149
        while (it.hasNext()) {
125
        while (it.hasNext()) {
150
            MidiDevice midiDevice = (MidiDevice) it.next();
126
            MidiDevice midiDevice = (MidiDevice) it.next();
151
            removeMidiWriteDevice(midiDevice);
127
            removeMidiWriteDevice(midiDevice);
152
        }
128
        }
153
    }
129
    }
154
130
155
    public void removeAllMidiReadDevices() {
131
    public void removeAllMidiReadDevices() {
156
        midiReadDevices.removeAllElements();
132
        midiReadDevices.removeAllElements();
157
        setChanged();
133
        setChanged();
158
        notifyObservers(midiReadDevices);
134
        notifyObservers(midiReadDevices);
159
        clearChanged();
135
        clearChanged();
160
    }
136
    }
161
137
162
    public void removeAllMidiWriteDevices() {
138
    public void removeAllMidiWriteDevices() {
163
        midiWriteDevices.removeAllElements();
139
        midiWriteDevices.removeAllElements();
164
        setChanged();
140
        setChanged();
165
        notifyObservers(midiWriteDevices);
141
        notifyObservers(midiWriteDevices);
166
        clearChanged();
142
        clearChanged();
167
    }
143
    }
168
-
 
169
    public MidiDeviceManager getMidiDeviceManager() {
-
 
170
        return midiDeviceManager;
-
 
171
    }
-
 
172
-
 
173
    public MidiRouterDeviceManager getMidiRouterDeviceManager() {
-
 
174
        return midiRouterDeviceManager;
-
 
175
    }
-
 
176
-
 
177
    public MidiFilterDeviceManager getMidiFilterManager() {
-
 
178
        return midiFilterManager;
-
 
179
    }
-
 
180
-
 
181
    /*
-
 
182
     * public MidiMapDeviceManager getMidiMapManager() { return midiMapManager;
-
 
183
     * }
-
 
184
     */
-
 
185
144
186
    public void connectDevices(MidiDevice transmittingDevice,
145
    public void connectDevices(MidiDevice transmittingDevice,
187
            MidiDevice receivingDevice) {
146
            MidiDevice receivingDevice) {
188
147
189
        if (devicesConnected(transmittingDevice, receivingDevice) == PHYSICALLY_CONNECTED) {
148
        if (devicesConnected(transmittingDevice, receivingDevice) == PHYSICALLY_CONNECTED) {
190
            return;
149
            return;
191
        }
150
        }
192
151
193
        logicallyConnectDevices(transmittingDevice, receivingDevice);
152
        logicallyConnectDevices(transmittingDevice, receivingDevice);
194
153
195
        if (!portsReleased
154
        if (!portsReleased
196
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
155
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
197
156
198
            physicallyConnectDevices(transmittingDevice, receivingDevice);
157
            physicallyConnectDevices(transmittingDevice, receivingDevice);
199
        }
158
        }
200
159
201
        setChanged();
160
        setChanged();
202
        notifyObservers();
161
        notifyObservers();
203
        clearChanged();
162
        clearChanged();
204
    }
163
    }
205
164
206
    private void physicallyConnectDevices(MidiDevice transmittingDevice,
165
    private void physicallyConnectDevices(MidiDevice transmittingDevice,
207
            MidiDevice receivingDevice) {
166
            MidiDevice receivingDevice) {
208
        try {
167
        try {
209
            if (!receivingDevice.isOpen()) {
168
            if (!receivingDevice.isOpen()) {
210
                receivingDevice.open();
169
                receivingDevice.open();
211
            }
170
            }
212
171
213
            Receiver receiver = receivingDevice.getReceiver();
172
            Receiver receiver = receivingDevice.getReceiver();
214
173
215
            if (!transmittingDevice.isOpen()) {
174
            if (!transmittingDevice.isOpen()) {
216
                transmittingDevice.open();
175
                transmittingDevice.open();
217
            }
176
            }
218
177
219
            transmittingDevice.getTransmitter().setReceiver(receiver);
178
            transmittingDevice.getTransmitter().setReceiver(receiver);
220
179
221
        } catch (MidiUnavailableException e) {
180
        } catch (MidiUnavailableException e) {
222
181
223
        } catch (NullPointerException e) {
182
        } catch (NullPointerException e) {
224
183
225
        }
184
        }
226
185
227
    }
186
    }
228
187
229
    private void logicallyConnectDevices(MidiDevice transmittingDevice,
188
    private void logicallyConnectDevices(MidiDevice transmittingDevice,
230
            MidiDevice receivingDevice) {
189
            MidiDevice receivingDevice) {
231
        logicalConnections.add(new LogicalConnection(transmittingDevice,
190
        logicalConnections.add(new LogicalConnection(transmittingDevice,
232
                receivingDevice));
191
                receivingDevice));
233
    }
192
    }
234
193
235
    public void disconnectDevices(MidiDevice transmittingDevice,
194
    public void disconnectDevices(MidiDevice transmittingDevice,
236
            MidiDevice receivingDevice) {
195
            MidiDevice receivingDevice) {
237
196
238
        if (devicesConnected(transmittingDevice, receivingDevice) == DISCONNECTED) {
197
        if (devicesConnected(transmittingDevice, receivingDevice) == DISCONNECTED) {
239
            return;
198
            return;
240
        }
199
        }
241
200
242
        logicallyDisconnectDevices(transmittingDevice, receivingDevice);
201
        logicallyDisconnectDevices(transmittingDevice, receivingDevice);
243
        if (!portsReleased
202
        if (!portsReleased
244
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
203
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
245
204
246
            physicallyDisconnectDevices(transmittingDevice, receivingDevice);
205
            physicallyDisconnectDevices(transmittingDevice, receivingDevice);
247
        }
206
        }
248
        setChanged();
207
        setChanged();
249
        notifyObservers();
208
        notifyObservers();
250
        clearChanged();
209
        clearChanged();
251
    }
210
    }
252
211
253
    private void physicallyDisconnectDevices(MidiDevice transmittingDevice,
212
    private void physicallyDisconnectDevices(MidiDevice transmittingDevice,
254
            MidiDevice receivingDevice) {
213
            MidiDevice receivingDevice) {
255
214
256
        // TK: to avoid a "java.util.ConcurrentModificationException" under
215
        // TK: to avoid a "java.util.ConcurrentModificationException" under
257
        // MacOS (using mmj)
216
        // MacOS (using mmj)
258
        // when a transmitter or receiver is closed, we put them into a list and
217
        // when a transmitter or receiver is closed, we put them into a list and
259
        // execute close()
218
        // execute close()
260
        // after iteration
219
        // after iteration
261
        List closeTransmitters = new ArrayList();
220
        List closeTransmitters = new ArrayList();
262
        List closeReceivers = new ArrayList();
221
        List closeReceivers = new ArrayList();
263
222
264
        Iterator it = transmittingDevice.getTransmitters().iterator();
223
        Iterator it = transmittingDevice.getTransmitters().iterator();
265
        while (it.hasNext()) {
224
        while (it.hasNext()) {
266
225
267
            Transmitter transmitter = (Transmitter) it.next();
226
            Transmitter transmitter = (Transmitter) it.next();
268
227
269
            Iterator it2 = receivingDevice.getReceivers().iterator();
228
            Iterator it2 = receivingDevice.getReceivers().iterator();
270
            while (it2.hasNext()) {
229
            while (it2.hasNext()) {
271
230
272
                Receiver receiver = (Receiver) it2.next();
231
                Receiver receiver = (Receiver) it2.next();
273
232
274
                if (transmitter.getReceiver() == receiver) {
233
                if (transmitter.getReceiver() == receiver) {
275
234
276
                    // transmitter.close();
235
                    // transmitter.close();
277
                    closeTransmitters.add(transmitter);
236
                    closeTransmitters.add(transmitter);
278
237
279
                    // receiver.close();
238
                    // receiver.close();
280
                    closeReceivers.add(receiver);
239
                    closeReceivers.add(receiver);
281
                }
240
                }
282
            }
241
            }
283
        }
242
        }
284
243
285
        int i;
244
        int i;
286
        for (i = 0; i < closeTransmitters.size(); ++i) {
245
        for (i = 0; i < closeTransmitters.size(); ++i) {
287
            Transmitter transmitter = (Transmitter) closeTransmitters.get(i);
246
            Transmitter transmitter = (Transmitter) closeTransmitters.get(i);
288
            transmitter.close();
247
            transmitter.close();
289
        }
248
        }
290
249
291
        for (i = 0; i < closeReceivers.size(); ++i) {
250
        for (i = 0; i < closeReceivers.size(); ++i) {
292
            Receiver receiver = (Receiver) closeReceivers.get(i);
251
            Receiver receiver = (Receiver) closeReceivers.get(i);
293
            receiver.close();
252
            receiver.close();
294
        }
253
        }
295
254
296
        int num_transmitters = (transmittingDevice.getTransmitters() != null) ? transmittingDevice
255
        int num_transmitters = (transmittingDevice.getTransmitters() != null) ? transmittingDevice
297
                .getTransmitters().size()
256
                .getTransmitters().size()
298
                : 0;
257
                : 0;
299
        int num_receivers = (transmittingDevice.getReceivers() != null) ? transmittingDevice
258
        int num_receivers = (transmittingDevice.getReceivers() != null) ? transmittingDevice
300
                .getReceivers().size()
259
                .getReceivers().size()
301
                : 0;
260
                : 0;
302
261
303
        if (num_transmitters == 0 && num_receivers == 0) {
262
        if (num_transmitters == 0 && num_receivers == 0) {
304
            transmittingDevice.close();
263
            transmittingDevice.close();
305
        }
264
        }
306
265
307
        num_transmitters = (receivingDevice.getTransmitters() != null) ? receivingDevice
266
        num_transmitters = (receivingDevice.getTransmitters() != null) ? receivingDevice
308
                .getTransmitters().size()
267
                .getTransmitters().size()
309
                : 0;
268
                : 0;
310
        num_receivers = (receivingDevice.getReceivers() != null) ? receivingDevice
269
        num_receivers = (receivingDevice.getReceivers() != null) ? receivingDevice
311
                .getReceivers().size()
270
                .getReceivers().size()
312
                : 0;
271
                : 0;
313
272
314
        if (num_transmitters == 0 && num_receivers == 0) {
273
        if (num_transmitters == 0 && num_receivers == 0) {
315
            receivingDevice.close();
274
            receivingDevice.close();
316
        }
275
        }
317
    }
276
    }
318
277
319
    private void logicallyDisconnectDevices(MidiDevice transmittingDevice,
278
    private void logicallyDisconnectDevices(MidiDevice transmittingDevice,
320
            MidiDevice receivingDevice) {
279
            MidiDevice receivingDevice) {
321
        logicalConnections.remove(new LogicalConnection(transmittingDevice,
280
        logicalConnections.remove(new LogicalConnection(transmittingDevice,
322
                receivingDevice));
281
                receivingDevice));
323
    }
282
    }
324
283
325
    public void disconnectDevice(MidiDevice midiDevice) {
284
    public void disconnectDevice(MidiDevice midiDevice) {
326
285
327
        Vector matches = new Vector();
286
        Vector matches = new Vector();
328
287
329
        Iterator it = logicalConnections.iterator();
288
        Iterator it = logicalConnections.iterator();
330
289
331
        while (it.hasNext()) {
290
        while (it.hasNext()) {
332
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
291
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
333
            if (logicalConnection.getSourceDevice() == midiDevice
292
            if (logicalConnection.getSourceDevice() == midiDevice
334
                    || logicalConnection.getTargetDevice() == midiDevice) {
293
                    || logicalConnection.getTargetDevice() == midiDevice) {
335
                matches.add(logicalConnection);
294
                matches.add(logicalConnection);
336
            }
295
            }
337
        }
296
        }
338
297
339
        it = matches.iterator();
298
        it = matches.iterator();
340
299
341
        while (it.hasNext()) {
300
        while (it.hasNext()) {
342
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
301
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
343
            disconnectDevices(logicalConnection.getSourceDevice(),
302
            disconnectDevices(logicalConnection.getSourceDevice(),
344
                    logicalConnection.getTargetDevice());
303
                    logicalConnection.getTargetDevice());
345
        }
304
        }
346
    }
305
    }
347
306
348
    public void disconnectAll() {
307
    public void disconnectAll() {
349
308
350
        Iterator it = midiReadDevices.iterator();
309
        Iterator it = midiReadDevices.iterator();
351
310
352
        while (it.hasNext()) {
311
        while (it.hasNext()) {
353
312
354
            MidiDevice transmittingDevice = (MidiDevice) it.next();
313
            MidiDevice transmittingDevice = (MidiDevice) it.next();
355
314
356
            Iterator it2 = midiWriteDevices.iterator();
315
            Iterator it2 = midiWriteDevices.iterator();
357
            while (it2.hasNext()) {
316
            while (it2.hasNext()) {
358
317
359
                MidiDevice receivingDevice = (MidiDevice) it2.next();
318
                MidiDevice receivingDevice = (MidiDevice) it2.next();
360
319
361
                disconnectDevices(transmittingDevice, receivingDevice);
320
                disconnectDevices(transmittingDevice, receivingDevice);
362
            }
321
            }
363
        }
322
        }
364
    }
323
    }
365
324
366
    public void setPortsReleased(boolean portsReleased) {
325
    public void setPortsReleased(boolean portsReleased) {
367
326
368
        this.portsReleased = portsReleased;
327
        this.portsReleased = portsReleased;
369
328
370
        if (portsReleased) {
329
        if (portsReleased) {
371
330
372
            Iterator it = logicalConnections.iterator();
331
            Iterator it = logicalConnections.iterator();
373
332
374
            while (it.hasNext()) {
333
            while (it.hasNext()) {
375
                LogicalConnection logicalConnection = (LogicalConnection) it
334
                LogicalConnection logicalConnection = (LogicalConnection) it
376
                        .next();
335
                        .next();
377
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
336
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
378
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
337
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
379
338
380
                if (!(sourceDevice instanceof VirtualMidiDevice)
339
                if (!(sourceDevice instanceof VirtualMidiDevice)
381
                        || !(targetDevice instanceof VirtualMidiDevice)) {
340
                        || !(targetDevice instanceof VirtualMidiDevice)) {
382
341
383
                    physicallyDisconnectDevices(sourceDevice, targetDevice);
342
                    physicallyDisconnectDevices(sourceDevice, targetDevice);
384
                }
343
                }
385
            }
344
            }
386
        } else {
345
        } else {
387
346
388
            Iterator it = logicalConnections.iterator();
347
            Iterator it = logicalConnections.iterator();
389
348
390
            while (it.hasNext()) {
349
            while (it.hasNext()) {
391
                LogicalConnection logicalConnection = (LogicalConnection) it
350
                LogicalConnection logicalConnection = (LogicalConnection) it
392
                        .next();
351
                        .next();
393
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
352
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
394
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
353
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
395
354
396
                if (!(sourceDevice instanceof VirtualMidiDevice)
355
                if (!(sourceDevice instanceof VirtualMidiDevice)
397
                        || !(targetDevice instanceof VirtualMidiDevice)) {
356
                        || !(targetDevice instanceof VirtualMidiDevice)) {
398
                    physicallyConnectDevices(sourceDevice, targetDevice);
357
                    physicallyConnectDevices(sourceDevice, targetDevice);
399
                }
358
                }
400
            }
359
            }
401
        }
360
        }
402
361
403
        setChanged();
362
        setChanged();
404
        notifyObservers();
363
        notifyObservers();
405
        clearChanged();
364
        clearChanged();
406
    }
365
    }
407
366
408
    public boolean getPortsReleased() {
367
    public boolean getPortsReleased() {
409
        return portsReleased;
368
        return portsReleased;
410
    }
369
    }
411
370
412
    public int devicesConnected(MidiDevice transmittingDevice,
371
    public int devicesConnected(MidiDevice transmittingDevice,
413
            MidiDevice receivingDevice) {
372
            MidiDevice receivingDevice) {
414
373
415
        try {
374
        try {
416
375
417
            Iterator it = transmittingDevice.getTransmitters().iterator();
376
            Iterator it = transmittingDevice.getTransmitters().iterator();
418
377
419
            while (it.hasNext()) {
378
            while (it.hasNext()) {
420
                Transmitter transmitter = (Transmitter) it.next();
379
                Transmitter transmitter = (Transmitter) it.next();
421
380
422
                Iterator it2 = receivingDevice.getReceivers().iterator();
381
                Iterator it2 = receivingDevice.getReceivers().iterator();
423
382
424
                while (it2.hasNext()) {
383
                while (it2.hasNext()) {
425
384
426
                    Receiver receiver = (Receiver) it2.next();
385
                    Receiver receiver = (Receiver) it2.next();
427
386
428
                    if (transmitter.getReceiver() == receiver) {
387
                    if (transmitter.getReceiver() == receiver) {
429
                        return PHYSICALLY_CONNECTED;
388
                        return PHYSICALLY_CONNECTED;
430
                    }
389
                    }
431
                }
390
                }
432
            }
391
            }
433
        } catch (Exception e) {
392
        } catch (Exception e) {
434
            return DISCONNECTED;
393
            return DISCONNECTED;
435
        }
394
        }
436
395
437
        if (logicalConnections.contains(new LogicalConnection(
396
        if (logicalConnections.contains(new LogicalConnection(
438
                transmittingDevice, receivingDevice))) {
397
                transmittingDevice, receivingDevice))) {
439
            return LOGICALLY_CONNECTED;
398
            return LOGICALLY_CONNECTED;
440
        }
399
        }
441
400
442
        return DISCONNECTED;
401
        return DISCONNECTED;
443
    }
402
    }
444
403
445
    public void reorder() {
-
 
446
        midiReadDevices.removeAll(midiDeviceManager
-
 
447
                .getSelectedMidiReadDevices());
-
 
448
        midiWriteDevices.removeAll(midiDeviceManager
-
 
449
                .getSelectedMidiWriteDevices());
-
 
450
        midiReadDevices.removeAll(midiRouterDeviceManager
-
 
451
                .getMidiRouterDevices());
-
 
452
        midiWriteDevices.removeAll(midiRouterDeviceManager
-
 
453
                .getMidiRouterDevices());
-
 
454
        midiReadDevices.removeAll(midiFilterManager.getMidiFilterDevices());
-
 
455
        midiWriteDevices.removeAll(midiFilterManager.getMidiFilterDevices());
-
 
456
-
 
457
        /*
-
 
458
         * midiReadDevices.removeAll(midiMapManager.getMidiMapDevices());
-
 
459
         * midiWriteDevices.removeAll(midiMapManager.getMidiMapDevices());
-
 
460
         */
-
 
461
-
 
462
        Iterator it = midiDeviceManager.getMidiReadDevices().iterator();
-
 
463
        while (it.hasNext()) {
-
 
464
-
 
465
            Object object = it.next();
-
 
466
-
 
467
            if (midiDeviceManager.getSelectedMidiReadDevices().contains(object)) {
-
 
468
                midiReadDevices.add(object);
-
 
469
            }
-
 
470
        }
-
 
471
-
 
472
        it = midiDeviceManager.getMidiWriteDevices().iterator();
-
 
473
        while (it.hasNext()) {
-
 
474
-
 
475
            Object object = it.next();
-
 
476
-
 
477
            if (midiDeviceManager.getSelectedMidiWriteDevices()
-
 
478
                    .contains(object)) {
-
 
479
                midiWriteDevices.add(object);
-
 
480
            }
-
 
481
        }
-
 
482
-
 
483
        midiReadDevices.addAll(midiRouterDeviceManager.getMidiRouterDevices());
-
 
484
        midiWriteDevices.addAll(midiRouterDeviceManager.getMidiRouterDevices());
-
 
485
        midiReadDevices.addAll(midiFilterManager.getMidiFilterDevices());
-
 
486
        midiWriteDevices.addAll(midiFilterManager.getMidiFilterDevices());
-
 
487
-
 
488
        /*
-
 
489
         * midiReadDevices.addAll(midiMapManager.getMidiMapDevices());
-
 
490
         * midiWriteDevices.addAll(midiMapManager.getMidiMapDevices());
-
 
491
         */
-
 
492
-
 
493
        setChanged();
-
 
494
        notifyObservers(midiReadDevices);
-
 
495
        clearChanged();
-
 
496
-
 
497
        setChanged();
-
 
498
        notifyObservers(midiWriteDevices);
-
 
499
        clearChanged();
-
 
500
    }
-
 
501
-
 
502
    public void update(Observable observable, Object object) {
-
 
503
        if (observable == midiDeviceManager
-
 
504
                || observable == midiRouterDeviceManager
-
 
505
                || observable == midiFilterManager
-
 
506
        /* || observable == midiMapManager */) {
-
 
507
-
 
508
            MidiDevice midiDevice = (MidiDevice) object;
-
 
509
-
 
510
            if (midiReadDevices.contains(midiDevice)
-
 
511
                    || midiWriteDevices.contains(midiDevice)) {
-
 
512
                disconnectDevice(midiDevice);
-
 
513
                removeMidiReadDevice(midiDevice);
-
 
514
                removeMidiWriteDevice(midiDevice);
-
 
515
            } else {
-
 
516
                reorder();
-
 
517
            }
-
 
518
        }
-
 
519
    }
-
 
520
404
521
    public static class LogicalConnection {
405
    public static class LogicalConnection {
522
406
523
        private MidiDevice sourceDevice;
407
        private MidiDevice sourceDevice;
524
408
525
        private MidiDevice targetDevice;
409
        private MidiDevice targetDevice;
526
410
527
        public LogicalConnection(MidiDevice sourceDevice,
411
        public LogicalConnection(MidiDevice sourceDevice,
528
                MidiDevice targetDevice) {
412
                MidiDevice targetDevice) {
529
            this.sourceDevice = sourceDevice;
413
            this.sourceDevice = sourceDevice;
530
            this.targetDevice = targetDevice;
414
            this.targetDevice = targetDevice;
531
        }
415
        }
532
416
533
        public MidiDevice getSourceDevice() {
417
        public MidiDevice getSourceDevice() {
534
            return sourceDevice;
418
            return sourceDevice;
535
        }
419
        }
536
420
537
        public MidiDevice getTargetDevice() {
421
        public MidiDevice getTargetDevice() {
538
            return targetDevice;
422
            return targetDevice;
539
        }
423
        }
540
424
541
        public boolean equals(Object object) {
425
        public boolean equals(Object object) {
542
            if (object instanceof LogicalConnection) {
426
            if (object instanceof LogicalConnection) {
543
                if (((LogicalConnection) object).getSourceDevice() == sourceDevice
427
                if (((LogicalConnection) object).getSourceDevice() == sourceDevice
544
                        && ((LogicalConnection) object).getTargetDevice() == targetDevice) {
428
                        && ((LogicalConnection) object).getTargetDevice() == targetDevice) {
545
                    return true;
429
                    return true;
546
                }
430
                }
547
            }
431
            }
548
            return false;
432
            return false;
549
        }
433
        }
550
    }
434
    }
551
}
435
}
552
 
436