Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)MidiDeviceRoutingGUI.java    beta7   2006/04/23
3
 *
4
 * Copyright (C) 2006    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.BorderLayout;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.Hashtable;
33
import java.util.Iterator;
34
import java.util.Observable;
35
import java.util.Observer;
36
 
37
import javax.sound.midi.MidiDevice;
38
import javax.swing.BorderFactory;
39
import javax.swing.DefaultListCellRenderer;
40
import javax.swing.DefaultListModel;
41
import javax.swing.Icon;
42
import javax.swing.JLabel;
43
import javax.swing.JList;
294 rutgerv 44
import javax.swing.JButton;
213 tk 45
import javax.swing.JPanel;
46
import javax.swing.JScrollPane;
47
import javax.swing.ListSelectionModel;
48
import javax.swing.SwingConstants;
49
import javax.swing.event.ChangeEvent;
50
import javax.swing.event.ChangeListener;
51
import javax.swing.event.ListSelectionEvent;
52
import javax.swing.event.ListSelectionListener;
53
 
54
import org.midibox.midi.MidiDeviceRouting;
55
import org.midibox.utils.gui.GuiUtils;
56
import org.midibox.utils.gui.ImageLoader;
57
 
290 rutgerv 58
public class MidiDeviceRoutingGUI extends JPanel implements ChangeListener, ListSelectionListener, ActionListener, Observer {
213 tk 59
 
60
    private MidiDeviceRouting midiDeviceRouting;
61
 
62
    private static Icon openIcon = ImageLoader.getImageIcon("midiPortOpen.png");
63
 
290 rutgerv 64
    private static Icon closedIcon = ImageLoader.getImageIcon("midiPortClosed.png");
213 tk 65
 
66
    private Hashtable icons = new Hashtable();
67
 
68
    private DefaultListModel midiReadDevicesListModel;
69
 
70
    private JList midiReadDevicesList;
71
 
72
    private JScrollPane midiReadDevicesScroller;
73
 
74
    private DefaultListModel midiWriteDevicesListModel;
75
 
76
    private JList midiWriteDevicesList;
77
 
78
    private JScrollPane midiWriteDevicesScroller;
294 rutgerv 79
 
80
    private JButton rescan;
213 tk 81
 
82
    public MidiDeviceRoutingGUI(MidiDeviceRouting midiDeviceRouting) {
83
        super(new BorderLayout());
84
 
85
        this.midiDeviceRouting = midiDeviceRouting;
86
        midiDeviceRouting.addObserver(this);
87
 
88
        midiReadDevicesListModel = new DefaultListModel();
89
        midiWriteDevicesListModel = new DefaultListModel();
90
 
91
        midiReadDevicesList = new JList(midiReadDevicesListModel);
290 rutgerv 92
        midiReadDevicesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
213 tk 93
        midiReadDevicesList.setLayoutOrientation(JList.VERTICAL);
94
        midiReadDevicesList.setVisibleRowCount(10);
95
        midiReadDevicesList.setCellRenderer(new MyListCellRenderer());
96
        midiReadDevicesList.setBackground(Color.WHITE);
97
        midiWriteDevicesList = new JList(midiWriteDevicesListModel);
290 rutgerv 98
        midiWriteDevicesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
213 tk 99
        midiWriteDevicesList.setLayoutOrientation(JList.VERTICAL);
100
        midiWriteDevicesList.setVisibleRowCount(10);
101
        midiWriteDevicesList.setCellRenderer(new MyListCellRenderer());
102
        midiWriteDevicesList.setBackground(Color.WHITE);
103
 
104
        JPanel listPane = new JPanel(new GridBagLayout());
105
 
106
        GridBagConstraints gbc = new GridBagConstraints();
107
        gbc.gridx = 1;
108
        gbc.gridy = 1;
109
        gbc.gridwidth = 1;
110
        gbc.gridheight = 1;
111
        gbc.insets = new Insets(2, 0, 2, 0);
112
        gbc.fill = GridBagConstraints.HORIZONTAL;
113
        gbc.weightx = 1.0;
114
        gbc.weighty = 0.0;
115
 
116
        listPane.setOpaque(false);
117
 
290 rutgerv 118
        listPane.add(new JLabel("MIDI Input devices",SwingConstants.CENTER), gbc);
213 tk 119
        gbc.gridy++;
120
 
121
        midiReadDevicesScroller = new JScrollPane(midiReadDevicesList);
122
        midiReadDevicesScroller.getViewport().addChangeListener(this);
123
        midiReadDevicesScroller
124
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
125
        midiReadDevicesScroller
126
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
127
 
128
        gbc.fill = GridBagConstraints.BOTH;
129
        gbc.weighty = 1.0;
130
        gbc.weighty = 1.0;
131
 
132
        listPane.add(midiReadDevicesScroller, gbc);
133
        gbc.gridx++;
134
 
135
        gbc.gridx++;
136
        gbc.gridy--;
137
 
138
        gbc.fill = GridBagConstraints.HORIZONTAL;
139
        gbc.weightx = 1.0;
140
        gbc.weighty = 0.0;
141
 
290 rutgerv 142
        listPane.add(new JLabel("MIDI Output devices",
213 tk 143
                SwingConstants.CENTER), gbc);
144
        gbc.gridy++;
145
 
146
        midiWriteDevicesScroller = new JScrollPane(midiWriteDevicesList);
147
        midiWriteDevicesScroller
148
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
149
        midiWriteDevicesScroller
150
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
151
        midiWriteDevicesScroller.getViewport().addChangeListener(this);
152
 
153
        gbc.fill = GridBagConstraints.BOTH;
154
        gbc.weightx = 1.0;
155
        gbc.weighty = 1.0;
156
 
157
        listPane.add(midiWriteDevicesScroller, gbc);
158
 
159
        int maxPreferredWidth = Math.max(midiReadDevicesScroller
160
                .getPreferredSize().width, midiWriteDevicesScroller
161
                .getPreferredSize().width);
162
        int maxPreferredHeight = Math.max(midiReadDevicesScroller
163
                .getPreferredSize().height, midiWriteDevicesScroller
164
                .getPreferredSize().height);
165
 
166
        GuiUtils.setComponentSize(midiReadDevicesScroller, maxPreferredWidth,
167
                maxPreferredHeight);
168
        GuiUtils.setComponentSize(midiWriteDevicesScroller, maxPreferredWidth,
169
                maxPreferredHeight);
170
 
171
        midiReadDevicesList.addListSelectionListener(this);
172
        midiWriteDevicesList.addListSelectionListener(this);
173
 
174
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
175
        listPane.setOpaque(false);
294 rutgerv 176
        add(listPane, BorderLayout.CENTER);
177
        rescan = new JButton("Rescan");
178
        rescan.addActionListener(this);
179
        add(rescan, BorderLayout.SOUTH);
213 tk 180
        populateTrees();
181
    }
182
 
183
    public MidiDeviceRouting getMidiDeviceRouting() {
184
        return midiDeviceRouting;
185
    }
186
 
187
    public Icon getMidiDeviceIcon(MidiDevice md) {
188
        if (icons.containsKey(md)) {
189
            return (Icon) icons.get(md);
190
        }
191
 
192
        if (icons.containsKey(md.getClass())) {
193
            return (Icon) icons.get(md.getClass());
194
        }
195
 
196
        try {
197
            if (md.isOpen()) {
198
                return openIcon;
199
            }
200
 
201
            return closedIcon;
202
 
203
        } catch (Exception e) {
204
            return closedIcon;
205
        }
206
    }
207
 
208
    public void redrawAll() {
209
        midiReadDevicesList.repaint();
290 rutgerv 210
        midiWriteDevicesList.repaint();
213 tk 211
    }
212
 
213
    private void populateTrees() {
214
        midiReadDevicesListModel.removeAllElements();
215
        Iterator it = midiDeviceRouting.getMidiReadDevices().iterator();
216
 
217
        while (it.hasNext()) {
218
            midiReadDevicesListModel.addElement(((MidiDevice) it.next())
219
                    .getDeviceInfo().getName());
220
        }
221
        midiReadDevicesList.setSelectedIndex(0);
222
        midiWriteDevicesListModel.removeAllElements();
223
        it = midiDeviceRouting.getMidiWriteDevices().iterator();
224
        while (it.hasNext()) {
225
            midiWriteDevicesListModel.addElement(((MidiDevice) it.next())
226
                    .getDeviceInfo().getName());
227
        }
228
        midiWriteDevicesList.setSelectedIndex(0);
229
        updateUI();
230
    }
231
 
232
    public void valueChanged(ListSelectionEvent lse) {
290 rutgerv 233
        if (lse.getSource()==midiReadDevicesList) {
234
            if (midiReadDevicesList.getSelectedIndex() > 0) {
235
                midiDeviceRouting.setInputDevice(midiReadDevicesList.getSelectedIndex());
213 tk 236
            }
290 rutgerv 237
        } else if (lse.getSource()==midiWriteDevicesList) {
238
            if (midiWriteDevicesList.getSelectedIndex() > 0) {
239
                midiDeviceRouting.setOutputDevice(midiWriteDevicesList.getSelectedIndex());    
240
            }
241
        }  
213 tk 242
    }
243
 
244
    public void stateChanged(ChangeEvent ce) {
245
    }
246
 
247
    public Dimension getMinimumSize() {
248
        return getPreferredSize();
249
    }
250
 
251
    public void update(Observable observable, Object object) {
252
        if (observable == midiDeviceRouting) {
290 rutgerv 253
            if (object == midiDeviceRouting.getMidiReadDevices() || object == midiDeviceRouting.getMidiWriteDevices()) {
213 tk 254
                populateTrees();
255
            }
290 rutgerv 256
            midiReadDevicesList.setSelectedIndex(midiDeviceRouting.getInputDeviceIndex());
257
            midiWriteDevicesList.setSelectedIndex(midiDeviceRouting.getOutputDeviceIndex());
213 tk 258
            redrawAll();           
259
        }
260
 
261
    }
262
 
263
    public void actionPerformed(ActionEvent ae) {
294 rutgerv 264
        if (ae.getSource()==rescan) {
265
            midiDeviceRouting.rescanDevices();
266
        }
213 tk 267
    }
268
 
269
    public class MyListCellRenderer extends DefaultListCellRenderer {
270
        public Component getListCellRendererComponent(JList list, Object value,
271
                int index, boolean isSelected, boolean hasFocus) {
272
 
273
            super.getListCellRendererComponent(list, value, index, isSelected,
274
                    hasFocus);
275
 
276
            setIcon(getMidiDeviceIcon(((MidiDevice) ((list == midiWriteDevicesList) ? midiDeviceRouting
277
                    .getMidiWriteDevices()
278
                    : midiDeviceRouting.getMidiReadDevices()).elementAt(index))));
279
 
280
            return this;
281
        }
290 rutgerv 282
    }  
213 tk 283
}