Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MidiParameterProperties.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.BorderLayout;
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.Observable;
29
import java.util.Observer;
30
 
31
import javax.swing.BorderFactory;
32
import javax.swing.JCheckBox;
33
import javax.swing.JComboBox;
34
import javax.swing.JLabel;
35
import javax.swing.JList;
36
import javax.swing.JPanel;
37
import javax.swing.JScrollPane;
38
import javax.swing.JSpinner;
39
import javax.swing.JToggleButton;
40
import javax.swing.SpinnerNumberModel;
41
import javax.swing.event.ChangeEvent;
42
import javax.swing.event.ChangeListener;
43
import javax.swing.event.ListSelectionEvent;
44
import javax.swing.event.ListSelectionListener;
45
 
46
import org.midibox.midi.MidiParameter;
47
import org.midibox.midi.MidiParameterControl;
48
import org.midibox.midi.MidiUtils;
49
 
50
public class MidiParameterProperties extends JPanel implements Observer,
51
        ActionListener, ListSelectionListener, ChangeListener {
52
 
721 adamjking 53
    protected MidiParameterControl midiParameterControl;
628 adamjking 54
 
55
    protected JToggleButton midiLearn;
56
 
57
    protected JSpinner channelSpinner;
58
 
59
    protected JComboBox statusComboBox;
60
 
61
    protected JList nameList;
62
 
63
    protected JScrollPane nameScrollPane;
64
 
65
    protected JLabel defaultValueLabel;
66
 
67
    protected JSpinner defaultValueSpinner;
68
 
69
    protected JCheckBox highResolution;
70
 
71
    protected JPanel statusPanel;
72
 
73
    protected JPanel midiLearnPanel;
74
 
75
    protected JPanel channelPanel;
76
 
77
    protected JPanel defaultPanel;
78
 
79
    protected JPanel mainPanel;
80
 
81
    protected JPanel numberPanel;
82
 
83
    protected boolean update = true;
84
 
721 adamjking 85
    public MidiParameterProperties(MidiParameterControl midiParameterControl) {
628 adamjking 86
        super(new BorderLayout());
721 adamjking 87
        this.midiParameterControl = midiParameterControl;
88
        midiParameterControl.addObserver(this);
628 adamjking 89
 
90
        add(createMainPanel(), BorderLayout.CENTER);
91
 
92
        midiLearnPanel = new JPanel(new BorderLayout());
93
 
94
        midiLearn = new JToggleButton("MIDI Learn");
95
        midiLearn.addActionListener(this);
96
 
97
        midiLearnPanel.add(midiLearn);
98
        midiLearnPanel
99
                .setBorder(BorderFactory.createTitledBorder("MIDI Learn"));
100
 
101
        add(midiLearnPanel, BorderLayout.NORTH);
102
 
103
        updateControls();
104
    }
105
 
106
    protected JPanel createMainPanel() {
107
 
108
        statusPanel = new JPanel(new GridBagLayout());
109
        GridBagConstraints gbc = new GridBagConstraints();
110
        gbc.anchor = GridBagConstraints.WEST;
111
        gbc.gridy = 0;
112
 
113
        statusComboBox = new JComboBox(new String[] { "Note Off/On",
114
                "Aftertouch", "Control Change", "Program Change",
115
                "Channel Pressure", "Pitch Bend" });
116
        statusComboBox.addActionListener(this);
117
 
118
        statusPanel.add(statusComboBox, gbc);
119
        gbc.gridy++;
120
        statusPanel.setBorder(BorderFactory.createTitledBorder("Status"));
121
 
122
        highResolution = new JCheckBox("14 bit");
123
        highResolution.addActionListener(this);
124
        statusPanel.add(highResolution, gbc);
125
 
126
        channelPanel = new JPanel(new GridBagLayout());
127
        gbc = new GridBagConstraints();
128
        gbc.anchor = GridBagConstraints.WEST;
129
        gbc.gridy = 0;
130
 
131
        channelSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 16, 1));
132
        channelSpinner.addChangeListener(this);
133
        channelPanel.add(channelSpinner, gbc);
134
 
135
        channelPanel.setBorder(BorderFactory.createTitledBorder("Channel"));
136
 
137
        defaultPanel = new JPanel();
138
 
139
        defaultValueLabel = new JLabel("Channel Pressure Value: ", JLabel.LEFT);
