Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MidiDeviceManagerGUI.java    beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
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
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
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
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.midi.gui;
22
 
23
import java.awt.Dimension;
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.Insets;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
739 adamjking 29
import java.util.LinkedHashMap;
628 adamjking 30
import java.util.Observable;
31
import java.util.Observer;
32
 
33
import javax.sound.midi.MidiDevice;
34
import javax.swing.JButton;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37
import javax.swing.JScrollPane;
38
import javax.swing.JTable;
39
import javax.swing.table.DefaultTableModel;
40
 
41
import org.midibox.midi.MidiDeviceManager;
42
 
43
public class MidiDeviceManagerGUI extends JPanel implements Observer,
44
        ActionListener {
45
 
46
    private MidiDeviceManager midiDeviceManager;
47
 
48
    private JTable midiReadDevices;
49
 
50
    private JTable midiWriteDevices;
51
 
52
    public MidiDeviceManagerGUI(MidiDeviceManager midiDeviceManager) {
53
        super(new GridBagLayout());
54
 
55
        this.midiDeviceManager = midiDeviceManager;
56
        midiDeviceManager.addObserver(this);
57
 
58
        GridBagConstraints gbc = new GridBagConstraints();
59
        gbc.gridx = 0;
60
        gbc.gridy = 0;
61
        gbc.gridwidth = 1;
62
        gbc.gridheight = 1;
63
        gbc.fill = GridBagConstraints.HORIZONTAL;
64
        gbc.weightx = 1.0;
65
        gbc.insets = new Insets(5, 5, 5, 5);
66
 
67
        gbc.weighty = 0.0;
68
        add(new JLabel("MIDI Devices - Readable", JLabel.CENTER), gbc);
69
        gbc.gridx++;
70
 
71
        add(new JLabel("MIDI Devices - Writeable", JLabel.CENTER), gbc);
72
        gbc.gridx--;
73
        gbc.gridy++;
74
 
75
        gbc.fill = GridBagConstraints.BOTH;
76
        gbc.weighty = 1.0;
77
        midiReadDevices = new JTable(new MidiDeviceTableModel(midiDeviceManager
78
                .getMidiReadDevices(), midiDeviceManager
79
                .getSelectedMidiReadDevices()));
80
        midiReadDevices.setPreferredScrollableViewportSize(new Dimension(200,
81
                200));
82
        midiReadDevices.setCellSelectionEnabled(false);
83
        midiReadDevices.setShowGrid(false);
84
 
85
        JScrollPane scrollPane = new JScrollPane(midiReadDevices);
86
        scrollPane
87
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
88
        add(scrollPane, gbc);
89
        gbc.gridx++;
90
 
91
        midiWriteDevices = new JTable(new MidiDeviceTableModel(
92
                midiDeviceManager.getMidiWriteDevices(), midiDeviceManager
93
                        .getSelectedMidiWriteDevices()));
94
        midiWriteDevices.setPreferredScrollableViewportSize(new Dimension(200,
95
                200));
96
        midiWriteDevices.setCellSelectionEnabled(false);
97
        midiWriteDevices.setShowGrid(false);
98
 
99
        scrollPane = new JScrollPane(midiWriteDevices);
100
        scrollPane
101
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
102
        add(scrollPane, gbc);
103
 
104
        gbc.gridx = 0;
105
        gbc.gridy++;
106
        gbc.gridwidth = 2;
107
        gbc.fill = GridBagConstraints.NONE;
108
        gbc.weightx = 0.0;
109
        gbc.weighty = 0.0;
110
        gbc.anchor = GridBagConstraints.WEST;
111
 
112
        JButton button = new JButton("Rescan Devices");
113
        button.setActionCommand("rescan");
114
        button.addActionListener(this);
115
        add(button, gbc);
116
    }
117
 
118
    public void update(Observable observable, Object object) {
119
        if (observable == midiDeviceManager) {
120
            ((DefaultTableModel) midiReadDevices.getModel())
121
                    .fireTableDataChanged();
122
            ((DefaultTableModel) midiWriteDevices.getModel())
123
                    .fireTableDataChanged();
124
        }
125
    }
126
 
127
    public void actionPerformed(ActionEvent ae) {
128
        if (ae.getActionCommand().equals("rescan")) {
129
            midiDeviceManager.rescanDevices();
130
        }
131
    }
132
 
133
    class MidiDeviceTableModel extends DefaultTableModel {
134
        private String[] headers = { "Selected", "MIDI Device Name" };
135
 
739 adamjking 136
        private LinkedHashMap midiDevices;
628 adamjking 137
 
739 adamjking 138
        private LinkedHashMap selectedMidiDevices;
628 adamjking 139
 
739 adamjking 140
        public MidiDeviceTableModel(LinkedHashMap midiDevices,
141
                LinkedHashMap selectedMidiDevices) {
628 adamjking 142
            this.midiDevices = midiDevices;
143
            this.selectedMidiDevices = selectedMidiDevices;
144
        }
145
 
146
        public int getColumnCount() {
147
            return headers.length;
148
        }
149
 
150
        public int getRowCount() {
151
            if (midiDevices != null) {
152
                return midiDevices.size();
153
            }
154
            return 0;
155
        }
156
 
157
        public String getColumnName(int col) {
158
            return headers[col];
159
        }
160
 
161
        public Object getValueAt(int row, int col) {
162
 
739 adamjking 163
            MidiDevice midiDevice = (MidiDevice) midiDevices.values().toArray()[row];
628 adamjking 164
 
165
            if (col == 0) {
739 adamjking 166
                return new Boolean(selectedMidiDevices
167
                        .containsValue(midiDevices.values().toArray()[row]));
628 adamjking 168
            } else if (col == 1) {
169
                return midiDevice.getDeviceInfo().getName();
170
            }
171
            return null;
172
        }
173
 
174
        public void setValueAt(Object aValue, int row, int column) {
175
            if (column == 0) {
176
                if (((Boolean) aValue).booleanValue()) {
177
 
178
                    if (midiDevices == midiDeviceManager.getMidiReadDevices()) {
179
                        midiDeviceManager
180
                                .selectMidiReadDevice((MidiDevice) midiDevices
739 adamjking 181
                                        .values().toArray()[row]);
182
 
628 adamjking 183
                    } else {
184
                        midiDeviceManager
185
                                .selectMidiWriteDevice((MidiDevice) midiDevices
739 adamjking 186
                                        .values().toArray()[row]);
628 adamjking 187
                    }
188
                } else {
189
                    if (midiDevices == midiDeviceManager.getMidiReadDevices()) {
190
                        midiDeviceManager
191
                                .deselectMidiReadDevice((MidiDevice) midiDevices
739 adamjking 192
                                        .values().toArray()[row]);
628 adamjking 193
                    } else {
194
                        midiDeviceManager
195
                                .deselectMidiWriteDevice((MidiDevice) midiDevices
739 adamjking 196
                                        .values().toArray()[row]);
628 adamjking 197
                    }
198
                }
199
            }
200
        }
201
 
202
        public Class getColumnClass(int c) {
203
            return getValueAt(0, c).getClass();
204
        }
205
 
206
        public boolean isCellEditable(int row, int col) {
207
            return col == 0;
208
        }
209
    }
210
}