/[svn]/jsampler/trunk/src/org/jsampler/HF.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/HF.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (show annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 4 months ago) by iliev
File size: 8930 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2006 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;
24
25 import java.awt.Component;
26 import java.awt.Dialog;
27 import java.awt.Font;
28 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;
35
36 import javax.swing.JOptionPane;
37 import javax.swing.UIManager;
38 import javax.swing.plaf.FontUIResource;
39
40 import org.linuxsampler.lscp.LSException;
41 import org.linuxsampler.lscp.LscpException;
42
43 import static org.jsampler.JSI18n.i18n;
44
45
46 /**
47 * This class contains some helper function.
48 * @author Grigor Iliev
49 */
50 public class HF {
51 // 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
58 getErrorMessage(Exception e) {
59 String msg = e.getMessage();
60
61 if(e instanceof LSException) {
62 LSException x = (LSException)e;
63 } else if(e instanceof LscpException) {
64
65 } else { msg = (msg != null ? msg : i18n.getError("unknownError")); }
66
67 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
75 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
83 showErrorMessage(String msg, Frame frame) {
84 JOptionPane.showMessageDialog (
85 frame, msg,
86 i18n.getError("error"),
87 JOptionPane.ERROR_MESSAGE
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
97 showErrorMessage(String msg, Dialog dlg) {
98 JOptionPane.showMessageDialog (
99 dlg, msg,
100 i18n.getError("error"),
101 JOptionPane.ERROR_MESSAGE
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
110 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
128 showErrorMessage(Exception e, Frame frame) {
129 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);
143
144 JOptionPane.showMessageDialog (
145 frame, msg,
146 i18n.getError("error"),
147 JOptionPane.ERROR_MESSAGE
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
157 showErrorMessage(Exception e, Dialog dlg) {
158 String msg = getErrorMessage(e);
159
160 CC.getLogger().log(Level.INFO, msg, e);
161
162 JOptionPane.showMessageDialog (
163 dlg, msg,
164 i18n.getError("error"),
165 JOptionPane.ERROR_MESSAGE
166 );
167 }
168
169 /**
170 * 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
176 showYesNoDialog(Component parent, String message) {
177 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
188 showYesNoDialog(Component parent, String message, String title) {
189 Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") };
190 int n = JOptionPane.showOptionDialog (
191 parent,
192 message, title,
193 JOptionPane.YES_NO_OPTION,
194 JOptionPane.QUESTION_MESSAGE,
195 null,
196 options, options[0]
197 );
198
199 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
207 setUIDefaultFont(String fontName) {
208 if(fontName == null) return;
209
210 java.util.Enumeration keys = UIManager.getDefaults().keys();
211 while(keys.hasMoreElements()) {
212 Object key = keys.nextElement();
213 Object value = UIManager.get(key);
214 if(value instanceof FontUIResource) {
215 Font f = (FontUIResource)value;
216 FontUIResource fr =
217 new FontUIResource(fontName, f.getStyle(), f.getSize());
218 UIManager.put(key, fr);
219 }
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 }

  ViewVC Help
Powered by ViewVC