140
        defaultPanel.add(defaultValueLabel);
141
 
142
        defaultValueSpinner = new JSpinner(new SpinnerNumberModel(0, 0,
721 adamjking 143
                midiParameterControl.getMidiMaxValue(), 1));
628 adamjking 144
        defaultValueSpinner.addChangeListener(this);
145
        defaultPanel.add(defaultValueSpinner);
146
 
147
        defaultPanel.setBorder(BorderFactory
148
                .createTitledBorder("Default \"On\" Value"));
149
 
150
        numberPanel = new JPanel(new BorderLayout(0, 4));
151
 
152
        nameList = new JList();
153
        nameList.addListSelectionListener(this);
154
        nameList.setVisibleRowCount(4);
155
        nameScrollPane = new JScrollPane(nameList);
156
        nameScrollPane
157
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
158
        numberPanel.add(nameScrollPane);
159
 
160
        numberPanel.setBorder(BorderFactory.createTitledBorder("Number"));
161
 
162
        mainPanel = new JPanel(new GridBagLayout());
163
        gbc = new GridBagConstraints();
164
        gbc.gridx = 0;
165
        gbc.gridy = 0;
166
        gbc.gridheight = 1;
167
        gbc.gridwidth = 1;
168
        gbc.weightx = 1.0;
169
 
170
        gbc.anchor = GridBagConstraints.NORTHWEST;
171
        gbc.fill = GridBagConstraints.BOTH;
172
 
173
        mainPanel.add(statusPanel, gbc);
174
        gbc.gridx++;
175
 
176
        mainPanel.add(channelPanel, gbc);
177
 
178
        gbc.gridx = 0;
179
        gbc.gridy++;
180
        gbc.gridwidth = 2;
181
 
182
        mainPanel.add(defaultPanel, gbc);
183
        gbc.gridy++;
184
 
185
        gbc.weighty = 1.0;
186
 
187
        mainPanel.add(numberPanel, gbc);
188
 
189
        return mainPanel;
190
    }
191
 
