1 |
iliev |
1286 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2005-2007 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 |
iliev |
1357 |
import java.awt.Point; |
28 |
iliev |
1286 |
|
29 |
iliev |
1540 |
import java.awt.event.ActionEvent; |
30 |
|
|
import java.awt.event.ActionListener; |
31 |
iliev |
1357 |
import java.awt.event.MouseAdapter; |
32 |
|
|
import java.awt.event.MouseEvent; |
33 |
|
|
|
34 |
|
|
import java.beans.PropertyChangeEvent; |
35 |
|
|
import java.beans.PropertyChangeListener; |
36 |
|
|
|
37 |
iliev |
1540 |
import java.text.NumberFormat; |
38 |
|
|
|
39 |
|
|
import javax.swing.BorderFactory; |
40 |
iliev |
1286 |
import javax.swing.Box; |
41 |
|
|
import javax.swing.BoxLayout; |
42 |
iliev |
1540 |
import javax.swing.JButton; |
43 |
iliev |
1286 |
import javax.swing.JLabel; |
44 |
iliev |
1540 |
import javax.swing.JPanel; |
45 |
iliev |
1286 |
import javax.swing.JSlider; |
46 |
iliev |
1357 |
import javax.swing.JToolTip; |
47 |
iliev |
1286 |
|
48 |
iliev |
1357 |
import javax.swing.event.ChangeEvent; |
49 |
|
|
import javax.swing.event.ChangeListener; |
50 |
|
|
|
51 |
|
|
import org.jsampler.CC; |
52 |
iliev |
1540 |
import org.jsampler.HF; |
53 |
iliev |
1357 |
|
54 |
|
|
import org.jsampler.event.SamplerAdapter; |
55 |
|
|
import org.jsampler.event.SamplerEvent; |
56 |
|
|
|
57 |
iliev |
1540 |
import org.jsampler.view.std.JSVolumeEditorPopup; |
58 |
|
|
|
59 |
iliev |
1357 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
60 |
|
|
import static org.jsampler.view.fantasia.FantasiaPrefs.preferences; |
61 |
iliev |
1540 |
import static org.jsampler.view.fantasia.FantasiaUtils.*; |
62 |
|
|
|
63 |
|
|
import static org.jsampler.view.std.JSVolumeEditorPopup.VolumeType; |
64 |
iliev |
1357 |
import static org.jsampler.view.std.StdPrefs.*; |
65 |
|
|
|
66 |
iliev |
1286 |
/** |
67 |
|
|
* |
68 |
|
|
* @author Grigor Iliev |
69 |
|
|
*/ |
70 |
|
|
public class ChannelsBar extends PixmapPane { |
71 |
|
|
private final JSlider slVolume = new JSlider(); |
72 |
iliev |
1540 |
JButton btnVolume = FantasiaUtils.createScreenButton("3 dB"); |
73 |
iliev |
1286 |
|
74 |
iliev |
1540 |
private final JLabel lStreams = createScreenLabel(" --"); |
75 |
|
|
private final JLabel lVoices = createScreenLabel("-- "); |
76 |
|
|
private JSVolumeEditorPopup popupVolume; |
77 |
|
|
|
78 |
|
|
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
79 |
|
|
|
80 |
iliev |
1286 |
/** Creates a new instance of <code>ChannelsBar</code> */ |
81 |
|
|
public |
82 |
|
|
ChannelsBar() { |
83 |
|
|
super(Res.gfxCreateChannel); |
84 |
iliev |
1540 |
|
85 |
|
|
numberFormat.setMaximumFractionDigits(1); |
86 |
|
|
popupVolume = new JSVolumeEditorPopup(btnVolume, VolumeType.MASTER); |
87 |
|
|
|
88 |
iliev |
1286 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
89 |
|
|
|
90 |
|
|
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
91 |
|
|
|
92 |
|
|
add(Box.createRigidArea(new Dimension(5, 0))); |
93 |
|
|
JLabel l = new JLabel(Res.iconVolume22); |
94 |
|
|
add(l); |
95 |
|
|
|
96 |
|
|
slVolume.setOpaque(false); |
97 |
|
|
slVolume.setFocusable(false); |
98 |
|
|
Dimension d = new Dimension(150, 22); |
99 |
|
|
slVolume.setPreferredSize(d); |
100 |
|
|
slVolume.setMaximumSize(d); |
101 |
|
|
|
102 |
|
|
add(slVolume); |
103 |
iliev |
1540 |
add(Box.createRigidArea(new Dimension(5, 0))); |
104 |
iliev |
1357 |
|
105 |
iliev |
1540 |
PixmapPane p = new PixmapPane(Res.gfxTextField); |
106 |
|
|
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
107 |
|
|
p.setPixmapInsets(new Insets(5, 5, 4, 5)); |
108 |
|
|
p.setBorder(BorderFactory.createEmptyBorder(1, 8, 1, 5)); |
109 |
|
|
|
110 |
|
|
lStreams.setFont(Res.fontScreenMono); |
111 |
|
|
lStreams.setHorizontalAlignment(JLabel.RIGHT); |
112 |
|
|
lStreams.setPreferredSize(lStreams.getPreferredSize()); |
113 |
|
|
lStreams.setMaximumSize(lStreams.getPreferredSize()); |
114 |
|
|
lStreams.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
115 |
|
|
p.add(lStreams); |
116 |
|
|
|
117 |
|
|
l = createScreenLabel("/"); |
118 |
|
|
l.setFont(Res.fontScreenMono); |
119 |
|
|
l.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
120 |
|
|
p.add(l); |
121 |
|
|
|
122 |
|
|
lVoices.setFont(Res.fontScreenMono); |
123 |
|
|
lVoices.setPreferredSize(lVoices.getPreferredSize()); |
124 |
|
|
lVoices.setMaximumSize(lVoices.getPreferredSize()); |
125 |
|
|
lVoices.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount")); |
126 |
|
|
p.add(lVoices); |
127 |
|
|
|
128 |
|
|
add(Box.createRigidArea(new Dimension(5, 0))); |
129 |
|
|
|
130 |
|
|
btnVolume.setIcon(Res.iconVolume14); |
131 |
|
|
btnVolume.setIconTextGap(2); |
132 |
|
|
btnVolume.setHorizontalAlignment(btnVolume.LEFT); |
133 |
|
|
d = btnVolume.getPreferredSize(); |
134 |
iliev |
1688 |
d.width = 59; |
135 |
iliev |
1540 |
btnVolume.setPreferredSize(d); |
136 |
|
|
btnVolume.setMaximumSize(d); |
137 |
|
|
p.add(btnVolume); |
138 |
|
|
|
139 |
|
|
p.setMaximumSize(p.getPreferredSize()); |
140 |
|
|
|
141 |
|
|
add(p); |
142 |
iliev |
1286 |
add(Box.createGlue()); |
143 |
|
|
|
144 |
|
|
d = new Dimension(420, 29); |
145 |
|
|
setMinimumSize(d); |
146 |
|
|
setPreferredSize(d); |
147 |
|
|
setMaximumSize(d); |
148 |
|
|
|
149 |
iliev |
1357 |
int i = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
150 |
|
|
slVolume.setMaximum(i); |
151 |
|
|
String s = MAXIMUM_MASTER_VOLUME; |
152 |
|
|
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
153 |
|
|
public void |
154 |
|
|
propertyChange(PropertyChangeEvent e) { |
155 |
|
|
int j = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
156 |
|
|
slVolume.setMaximum(j); |
157 |
|
|
} |
158 |
|
|
}); |
159 |
|
|
|
160 |
|
|
slVolume.addChangeListener(new ChangeListener() { |
161 |
|
|
public void |
162 |
|
|
stateChanged(ChangeEvent e) { setVolume(); } |
163 |
|
|
}); |
164 |
|
|
|
165 |
|
|
CC.getSamplerModel().addSamplerListener(new SamplerAdapter() { |
166 |
|
|
public void |
167 |
|
|
volumeChanged(SamplerEvent e) { updateVolume(); } |
168 |
iliev |
1545 |
|
169 |
|
|
public void |
170 |
|
|
totalVoiceCountChanged(SamplerEvent e) { |
171 |
|
|
int i = CC.getSamplerModel().getTotalVoiceCount(); |
172 |
|
|
if(i == 0) lVoices.setText("--"); |
173 |
|
|
else lVoices.setText(String.valueOf(i)); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
public void |
177 |
|
|
totalStreamCountChanged(SamplerEvent e) { |
178 |
|
|
int i = CC.getSamplerModel().getTotalStreamCount(); |
179 |
|
|
if(i == 0) lStreams.setText("--"); |
180 |
|
|
else lStreams.setText(String.valueOf(i)); |
181 |
|
|
} |
182 |
iliev |
1357 |
}); |
183 |
|
|
|
184 |
|
|
updateVolume(); |
185 |
|
|
|
186 |
iliev |
1540 |
btnVolume.addActionListener(new ActionListener() { |
187 |
iliev |
1357 |
public void |
188 |
iliev |
1540 |
actionPerformed(ActionEvent e) { |
189 |
|
|
if(popupVolume.isVisible()) { |
190 |
|
|
popupVolume.commit(); |
191 |
|
|
popupVolume.hide(); |
192 |
|
|
} else { |
193 |
|
|
float vol = CC.getSamplerModel().getVolume(); |
194 |
|
|
popupVolume.setCurrentVolume(vol); |
195 |
|
|
popupVolume.show(); |
196 |
iliev |
1357 |
} |
197 |
|
|
} |
198 |
iliev |
1540 |
}); |
199 |
|
|
|
200 |
|
|
popupVolume.addActionListener(new ActionListener() { |
201 |
iliev |
1357 |
public void |
202 |
iliev |
1540 |
actionPerformed(ActionEvent e) { |
203 |
|
|
CC.getSamplerModel().setBackendVolume(popupVolume.getVolumeFactor()); |
204 |
iliev |
1357 |
} |
205 |
|
|
}); |
206 |
iliev |
1540 |
|
207 |
|
|
s = VOL_MEASUREMENT_UNIT_DECIBEL; |
208 |
|
|
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
209 |
|
|
public void |
210 |
|
|
propertyChange(PropertyChangeEvent e) { |
211 |
|
|
setVolume(); |
212 |
|
|
} |
213 |
|
|
}); |
214 |
iliev |
1286 |
} |
215 |
|
|
|
216 |
iliev |
1357 |
private void |
217 |
|
|
setVolume() { |
218 |
|
|
int volume = slVolume.getValue(); |
219 |
|
|
|
220 |
iliev |
1540 |
if(CC.getViewConfig().isMeasurementUnitDecibel()) { |
221 |
|
|
double dB = HF.percentsToDecibels(volume); |
222 |
|
|
btnVolume.setText(numberFormat.format(dB) + "dB"); |
223 |
|
|
} else { |
224 |
|
|
btnVolume.setText(volume + "%"); |
225 |
|
|
} |
226 |
|
|
|
227 |
iliev |
1357 |
if(slVolume.getValueIsAdjusting()) return; |
228 |
|
|
|
229 |
|
|
int vol = (int)(CC.getSamplerModel().getVolume() * 100); |
230 |
|
|
|
231 |
|
|
if(vol == slVolume.getValue()) return; |
232 |
|
|
|
233 |
|
|
/* |
234 |
|
|
* If the model's volume is not equal to the slider |
235 |
|
|
* value we assume that the change is due to user input. |
236 |
|
|
* So we must update the volume at the backend too. |
237 |
|
|
*/ |
238 |
|
|
float v = slVolume.getValue(); |
239 |
|
|
v /= 100; |
240 |
|
|
CC.getSamplerModel().setBackendVolume(v); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
private void |
244 |
|
|
updateVolume() { |
245 |
|
|
slVolume.setValue((int)(CC.getSamplerModel().getVolume() * 100)); |
246 |
|
|
} |
247 |
|
|
|
248 |
iliev |
1286 |
} |