Subversion Repositories svn.mios

Rev

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

Rev 624 Rev 628
Line 1... Line 1...
1
/*
1
/*
2
 * @(#)MidiDeviceRouting.java   beta7   2006/04/23
2
 * @(#)MidiDeviceRouting.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;
21
package org.midibox.midi;
22
22
-
 
23
import java.util.ArrayList;
23
import java.util.Collection;
24
import java.util.Collection;
24
import java.util.Iterator;
25
import java.util.Iterator;
25
import java.util.List;
26
import java.util.List;
26
import java.util.ArrayList;
-
 
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.MidiSystem;
-
 
33
import javax.sound.midi.MidiUnavailableException;
32
import javax.sound.midi.MidiUnavailableException;
34
import javax.sound.midi.Receiver;
33
import javax.sound.midi.Receiver;
35
import javax.sound.midi.Sequencer;
-
 
36
import javax.sound.midi.Synthesizer;
-
 
37
import javax.sound.midi.Transmitter;
34
import javax.sound.midi.Transmitter;
38
import javax.swing.JOptionPane;
-
 
39
35
-
 
36
public class MidiDeviceRouting extends Observable implements Observer {
-
 
37
-
 
38
    public static int DISCONNECTED = 0;
-
 
39
-
 
40
    public static int LOGICALLY_CONNECTED = 1;
-
 
41
40
import org.midibox.sidlibr.SysExControllerDevice;
42
    public static int PHYSICALLY_CONNECTED = 2;
41
43
42
import java.util.prefs.*;
-
 
43
public class MidiDeviceRouting extends Observable {
-
 
44
    private Vector midiReadDevices;
44
    private Vector midiReadDevices;
-
 
45
45
    private Vector midiWriteDevices;
46
    private Vector midiWriteDevices;
46
   
47
47
    private MidiDevice localMidiDevice;
48
    private Vector logicalConnections;
48
    private MidiDevice inputMidiDevice;
-
 
49
    private MidiDevice outputMidiDevice;
-
 
50
   
49
51
    public MidiDeviceRouting(MidiDevice localMidiDevice) {
-
 
52
        this.localMidiDevice = localMidiDevice;
50
    private MidiDeviceManager midiDeviceManager;
53
       
51
-
 
52
    private MidiRouterDeviceManager midiRouterDeviceManager;
-
 
53
-
 
54
    private MidiFilterDeviceManager midiFilterManager;
-
 
55
-
 
56
    /*
-
 
57
     * private MidiMapDeviceManager midiMapManager;
-
 
58
     */
-
 
59
-
 
60
    private boolean portsReleased;
-
 
61
-
 
62
    public MidiDeviceRouting() {
54
        midiReadDevices = new Vector();
63
        midiReadDevices = new Vector();
55
        midiWriteDevices = new Vector();   
64
        midiWriteDevices = new Vector();
56
       
65
57
        rescanDevices();
66
        logicalConnections = new Vector();
-
 
67
58
        if (getMidiReadDevices().size() < 1) {
68
        midiDeviceManager = new MidiDeviceManager();
59
            JOptionPane.showMessageDialog(null,"Your system does not appear to have any MIDI-devices connected (please close program)!.","Error",JOptionPane.OK_OPTION);
69
        midiDeviceManager.addObserver(this);
60
        } else {
70
61
            inputMidiDevice = (MidiDevice)getMidiReadDevices().elementAt(0);
71
        midiRouterDeviceManager = new MidiRouterDeviceManager();
62
            outputMidiDevice = (MidiDevice)getMidiWriteDevices().elementAt(0);     
72
        midiRouterDeviceManager.addObserver(this);
-
 
73
63
            connectDevices(inputMidiDevice, localMidiDevice);
74
        midiFilterManager = new MidiFilterDeviceManager();
64
            connectDevices(localMidiDevice, outputMidiDevice); 
75
        midiFilterManager.addObserver(this);
-
 
76
65
        }
77
        /*
-
 
78
         * midiMapManager = new MidiMapDeviceManager();
-
 
79
         * midiMapManager.addObserver(this);
-
 
80
         */
66
    }
81
    }
67
82
68
    public Vector getMidiReadDevices() {
83
    public Vector getMidiReadDevices() {
69
        return midiReadDevices;
84
        return midiReadDevices;
70
    }
85
    }