192
    public void updateControls() {
193
 
721 adamjking 194
        boolean isLearn = midiParameterControl.isLearn();
628 adamjking 195
 
196
        midiLearn.setSelected(isLearn);
197
 
721 adamjking 198
        int status = midiParameterControl.getMidiStatus();
628 adamjking 199
 
200
        statusComboBox
721 adamjking 201
                .setSelectedIndex((midiParameterControl.getMidiStatus() >> 4) - 9);
628 adamjking 202
        statusComboBox.setEnabled(!isLearn);
203
 
204
        String[] listData = new String[128];
205
 
206
        for (int i = 0; i < listData.length; i++) {
207
            listData[i] = i + ": " + MidiUtils.getKeyName(i);
208
        }
209
 
210
        if (status == MidiParameter.NOTE_ON) {
211
            nameList.setEnabled(true && !isLearn);
212
            nameScrollPane.setEnabled(true);
213
 
214
        } else if (status == MidiParameter.AFTERTOUCH) {
215
            nameList.setEnabled(true && !isLearn);
216
            nameScrollPane.setEnabled(true && !isLearn);
217
 
218
        } else if (status == MidiParameter.CONTROL_CHANGE) {
219
            nameList.setEnabled(true && !isLearn);
220
            nameScrollPane.setEnabled(true && !isLearn);
221
 
721 adamjking 222
            if (midiParameterControl.isHighResolution()) {
628 adamjking 223
                for (int i = 0; i < listData.length; i++) {
224
                    listData[i] = i + ": " + MidiUtils.cc14BitNames[i];
225
                }
226
            } else {
227
                for (int i = 0; i < listData.length; i++) {
228
                    listData[i] = i + ": " + MidiUtils.ccNames[i];
229
                }
230
            }
231
        } else if (status == MidiParameter.PROGRAM_CHANGE) {
232
            nameList.setEnabled(false);
233
            nameScrollPane.setEnabled(false);
234
 
235
        } else if (status == MidiParameter.CHANNEL_PRESSURE) {
236
            nameList.setEnabled(false);
237
            nameScrollPane.setEnabled(false);
238
 
239
        } else if (status == MidiParameter.PITCH_BEND) {
240
            nameList.setEnabled(false);
241
            nameScrollPane.setEnabled(false);
242
        }
243
 
244
        nameList.setListData(listData);
721 adamjking 245
        nameList.setSelectedIndex(midiParameterControl.getMidiNumber());
246
        nameList.ensureIndexIsVisible(midiParameterControl.getMidiNumber());
628 adamjking 247
 
248
        if (status == MidiParameter.NOTE_ON) {
249
            defaultValueLabel.setText("Note On Velocity:       ");
250
 
251
        } else if (status == MidiParameter.AFTERTOUCH) {
252
            defaultValueLabel.setText("Aftertouch Pressure:    ");
253
 
254
        } else if (status == MidiParameter.CONTROL_CHANGE) {
255
            defaultValueLabel.setText("Control Change Value:   ");
256
 
257
        } else if (status == MidiParameter.PROGRAM_CHANGE) {
258
            defaultValueLabel.setText("Program Change No:      ");
259
 
260
        } else if (status == MidiParameter.CHANNEL_PRESSURE) {
261
            defaultValueLabel.setText("Channel Pressure Value: ");
262
 
263
        } else if (status == MidiParameter.PITCH_BEND) {
264
            defaultValueLabel.setText("Pitch Bend Value:       ");
265
        }
266
 
267
        channelSpinner.getModel().setValue(
721 adamjking 268
                new Integer(midiParameterControl.getMidiChannel() + 1));
628 adamjking 269
        channelSpinner.setEnabled(!isLearn);
270
 
271
        ((SpinnerNumberModel) defaultValueSpinner.getModel())
721 adamjking 272
                .setValue(new Integer(midiParameterControl.getMidiDefaultValue()));
628 adamjking 273
        ((SpinnerNumberModel) defaultValueSpinner.getModel())
721 adamjking 274
                .setMaximum(new Integer(midiParameterControl.getMidiMaxValue()));
628 adamjking 275
        defaultValueSpinner.setEnabled(!isLearn);
276
 
721 adamjking 277
        highResolution.setSelected(((MidiParameterControl) midiParameterControl)
628 adamjking 278
                .isHighResolution());
279
        highResolution
721 adamjking 280
                .setEnabled(!((MidiParameterControl) midiParameterControl).isLearn()
281
                        && ((MidiParameterControl) midiParameterControl)
628 adamjking 282
                                .getMidiStatus() == MidiParameterControl.CONTROL_CHANGE);
283
    }
284
 
285
    public void stateChanged(ChangeEvent ce) {
286
        if (update) {
287
            update = false;
288
            Object source = ce.getSource();
289
 
290
            if (source == channelSpinner) {
721 adamjking 291
                midiParameterControl.setMidiChannel(((Integer) channelSpinner
628 adamjking 292
                        .getModel().getValue()).intValue() - 1);
293
            }
294
            if (source == defaultValueSpinner) {
721 adamjking 295
                midiParameterControl
628 adamjking 296
                        .setMidiDefaultValue(((Integer) defaultValueSpinner
297
                                .getModel().getValue()).intValue());
298
            }
299
            update = true;
300
        }
301
    }
302
 
303
    public void valueChanged(ListSelectionEvent lse) {
304
        if (update) {
305
            update = false;
306
            if (nameList.getSelectedIndex() >= 0) {
721 adamjking 307
                midiParameterControl.setMidiNumber(nameList.getSelectedIndex());
628 adamjking 308
            }
309
            update = true;
310
        }
311
    }
312
 
313
    public void update(Observable observable, Object object) {
721 adamjking 314
        if (observable == midiParameterControl) {
628 adamjking 315
            if (object != MidiParameter.VALUE) {
316
                updateControls();
317
            }
318
        }
319
    }
320
 
321
    public void actionPerformed(ActionEvent ae) {
322
        if (update) {
323
            update = false;
324
 
325
            Object source = ae.getSource();
326
 
327
            if (source == statusComboBox) {
721 adamjking 328
                midiParameterControl
628 adamjking 329
                        .setMidiStatus((statusComboBox.getSelectedIndex() + 9) << 4);
330
            } else if (source == midiLearn) {
721 adamjking 331
                midiParameterControl.setLearn(midiLearn.isSelected());
628 adamjking 332
            } else if (source == highResolution) {
721 adamjking 333
                midiParameterControl.setHighResolution(highResolution.isSelected());
628 adamjking 334
            }
335
            update = true;
336
        }
337
    }
338
}