1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.fantasia; |
24 |
|
25 |
import java.awt.Dimension; |
26 |
import java.awt.Insets; |
27 |
|
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
|
31 |
import java.beans.PropertyChangeEvent; |
32 |
import java.beans.PropertyChangeListener; |
33 |
|
34 |
import java.text.NumberFormat; |
35 |
|
36 |
import javax.swing.BorderFactory; |
37 |
import javax.swing.Box; |
38 |
import javax.swing.BoxLayout; |
39 |
import javax.swing.JButton; |
40 |
import javax.swing.JLabel; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.JSlider; |
43 |
|
44 |
import javax.swing.event.ChangeEvent; |
45 |
import javax.swing.event.ChangeListener; |
46 |
|
47 |
import org.jsampler.CC; |
48 |
import org.jsampler.HF; |
49 |
|
50 |
import org.jsampler.event.SamplerAdapter; |
51 |
import org.jsampler.event.SamplerEvent; |
52 |
|
53 |
import org.jsampler.view.fantasia.basic.*; |
54 |
|
55 |
import org.jsampler.view.std.JSVolumeEditorPopup; |
56 |
|
57 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
58 |
import static org.jsampler.view.fantasia.FantasiaPrefs.preferences; |
59 |
import static org.jsampler.view.fantasia.FantasiaUtils.*; |
60 |
|
61 |
import static org.jsampler.view.std.JSVolumeEditorPopup.VolumeType; |
62 |
import static org.jsampler.view.std.StdPrefs.*; |
63 |
|
64 |
/** |
65 |
* |
66 |
* @author Grigor Iliev |
67 |
*/ |
68 |
public class ChannelsBar extends PixmapPane { |
69 |
private final JSlider slVolume = new JSlider(); |
70 |
JButton btnVolume = FantasiaUtils.createScreenButton("3 dB"); |
71 |
|
72 |
private final JLabel lStreams = createScreenLabel(" --"); |
73 |
private final JLabel lVoices = createScreenLabel("-- "); |
74 |
private JSVolumeEditorPopup popupVolume; |
75 |
|
76 |
private final FantasiaToggleButtonsPanel buttonsPanel; |
77 |
|
78 |
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
79 |
|
80 |
/** Creates a new instance of <code>ChannelsBar</code> */ |
81 |
public |
82 |
ChannelsBar(FantasiaToggleButtonsPanel buttonsPanel) { |
83 |
super(Res.gfxCreateChannel); |
84 |
|
85 |
this.buttonsPanel = buttonsPanel; |
86 |
|
87 |
numberFormat.setMaximumFractionDigits(1); |
88 |
popupVolume = new JSVolumeEditorPopup(btnVolume, VolumeType.MASTER); |
89 |
|
90 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
91 |
|
92 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
93 |
|
94 |
add(Box.createRigidArea(new Dimension(5, 0))); |
95 |
JLabel l = new JLabel(Res.iconVolume22); |
96 |
add(l); |
97 |
|
98 |
slVolume.setUI(new FantasiaFaderUI(slVolume)); |
99 |
slVolume.putClientProperty("Fader.knobSize", new Dimension(15, 22)); |
100 |
slVolume.setOpaque(false); |
101 |
slVolume.setFocusable(false); |
102 |
Dimension d = new Dimension(150, 22); |
103 |
slVolume.setPreferredSize(d); |
104 |
slVolume.setMaximumSize(d); |
105 |
slVolume.setAlignmentY(CENTER_ALIGNMENT); |
106 |
|
107 |
add(slVolume); |
108 |
add(Box.createRigidArea(new Dimension(5, 0))); |
109 |
|
110 |
PixmapPane p = new PixmapPane(Res.gfxTextField); |
111 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
112 |
p.setPixmapInsets(new Insets(5, 5, 4, 5)); |
113 |
p.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 5)); |
114 |
|
115 |
lStreams.setFont(Res.fontScreenMono); |
116 |
lStreams.setHorizontalAlignment(JLabel.RIGHT); |
117 |
lStreams.setPreferredSize(lStreams.getPreferredSize()); |
118 |
lStreams.setMaximumSize(lStreams.getPreferredSize()); |
119 |
lStreams.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
120 |
p.add(lStreams); |
121 |
|
122 |
l = createScreenLabel("/"); |
123 |
l.setFont(Res.fontScreenMono); |
124 |
l.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
125 |
p.add(l); |
126 |
|
127 |
lVoices.setFont(Res.fontScreenMono); |
128 |
lVoices.setPreferredSize(lVoices.getPreferredSize()); |
129 |
lVoices.setMaximumSize(lVoices.getPreferredSize()); |
130 |
lVoices.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
131 |
p.add(lVoices); |
132 |
|
133 |
btnVolume.setIcon(Res.iconVolume14); |
134 |
btnVolume.setIconTextGap(2); |
135 |
btnVolume.setHorizontalAlignment(JButton.LEFT); |
136 |
d = btnVolume.getPreferredSize(); |
137 |
d.width = 65; |
138 |
btnVolume.setPreferredSize(d); |
139 |
btnVolume.setMaximumSize(d); |
140 |
p.add(btnVolume); |
141 |
|
142 |
p.setMaximumSize(p.getPreferredSize()); |
143 |
p.setAlignmentY(CENTER_ALIGNMENT); |
144 |
|
145 |
add(p); |
146 |
|
147 |
add(Box.createRigidArea(new Dimension(2, 0))); |
148 |
add(createVSeparator()); |
149 |
add(Box.createRigidArea(new Dimension(6, 0))); |
150 |
|
151 |
buttonsPanel.setAlignmentY(CENTER_ALIGNMENT); |
152 |
add(buttonsPanel); |
153 |
add(Box.createGlue()); |
154 |
|
155 |
d = new Dimension(420, 29); |
156 |
setMinimumSize(d); |
157 |
setPreferredSize(d); |
158 |
setMaximumSize(d); |
159 |
|
160 |
int i = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
161 |
slVolume.setMaximum(i); |
162 |
String s = MAXIMUM_MASTER_VOLUME; |
163 |
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
164 |
public void |
165 |
propertyChange(PropertyChangeEvent e) { |
166 |
int j = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
167 |
slVolume.setMaximum(j); |
168 |
} |
169 |
}); |
170 |
|
171 |
slVolume.addChangeListener(new ChangeListener() { |
172 |
public void |
173 |
stateChanged(ChangeEvent e) { setVolume(); } |
174 |
}); |
175 |
|
176 |
CC.getSamplerModel().addSamplerListener(new SamplerAdapter() { |
177 |
public void |
178 |
volumeChanged(SamplerEvent e) { updateVolume(); } |
179 |
|
180 |
public void |
181 |
totalVoiceCountChanged(SamplerEvent e) { |
182 |
int i = CC.getSamplerModel().getTotalVoiceCount(); |
183 |
if(i == 0) lVoices.setText("--"); |
184 |
else lVoices.setText(String.valueOf(i)); |
185 |
} |
186 |
|
187 |
public void |
188 |
totalStreamCountChanged(SamplerEvent e) { |
189 |
int i = CC.getSamplerModel().getTotalStreamCount(); |
190 |
if(i == 0) lStreams.setText("--"); |
191 |
else lStreams.setText(String.valueOf(i)); |
192 |
} |
193 |
}); |
194 |
|
195 |
updateVolume(); |
196 |
|
197 |
btnVolume.addActionListener(new ActionListener() { |
198 |
public void |
199 |
actionPerformed(ActionEvent e) { |
200 |
if(popupVolume.isVisible()) { |
201 |
popupVolume.commit(); |
202 |
popupVolume.hide(); |
203 |
} else { |
204 |
float vol = CC.getSamplerModel().getVolume(); |
205 |
popupVolume.setCurrentVolume(vol); |
206 |
popupVolume.show(); |
207 |
} |
208 |
} |
209 |
}); |
210 |
|
211 |
popupVolume.addActionListener(new ActionListener() { |
212 |
public void |
213 |
actionPerformed(ActionEvent e) { |
214 |
CC.getSamplerModel().setBackendVolume(popupVolume.getVolumeFactor()); |
215 |
} |
216 |
}); |
217 |
|
218 |
s = VOL_MEASUREMENT_UNIT_DECIBEL; |
219 |
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
220 |
public void |
221 |
propertyChange(PropertyChangeEvent e) { |
222 |
setVolume(); |
223 |
} |
224 |
}); |
225 |
} |
226 |
|
227 |
/*@Override |
228 |
protected void |
229 |
paintComponent(Graphics g) { |
230 |
if(isOpaque()) super.paintComponent(g); |
231 |
|
232 |
double h = getSize().getHeight(); |
233 |
double w = getSize().getWidth(); |
234 |
Graphics2D g2 = (Graphics2D)g; |
235 |
|
236 |
FantasiaPainter.paintGradient(g2, 0, 0, w - 1, h - 1); |
237 |
RoundCorners rc = new RoundCorners(false, true, true, false); |
238 |
FantasiaPainter.paintOuterBorder(g2, 0, -1, w - 1, h - 1, rc); |
239 |
|
240 |
}*/ |
241 |
|
242 |
private void |
243 |
setVolume() { |
244 |
int volume = slVolume.getValue(); |
245 |
|
246 |
if(CC.getViewConfig().isMeasurementUnitDecibel()) { |
247 |
double dB = HF.percentsToDecibels(volume); |
248 |
btnVolume.setText(numberFormat.format(dB) + "dB"); |
249 |
} else { |
250 |
btnVolume.setText(volume + "%"); |
251 |
} |
252 |
|
253 |
if(slVolume.getValueIsAdjusting()) return; |
254 |
|
255 |
int vol = (int)(CC.getSamplerModel().getVolume() * 100); |
256 |
|
257 |
if(vol == slVolume.getValue()) return; |
258 |
|
259 |
/* |
260 |
* If the model's volume is not equal to the slider |
261 |
* value we assume that the change is due to user input. |
262 |
* So we must update the volume at the backend too. |
263 |
*/ |
264 |
float v = slVolume.getValue(); |
265 |
v /= 100; |
266 |
CC.getSamplerModel().setBackendVolume(v); |
267 |
} |
268 |
|
269 |
private void |
270 |
updateVolume() { |
271 |
slVolume.setValue((int)(CC.getSamplerModel().getVolume() * 100)); |
272 |
} |
273 |
|
274 |
protected JPanel |
275 |
createVSeparator() { |
276 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
277 |
p.setOpaque(false); |
278 |
p.setPreferredSize(new Dimension(2, 29)); |
279 |
p.setMinimumSize(p.getPreferredSize()); |
280 |
p.setMaximumSize(p.getPreferredSize()); |
281 |
return p; |
282 |
} |
283 |
} |