71
86
72
    public Vector getMidiWriteDevices() {
87
    public Vector getMidiWriteDevices() {
73
        return midiWriteDevices;
88
        return midiWriteDevices;
74
    }
89
    }
75
   
90
76
    public int getInputDeviceIndex() {
91
    public void addMidiReadDevice(MidiDevice midiDevice) {
77
        return midiReadDevices.indexOf(inputMidiDevice);
92
        midiReadDevices.add(midiDevice);
-
 
93
        setChanged();
-
 
94
        notifyObservers(midiReadDevices);
-
 
95
        clearChanged();
78
    }
96
    }
79
   
97
80
    public int getOutputDeviceIndex() {
98
    public void addMidiWriteDevice(MidiDevice midiDevice) {
81
        return midiWriteDevices.indexOf(outputMidiDevice);     
99
        midiWriteDevices.add(midiDevice);
-
 
100
        setChanged();
-
 
101
        notifyObservers(midiReadDevices);
-
 
102
        clearChanged();
82
    }
103
    }
83
104
84
    public void reconnectAllDevices() {     // This function is a workaround for the SysEx (string length doesn't reset) bug in the javax.sound.midi class
-
 
85
        System.out.println("Now reconnecting!");
-
 
86
        disconnectDevices(inputMidiDevice, localMidiDevice);
105
    public void addMidiReadDevices(Collection midiDevices) {
87
        System.out.println("Disconnection of " + inputMidiDevice.getDeviceInfo() + " succesfull");
-
 
88
        disconnectDevices(localMidiDevice, outputMidiDevice);
106
        Iterator it = midiDevices.iterator();
89
        System.out.println("Disconnection of " + outputMidiDevice.getDeviceInfo() + " succesfull");
-
 
90
107
91
        connectDevices(inputMidiDevice, localMidiDevice);
108
        while (it.hasNext()) {
92
        System.out.println("Reconnection of " + inputMidiDevice.getDeviceInfo() + " succesfull");
109
            MidiDevice midiDevice = (MidiDevice) it.next();
93
        connectDevices(localMidiDevice, outputMidiDevice);
110
            addMidiReadDevice(midiDevice);
94
        System.out.println("Reconnection of " + outputMidiDevice.getDeviceInfo() + " succesfull");     
-
 
-
 
111
        }
95
    }
112
    }
96
   
113
97
    public int findInputDeviceHash(int hash) {
114
    public void addMidiWriteDevices(Collection midiDevices) {
98
        int temp = -1;
115
        Iterator it = midiDevices.iterator();
-
 
116
99
        for(int i=0;i<midiReadDevices.size();i++) {    
117
        while (it.hasNext()) {
100
            if (((MidiDevice)midiReadDevices.elementAt(i)).getDeviceInfo().toString().hashCode()==hash) {
118
            MidiDevice midiDevice = (MidiDevice) it.next();
101
                temp = i;
119
            addMidiWriteDevice(midiDevice);
102
                break;
-
 
103
            }
120
        }
104
        }
121
    }
-
 
122
-
 
123
    public void removeMidiReadDevice(MidiDevice midiDevice) {
-
 
124
        midiReadDevices.remove(midiDevice);
105
        return temp;
125
        setChanged();
-
 
126
        notifyObservers(midiReadDevices);
-
 
127
        clearChanged();
106
    }
128
    }
107
   
129
108
    public int findOuputDeviceHash(int hash) {
130
    public void removeMidiWriteDevice(MidiDevice midiDevice) {
-
 
131
        midiWriteDevices.remove(midiDevice);
109
        int temp = -1;
132
        setChanged();
110
        for(int i=0;i<midiWriteDevices.size();i++) {           
133
        notifyObservers(midiWriteDevices);
111
            if (((MidiDevice)midiWriteDevices.elementAt(i)).getDeviceInfo().toString().hashCode()==hash) {
-
 
112
                temp = i;
-
 
113
                break;
134
        clearChanged();
114
            }
135
    }
-
 
136
-
 
137
    public void removeMidiReadDevices(Collection midiDevices) {
-
 
138
        Iterator it = midiDevices.iterator();
-
 
139
-
 
140
        while (it.hasNext()) {
-
 
141
            MidiDevice midiDevice = (MidiDevice) it.next();
-
 
142
            removeMidiReadDevice(midiDevice);
115
        }
143
        }
116
        return temp;
-
 
117
    }
144
    }
