OSDN Git Service

e0e89a410dc1f8090af7f95264bc8b1931180e16
[midichordhelper/MIDIChordHelper.git] / src / camidion / chordhelper / mididevice / MidiDeviceModel.java
1 package camidion.chordhelper.mididevice;
2
3 import javax.sound.midi.MidiDevice;
4 import javax.sound.midi.MidiSystem;
5 import javax.sound.midi.MidiUnavailableException;
6 import javax.sound.midi.Receiver;
7 import javax.sound.midi.Transmitter;
8 import javax.swing.tree.TreePath;
9
10 /**
11  * 1個の{@link MidiDevice}を表すモデル
12  */
13 public class MidiDeviceModel {
14         /**
15          * {@link javax.swing.JTree}で使用するツリー表示のパスを返します。
16          * @return ツリーパス
17          */
18         public TreePath getTreePath() { return treePath; }
19         private TreePath treePath;
20         /**
21          * このリストのMIDIデバイスの入出力タイプを返します。
22          * @return このリストのMIDIデバイスの入出力タイプ
23          */
24         public MidiDeviceInOutType getInOutType() { return ioType; }
25         private MidiDeviceInOutType ioType;
26         /**
27          * 対象MIDIデバイスを返します。
28          * @return 対象MIDIデバイス
29          */
30         public MidiDevice getMidiDevice() { return device; }
31         protected MidiDevice device;
32         /**
33          * 対象MIDIデバイスの名前を返します。
34          */
35         @Override
36         public String toString() { return device.getDeviceInfo().toString(); }
37         /**
38          * {@link Transmitter} のリストモデルを返します。サポートしていない場合はnullを返します。
39          * @return リストモデルまたはnull
40          */
41         public TransmitterListModel getTransmitterListModel() { return txListModel; }
42         private TransmitterListModel txListModel;
43         /**
44          * {@link Receiver} のリストモデルを返します。サポートしていない場合はnullを返します。
45          * @return リストモデルまたはnull
46          */
47         public ReceiverListModel getReceiverListModel() { return rxListModel; }
48         private ReceiverListModel rxListModel;
49         /**
50          * このMIDIデバイスモデルを収容しているツリーモデルを返します。
51          */
52         public MidiDeviceTreeModel getDeviceTreeModel() { return deviceTreeModel; }
53         protected MidiDeviceTreeModel deviceTreeModel;
54         /**
55          * MIDIデバイス情報からMIDIデバイスモデルを構築します。
56          *
57          * @param deviceInfo 対象MIDIデバイス情報
58          * @param deviceTreeModel 収容先のMIDIデバイスツリーモデル
59          * @throws MidiUnavailableException {@link MidiSystem#getMidiDevice(MidiDevice.Info)}からの例外
60          */
61         public MidiDeviceModel(MidiDevice.Info deviceInfo, MidiDeviceTreeModel deviceTreeModel) throws MidiUnavailableException {
62                 this(MidiSystem.getMidiDevice(deviceInfo), deviceTreeModel);
63         }
64         /**
65          * MIDIデバイスからモデルを構築します。
66          *
67          * @param device 対象MIDIデバイス
68          * @param deviceTreeModel このモデルの親ツリー
69          */
70         public MidiDeviceModel(MidiDevice device, MidiDeviceTreeModel deviceTreeModel) {
71                 this.deviceTreeModel = deviceTreeModel;
72                 this.device = device;
73                 if( device.getMaxTransmitters() != 0 ) txListModel = new TransmitterListModel(this);
74                 if( device.getMaxReceivers() != 0 ) rxListModel = new ReceiverListModel(this);
75                 ioType = MidiDeviceInOutType.getValueFor(device);
76                 treePath = new TreePath(new Object[] {deviceTreeModel, ioType ,this});
77         }
78         /**
79          * このMIDIデバイスを開きます。
80          * MIDIデバイスを {@link MidiDevice#open()} で開き、
81          * レシーバをサポートしている場合は {@link MidiDevice#getReceiver()} でレシーバを1個開きます。
82          *
83          * @throws MidiUnavailableException リソースの制約のためにデバイス開けない、
84          * またはレシーバを使用できない場合にスローされる。
85          */
86         public void open() throws MidiUnavailableException {
87                 device.open();
88                 if( rxListModel != null ) rxListModel.openSingleReceiver();
89         }
90         /**
91          * このMIDIデバイスを {@link MidiDevice#close()} で閉じます。
92          * このMIDIデバイスの{@link Receiver}に接続している他デバイスの{@link Transmitter}があれば、
93          * それらも全て閉じます。
94          */
95         public void close() {
96                 if( rxListModel != null ) rxListModel.closeTransmitters();
97                 device.close();
98         }
99 }