1 |
iliev |
1308 |
/* |
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 |
|
|
package org.jsampler.view.std; |
23 |
|
|
|
24 |
iliev |
1496 |
import java.awt.Desktop; |
25 |
|
|
|
26 |
iliev |
1540 |
import java.awt.event.ActionEvent; |
27 |
|
|
import java.awt.event.ActionListener; |
28 |
|
|
import java.awt.event.MouseAdapter; |
29 |
|
|
import java.awt.event.MouseEvent; |
30 |
|
|
|
31 |
|
|
import java.beans.PropertyChangeEvent; |
32 |
|
|
import java.beans.PropertyChangeListener; |
33 |
|
|
|
34 |
iliev |
1496 |
import java.net.URI; |
35 |
|
|
|
36 |
iliev |
1540 |
import java.text.NumberFormat; |
37 |
|
|
|
38 |
iliev |
1308 |
import java.util.Vector; |
39 |
|
|
|
40 |
iliev |
1540 |
import javax.swing.JComboBox; |
41 |
|
|
import javax.swing.JSlider; |
42 |
|
|
import javax.swing.JToolTip; |
43 |
|
|
import javax.swing.Popup; |
44 |
|
|
import javax.swing.PopupFactory; |
45 |
|
|
|
46 |
|
|
import javax.swing.event.ChangeEvent; |
47 |
|
|
import javax.swing.event.ChangeListener; |
48 |
iliev |
1308 |
import org.jsampler.CC; |
49 |
iliev |
1496 |
import org.jsampler.HF; |
50 |
iliev |
1308 |
import org.jsampler.JSPrefs; |
51 |
|
|
|
52 |
iliev |
1496 |
import static org.jsampler.view.std.StdI18n.i18n; |
53 |
iliev |
1540 |
import static org.jsampler.view.std.StdPrefs.*; |
54 |
iliev |
1308 |
|
55 |
iliev |
1496 |
|
56 |
iliev |
1308 |
/** |
57 |
|
|
* |
58 |
|
|
* @author Grigor Iliev |
59 |
|
|
*/ |
60 |
|
|
public class StdUtils { |
61 |
|
|
|
62 |
|
|
/** Forbids the instantiation of this class */ |
63 |
|
|
private |
64 |
|
|
StdUtils() { } |
65 |
|
|
|
66 |
|
|
private static JSPrefs |
67 |
|
|
preferences() { return CC.getViewConfig().preferences(); } |
68 |
|
|
|
69 |
iliev |
1540 |
public static JComboBox |
70 |
|
|
createPathComboBox() { |
71 |
|
|
final JComboBox cb = new JComboBox(); |
72 |
|
|
cb.setEditable(true); |
73 |
|
|
cb.addActionListener(new ActionListener() { |
74 |
|
|
public void |
75 |
|
|
actionPerformed(ActionEvent e) { |
76 |
|
|
if(cb.getSelectedItem() == null) { |
77 |
|
|
cb.setToolTipText(null); |
78 |
|
|
return; |
79 |
|
|
} |
80 |
|
|
String s = cb.getSelectedItem().toString(); |
81 |
|
|
if(s.length() < 15) cb.setToolTipText(null); |
82 |
|
|
else cb.setToolTipText(s); |
83 |
|
|
} |
84 |
|
|
}); |
85 |
|
|
|
86 |
|
|
return cb; |
87 |
|
|
} |
88 |
|
|
|
89 |
iliev |
1308 |
/** |
90 |
|
|
* Updates the specified string list property by adding the specified |
91 |
|
|
* element on the top. Also restricts the maximum number of elements to 12. |
92 |
|
|
*/ |
93 |
|
|
public static void |
94 |
|
|
updateRecentElements(String property, String newElement) { |
95 |
|
|
String[] elements = preferences().getStringListProperty(property); |
96 |
|
|
Vector<String> v = new Vector<String>(); |
97 |
|
|
v.add(newElement); |
98 |
|
|
for(String s : elements) { |
99 |
|
|
if(!newElement.equals(s)) v.add(s); |
100 |
|
|
} |
101 |
|
|
if(v.size() > 12) v.setSize(12); |
102 |
|
|
|
103 |
|
|
elements = v.toArray(new String[v.size()]); |
104 |
|
|
preferences().setStringListProperty(property, elements); |
105 |
|
|
} |
106 |
iliev |
1496 |
|
107 |
|
|
public static boolean |
108 |
|
|
checkDesktopSupported() { |
109 |
|
|
if(Desktop.isDesktopSupported()) return true; |
110 |
|
|
|
111 |
|
|
String s = i18n.getError("StdUtils.DesktopApiNotSupported"); |
112 |
|
|
HF.showErrorMessage(s, CC.getMainFrame()); |
113 |
|
|
|
114 |
|
|
return false; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
public static void |
118 |
|
|
browse(String uri) { |
119 |
|
|
if(!checkDesktopSupported()) return; |
120 |
|
|
|
121 |
|
|
try { Desktop.getDesktop().browse(new URI(uri)); } |
122 |
|
|
catch(Exception x) { x.printStackTrace(); } |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
public static void |
126 |
|
|
mail(String uri) { |
127 |
|
|
if(!StdUtils.checkDesktopSupported()) return; |
128 |
|
|
|
129 |
|
|
Desktop desktop = Desktop.getDesktop(); |
130 |
|
|
|
131 |
|
|
try { Desktop.getDesktop().mail(new URI(uri)); } |
132 |
|
|
catch(Exception x) { x.printStackTrace(); } |
133 |
|
|
} |
134 |
iliev |
1540 |
|
135 |
|
|
public static JSlider |
136 |
|
|
createVolumeSlider() { |
137 |
|
|
return new VolumeSlider(); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
private static class VolumeSlider extends JSlider { |
141 |
|
|
private Popup popup = null; |
142 |
|
|
private final JToolTip tip = new JToolTip(); |
143 |
|
|
private static NumberFormat numberFormat = NumberFormat.getInstance(); |
144 |
|
|
|
145 |
|
|
VolumeSlider() { |
146 |
|
|
super(0, 100, 100); |
147 |
|
|
numberFormat.setMaximumFractionDigits(1); |
148 |
iliev |
1567 |
// Setting the tooltip size (workaround for preserving that size) |
149 |
|
|
boolean b = CC.getViewConfig().isMeasurementUnitDecibel(); |
150 |
|
|
if(b) tip.setTipText(i18n.getLabel("StdUtils.volumeDecibels", "-30.0")); |
151 |
|
|
else tip.setTipText(i18n.getLabel("StdUtils.volume", "100")); |
152 |
|
|
tip.setPreferredSize(tip.getPreferredSize()); |
153 |
iliev |
1540 |
tip.setMinimumSize(tip.getPreferredSize()); |
154 |
iliev |
1567 |
/////// |
155 |
iliev |
1540 |
tip.setComponent(this); |
156 |
|
|
tip.setTipText(i18n.getLabel("StdUtils.volume", 0)); |
157 |
|
|
|
158 |
|
|
updateVolumeInfo(); |
159 |
|
|
|
160 |
|
|
addMouseListener(new MouseAdapter() { |
161 |
|
|
public void |
162 |
|
|
mousePressed(MouseEvent e) { |
163 |
|
|
if(popup != null) { |
164 |
|
|
popup.hide(); |
165 |
|
|
popup = null; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
if(!VolumeSlider.this.isEnabled()) return; |
169 |
|
|
|
170 |
|
|
java.awt.Point p = VolumeSlider.this.getLocationOnScreen(); |
171 |
|
|
PopupFactory pf = PopupFactory.getSharedInstance(); |
172 |
|
|
popup = pf.getPopup(VolumeSlider.this, tip, p.x, p.y - 22); |
173 |
|
|
popup.show(); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
public void |
177 |
|
|
mouseReleased(MouseEvent e) { |
178 |
|
|
if(popup != null) { |
179 |
|
|
popup.hide(); |
180 |
|
|
popup = null; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
}); |
184 |
|
|
|
185 |
|
|
addChangeListener(new ChangeListener() { |
186 |
|
|
public void |
187 |
|
|
stateChanged(ChangeEvent e) { updateVolumeInfo(); } |
188 |
|
|
}); |
189 |
|
|
|
190 |
|
|
String s = VOL_MEASUREMENT_UNIT_DECIBEL; |
191 |
|
|
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
192 |
|
|
public void |
193 |
|
|
propertyChange(PropertyChangeEvent e) { |
194 |
iliev |
1567 |
// We use this to set the size of the lVolume |
195 |
|
|
// to prevent the frequent resizing of lVolume component |
196 |
|
|
boolean b = CC.getViewConfig().isMeasurementUnitDecibel(); |
197 |
|
|
tip.setPreferredSize(null); |
198 |
|
|
String s; |
199 |
|
|
if(b) s = i18n.getLabel("StdUtils.volumeDecibels", "-30.0"); |
200 |
|
|
else s = i18n.getLabel("StdUtils.volume", "100"); |
201 |
|
|
tip.setTipText(s); |
202 |
|
|
tip.setPreferredSize(tip.getPreferredSize()); |
203 |
|
|
tip.setMinimumSize(tip.getPreferredSize()); |
204 |
|
|
/////// |
205 |
iliev |
1540 |
updateVolumeInfo(); |
206 |
|
|
} |
207 |
|
|
}); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
private void |
211 |
|
|
updateVolumeInfo() { |
212 |
|
|
String s; |
213 |
|
|
if(CC.getViewConfig().isMeasurementUnitDecibel()) { |
214 |
|
|
double d = HF.percentsToDecibels(getValue()); |
215 |
|
|
s = i18n.getLabel("StdUtils.volumeDecibels", numberFormat.format(d)); |
216 |
|
|
} else { |
217 |
|
|
s = i18n.getLabel("StdUtils.volume", getValue()); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
setToolTipText(s); |
221 |
|
|
tip.setTipText(s); |
222 |
|
|
tip.repaint(); |
223 |
|
|
} |
224 |
|
|
} |
225 |
iliev |
1308 |
} |