Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
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.sidedit.gui.controls;
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;
628 adamjking 28
import java.awt.event.MouseEvent;
29
import java.awt.event.MouseListener;
696 rutgerv 30
import java.util.Arrays;
213 tk 31
import java.util.Observable;
32
import java.util.Observer;
33
import java.util.Vector;
628 adamjking 34
 
213 tk 35
import javax.swing.JDialog;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.JTextField;
39
import javax.swing.SwingConstants;
628 adamjking 40
 
41
import org.midibox.sidedit.SIDSysexParameter;
42
import org.midibox.sidedit.SIDSysexParameterControl;
213 tk 43
import org.midibox.sidedit.gui.MBSIDV2EditorGUI;
44
import org.midibox.utils.gui.FontLoader;
45
 
628 adamjking 46
public class SIDSysexParameterControlGUI extends JPanel implements Observer,
47
        ActionListener, MouseListener {
213 tk 48
 
49
    protected SIDSysexParameterControl midiParameter;
628 adamjking 50
 
213 tk 51
    protected Vector midiParameters;
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;
628 adamjking 72
 
213 tk 73
    protected JDialog midiParameterPropertiesDialog;
74
 
75
    public SIDSysexParameterControlGUI(SIDSysexParameterControl midiParameter,
76
            boolean showLabel, String labelLocation, boolean valueBelow,
77
            boolean showValue) {
78
 
79
        super(new BorderLayout());
80
        this.midiParameter = midiParameter;
81
        this.showLabel = showLabel;
82
        this.labelLocation = labelLocation;
83
        this.valueBelow = valueBelow;
84
        this.showValue = showValue;
85
        this.midiParameters = new Vector();
628 adamjking 86
 
213 tk 87
        midiParameter.addObserver(this);
88
 
89
        valueBuffer = new StringBuffer();
90
 
628 adamjking 91
        if ((showLabel) || (showValue)) {
213 tk 92
            add(createLabelPanel(), labelLocation);
93
        }
94
        addMouseListener(this);
95
        setOpaque(false);
96
    }
97
 
98
    protected JPanel createLabelPanel() {
628 adamjking 99
        JPanel labelPanel = new JPanel(new GridLayout(
100
                (valueBelow ? (showLabel ? 2 : 1) : 1), (valueBelow ? 1
101
                        : (showLabel ? 2 : 1))));
213 tk 102
        labelPanel.setOpaque(false);
103
 
104
        if (showLabel) {
105
            midiParameterLabel = new JLabel(midiParameter.getMidiName()
628 adamjking 106
                    .toUpperCase(),
107
                    (showValue) ? ((valueBelow ? SwingConstants.CENTER
108
                            : SwingConstants.RIGHT)) : SwingConstants.CENTER);
696 rutgerv 109
            midiParameterLabel.setForeground(labelFieldColor);
213 tk 110
            midiParameterLabel.setOpaque(false);
111
            midiParameterLabel.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
112
            labelPanel.add(midiParameterLabel);
628 adamjking 113
        }
114
 
213 tk 115
        if (showValue) {
116
            valueField = new JTextField();
117
            valueField.setHorizontalAlignment(JTextField.CENTER);
118
            valueField.setFont(FontLoader.getFont("uni05_53.ttf", 8f));
119
            valueField.setForeground(valueFieldColor);
120
            valueField.addActionListener(this);
121
            valueField.setOpaque(false);
122
            valueField.setBorder(null);
123
            labelPanel.add(valueField);
124
        }
628 adamjking 125
 
213 tk 126
        return labelPanel;
127
    }
128
 
129
    protected JTextField createValueField() {
130
        valueField = new JTextField();
131
        return valueField;
132
    }
133
 
134
    public void setValueFieldColor(Color valueFieldColor) {
135
        this.valueFieldColor = valueFieldColor;
136
        this.valueField.setForeground(valueFieldColor);
137
    }
138
 
139
    public void setLabelFieldColor(Color labelFieldColor) {
140
        this.labelFieldColor = labelFieldColor;
141
        this.midiParameterLabel.setForeground(labelFieldColor);
142
    }
628 adamjking 143
 
213 tk 144
    public SIDSysexParameterControl getMidiParameter() {
145
        return midiParameter;
146
    }
147
 
148
    public void addMidiParameter(SIDSysexParameterControl midiPar) {
149
        midiParameters.add(midiPar);
150
        midiPar.addObserver(this);
151
    }
628 adamjking 152
 
213 tk 153
    public void removeMidiParameter(SIDSysexParameterControl midiPar) {
154
        midiParameters.remove(midiPar);
155
        midiPar.deleteObserver(this);
156
    }
628 adamjking 157
 
213 tk 158
    public void updateGraphics() {
159
        if (valueField != null) {
160
            updateValueField();
161
        }
162
    }
738 adamjking 163
 
696 rutgerv 164
    public void setSnapVals(int[] vals, String[] alias) {
165
        midiParameter.snapVals = vals;
166
        midiParameter.snapAlias = alias;
167
        for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 168
            SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
169
                    .elementAt(c);
696 rutgerv 170
            mp.snapVals = vals;
171
            mp.snapAlias = alias;
254 rutgerv 172
        }
173
    }
