Subversion Repositories svn.mios

Rev

Rev 721 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MidiParameterControlGUI.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.Color;
25
import java.awt.GridLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.awt.event.MouseEvent;
29
import java.awt.event.MouseListener;
30
import java.awt.event.WindowAdapter;
31
import java.awt.event.WindowEvent;
32
import java.util.Observable;
33
import java.util.Observer;
34
 
35
import javax.swing.JDialog;
36
import javax.swing.JLabel;
37
import javax.swing.JMenuItem;
38
import javax.swing.JPanel;
39
import javax.swing.JPopupMenu;
40
import javax.swing.JTextField;
41
import javax.swing.SwingConstants;
42
 
43
import org.midibox.midi.MidiParameter;
44
import org.midibox.midi.MidiParameterControl;
639 adamjking 45
import org.midibox.utils.gui.DialogOwner;
628 adamjking 46
import org.midibox.utils.gui.FontLoader;
47
 
48
public class MidiParameterControlGUI extends JPanel implements Observer,
49
        ActionListener, MouseListener {
50
 
721 adamjking 51
    protected MidiParameterControl midiParameterControl;
628 adamjking 52
 
53
    protected boolean showLabel;
54
 
55
    protected String labelLocation;
56
 
57
    protected boolean valueBelow;
58
 
59
    protected boolean showValue;
60
 
61
    protected JLabel midiParameterLabel;
62
 
63
    protected JTextField valueField;
64
 
65
    protected StringBuffer valueBuffer;
66
 
67
    protected Color valueFieldColor = new Color(150, 20, 0);
68
 
69
    protected Color labelFieldColor = Color.BLACK;
70
 
71
    protected boolean update = true;
72
 
73
    protected JDialog midiParameterPropertiesDialog;
74
 
721 adamjking 75
    public MidiParameterControlGUI(MidiParameterControl midiParameterControl,
628 adamjking 76
            boolean showLabel, String labelLocation, boolean valueBelow,
77
            boolean showValue) {
78
 
79
        super(new BorderLayout());
80
 
721 adamjking 81
        this.midiParameterControl = midiParameterControl;
628 adamjking 82
        this.showLabel = showLabel;
83
        this.labelLocation = labelLocation;
84
        this.valueBelow = valueBelow;
85
        this.showValue = showValue;
86
 
721 adamjking 87
        midiParameterControl.addObserver(this);
628 adamjking 88
 
89
        valueBuffer = new StringBuffer();
90
 
91
        if (showLabel) {
92
            add(createLabelPanel(), labelLocation);
93
        }
94
 
95
        addMouseListener(this);
96
 
97
        setOpaque(false);
98
    }
99
 
100
    protected JPopupMenu createPopMenu() {
723 adamjking 101
        JPopupMenu popupMenu = new JPopupMenu(midiParameterControl
102
                .getMidiName());
628 adamjking 103
 
104
        JMenuItem menuItem = new JMenuItem("MIDI Properties");
105
        menuItem.setActionCommand("midiproperties");
106
        menuItem.addActionListener(this);
107
        popupMenu.add(menuItem);
108
 
109
        return popupMenu;
110
    }
111
 
112
    protected JPanel createLabelPanel() {
113
 
114
        JPanel labelPanel = new JPanel((showValue) ? new GridLayout(
115
                (valueBelow ? 2 : 1), (valueBelow ? 1 : 2)) : new GridLayout(1,
116
                1));
117
        labelPanel.setOpaque(false);
118
 
721 adamjking 119
        midiParameterLabel = new JLabel(midiParameterControl.getMidiName()
628 adamjking 120
                .toUpperCase(),
121
                (showValue) ? ((valueBelow ? SwingConstants.CENTER
122
                        : SwingConstants.RIGHT)) : SwingConstants.CENTER);
123
        midiParameterLabel.setOpaque(false);
124
        midiParameterLabel.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
125
        midiParameterLabel.setForeground(labelFieldColor);
126
        labelPanel.add(midiParameterLabel);
127
 
128
        if (showValue) {
129
            valueField = new JTextField();
130
            valueField.setHorizontalAlignment(JTextField.CENTER);
131
            valueField.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
132
            valueField.setForeground(valueFieldColor);
133
            valueField.addActionListener(this);
134
            valueField.setOpaque(false);
135
            valueField.setBorder(null);
136
            labelPanel.add(valueField);
137
        }
138
 
139
        return labelPanel;
140
    }
141
 
142
    protected JTextField createValueField() {
143
        valueField = new JTextField();
144
        return valueField;
145
    }
146
 
147
    public void setValueFieldColor(Color valueFieldColor) {
148
        this.valueFieldColor = valueFieldColor;
149
        this.valueField.setForeground(valueFieldColor);
150
    }
151
 
152
    public void setLabelFieldColor(Color labelFieldColor) {
153
        this.labelFieldColor = labelFieldColor;
154
        this.midiParameterLabel.setForeground(labelFieldColor);
155
    }
156
 
157
    public MidiParameterControl getMidiParameterControl() {
721 adamjking 158
        return midiParameterControl;
628 adamjking 159
    }
160
 
161
    public void updateGraphics() {
162
        if (valueField != null) {
163
            updateValueField();
164
        }
165
    }
166
 
167
    public void updateValueField() {
168
        valueBuffer.delete(0, valueBuffer.length());
721 adamjking 169
        valueBuffer.insert(0, midiParameterControl.getMidiValue());
628 adamjking 170
        valueField.setText(valueBuffer.toString());
171
    }
172
 
173
    private void showMidiParameterProperties() {
174
        if (midiParameterPropertiesDialog == null) {
175
            final MidiParameterControlProperties midiParameterProperties = new MidiParameterControlProperties(
176
                    getMidiParameterControl());
177
 
642 adamjking 178
            midiParameterPropertiesDialog = new JDialog(DialogOwner.getFrame(),
179
                    "MIDI Properties", false);
628 adamjking 180
            midiParameterPropertiesDialog
181
                    .setContentPane(midiParameterProperties);
182
            midiParameterPropertiesDialog.pack();
183
            midiParameterPropertiesDialog.setLocationRelativeTo(this);
184
            midiParameterPropertiesDialog.setVisible(true);
185
 
186
            midiParameterPropertiesDialog
187
                    .addWindowListener(new WindowAdapter() {
188
                        public void windowClosing(WindowEvent we) {
721 adamjking 189
                            midiParameterControl
628 adamjking 190
                                    .deleteObserver(midiParameterProperties);
191
                            midiParameterPropertiesDialog = null;
192
                        }
193
                    });
194
        }
195
        midiParameterPropertiesDialog.requestFocus();
196
    }
197
 
198
    public void mouseClicked(MouseEvent e) {
199
 
200
    }
201
 
202
    public void mouseEntered(MouseEvent e) {
203
 
204
    }
205
 
206
    public void mouseExited(MouseEvent e) {
207
 
208
    }
209
 
210
    public void mousePressed(MouseEvent e) {
211
        if (e.getButton() == MouseEvent.BUTTON3) {
212
            createPopMenu().show(this, e.getX(), e.getY());
213
        }
214
    }
215
 
216
    public void mouseReleased(MouseEvent e) {
217
 
218
    }
219
 
220
    public void actionPerformed(ActionEvent ae) {
221
        Object source = ae.getSource();
222
        if (source == valueField) {
223
            try {
721 adamjking 224
                midiParameterControl.setMidiValue(Integer.parseInt(valueField
628 adamjking 225
                        .getText()), true);
226
            } catch (Exception e) {
723 adamjking 227
                midiParameterControl.setMidiValue(midiParameterControl
228
                        .getMidiValue(), true);
628 adamjking 229
            }
230
        } else if (ae.getActionCommand().equals("midilearn")) {
231
 
232
        } else if (ae.getActionCommand().equals("midiproperties")) {
233
            showMidiParameterProperties();
234
        }
235
    }
236
 
237
    public void update(Observable observable, Object object) {
721 adamjking 238
        if (observable == midiParameterControl) {
628 adamjking 239
            if (object != MidiParameter.VALUE) {
240
                if (showLabel) {
723 adamjking 241
                    midiParameterLabel.setText(midiParameterControl
242
                            .getMidiName().toUpperCase());
628 adamjking 243
                }
244
            }
245
            updateGraphics();
246
        }
247
    }
248
}