Subversion Repositories svn.mios

Rev

Rev 741 | 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
 
741 adamjking 23
import java.awt.Component;
628 adamjking 24
import java.awt.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
739 adamjking 30
import java.util.LinkedHashMap;
628 adamjking 31
import java.util.Observable;
32
import java.util.Observer;
33
 
34
import javax.sound.midi.MidiDevice;
741 adamjking 35
import javax.swing.AbstractCellEditor;
628 adamjking 36
import javax.swing.JButton;
741 adamjking 37
import javax.swing.JCheckBox;
628 adamjking 38
import javax.swing.JLabel;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41
import javax.swing.JTable;
741 adamjking 42
import javax.swing.table.DefaultTableCellRenderer;
628 adamjking 43
import javax.swing.table.DefaultTableModel;
741 adamjking 44
import javax.swing.table.TableCellEditor;
628 adamjking 45
 
46
import org.midibox.midi.MidiDeviceManager;
47
 
48
public class MidiDeviceManagerGUI extends JPanel implements Observer,
49
        ActionListener {
50
 
51
    private MidiDeviceManager midiDeviceManager;
52
 
53
    private JTable midiReadDevices;
54
 
55
    private JTable midiWriteDevices;
56
 
57
    public MidiDeviceManagerGUI(MidiDeviceManager midiDeviceManager) {
58
        super(new GridBagLayout());
59
 
60
        this.midiDeviceManager = midiDeviceManager;
61
        midiDeviceManager.addObserver(this);
62
 
63
        GridBagConstraints gbc = new GridBagConstraints();
64
        gbc.gridx = 0;
65
        gbc.gridy = 0;
66
        gbc.gridwidth = 1;
67
        gbc.gridheight = 1;
68
        gbc.fill = GridBagConstraints.HORIZONTAL;
69
        gbc.weightx = 1.0;
70
        gbc.insets = new Insets(5, 5, 5, 5);
71
 
72
        gbc.weighty = 0.0;
73
        add(new JLabel("MIDI Devices - Readable", JLabel.CENTER), gbc);
74
        gbc.gridx++;
75
 
76
        add(new JLabel("MIDI Devices - Writeable", JLabel.CENTER), gbc);
77
        gbc.gridx--;
78
        gbc.gridy++;
79
 
80
        gbc.fill = GridBagConstraints.BOTH;
81
        gbc.weighty = 1.0;
82
        midiReadDevices = new JTable(new MidiDeviceTableModel(midiDeviceManager
83
                .getMidiReadDevices(), midiDeviceManager
84
                .getSelectedMidiReadDevices()));
85
        midiReadDevices.setPreferredScrollableViewportSize(new Dimension(200,
86
                200));
87
        midiReadDevices.setCellSelectionEnabled(false);
88
        midiReadDevices.setShowGrid(false);
741 adamjking 89
        midiReadDevices.setDefaultRenderer(Boolean.class,
90
                new MidiDeviceTableCellRenderer(midiDeviceManager
91
                        .getMidiReadDevices(), midiDeviceManager
92
                        .getSelectedMidiReadDevices()));
93
        midiReadDevices.setDefaultEditor(Boolean.class,
94
                new MidiDeviceTableCellEditor(midiDeviceManager
95
                        .getMidiReadDevices(), midiDeviceManager
96
                        .getSelectedMidiReadDevices()));
628 adamjking 97
 
98
        JScrollPane scrollPane = new JScrollPane(midiReadDevices);
99
        scrollPane
100
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
101
        add(scrollPane, gbc);
102
        gbc.gridx++;
103
 
104
        midiWriteDevices = new JTable(new MidiDeviceTableModel(
105
                midiDeviceManager.getMidiWriteDevices(), midiDeviceManager
106
                        .getSelectedMidiWriteDevices()));
107
        midiWriteDevices.setPreferredScrollableViewportSize(new Dimension(200,
108
                200));
109
        midiWriteDevices.setCellSelectionEnabled(false);
110
        midiWriteDevices.setShowGrid(false);
741 adamjking 111
        midiWriteDevices.setDefaultRenderer(Boolean.class,
112
                new MidiDeviceTableCellRenderer(midiDeviceManager
113
                        .getMidiWriteDevices(), midiDeviceManager
114
                        .getSelectedMidiWriteDevices()));
115
        midiWriteDevices.setDefaultEditor(Boolean.class,
116
                new MidiDeviceTableCellEditor(midiDeviceManager
117
                        .getMidiWriteDevices(), midiDeviceManager
118
                        .getSelectedMidiWriteDevices()));
628 adamjking 119
 
120
        scrollPane = new JScrollPane(midiWriteDevices);
121
        scrollPane
122
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
123
        add(scrollPane, gbc);
124
 
125
        gbc.gridx = 0;
126
        gbc.gridy++;
127
        gbc.gridwidth = 2;
128
        gbc.fill = GridBagConstraints.NONE;
129
        gbc.weightx = 0.0;
130
        gbc.weighty = 0.0;
131
        gbc.anchor = GridBagConstraints.WEST;
132
 
133
        JButton button = new JButton("Rescan Devices");
134
        button.setActionCommand("rescan");
135
        button.addActionListener(this);
136
        add(button, gbc);
137
    }
138
 
139
    public void update(Observable observable, Object object) {
140
        if (observable == midiDeviceManager) {
141
            ((DefaultTableModel) midiReadDevices.getModel())
142
                    .fireTableDataChanged();
143
            ((DefaultTableModel) midiWriteDevices.getModel())
144
                    .fireTableDataChanged();
145
        }
146
    }
147
 
148
    public void actionPerformed(ActionEvent ae) {
149
        if (ae.getActionCommand().equals("rescan")) {
150
            midiDeviceManager.rescanDevices();
151
        }
152
    }
153
 
741 adamjking 154
    class MidiDeviceTableCellEditor extends AbstractCellEditor implements
155
            TableCellEditor {
156
 
157
        private LinkedHashMap midiDevices;
158
 
159
        private LinkedHashMap selectedMidiDevices;
160
 
161
        private JCheckBox checkBox;
162
 
163
        public MidiDeviceTableCellEditor(LinkedHashMap midiDevices,
164
                LinkedHashMap selectedMidiDevices) {
165
            this.midiDevices = midiDevices;
166
            this.selectedMidiDevices = selectedMidiDevices;
167
        }
168
 
169
        public Component getTableCellEditorComponent(JTable table,
170
                Object value, boolean isSelected, int row, int column) {
171
 
172
            MidiDevice midiDevice = (MidiDevice) midiDevices.values().toArray()[row];
173
 
174
            checkBox = new JCheckBox(midiDevice.getDeviceInfo().getName(),
175
                    ((Boolean) value).booleanValue());
176
 
177
            checkBox.setOpaque(false);
808 adamjking 178
 
741 adamjking 179
            checkBox.addActionListener(new ActionListener() {
180
                public void actionPerformed(ActionEvent ae) {
181
                    fireEditingStopped();
182
                }
183
            });
184
 
185
            return checkBox;
186
        }
187
 
188
        public Object getCellEditorValue() {
189
 
190
            return new Boolean(checkBox.isSelected());
191
        }
192
    }
193
 
194
    class MidiDeviceTableCellRenderer extends DefaultTableCellRenderer {
195
 
196
        private LinkedHashMap midiDevices;
197
 
198
        private LinkedHashMap selectedMidiDevices;
199
 
200
        public MidiDeviceTableCellRenderer(LinkedHashMap midiDevices,
201
                LinkedHashMap selectedMidiDevices) {
202
            this.midiDevices = midiDevices;
203
            this.selectedMidiDevices = selectedMidiDevices;
204
        }
205
 
206
        public Component getTableCellRendererComponent(JTable table,
207
                Object value, boolean isSelected, boolean hasFocus, int row,
208
                int column) {
209
 
210
            MidiDevice midiDevice = (MidiDevice) midiDevices.values().toArray()[row];
211
 
212
            JCheckBox checkBox = new JCheckBox(midiDevice.getDeviceInfo()
213
                    .getName(), ((Boolean) value).booleanValue());
214
 
215
            checkBox.setOpaque(false);
216
 
217
            return checkBox;
218
        }
219
    }
220
 
628 adamjking 221
    class MidiDeviceTableModel extends DefaultTableModel {
222
 
741 adamjking 223
        private String[] headers = { "MIDI Device" };
224
 
739 adamjking 225
        private LinkedHashMap midiDevices;
628 adamjking 226
 
739 adamjking 227
        private LinkedHashMap selectedMidiDevices;
628 adamjking 228
 
739 adamjking 229
        public MidiDeviceTableModel(LinkedHashMap midiDevices,
230
                LinkedHashMap selectedMidiDevices) {
628 adamjking 231
            this.midiDevices = midiDevices;
232
            this.selectedMidiDevices = selectedMidiDevices;
233
        }
234
 
235
        public int getColumnCount() {
236
            return headers.length;
237
        }
238
 
239
        public int getRowCount() {
240
            if (midiDevices != null) {
241
                return midiDevices.size();
242
            }
243
            return 0;
244
        }
245
 
246
        public String getColumnName(int col) {
247
            return headers[col];
248
        }
249
 
250
        public Object getValueAt(int row, int col) {
251
 
739 adamjking 252
            MidiDevice midiDevice = (MidiDevice) midiDevices.values().toArray()[row];
628 adamjking 253
 
254
            if (col == 0) {
739 adamjking 255
                return new Boolean(selectedMidiDevices
256
                        .containsValue(midiDevices.values().toArray()[row]));
628 adamjking 257
            }
258
            return null;
259
        }
260
 
261
        public void setValueAt(Object aValue, int row, int column) {
262
            if (column == 0) {
263
                if (((Boolean) aValue).booleanValue()) {
264
 
265
                    if (midiDevices == midiDeviceManager.getMidiReadDevices()) {
266
                        midiDeviceManager
267
                                .selectMidiReadDevice((MidiDevice) midiDevices
739 adamjking 268
                                        .values().toArray()[row]);
269
 
628 adamjking 270
                    } else {
271
                        midiDeviceManager
272
                                .selectMidiWriteDevice((MidiDevice) midiDevices
739 adamjking 273
                                        .values().toArray()[row]);
628 adamjking 274
                    }
275
                } else {
276
                    if (midiDevices == midiDeviceManager.getMidiReadDevices()) {
277
                        midiDeviceManager
278
                                .deselectMidiReadDevice((MidiDevice) midiDevices
739 adamjking 279
                                        .values().toArray()[row]);
628 adamjking 280
                    } else {
281
                        midiDeviceManager
282
                                .deselectMidiWriteDevice((MidiDevice) midiDevices
739 adamjking 283
                                        .values().toArray()[row]);
628 adamjking 284
                    }
285
                }
286
            }
287
        }
288
 
289
        public Class getColumnClass(int c) {
290
            return getValueAt(0, c).getClass();
291
        }
292
 
293
        public boolean isCellEditable(int row, int col) {
294
            return col == 0;
295
        }
296
    }
297
}