Subversion Repositories svn.mios

Rev

Rev 732 | Rev 739 | 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
 
722 adamjking 15
    public static final String TAG_ROOT_ELEMENT = "midiDeviceManager";
721 adamjking 16
 
732 adamjking 17
    public static final String TAG_DESELECTED_MIDI_READ_DEVICES = "deselectedMidiReadDevices";
721 adamjking 18
 
732 adamjking 19
    public static final String TAG_DESELECTED_MIDI_WRITE_DEVICES = "deselectedMidiWriteDevices";
721 adamjking 20
 
732 adamjking 21
    public static final String TAG_DESELECTED_MIDI_READ_DEVICE = "deselectedMidiReadDevice";
721 adamjking 22
 
732 adamjking 23
    public static final String TAG_DESELECTED_MIDI_WRITE_DEVICE = "deselectedMidiWriteDevice";
721 adamjking 24
 
722 adamjking 25
    protected MidiDeviceManager midiDeviceManager;
26
 
27
    public MidiDeviceManagerXML(MidiDeviceManager midiDeviceManager,
28
            String rootElementTag) {
29
 
721 adamjking 30
        super(rootElementTag);
722 adamjking 31
 
721 adamjking 32
        this.midiDeviceManager = midiDeviceManager;
722 adamjking 33
 
732 adamjking 34
        tags.add(TAG_DESELECTED_MIDI_READ_DEVICES);
35
        tags.add(TAG_DESELECTED_MIDI_WRITE_DEVICES);
36
        tags.add(TAG_DESELECTED_MIDI_READ_DEVICE);
37
        tags.add(TAG_DESELECTED_MIDI_WRITE_DEVICE);
721 adamjking 38
    }
722 adamjking 39
 
721 adamjking 40
    protected void parseElement(Element element) {
722 adamjking 41
 
721 adamjking 42
        super.parseElement(element);
722 adamjking 43
 
721 adamjking 44
        String name = element.getNodeName();
722 adamjking 45
 
721 adamjking 46
        if (name == rootElementTag) {
722 adamjking 47
 
721 adamjking 48
            midiDeviceManager.rescanDevices();
49
 
732 adamjking 50
        } else if (name == TAG_DESELECTED_MIDI_READ_DEVICES) {
722 adamjking 51
 
732 adamjking 52
        } else if (name == TAG_DESELECTED_MIDI_READ_DEVICE) {
722 adamjking 53
 
54
            int hash = Integer.parseInt(element
55
                    .getAttribute(MidiDeviceXML.ATTR_HASH_CODE));
56
 
57
            MidiDevice midiDevice = findDeviceHash(midiDeviceManager
58
                    .getMidiReadDevices(), hash);
59
 
721 adamjking 60
            if (midiDevice != null) {
722 adamjking 61
 
732 adamjking 62
                midiDeviceManager.deselectMidiReadDevice(midiDevice);
721 adamjking 63
            }
64
 
732 adamjking 65
        } else if (name == TAG_DESELECTED_MIDI_WRITE_DEVICES) {
722 adamjking 66
 
732 adamjking 67
        } else if (name == TAG_DESELECTED_MIDI_WRITE_DEVICE) {
722 adamjking 68
 
69
            int hash = Integer.parseInt(element
70
                    .getAttribute(MidiDeviceXML.ATTR_HASH_CODE));
71
 
72
            MidiDevice midiDevice = findDeviceHash(midiDeviceManager
73
                    .getMidiWriteDevices(), hash);
74
 
721 adamjking 75
            if (midiDevice != null) {
722 adamjking 76
 
732 adamjking 77
                midiDeviceManager.deselectMidiWriteDevice(midiDevice);
721 adamjking 78
            }
79
        }
80
    }
81
 
82
    public void saveXML(Node node) {
83
 
84
        super.saveXML(node);
85
 
732 adamjking 86
        Element deselectedMidiReadDevicesElement = document
87
                .createElement(TAG_DESELECTED_MIDI_READ_DEVICES);
721 adamjking 88
 
732 adamjking 89
        rootElement.appendChild(deselectedMidiReadDevicesElement);
721 adamjking 90
 
732 adamjking 91
        Iterator it = midiDeviceManager.getMidiReadDevices().iterator();
721 adamjking 92
 
93
        while (it.hasNext()) {
94
 
95
            MidiDevice midiDevice = (MidiDevice) it.next();
96
 
732 adamjking 97
            if (!midiDeviceManager.getSelectedMidiReadDevices().contains(
98
                    midiDevice)) {
721 adamjking 99
 
732 adamjking 100
                MidiDeviceXML midiDeviceXML = new MidiDeviceXML(midiDevice,
101
                        TAG_DESELECTED_MIDI_READ_DEVICE);
102
 
103
                midiDeviceXML.saveXML(deselectedMidiReadDevicesElement);
104
            }
721 adamjking 105
        }
722 adamjking 106
 
732 adamjking 107
        Element deselectedMidiWriteDevicesElement = document
108
                .createElement(TAG_DESELECTED_MIDI_WRITE_DEVICES);
721 adamjking 109
 
732 adamjking 110
        rootElement.appendChild(deselectedMidiWriteDevicesElement);
721 adamjking 111
 
732 adamjking 112
        it = midiDeviceManager.getMidiWriteDevices().iterator();
721 adamjking 113
 
114
        while (it.hasNext()) {
115
 
116
            MidiDevice midiDevice = (MidiDevice) it.next();
117
 
732 adamjking 118
            if (!midiDeviceManager.getSelectedMidiWriteDevices().contains(
119
                    midiDevice)) {
721 adamjking 120
 
732 adamjking 121
                MidiDeviceXML midiDeviceXML = new MidiDeviceXML(midiDevice,
122
                        TAG_DESELECTED_MIDI_WRITE_DEVICE);
123
 
124
                midiDeviceXML.saveXML(deselectedMidiWriteDevicesElement);
125
            }
721 adamjking 126
        }
127
    }
128
 
129
    protected MidiDevice findDeviceHash(Vector midiDevices, int hash) {
130
 
131
        Iterator it = midiDevices.iterator();
722 adamjking 132
 
721 adamjking 133
        while (it.hasNext()) {
722 adamjking 134
 
721 adamjking 135
            MidiDevice midiDevice = (MidiDevice) it.next();
722 adamjking 136
 
721 adamjking 137
            if (midiDevice.getDeviceInfo().toString().hashCode() == hash) {
722 adamjking 138
 
721 adamjking 139
                return midiDevice;
722 adamjking 140
            }
721 adamjking 141
        }
722 adamjking 142
 
721 adamjking 143
        return null;
144
    }
709 adamjking 145
}