738 adamjking 174
 
213 tk 175
    public void setSnap(boolean b) {
628 adamjking 176
        if (b) { // Turn on snap
738 adamjking 177
            midiParameter.snap = true;
178
            if (Arrays.binarySearch(midiParameter.snapVals, midiParameter
179
                    .getMidiValue()) == -1) { // If current midi value is not
808 adamjking 180
                // part of the snap values reset
181
                // to snapvals[0];
696 rutgerv 182
                midiParameter.setMidiValue(midiParameter.snapVals[0], true);
254 rutgerv 183
                for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 184
                    SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
185
                            .elementAt(c);
254 rutgerv 186
                    mp.snap = true;
187
                }
628 adamjking 188
            } else { // Keep current midi value, only update labels
254 rutgerv 189
                for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 190
                    SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
191
                            .elementAt(c);
254 rutgerv 192
                    mp.snap = true;
193
                }
194
                updateGraphics();
213 tk 195
            }
628 adamjking 196
 
197
        } else { // Turn off snap
198
            midiParameter.snap = false;
213 tk 199
            for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 200
                SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
201
                        .elementAt(c);
213 tk 202
                mp.snap = false;
628 adamjking 203
            }
254 rutgerv 204
            updateGraphics();
213 tk 205
        }
206
    }
207
 
208
    public void updateValueField() {
696 rutgerv 209
        valueField.setText(midiParameter.getMidiValueWithAlias());
213 tk 210
    }
628 adamjking 211
 
213 tk 212
    public void actionPerformed(ActionEvent ae) {
213
        Object source = ae.getSource();
214
        if (source == valueField) {
738 adamjking 215
            midiParameter.SetMidiValueWithAlias(valueField.getText());
696 rutgerv 216
            for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 217
                SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
218
                        .elementAt(c);
696 rutgerv 219
                mp.setMidiValue(midiParameter.getMidiValue(), false);
213 tk 220
            }
628 adamjking 221
        }
213 tk 222
    }
223
 
224
    public void update(Observable observable, Object object) {
225
        if (observable == midiParameter) {
226
            if (object != SIDSysexParameter.VALUE) {
227
                if (showLabel) {
738 adamjking 228
                    midiParameterLabel.setText(midiParameter.getMidiName()
229
                            .toUpperCase());
213 tk 230
                }
231
            }
232
            updateGraphics();
233
        }
234
    }
235
 
628 adamjking 236
    public void mousePressed(MouseEvent e) {
237
    }
213 tk 238
 
628 adamjking 239
    public void mouseReleased(MouseEvent e) {
240
    }
213 tk 241
 
628 adamjking 242
    public void mouseEntered(MouseEvent e) {
243
        if (midiParameter.getTooltipListener().getClass() == MBSIDV2EditorGUI.class) {
244
            ((MBSIDV2EditorGUI) midiParameter.getTooltipListener())
245
                    .setTooltip(midiParameter.getTooltip());
246
        }
247
    }
213 tk 248
 
628 adamjking 249
    public void mouseExited(MouseEvent e) {
250
        if (midiParameter.getTooltipListener().getClass() == MBSIDV2EditorGUI.class) {
251
            ((MBSIDV2EditorGUI) midiParameter.getTooltipListener())
252
                    .setTooltip("");
253
        }
254
    }
255
 
256
    public void mouseClicked(MouseEvent e) {
257
    }
213 tk 258
}