Subversion Repositories svn.mios

Rev

Rev 642 | Go to most recent revision | 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() {
721 adamjking 101
        JPopupMenu popupMenu = new JPopupMenu(midiParameterControl.getMidiName());
628 adamjking 102
 
103
        JMenuItem menuItem = new JMenuItem("MIDI Properties");
104
        menuItem.setActionCommand("midiproperties");
105
        menuItem.addActionListener(this);
106
        popupMenu.add(menuItem);
107
 
108
        return popupMenu;
109
    }
110
 
111
    protected JPanel createLabelPanel() {
112
 
113
        JPanel labelPanel = new JPanel((showValue) ? new GridLayout(
114
                (valueBelow ? 2 : 1), (valueBelow ? 1 : 2)) : new GridLayout(1,
115
                1));
116
        labelPanel.setOpaque(false);
117
 
721 adamjking 118
        midiParameterLabel = new JLabel(midiParameterControl.getMidiName()
628 adamjking 119
                .toUpperCase(),
120
                (showValue) ? ((valueBelow ? SwingConstants.CENTER
121
                        : SwingConstants.RIGHT)) : SwingConstants.CENTER);
122
        midiParameterLabel.setOpaque(false);
123
        midiParameterLabel.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
124
        midiParameterLabel.setForeground(labelFieldColor);
125
        labelPanel.add(midiParameterLabel);
126
 
127
        if (showValue) {
128
            valueField = new JTextField();
129
            valueField.setHorizontalAlignment(JTextField.CENTER);
130
            valueField.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
131
            valueField.setForeground(valueFieldColor);
132
            valueField.addActionListener(this);
133
            valueField.setOpaque(false);
134
            valueField.setBorder(null);
135
            labelPanel.add(valueField);
136
        }
137
 
138
        return labelPanel;
139
    }
140
 
141
    protected JTextField createValueField() {
142
        valueField = new JTextField();
143
        return valueField;
144
    }
145
 
146
    public void setValueFieldColor(Color valueFieldColor) {
147
        this.valueFieldColor = valueFieldColor;
148
        this.valueField.setForeground(valueFieldColor);
149
    }
150
 
151
    public void setLabelFieldColor(Color labelFieldColor) {
152
        this.labelFieldColor = labelFieldColor;
153
        this.midiParameterLabel.setForeground(labelFieldColor);
154
    }
155
 
156
    public MidiParameterControl getMidiParameterControl() {
721 adamjking 157
        return midiParameterControl;
628 adamjking 158
    }
159
 
160
    public void updateGraphics() {
161
        if (valueField != null) {
162
            updateValueField();
163
        }
164
    }
165
 
166
    public void updateValueField() {
167
        valueBuffer.delete(0, valueBuffer.length());
721 adamjking 168
        valueBuffer.insert(0, midiParameterControl.getMidiValue());
628 adamjking 169
        valueField.setText(valueBuffer.toString());
170
    }
171
 
172
    private void showMidiParameterProperties() {
173
        if (midiParameterPropertiesDialog == null) {
174
            final MidiParameterControlProperties midiParameterProperties = new MidiParameterControlProperties(
175
                    getMidiParameterControl());
176
 
642 adamjking 177
            midiParameterPropertiesDialog = new JDialog(DialogOwner.getFrame(),
178
                    "MIDI Properties", false);
628 adamjking 179
            midiParameterPropertiesDialog
180
                    .setContentPane(midiParameterProperties);
181
            midiParameterPropertiesDialog.pack();
182
            midiParameterPropertiesDialog.setLocationRelativeTo(this);
183
            midiParameterPropertiesDialog.setVisible(true);
184
 
185
            midiParameterPropertiesDialog
186
                    .addWindowListener(new WindowAdapter() {
187
                        public void windowClosing(WindowEvent we) {
721 adamjking 188
                            midiParameterControl
628 adamjking 189
                                    .deleteObserver(midiParameterProperties);
190
                            midiParameterPropertiesDialog = null;
191
                        }
192
                    });
193
        }
194
        midiParameterPropertiesDialog.requestFocus();
195
    }
196
 
197
    public void mouseClicked(MouseEvent e) {
198
 
199
    }
200
 
201
    public void mouseEntered(MouseEvent e) {
202
 
203
    }
204
 
205
    public void mouseExited(MouseEvent e) {
206
 
207
    }
208
 
209
    public void mousePressed(MouseEvent e) {
210
        if (e.getButton() == MouseEvent.BUTTON3) {
211
            createPopMenu().show(this, e.getX(), e.getY());
212
        }
213
    }
214
 
215
    public void mouseReleased(MouseEvent e) {
216
 
217
    }
218
 
219
    public void actionPerformed(ActionEvent ae) {
220
        Object source = ae.getSource();
221
        if (source == valueField) {
222
            try {
721 adamjking 223
                midiParameterControl.setMidiValue(Integer.parseInt(valueField
628 adamjking 224
                        .getText()), true);
225
            } catch (Exception e) {
721 adamjking 226
                midiParameterControl.setMidiValue(midiParameterControl.getMidiValue(), true);
628 adamjking 227
            }
228
        } else if (ae.getActionCommand().equals("midilearn")) {
229
 
230
        } else if (ae.getActionCommand().equals("midiproperties")) {
231
            showMidiParameterProperties();
232
        }
233
    }
234
 
235
    public void update(Observable observable, Object object) {
721 adamjking 236
        if (observable == midiParameterControl) {
628 adamjking 237
            if (object != MidiParameter.VALUE) {
238
                if (showLabel) {
721 adamjking 239
                    midiParameterLabel.setText(midiParameterControl.getMidiName()
628 adamjking 240
                            .toUpperCase());
241
                }
242
            }
243
            updateGraphics();
244
        }
245
    }
246
}