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 |
1357 |
import java.awt.event.MouseAdapter; |
30 |
|
|
import java.awt.event.MouseEvent; |
31 |
|
|
|
32 |
|
|
import java.beans.PropertyChangeEvent; |
33 |
|
|
import java.beans.PropertyChangeListener; |
34 |
|
|
|
35 |
iliev |
1286 |
import javax.swing.Box; |
36 |
|
|
import javax.swing.BoxLayout; |
37 |
|
|
import javax.swing.JLabel; |
38 |
|
|
import javax.swing.JSlider; |
39 |
iliev |
1357 |
import javax.swing.JToolTip; |
40 |
|
|
import javax.swing.Popup; |
41 |
|
|
import javax.swing.PopupFactory; |
42 |
iliev |
1286 |
|
43 |
iliev |
1357 |
import javax.swing.event.ChangeEvent; |
44 |
|
|
import javax.swing.event.ChangeListener; |
45 |
|
|
|
46 |
|
|
import org.jsampler.CC; |
47 |
|
|
|
48 |
|
|
import org.jsampler.event.SamplerAdapter; |
49 |
|
|
import org.jsampler.event.SamplerEvent; |
50 |
|
|
|
51 |
|
|
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
52 |
|
|
import static org.jsampler.view.fantasia.FantasiaPrefs.preferences; |
53 |
|
|
import static org.jsampler.view.std.StdPrefs.*; |
54 |
|
|
|
55 |
iliev |
1286 |
/** |
56 |
|
|
* |
57 |
|
|
* @author Grigor Iliev |
58 |
|
|
*/ |
59 |
|
|
public class ChannelsBar extends PixmapPane { |
60 |
|
|
private final JSlider slVolume = new JSlider(); |
61 |
iliev |
1357 |
private Popup popup = null; |
62 |
|
|
private final JToolTip tip = new JToolTip(); |
63 |
iliev |
1286 |
|
64 |
|
|
/** Creates a new instance of <code>ChannelsBar</code> */ |
65 |
|
|
public |
66 |
|
|
ChannelsBar() { |
67 |
|
|
super(Res.gfxCreateChannel); |
68 |
|
|
setPixmapInsets(new Insets(1, 1, 1, 1)); |
69 |
|
|
|
70 |
|
|
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
71 |
|
|
|
72 |
|
|
add(Box.createRigidArea(new Dimension(5, 0))); |
73 |
|
|
JLabel l = new JLabel(Res.iconVolume22); |
74 |
|
|
add(l); |
75 |
|
|
|
76 |
|
|
slVolume.setOpaque(false); |
77 |
|
|
slVolume.setFocusable(false); |
78 |
|
|
Dimension d = new Dimension(150, 22); |
79 |
|
|
slVolume.setPreferredSize(d); |
80 |
|
|
slVolume.setMaximumSize(d); |
81 |
|
|
|
82 |
|
|
add(slVolume); |
83 |
iliev |
1357 |
|
84 |
iliev |
1286 |
add(Box.createGlue()); |
85 |
|
|
|
86 |
|
|
d = new Dimension(420, 29); |
87 |
|
|
setMinimumSize(d); |
88 |
|
|
setPreferredSize(d); |
89 |
|
|
setMaximumSize(d); |
90 |
|
|
|
91 |
iliev |
1357 |
// Setting the tooltip size |
92 |
|
|
tip.setTipText(i18n.getLabel("ChannelsBar.volume", 1000)); |
93 |
|
|
tip.setMinimumSize(tip.getPreferredSize()); |
94 |
|
|
tip.setPreferredSize(tip.getPreferredSize()); // workaround for preserving that size |
95 |
|
|
tip.setComponent(slVolume); |
96 |
|
|
/////// |
97 |
|
|
|
98 |
|
|
int i = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
99 |
|
|
slVolume.setMaximum(i); |
100 |
|
|
String s = MAXIMUM_MASTER_VOLUME; |
101 |
|
|
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
102 |
|
|
public void |
103 |
|
|
propertyChange(PropertyChangeEvent e) { |
104 |
|
|
int j = preferences().getIntProperty(MAXIMUM_MASTER_VOLUME); |
105 |
|
|
slVolume.setMaximum(j); |
106 |
|
|
} |
107 |
|
|
}); |
108 |
|
|
|
109 |
|
|
slVolume.addChangeListener(new ChangeListener() { |
110 |
|
|
public void |
111 |
|
|
stateChanged(ChangeEvent e) { setVolume(); } |
112 |
|
|
}); |
113 |
|
|
|
114 |
|
|
CC.getSamplerModel().addSamplerListener(new SamplerAdapter() { |
115 |
|
|
public void |
116 |
|
|
volumeChanged(SamplerEvent e) { updateVolume(); } |
117 |
|
|
}); |
118 |
|
|
|
119 |
|
|
updateVolume(); |
120 |
|
|
|
121 |
|
|
slVolume.addMouseListener(new MouseAdapter() { |
122 |
|
|
public void |
123 |
|
|
mousePressed(MouseEvent e) { |
124 |
|
|
if(popup != null) { |
125 |
|
|
popup.hide(); |
126 |
|
|
popup = null; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
Point p = slVolume.getLocationOnScreen(); |
130 |
|
|
PopupFactory pf = PopupFactory.getSharedInstance(); |
131 |
|
|
popup = pf.getPopup(slVolume, tip, p.x, p.y - 22); |
132 |
|
|
popup.show(); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
public void |
136 |
|
|
mouseReleased(MouseEvent e) { |
137 |
|
|
if(popup != null) { |
138 |
|
|
popup.hide(); |
139 |
|
|
popup = null; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
}); |
143 |
iliev |
1286 |
} |
144 |
|
|
|
145 |
iliev |
1357 |
private void |
146 |
|
|
setVolume() { |
147 |
|
|
int volume = slVolume.getValue(); |
148 |
|
|
String s = i18n.getLabel("ChannelsBar.volume", volume); |
149 |
|
|
slVolume.setToolTipText(s); |
150 |
|
|
tip.setTipText(s); |
151 |
|
|
tip.repaint(); |
152 |
|
|
|
153 |
|
|
if(slVolume.getValueIsAdjusting()) return; |
154 |
|
|
|
155 |
|
|
int vol = (int)(CC.getSamplerModel().getVolume() * 100); |
156 |
|
|
|
157 |
|
|
if(vol == slVolume.getValue()) return; |
158 |
|
|
|
159 |
|
|
/* |
160 |
|
|
* If the model's volume is not equal to the slider |
161 |
|
|
* value we assume that the change is due to user input. |
162 |
|
|
* So we must update the volume at the backend too. |
163 |
|
|
*/ |
164 |
|
|
float v = slVolume.getValue(); |
165 |
|
|
v /= 100; |
166 |
|
|
CC.getSamplerModel().setBackendVolume(v); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
private void |
170 |
|
|
updateVolume() { |
171 |
|
|
slVolume.setValue((int)(CC.getSamplerModel().getVolume() * 100)); |
172 |
|
|
} |
173 |
|
|
|
174 |
iliev |
1286 |
} |