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 |
*/ |
174 |
public static boolean |
175 |
showYesNoDialog(Component parent, String message) { |
176 |
return showYesNoDialog(parent, message, ""); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
181 |
* @param parent The parent <code>Component</code> for the dialog. |
182 |
* @param message The message to display. |
183 |
* @param title The dialog's title. |
184 |
*/ |
185 |
public static boolean |
186 |
showYesNoDialog(Component parent, String message, String title) { |
187 |
Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") }; |
188 |
int n = JOptionPane.showOptionDialog ( |
189 |
parent, |
190 |
message, title, |
191 |
JOptionPane.YES_NO_OPTION, |
192 |
JOptionPane.QUESTION_MESSAGE, |
193 |
null, |
194 |
options, options[0] |
195 |
); |
196 |
|
197 |
return n == 0; |
198 |
} |
199 |
|
200 |
/** |
201 |
* Sets the default font to be used in the GUI. |
202 |
* @param fontName The name of the font to be used as default. |
203 |
*/ |
204 |
public static void |
205 |
setUIDefaultFont(String fontName) { |
206 |
if(fontName == null) return; |
207 |
|
208 |
java.util.Enumeration keys = UIManager.getDefaults().keys(); |
209 |
while(keys.hasMoreElements()) { |
210 |
Object key = keys.nextElement(); |
211 |
Object value = UIManager.get(key); |
212 |
if(value instanceof FontUIResource) { |
213 |
Font f = (FontUIResource)value; |
214 |
FontUIResource fr = |
215 |
new FontUIResource(fontName, f.getStyle(), f.getSize()); |
216 |
UIManager.put(key, fr); |
217 |
} |
218 |
} |
219 |
} |
220 |
/////// |
221 |
|
222 |
/** |
223 |
* Deletes the specified file, if exists and |
224 |
* is located in the JSampler's home directory. |
225 |
* @param file The file to delete. |
226 |
*/ |
227 |
public static void |
228 |
deleteFile(String file) { |
229 |
String s = CC.getJSamplerHome(); |
230 |
if(s == null) return; |
231 |
|
232 |
try { |
233 |
File f = new File(s + File.separator + file); |
234 |
if(f.isFile()) f.delete(); |
235 |
} catch(Exception x) { |
236 |
CC.getLogger().log(Level.INFO, getErrorMessage(x), x); |
237 |
} |
238 |
} |
239 |
|
240 |
/** |
241 |
* Create a backup copy of the specified file, located in the JSampler's home directory. |
242 |
* @param file The name of the file to backup. |
243 |
* @param bkpFile The backup name of the file. |
244 |
* @return <code>true</code> if the file is backuped successfully. |
245 |
*/ |
246 |
public static boolean |
247 |
createBackup(String file, String bkpFile) { |
248 |
if(file == null || bkpFile == null) return false; |
249 |
if(file.length() == 0 || bkpFile.length() == 0) return false; |
250 |
|
251 |
String s = CC.getJSamplerHome(); |
252 |
if(s == null) return false; |
253 |
|
254 |
File f = new File(s + File.separator + file); |
255 |
if(!f.isFile()) return false; |
256 |
|
257 |
try { |
258 |
FileInputStream fis = new FileInputStream(s + File.separator + file); |
259 |
|
260 |
FileOutputStream fos; |
261 |
fos = new FileOutputStream(s + File.separator + bkpFile, false); |
262 |
|
263 |
int i = fis.read(); |
264 |
while(i != -1) { |
265 |
fos.write(i); |
266 |
i = fis.read(); |
267 |
} |
268 |
} catch(Exception x) { |
269 |
CC.getLogger().log(Level.INFO, getErrorMessage(x), x); |
270 |
return false; |
271 |
} |
272 |
|
273 |
return true; |
274 |
} |
275 |
|
276 |
|
277 |
} |