/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/ChannelsBar.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/fantasia/ChannelsBar.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 8160 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2011 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.JSPrefs.*;
58 import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
59 import static org.jsampler.view.fantasia.FantasiaPrefs.preferences;
60 import static org.jsampler.view.fantasia.FantasiaUtils.*;
61 import static org.jsampler.view.std.JSVolumeEditorPopup.VolumeType;
62
63 /**
64 *
65 * @author Grigor Iliev
66 */
67 public class ChannelsBar extends PixmapPane {
68 private final JSlider slVolume = new JSlider();
69 JButton btnVolume = FantasiaUtils.createScreenButton("3 dB");
70
71 private final JLabel lStreams = createScreenLabel(" --");
72 private final JLabel lVoices = createScreenLabel("-- ");
73 private JSVolumeEditorPopup popupVolume;
74
75 private final FantasiaToggleButtonsPanel buttonsPanel;
76
77 private static NumberFormat numberFormat = NumberFormat.getInstance();
78
79 /** Creates a new instance of <code>ChannelsBar</code> */
80 public
81 ChannelsBar(FantasiaToggleButtonsPanel buttonsPanel) {
82 super(Res.gfxCreateChannel);
83
84 this.buttonsPanel = buttonsPanel;
85
86 numberFormat.setMaximumFractionDigits(1);
87 popupVolume = new JSVolumeEditorPopup(btnVolume, VolumeType.MASTER);
88
89 setPixmapInsets(new Insets(1, 1, 1, 1));
90
91 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
92
93 add(Box.createRigidArea(new Dimension(5, 0)));
94 JLabel l = new JLabel(Res.iconVolume22);
95 add(l);
96
97 slVolume.setUI(new FantasiaFaderUI(slVolume));
98 slVolume.putClientProperty("Fader.knobSize", new Dimension(15, 22));
99 slVolume.setOpaque(false);
100 slVolume.setFocusable(false);
101 Dimension d = new Dimension(150, 22);
102 slVolume.setPreferredSize(d);
103 slVolume.setMaximumSize(d);
104 slVolume.setAlignmentY(CENTER_ALIGNMENT);
105
106 add(slVolume);
107 add(Box.createRigidArea(new Dimension(5, 0)));
108
109 PixmapPane p = new PixmapPane(Res.gfxTextField);
110 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
111 p.setPixmapInsets(new Insets(5, 5, 4, 5));
112 p.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 5));
113
114 lStreams.setFont(Res.fontScreenMono);
115 lStreams.setHorizontalAlignment(JLabel.RIGHT);
116 lStreams.setPreferredSize(lStreams.getPreferredSize());
117 lStreams.setMaximumSize(lStreams.getPreferredSize());
118 lStreams.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount"));
119 p.add(lStreams);
120
121 l = createScreenLabel("/");
122 l.setFont(Res.fontScreenMono);
123 l.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount"));
124 p.add(l);
125
126 lVoices.setFont(Res.fontScreenMono);
127 lVoices.setPreferredSize(lVoices.getPreferredSize());
128 lVoices.setMaximumSize(lVoices.getPreferredSize());
129 lVoices.setToolTipText(i18n.getLabel("ChannelsBar.streamVoiceCount"));
130 p.add(lVoices);
131
132 btnVolume.setIcon(Res.iconVolume14);
133 btnVolume.setIconTextGap(2);
134 btnVolume.setHorizontalAlignment(JButton.LEFT);
135 d = btnVolume.getPreferredSize();
136 d.width = 65;
137 btnVolume.setPreferredSize(d);
138 btnVolume.setMaximumSize(d);
139 p.add(btnVolume);
140
141 p.setMaximumSize(p.getPreferredSize());
142 p.setAlignmentY(CENTER_ALIGNMENT);
143
144 add(p);
145
146 add(Box.createRigidArea(new Dimension(2, 0)));
147 add(createVSeparator());
148 add(Box.createRigidArea(new Dimension(6, 0)));
149
150 buttonsPanel.setAlignmentY(CENTER_ALIGNMENT);
151 add(buttonsPanel);
152 add(Box.createGlue());
153
154 d = new Dimension(420, 29);
155 setMinimumSize(d);
156 setPreferredSize(d);
157 setMaximumSize(d);
158
159 int i = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME);
160 slVolume.setMaximum(i);
161 String s = MAXIMUM_MASTER_VOLUME;
162 preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
163 public void
164 propertyChange(PropertyChangeEvent e) {
165 int j = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME);
166 slVolume.setMaximum(j);
167 }
168 });
169
170 slVolume.addChangeListener(new ChangeListener() {
171 public void
172 stateChanged(ChangeEvent e) { setVolume(); }
173 });
174
175 CC.getSamplerModel().addSamplerListener(new SamplerAdapter() {
176 public void
177 volumeChanged(SamplerEvent e) { updateVolume(); }
178
179 public void
180 totalVoiceCountChanged(SamplerEvent e) {
181 int i = CC.getSamplerModel().getTotalVoiceCount();
182 if(i == 0) lVoices.setText("--");
183 else lVoices.setText(String.valueOf(i));
184 }
185
186 public void
187 totalStreamCountChanged(SamplerEvent e) {
188 int i = CC.getSamplerModel().getTotalStreamCount();
189 if(i == 0) lStreams.setText("--");
190 else lStreams.setText(String.valueOf(i));
191 }
192 });
193
194 updateVolume();
195
196 btnVolume.addActionListener(new ActionListener() {
197 public void
198 actionPerformed(ActionEvent e) {
199 if(popupVolume.isVisible()) {
200 popupVolume.commit();
201 popupVolume.hide();
202 } else {
203 float vol = CC.getSamplerModel().getVolume();
204 popupVolume.setCurrentVolume(vol);
205 popupVolume.show();
206 }
207 }
208 });
209
210 popupVolume.addActionListener(new ActionListener() {
211 public void
212 actionPerformed(ActionEvent e) {
213 CC.getSamplerModel().setBackendVolume(popupVolume.getVolumeFactor());
214 }
215 });
216
217 s = VOL_MEASUREMENT_UNIT_DECIBEL;
218 preferences().addPropertyChangeListener(s, new PropertyChangeListener() {
219 public void
220 propertyChange(PropertyChangeEvent e) {
221 setVolume();
222 }
223 });
224 }
225
226 /*@Override
227 protected void
228 paintComponent(Graphics g) {
229 if(isOpaque()) super.paintComponent(g);
230
231 double h = getSize().getHeight();
232 double w = getSize().getWidth();
233 Graphics2D g2 = (Graphics2D)g;
234
235 FantasiaPainter.paintGradient(g2, 0, 0, w - 1, h - 1);
236 RoundCorners rc = new RoundCorners(false, true, true, false);
237 FantasiaPainter.paintOuterBorder(g2, 0, -1, w - 1, h - 1, rc);
238
239 }*/
240
241 private void
242 setVolume() {
243 int volume = slVolume.getValue();
244
245 if(CC.getViewConfig().isMeasurementUnitDecibel()) {
246 double dB = HF.percentsToDecibels(volume);
247 btnVolume.setText(numberFormat.format(dB) + "dB");
248 } else {
249 btnVolume.setText(volume + "%");
250 }
251
252 if(slVolume.getValueIsAdjusting()) return;
253
254 int vol = (int)(CC.getSamplerModel().getVolume() * 100);
255
256 if(vol == slVolume.getValue()) return;
257
258 /*
259 * If the model's volume is not equal to the slider
260 * value we assume that the change is due to user input.
261 * So we must update the volume at the backend too.
262 */
263 float v = slVolume.getValue();
264 v /= 100;
265 CC.getSamplerModel().setBackendVolume(v);
266 }
267
268 private void
269 updateVolume() {
270 slVolume.setValue((int)(CC.getSamplerModel().getVolume() * 100));
271 }
272
273 protected JPanel
274 createVSeparator() {
275 PixmapPane p = new PixmapPane(Res.gfxVLine);
276 p.setOpaque(false);
277 p.setPreferredSize(new Dimension(2, 29));
278 p.setMinimumSize(p.getPreferredSize());
279 p.setMaximumSize(p.getPreferredSize());
280 return p;
281 }
282 }

  ViewVC Help
Powered by ViewVC