118
   
145
119
    public int getInputDeviceHash() {
146
    public void removeMidiWriteDevices(Collection midiDevices) {
120
        return inputMidiDevice.getDeviceInfo().toString().hashCode();
147
        Iterator it = midiDevices.iterator();
121
    }
-
 
122
   
148
123
    public int getOutputDeviceHash() {
149
        while (it.hasNext()) {
124
        return outputMidiDevice.getDeviceInfo().toString().hashCode();
150
            MidiDevice midiDevice = (MidiDevice) it.next();
-
 
151
            removeMidiWriteDevice(midiDevice);
-
 
152
        }
125
    }
153
    }
126
   
154
127
    public void closeMidi() {
155
    public void removeAllMidiReadDevices() {
128
        disconnectDevices(inputMidiDevice, localMidiDevice);
156
        midiReadDevices.removeAllElements();
-
 
157
        setChanged();
129
        disconnectDevices(localMidiDevice, outputMidiDevice);
158
        notifyObservers(midiReadDevices);
-
 
159
        clearChanged();
130
    }
160
    }
131
       
161
132
    public void setInputDevice(int index) {    
162
    public void removeAllMidiWriteDevices() {
133
        if (index != getInputDeviceIndex()) {
-
 
134
            disconnectDevices(inputMidiDevice, localMidiDevice);
163
        midiWriteDevices.removeAllElements();
135
            inputMidiDevice = (MidiDevice)midiReadDevices.elementAt(index);
-
 
136
            connectDevices(inputMidiDevice, localMidiDevice);
-
 
137
            System.out.println("Connection of " + inputMidiDevice.getDeviceInfo() + " succesfull");
-
 
138
            setChanged();
164
        setChanged();
139
            notifyObservers();
165
        notifyObservers(midiWriteDevices);
140
            clearChanged();    
166
        clearChanged();
141
        }
167
    }
-
 
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
-
 
186
    public void connectDevices(MidiDevice transmittingDevice,
-
 
187
            MidiDevice receivingDevice) {
-
 
188
-
 
189
        if (devicesConnected(transmittingDevice, receivingDevice) == PHYSICALLY_CONNECTED) {
-
 
190
            return;
-
 
191
        }
-
 
192
-
 
193
        logicallyConnectDevices(transmittingDevice, receivingDevice);
-
 
194
-
 
195
        if (!portsReleased
-
 
196
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
-
 
197
-
 
198
            physicallyConnectDevices(transmittingDevice, receivingDevice);
142
    }
199
        }
143
   
200
144
    public void setOutputDevice(int index) {
-
 
145
        if (index != getOutputDeviceIndex()) {
-
 
146
            disconnectDevices(localMidiDevice, outputMidiDevice);
-
 
147
            outputMidiDevice = (MidiDevice)midiWriteDevices.elementAt(index);
-
 
148
            connectDevices(localMidiDevice, outputMidiDevice);
-
 
149
            System.out.println("Connection of " + outputMidiDevice.getDeviceInfo() + " succesfull");
-
 
150
            setChanged();
201
        setChanged();
151
            notifyObservers();
202
        notifyObservers();
152
            clearChanged();
203
        clearChanged();
153
        }
-
 
154
    }
204
    }
155
   
205
156
    private void connectDevices(MidiDevice transmittingDevice,
206
    private void physicallyConnectDevices(MidiDevice transmittingDevice,
157
            MidiDevice receivingDevice) {
207
            MidiDevice receivingDevice) {
158
        try {
208
        try {
159
            if (!receivingDevice.isOpen()) {
209
            if (!receivingDevice.isOpen()) {
160
                receivingDevice.open();
210
                receivingDevice.open();
161
            }
211
            }
Line 171... Line 221...
171
        } catch (MidiUnavailableException e) {
221
        } catch (MidiUnavailableException e) {
172
           
222
173
        } catch (NullPointerException e) {
223
        } catch (NullPointerException e) {
174
           
224
175
        }
225
        }
-
 
226
176
    }
227
    }
