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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 787 by iliev, Mon Oct 10 16:03:12 2005 UTC revision 1752 by iliev, Mon Aug 11 22:51:24 2008 UTC
# Line 1  Line 1 
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-2008 Grigor Iliev <grigor@grigoriliev.com>
5   *   *
6   *   This file is part of JSampler.   *   This file is part of JSampler.
7   *   *
# Line 27  import java.awt.Dialog; Line 27  import java.awt.Dialog;
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;
# Line 40  import static org.jsampler.JSI18n.i18n; Line 44  import static org.jsampler.JSI18n.i18n;
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();
# Line 58  public class HF { Line 67  public class HF {
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 parentComponent determines the Frame in which the dialog is displayed
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, Component parentComponent) {
84                  JOptionPane.showMessageDialog (                  JOptionPane.showMessageDialog (
85                          frame, msg,                          parentComponent, msg,
86                          i18n.getError("error"),                          i18n.getError("error"),
87                          JOptionPane.ERROR_MESSAGE                          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          public static void
96          showErrorMessage(String msg, Dialog dlg) {          showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); }
                 JOptionPane.showMessageDialog (  
                         dlg, msg,  
                         i18n.getError("error"),  
                         JOptionPane.ERROR_MESSAGE  
                 );  
         }  
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          public static void
104          showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); }          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          public static void
114          showErrorMessage(Exception e, Frame frame) {          showErrorMessage(Exception e, Frame frame) {
115                  String msg = getErrorMessage(e);                  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);                  CC.getLogger().log(Level.INFO, msg, e);
129                                                    
# Line 95  public class HF { Line 134  public class HF {
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          public static void
143          showErrorMessage(Exception e, Dialog dlg) {          showErrorMessage(Exception e, Dialog dlg) {
144                  String msg = getErrorMessage(e);                  String msg = getErrorMessage(e);
145                                    
146                  CC.getLogger().log(Level.INFO, msg, e);                  CC.getLogger().log(Level.INFO, msg, e);
147                                            
148                  JOptionPane.showMessageDialog (                  JOptionPane.showMessageDialog (
149                          dlg, msg,                          dlg, msg,
150                          i18n.getError("error"),                          i18n.getError("error"),
# Line 110  public class HF { Line 154  public class HF {
154                    
155          /**          /**
156           * 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.
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          public static boolean
162          showYesNoDialog(Component parent, String message) {          showYesNoDialog(Component parent, String message) {
163                  return showYesNoDialog(parent, message, "");                  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          public static boolean
174          showYesNoDialog(Component parent, String message, String title) {          showYesNoDialog(Component parent, String message, String title) {
175                  Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") };                  Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") };
# Line 132  public class HF { Line 185  public class HF {
185                  return n == 0;                  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          public static void
193          setUIDefaultFont(String fontName) {          setUIDefaultFont(String fontName) {
194                  if(fontName == null) return;                  if(fontName == null) return;
# Line 149  public class HF { Line 206  public class HF {
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  }  }

Legend:
Removed from v.787  
changed lines
  Added in v.1752

  ViewVC Help
Powered by ViewVC