Subversion Repositories svn.mios

Rev

Rev 709 | Rev 722 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
709 adamjking 1
package org.midibox.midi.xml;
2
 
721 adamjking 3
import java.util.Iterator;
4
import java.util.Vector;
709 adamjking 5
 
721 adamjking 6
import javax.sound.midi.MidiDevice;
7
 
8
import org.midibox.midi.MidiDeviceManager;
9
import org.midibox.utils.xml.XMLUtils;
10
import org.w3c.dom.Element;
11
import org.w3c.dom.Node;
12
 
13
public class MidiDeviceManagerXML extends XMLUtils {
14
 
15
    protected MidiDeviceManager midiDeviceManager;
16
 
17
    protected String midiDeviceManagerTag = "midiDeviceManager";
18
 
19
    protected String selectedMidiReadDevicesTag = "selectedMidiReadDevices";
20
 
21
    protected String selectedMidiWriteDevicesTag = "selectedMidiWriteDevices";
22
 
23
    protected String selectedMidiReadDeviceTag = "selectedMidiReadDevice";
24
 
25
    protected String selectedMidiWriteDeviceTag = "selectedMidiWriteDevice";
26
 
27
    protected String hashAttr = "hashCode";
28
 
29
    public MidiDeviceManagerXML(MidiDeviceManager midiDeviceManager, String rootElementTag)  {
30
 
31
        super(rootElementTag);
32
 
33
        this.midiDeviceManager = midiDeviceManager;
34
 
35
        tags.add(selectedMidiReadDevicesTag);
36
        tags.add(selectedMidiWriteDevicesTag);
37
        tags.add(selectedMidiReadDeviceTag);
38
        tags.add(selectedMidiWriteDeviceTag);
39
    }
40
 
41
 
42
    protected void parseElement(Element element) {
43
 
44
        super.parseElement(element);
45
 
46
        String name = element.getNodeName();
47
 
48
        if (name == rootElementTag) {
49
 
50
            midiDeviceManager.rescanDevices();
51
 
52
        } else if (name == selectedMidiReadDevicesTag){        
53
 
54
            midiDeviceManager.getSelectedMidiReadDevices().removeAllElements();
55
 
56
        } else if (name == selectedMidiReadDeviceTag){
57
 
58
            int hash = Integer.parseInt(element.getAttribute(hashAttr));
59
 
60
            MidiDevice midiDevice  = findDeviceHash(midiDeviceManager.getMidiReadDevices(), hash);
61
 
62
            if (midiDevice != null) {
63
 
64
                midiDeviceManager.selectMidiReadDevice(midiDevice);
65
            }
66
 
67
        } else if (name == selectedMidiWriteDevicesTag){           
68
 
69
            midiDeviceManager.getSelectedMidiWriteDevices().removeAllElements();   
70
 
71
        } else if (name == selectedMidiWriteDeviceTag){
72
 
73
            int hash = Integer.parseInt(element.getAttribute(hashAttr));
74
 
75
            MidiDevice midiDevice  = findDeviceHash(midiDeviceManager.getMidiWriteDevices(), hash);
76
 
77
            if (midiDevice != null) {
78
 
79
                midiDeviceManager.selectMidiWriteDevice(midiDevice);
80
            }
81
        }
82
    }
83
 
84
    public void saveXML(Node node) {
85
 
86
        super.saveXML(node);
87
 
88
        Element selectedMidiReadDevicesElement = document
89
                .createElement(selectedMidiReadDevicesTag);
90
 
91
        rootElement.appendChild(selectedMidiReadDevicesElement);
92
 
93
        Iterator it = midiDeviceManager.getSelectedMidiReadDevices().iterator();
94
 
95
        while (it.hasNext()) {
96
 
97
            MidiDevice midiDevice = (MidiDevice) it.next();
98
 
99
            MidiDeviceXML midiDeviceXML = new MidiDeviceXML(midiDevice, selectedMidiReadDeviceTag);
100
 
101
            midiDeviceXML.saveXML(selectedMidiReadDevicesElement);
102
        }
103
 
104
        Element selectedMidiWriteDevicesElement = document
105
                .createElement(selectedMidiWriteDevicesTag);
106
 
107
        rootElement.appendChild(selectedMidiWriteDevicesElement);
108
 
109
        it = midiDeviceManager.getSelectedMidiWriteDevices().iterator();
110
 
111
        while (it.hasNext()) {
112
 
113
            MidiDevice midiDevice = (MidiDevice) it.next();
114
 
115
            MidiDeviceXML midiDeviceXML = new MidiDeviceXML(midiDevice, selectedMidiWriteDeviceTag);
116
 
117
            midiDeviceXML.saveXML(selectedMidiWriteDevicesElement);
118
        }
119
    }
120
 
121
 
122
    protected MidiDevice findDeviceHash(Vector midiDevices, int hash) {
123
 
124
        Iterator it = midiDevices.iterator();
125
 
126
        while (it.hasNext()) {
127
 
128
            MidiDevice midiDevice = (MidiDevice) it.next();
129
 
130
            if (midiDevice.getDeviceInfo().toString().hashCode() == hash) {
131
 
132
                return midiDevice;
133
            }                      
134
        }
135
 
136
        return null;
137
    }
709 adamjking 138
}