177
   
228
178
    private void disconnectDevices(MidiDevice transmittingDevice,
229
    private void logicallyConnectDevices(MidiDevice transmittingDevice,
179
            MidiDevice receivingDevice) {
230
            MidiDevice receivingDevice) {
-
 
231
        logicalConnections.add(new LogicalConnection(transmittingDevice,
-
 
232
                receivingDevice));
-
 
233
    }
180
234
-
 
235
    public void disconnectDevices(MidiDevice transmittingDevice,
-
 
236
            MidiDevice receivingDevice) {
-
 
237
-
 
238
        if (devicesConnected(transmittingDevice, receivingDevice) == DISCONNECTED) {
-
 
239
            return;
-
 
240
        }
-
 
241
-
 
242
        logicallyDisconnectDevices(transmittingDevice, receivingDevice);
-
 
243
        if (!portsReleased
-
 
244
                || ((transmittingDevice instanceof VirtualMidiDevice) && (receivingDevice instanceof VirtualMidiDevice))) {
-
 
245
-
 
246
            physicallyDisconnectDevices(transmittingDevice, receivingDevice);
-
 
247
        }
-
 
248
        setChanged();
-
 
249
        notifyObservers();
-
 
250
        clearChanged();
-
 
251
    }
-
 
252
-
 
253
    private void physicallyDisconnectDevices(MidiDevice transmittingDevice,
-
 
254
            MidiDevice receivingDevice) {
-
 
255
181
        // TK: to avoid a "java.util.ConcurrentModificationException" under MacOS (using mmj)
256
        // TK: to avoid a "java.util.ConcurrentModificationException" under
-
 
257
        // MacOS (using mmj)
182
        // when a transmitter or receiver is closed, we put them into a list and execute close()
258
        // when a transmitter or receiver is closed, we put them into a list and
-
 
259
        // execute close()
183
        // after iteration
260
        // after iteration
184
        List closeTransmitters = new ArrayList();
261
        List closeTransmitters = new ArrayList();
185
        List closeReceivers = new ArrayList();
262
        List closeReceivers = new ArrayList();
186
263
187
        Iterator it = transmittingDevice.getTransmitters().iterator();
264
        Iterator it = transmittingDevice.getTransmitters().iterator();
Line 196... Line 273...
196
273
197
                if (transmitter.getReceiver() == receiver) {
274
                if (transmitter.getReceiver() == receiver) {
198
275
199
                    // transmitter.close();
276
                    // transmitter.close();
200
                    closeTransmitters.add(transmitter);
277
                    closeTransmitters.add(transmitter);
201
                   
-
 
202
                    System.out.println("Disconnecting transmitter: " + transmittingDevice.getDeviceInfo());
-
 
203
-
 
204
                    // TK: for mmj under MacOS it is required to check, if getTransmitters()/getReceivers() are != null
-
 
205
                    int num_transmitters = (transmittingDevice.getTransmitters() != null) ? transmittingDevice.getTransmitters().size() : 0;
-
 
206
                    int num_receivers = (transmittingDevice.getReceivers() != null) ? transmittingDevice.getReceivers().size() : 0;
-
 
207
                    System.out.println("Number of transmitters: " + num_transmitters);
-
 
208
                    System.out.println("Number of receivers: " + num_receivers);
-
 
209
278
210
                    // receiver.close();
279
                    // receiver.close();
211
                    closeReceivers.add(receiver);
280
                    closeReceivers.add(receiver);
212
                   
-
 
213
                    System.out.println("Disconnecting receiver: " + receivingDevice.getDeviceInfo());
-
 
214
-
 
215
                    // TK: for mmj under MacOS it is required to check, if getTransmitters()/getReceivers() are != null
-
 
216
                    num_transmitters = (receivingDevice.getTransmitters() != null) ? receivingDevice.getTransmitters().size() : 0;
-
 
217
                    num_receivers = (receivingDevice.getReceivers() != null) ? receivingDevice.getReceivers().size() : 0;
-
 
218
                    System.out.println("Number of transmitters: " + num_transmitters);
-
 
219
                    System.out.println("Number of receivers: " + num_receivers);
-
 
220
                   
-
 
221
                }
281
                }
222
            }
282
            }
223
        }
283
        }
