1 |
/* |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
* Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
* |
6 |
* This file is part of JSampler. |
* This file is part of JSampler. |
7 |
* |
* |
27 |
import java.awt.Font; |
import java.awt.Font; |
28 |
import java.awt.Frame; |
import java.awt.Frame; |
29 |
|
|
30 |
|
import java.io.File; |
31 |
|
import java.io.FileInputStream; |
32 |
|
import java.io.FileOutputStream; |
33 |
|
|
34 |
import java.util.logging.Level; |
import java.util.logging.Level; |
35 |
|
|
36 |
import javax.swing.JOptionPane; |
import javax.swing.JOptionPane; |
44 |
|
|
45 |
|
|
46 |
/** |
/** |
47 |
* |
* This class contains some helper function. |
48 |
* @author Grigor Iliev |
* @author Grigor Iliev |
49 |
*/ |
*/ |
50 |
public class HF { |
public class HF { |
51 |
// GUI HELPER FUNCIONS |
// GUI HELPER FUNCIONS |
52 |
|
|
53 |
|
/** |
54 |
|
* Returns more meaningful, non-<code>null</code> message. |
55 |
|
* @return More meaningful, non-<code>null</code> message. |
56 |
|
*/ |
57 |
public static String |
public static String |
58 |
getErrorMessage(Exception e) { |
getErrorMessage(Exception e) { |
59 |
String msg = e.getMessage(); |
String msg = e.getMessage(); |
67 |
return msg; |
return msg; |
68 |
} |
} |
69 |
|
|
70 |
|
/** |
71 |
|
* Shows a dialog with the specified error message. |
72 |
|
* @param msg The error message to be shown. |
73 |
|
*/ |
74 |
public static void |
public static void |
75 |
showErrorMessage(String msg) { showErrorMessage(msg, CC.getMainFrame()); } |
showErrorMessage(String msg) { showErrorMessage(msg, CC.getMainFrame()); } |
76 |
|
|
77 |
|
/** |
78 |
|
* Shows a dialog with the specified error message. |
79 |
|
* @param frame The parent <code>Frame</code> for the dialog. |
80 |
|
* @param msg The error message to be shown. |
81 |
|
*/ |
82 |
public static void |
public static void |
83 |
showErrorMessage(String msg, Frame frame) { |
showErrorMessage(String msg, Frame frame) { |
84 |
JOptionPane.showMessageDialog ( |
JOptionPane.showMessageDialog ( |
88 |
); |
); |
89 |
} |
} |
90 |
|
|
91 |
|
/** |
92 |
|
* Shows a dialog with the specified error message. |
93 |
|
* @param dlg The parent <code>Dialog</code> from which the dialog is displayed. |
94 |
|
* @param msg The error message to be shown. |
95 |
|
*/ |
96 |
public static void |
public static void |
97 |
showErrorMessage(String msg, Dialog dlg) { |
showErrorMessage(String msg, Dialog dlg) { |
98 |
JOptionPane.showMessageDialog ( |
JOptionPane.showMessageDialog ( |
102 |
); |
); |
103 |
} |
} |
104 |
|
|
105 |
|
/** |
106 |
|
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
107 |
|
* @param e The <code>Exception</code> from which the error message is obtained. |
108 |
|
*/ |
109 |
public static void |
public static void |
110 |
showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); } |
showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); } |
111 |
|
|
112 |
|
/** |
113 |
|
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
114 |
|
* @param e The <code>Exception</code> from which the error message is obtained. |
115 |
|
* @param prefix The prefix to be added to the error message. |
116 |
|
*/ |
117 |
|
public static void |
118 |
|
showErrorMessage(Exception e, String prefix) { |
119 |
|
showErrorMessage(e, CC.getMainFrame(), prefix); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
|
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
124 |
|
* @param e The <code>Exception</code> from which the error message is obtained. |
125 |
|
* @param frame The parent <code>Frame</code> for the dialog. |
126 |
|
*/ |
127 |
public static void |
public static void |
128 |
showErrorMessage(Exception e, Frame frame) { |
showErrorMessage(Exception e, Frame frame) { |
129 |
String msg = getErrorMessage(e); |
showErrorMessage(e, frame, ""); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
|
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
134 |
|
* @param e The <code>Exception</code> from which the error message is obtained. |
135 |
|
* @param frame The parent <code>Frame</code> for the dialog. |
136 |
|
* @param prefix The prefix to be added to the error message. |
137 |
|
*/ |
138 |
|
public static void |
139 |
|
showErrorMessage(Exception e, Frame frame, String prefix) { |
140 |
|
String msg = prefix + getErrorMessage(e); |
141 |
|
|
142 |
CC.getLogger().log(Level.INFO, msg, e); |
CC.getLogger().log(Level.INFO, msg, e); |
143 |
|
|
148 |
); |
); |
149 |
} |
} |
150 |
|
|
151 |
|
/** |
152 |
|
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
153 |
|
* @param e The <code>Exception</code> from which the error message is obtained. |
154 |
|
* @param dlg The parent <code>Dialog</code> from which the dialog is displayed. |
155 |
|
*/ |
156 |
public static void |
public static void |
157 |
showErrorMessage(Exception e, Dialog dlg) { |
showErrorMessage(Exception e, Dialog dlg) { |
158 |
String msg = getErrorMessage(e); |
String msg = getErrorMessage(e); |
159 |
|
|
160 |
CC.getLogger().log(Level.INFO, msg, e); |
CC.getLogger().log(Level.INFO, msg, e); |
161 |
|
|
162 |
JOptionPane.showMessageDialog ( |
JOptionPane.showMessageDialog ( |
163 |
dlg, msg, |
dlg, msg, |
164 |
i18n.getError("error"), |
i18n.getError("error"), |
168 |
|
|
169 |
/** |
/** |
170 |
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
171 |
* |
* @param parent The parent <code>Component</code> for the dialog. |
172 |
|
* @param message The message to display. |
173 |
|
* @return <code>true</code> if the user chooses "yes", <code>false</code> otherwise. |
174 |
*/ |
*/ |
175 |
public static boolean |
public static boolean |
176 |
showYesNoDialog(Component parent, String message) { |
showYesNoDialog(Component parent, String message) { |
177 |
return showYesNoDialog(parent, message, ""); |
return showYesNoDialog(parent, message, ""); |
178 |
} |
} |
179 |
|
|
180 |
|
/** |
181 |
|
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
182 |
|
* @param parent The parent <code>Component</code> for the dialog. |
183 |
|
* @param message The message to display. |
184 |
|
* @param title The dialog's title. |
185 |
|
* @return <code>true</code> if the user chooses "yes", <code>false</code> otherwise. |
186 |
|
*/ |
187 |
public static boolean |
public static boolean |
188 |
showYesNoDialog(Component parent, String message, String title) { |
showYesNoDialog(Component parent, String message, String title) { |
189 |
Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") }; |
Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") }; |
199 |
return n == 0; |
return n == 0; |
200 |
} |
} |
201 |
|
|
202 |
|
/** |
203 |
|
* Sets the default font to be used in the GUI. |
204 |
|
* @param fontName The name of the font to be used as default. |
205 |
|
*/ |
206 |
public static void |
public static void |
207 |
setUIDefaultFont(String fontName) { |
setUIDefaultFont(String fontName) { |
208 |
if(fontName == null) return; |
if(fontName == null) return; |
220 |
} |
} |
221 |
} |
} |
222 |
/////// |
/////// |
223 |
|
|
224 |
|
/** |
225 |
|
* Deletes the specified file, if exists and |
226 |
|
* is located in the JSampler's home directory. |
227 |
|
* @param file The file to delete. |
228 |
|
*/ |
229 |
|
public static void |
230 |
|
deleteFile(String file) { |
231 |
|
String s = CC.getJSamplerHome(); |
232 |
|
if(s == null) return; |
233 |
|
|
234 |
|
try { |
235 |
|
File f = new File(s + File.separator + file); |
236 |
|
if(f.isFile()) f.delete(); |
237 |
|
} catch(Exception x) { |
238 |
|
CC.getLogger().log(Level.INFO, getErrorMessage(x), x); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
|
* Create a backup copy of the specified file, located in the JSampler's home directory. |
244 |
|
* @param file The name of the file to backup. |
245 |
|
* @param bkpFile The backup name of the file. |
246 |
|
* @return <code>true</code> if the file is backuped successfully. |
247 |
|
*/ |
248 |
|
public static boolean |
249 |
|
createBackup(String file, String bkpFile) { |
250 |
|
if(file == null || bkpFile == null) return false; |
251 |
|
if(file.length() == 0 || bkpFile.length() == 0) return false; |
252 |
|
|
253 |
|
String s = CC.getJSamplerHome(); |
254 |
|
if(s == null) return false; |
255 |
|
|
256 |
|
File f = new File(s + File.separator + file); |
257 |
|
if(!f.isFile()) return false; |
258 |
|
|
259 |
|
try { |
260 |
|
FileInputStream fis = new FileInputStream(s + File.separator + file); |
261 |
|
|
262 |
|
FileOutputStream fos; |
263 |
|
fos = new FileOutputStream(s + File.separator + bkpFile, false); |
264 |
|
|
265 |
|
int i = fis.read(); |
266 |
|
while(i != -1) { |
267 |
|
fos.write(i); |
268 |
|
i = fis.read(); |
269 |
|
} |
270 |
|
} catch(Exception x) { |
271 |
|
CC.getLogger().log(Level.INFO, getErrorMessage(x), x); |
272 |
|
return false; |
273 |
|
} |
274 |
|
|
275 |
|
return true; |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
|
* Converts the volume value specified in percents to decibels. |
280 |
|
*/ |
281 |
|
public static double |
282 |
|
percentsToDecibels(int vol) { |
283 |
|
if(vol == 0) return Double.NEGATIVE_INFINITY; |
284 |
|
double i = vol; |
285 |
|
i /= 100; |
286 |
|
i = 20 * Math.log10(i); |
287 |
|
return i; |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
/** |
293 |
|
* Converts the volume value specified in decibels to percents. |
294 |
|
*/ |
295 |
|
public static int |
296 |
|
decibelsToPercents(double vol) { |
297 |
|
if(vol == Double.NEGATIVE_INFINITY) return 0; |
298 |
|
double i = Math.pow(10, vol/20); |
299 |
|
i *= 100; |
300 |
|
return (int)i; |
301 |
|
} |
302 |
|
|
303 |
|
/** |
304 |
|
* Converts the volume value specified in decibels to volume factor. |
305 |
|
*/ |
306 |
|
public static float |
307 |
|
decibelsToFactor(double vol) { |
308 |
|
if(vol == Double.NEGATIVE_INFINITY) return 0; |
309 |
|
double i = Math.pow(10, vol/20); |
310 |
|
return (float)i; |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Converts the volume value specified in percents to volume factor. |
315 |
|
*/ |
316 |
|
public static float |
317 |
|
percentsToFactor(int vol) { |
318 |
|
float f = vol; |
319 |
|
f /= 100; |
320 |
|
return f; |
321 |
|
} |
322 |
} |
} |