/[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 1752 - (show annotations) (download)
Mon Aug 11 22:51:24 2008 UTC (15 years, 8 months ago) by iliev
File size: 8604 byte(s)
* Added toolbar to the Database Instrument Chooser dialog
* Instrument Chooser and Database Instrument Chooser dialogs
  are now resizable
* Fantasia: Added toolbar to the Right-Side Pane's Instruments Database

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2008 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 parentComponent determines the Frame in which the dialog is displayed
80 * @param msg The error message to be shown.
81 */
82 public static void
83 showErrorMessage(String msg, Component parentComponent) {
84 JOptionPane.showMessageDialog (
85 parentComponent, msg,
86 i18n.getError("error"),
87 JOptionPane.ERROR_MESSAGE
88 );
89 }
90
91 /**
92 * Shows a dialog with error message obtained by {@link #getErrorMessage} method.
93 * @param e The <code>Exception</code> from which the error message is obtained.
94 */
95 public static void
96 showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); }
97
98 /**
99 * Shows a dialog with error message obtained by {@link #getErrorMessage} method.
100 * @param e The <code>Exception</code> from which the error message is obtained.
101 * @param prefix The prefix to be added to the error message.
102 */
103 public static void
104 showErrorMessage(Exception e, String prefix) {
105 showErrorMessage(e, CC.getMainFrame(), prefix);
106 }
107
108 /**
109 * Shows a dialog with error message obtained by {@link #getErrorMessage} method.
110 * @param e The <code>Exception</code> from which the error message is obtained.
111 * @param frame The parent <code>Frame</code> for the dialog.
112 */
113 public static void
114 showErrorMessage(Exception e, Frame frame) {
115 showErrorMessage(e, frame, "");
116 }
117
118 /**
119 * Shows a dialog with error message obtained by {@link #getErrorMessage} method.
120 * @param e The <code>Exception</code> from which the error message is obtained.
121 * @param frame The parent <code>Frame</code> for the dialog.
122 * @param prefix The prefix to be added to the error message.
123 */
124 public static void
125 showErrorMessage(Exception e, Frame frame, String prefix) {
126 String msg = prefix + getErrorMessage(e);
127
128 CC.getLogger().log(Level.INFO, msg, e);
129
130 JOptionPane.showMessageDialog (
131 frame, msg,
132 i18n.getError("error"),
133 JOptionPane.ERROR_MESSAGE
134 );
135 }
136
137 /**
138 * Shows a dialog with error message obtained by {@link #getErrorMessage} method.
139 * @param e The <code>Exception</code> from which the error message is obtained.
140 * @param dlg The parent <code>Dialog</code> from which the dialog is displayed.
141 */
142 public static void
143 showErrorMessage(Exception e, Dialog dlg) {
144 String msg = getErrorMessage(e);
145
146 CC.getLogger().log(Level.INFO, msg, e);
147
148 JOptionPane.showMessageDialog (
149 dlg, msg,
150 i18n.getError("error"),
151 JOptionPane.ERROR_MESSAGE
152 );
153 }
154
155 /**
156 * Brings up a question dialog with Yes, No options, empty title and the specified message.
157 * @param parent The parent <code>Component</code> for the dialog.
158 * @param message The message to display.
159 * @return <code>true</code> if the user chooses "yes", <code>false</code> otherwise.
160 */
161 public static boolean
162 showYesNoDialog(Component parent, String message) {
163 return showYesNoDialog(parent, message, "");
164 }
165
166 /**
167 * Brings up a question dialog with Yes, No options, empty title and the specified message.
168 * @param parent The parent <code>Component</code> for the dialog.
169 * @param message The message to display.
170 * @param title The dialog's title.
171 * @return <code>true</code> if the user chooses "yes", <code>false</code> otherwise.
172 */
173 public static boolean
174 showYesNoDialog(Component parent, String message, String title) {
175 Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") };
176 int n = JOptionPane.showOptionDialog (
177 parent,
178 message, title,
179 JOptionPane.YES_NO_OPTION,
180 JOptionPane.QUESTION_MESSAGE,
181 null,
182 options, options[0]
183 );
184
185 return n == 0;
186 }
187
188 /**
189 * Sets the default font to be used in the GUI.
190 * @param fontName The name of the font to be used as default.
191 */
192 public static void
193 setUIDefaultFont(String fontName) {
194 if(fontName == null) return;
195
196 java.util.Enumeration keys = UIManager.getDefaults().keys();
197 while(keys.hasMoreElements()) {
198 Object key = keys.nextElement();
199 Object value = UIManager.get(key);
200 if(value instanceof FontUIResource) {
201 Font f = (FontUIResource)value;
202 FontUIResource fr =
203 new FontUIResource(fontName, f.getStyle(), f.getSize());
204 UIManager.put(key, fr);
205 }
206 }
207 }
208 ///////
209
210 /**
211 * Deletes the specified file, if exists and
212 * is located in the JSampler's home directory.
213 * @param file The file to delete.
214 */
215 public static void
216 deleteFile(String file) {
217 String s = CC.getJSamplerHome();
218 if(s == null) return;
219
220 try {
221 File f = new File(s + File.separator + file);
222 if(f.isFile()) f.delete();
223 } catch(Exception x) {
224 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
225 }
226 }
227
228 /**
229 * Create a backup copy of the specified file, located in the JSampler's home directory.
230 * @param file The name of the file to backup.
231 * @param bkpFile The backup name of the file.
232 * @return <code>true</code> if the file is backuped successfully.
233 */
234 public static boolean
235 createBackup(String file, String bkpFile) {
236 if(file == null || bkpFile == null) return false;
237 if(file.length() == 0 || bkpFile.length() == 0) return false;
238
239 String s = CC.getJSamplerHome();
240 if(s == null) return false;
241
242 File f = new File(s + File.separator + file);
243 if(!f.isFile()) return false;
244
245 try {
246 FileInputStream fis = new FileInputStream(s + File.separator + file);
247
248 FileOutputStream fos;
249 fos = new FileOutputStream(s + File.separator + bkpFile, false);
250
251 int i = fis.read();
252 while(i != -1) {
253 fos.write(i);
254 i = fis.read();
255 }
256 } catch(Exception x) {
257 CC.getLogger().log(Level.INFO, getErrorMessage(x), x);
258 return false;
259 }
260
261 return true;
262 }
263
264 /**
265 * Converts the volume value specified in percents to decibels.
266 */
267 public static double
268 percentsToDecibels(int vol) {
269 if(vol == 0) return Double.NEGATIVE_INFINITY;
270 double i = vol;
271 i /= 100;
272 i = 20 * Math.log10(i);
273 return i;
274 }
275
276
277
278 /**
279 * Converts the volume value specified in decibels to percents.
280 */
281 public static int
282 decibelsToPercents(double vol) {
283 if(vol == Double.NEGATIVE_INFINITY) return 0;
284 double i = Math.pow(10, vol/20);
285 i *= 100;
286 return (int)i;
287 }
288
289 /**
290 * Converts the volume value specified in decibels to volume factor.
291 */
292 public static float
293 decibelsToFactor(double vol) {
294 if(vol == Double.NEGATIVE_INFINITY) return 0;
295 double i = Math.pow(10, vol/20);
296 return (float)i;
297 }
298
299 /**
300 * Converts the volume value specified in percents to volume factor.
301 */
302 public static float
303 percentsToFactor(int vol) {
304 float f = vol;
305 f /= 100;
306 return f;
307 }
308 }

  ViewVC Help
Powered by ViewVC