224
284
225
        int i;
285
        int i;
Line 249... Line 309...
249
                : 0;
309
                : 0;
250
        num_receivers = (receivingDevice.getReceivers() != null) ? receivingDevice
310
        num_receivers = (receivingDevice.getReceivers() != null) ? receivingDevice
251
                .getReceivers().size()
311
                .getReceivers().size()
252
                : 0;
312
                : 0;
253
313
254
        if (receivingDevice.getTransmitters().size() == 0
314
        if (num_transmitters == 0 && num_receivers == 0) {
255
                && receivingDevice.getReceivers().size() == 0) {
-
 
256
            receivingDevice.close();
315
            receivingDevice.close();
257
        }
316
        }
258
    }
317
    }
259
   
318
-
 
319
    private void logicallyDisconnectDevices(MidiDevice transmittingDevice,
260
    public void rescanDevices() {
320
            MidiDevice receivingDevice) {
261
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
321
        logicalConnections.remove(new LogicalConnection(transmittingDevice,
-
 
322
                receivingDevice));
-
 
323
    }
262
324
263
        Vector tempWriteDevices = new Vector();
-
 
264
        Vector tempReadDevices = new Vector();
325
    public void disconnectDevice(MidiDevice midiDevice) {
265
       
326
266
        for (int i = 0; i < infos.length; i++) {
327
        Vector matches = new Vector();
267
            try {
-
 
268
                MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
-
 
269
               
328
270
                if (!(device instanceof Sequencer)
-
 
271
                        && !(device instanceof Synthesizer)) {
329
        Iterator it = logicalConnections.iterator();
272
330
-
 
331
        while (it.hasNext()) {
-
 
332
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
273
                    int noReceivers = device.getMaxReceivers();
333
            if (logicalConnection.getSourceDevice() == midiDevice
274
                    int noTransmitters = device.getMaxTransmitters();
334
                    || logicalConnection.getTargetDevice() == midiDevice) {
-
 
335
                matches.add(logicalConnection);
-
 
336
            }
-
 
337
        }
275
338
-
 
339
        it = matches.iterator();
-
 
340
276
                    if (noReceivers != 0) {
341
        while (it.hasNext()) {
-
 
342
            LogicalConnection logicalConnection = (LogicalConnection) it.next();
-
 
343
            disconnectDevices(logicalConnection.getSourceDevice(),
-
 
344
                    logicalConnection.getTargetDevice());
-
 
345
        }
-
 
346
    }
-
 
347
-
 
348
    public void disconnectAll() {
-
 
349
-
 
350
        Iterator it = midiReadDevices.iterator();
-
 
351
-
 
352
        while (it.hasNext()) {
-
 
353
-
 
354
            MidiDevice transmittingDevice = (MidiDevice) it.next();
-
 
355
277
                        tempWriteDevices.add(device);
356
            Iterator it2 = midiWriteDevices.iterator();
-
 
357
            while (it2.hasNext()) {
-
 
358
-
 
359
                MidiDevice receivingDevice = (MidiDevice) it2.next();
-
 
360
-
 
361
                disconnectDevices(transmittingDevice, receivingDevice);
-
 
362
            }
-
 
363
        }
-
 
364
    }
-
 
365
-
 
366
    public void setPortsReleased(boolean portsReleased) {
-
 
367
-
 
368
        this.portsReleased = portsReleased;
-
 
369
-
 
370
        if (portsReleased) {
-
 
371
-
 
372
            Iterator it = logicalConnections.iterator();
-
 
373
-
 
374
            while (it.hasNext()) {
-
 
375
                LogicalConnection logicalConnection = (LogicalConnection) it
278
                        setChanged();
376
                        .next();
-
 
377
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
-
 
378
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
-
 
379
-
 
380
                if (!(sourceDevice instanceof VirtualMidiDevice)
-
 
381
                        || !(targetDevice instanceof VirtualMidiDevice)) {
-
 
382
-
 
383
                    physicallyDisconnectDevices(sourceDevice, targetDevice);
-
 
384
                }
-
 
385
            }
-
 
386
        } else {
-
 
387
-
 
388
            Iterator it = logicalConnections.iterator();
-
 
389
279
                        notifyObservers(device);
390
            while (it.hasNext()) {
-
 
391
                LogicalConnection logicalConnection = (LogicalConnection) it
280
                        clearChanged();
392
                        .next();
-
 
393
                MidiDevice sourceDevice = logicalConnection.getSourceDevice();
-
 
394
                MidiDevice targetDevice = logicalConnection.getTargetDevice();
-
 
395
-
 
396
                if (!(sourceDevice instanceof VirtualMidiDevice)
-
 
397
                        || !(targetDevice instanceof VirtualMidiDevice)) {
-
 
398
                    physicallyConnectDevices(sourceDevice, targetDevice);
-
 
399
                }
-
 
400
            }
281
                    }
401
        }
282
402
283
                    if (noTransmitters != 0) {
-
 
284
                        tempReadDevices.add(device);
-
 
285
                        setChanged();
403
        setChanged();
286
                        notifyObservers(device);
404
        notifyObservers();
287
                        clearChanged();
405
        clearChanged();
288
                    }
406
    }
-
 
407
-
 
408
    public boolean getPortsReleased() {
-
 
409
        return portsReleased;
289
                }
410
    }
290
            } catch (MidiUnavailableException e) {
-
 
291
411
-
 
412
    public int devicesConnected(MidiDevice transmittingDevice,
-
 
413
            MidiDevice receivingDevice) {
-
 
414
-
 
415
        try {
-
 
416
-
 
417
            Iterator it = transmittingDevice.getTransmitters().iterator();
-
 
418
-
 
419
            while (it.hasNext()) {
-
 
420
                Transmitter transmitter = (Transmitter) it.next();
-
 
421
-
 
422
                Iterator it2 = receivingDevice.getReceivers().iterator();
-
 
423
-
 
424
                while (it2.hasNext()) {
-
 
425
-
 
426
                    Receiver receiver = (Receiver) it2.next();
-
 
427
-
 
428
                    if (transmitter.getReceiver() == receiver) {
-
 
429
                        return PHYSICALLY_CONNECTED;
-
 
430
                    }
-
 
431
                }
292
            }
432
            }
-
 
433
        } catch (Exception e) {
-
 
434
            return DISCONNECTED;
293
        }
435
        }
294
       
436
-
 
437
        if (logicalConnections.contains(new LogicalConnection(
-
 
438
                transmittingDevice, receivingDevice))) {
-
 
439
            return LOGICALLY_CONNECTED;
-
 
440
        }
-
 
441
-
 
442
        return DISCONNECTED;
-
 
443
    }
-
 
444
-
 
445
    public void reorder() {
295
        midiReadDevices = tempReadDevices;     
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
296
        setChanged();
493
        setChanged();
297
        notifyObservers(midiReadDevices);
494
        notifyObservers(midiReadDevices);
298
        clearChanged();
495
        clearChanged();
299
       
496
300
        midiWriteDevices = tempWriteDevices;
-
 
301
        setChanged();
497
        setChanged();
302
        notifyObservers(midiWriteDevices);
498
        notifyObservers(midiWriteDevices);
303
        clearChanged();
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
-
 
521
    public static class LogicalConnection {
-
 
522
-
 
523
        private MidiDevice sourceDevice;
-
 
524
-
 
525
        private MidiDevice targetDevice;
-
 
526
-
 
527
        public LogicalConnection(MidiDevice sourceDevice,
-
 
528
                MidiDevice targetDevice) {
-
 
529
            this.sourceDevice = sourceDevice;
-
 
530
            this.targetDevice = targetDevice;
-
 
531
        }
-
 
532
-
 
533
        public MidiDevice getSourceDevice() {
-
 
534
            return sourceDevice;
-
 
535
        }
-
 
536
-
 
537
        public MidiDevice getTargetDevice() {
-
 
538
            return targetDevice;
-
 
539
        }
-
 
540
-
 
541
        public boolean equals(Object object) {
-
 
542
            if (object instanceof LogicalConnection) {
-
 
543
                if (((LogicalConnection) object).getSourceDevice() == sourceDevice
-
 
544
                        && ((LogicalConnection) object).getTargetDevice() == targetDevice) {
-
 
545
                    return true;
-
 
546
                }
-
 
547
            }
-
 
548
            return false;
-
 
549
        }
304
    }  
550
    }
305
}
551
}