1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
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.util.logging.Level; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.UIManager; |
34 |
import javax.swing.plaf.FontUIResource; |
35 |
|
36 |
import org.linuxsampler.lscp.LSException; |
37 |
import org.linuxsampler.lscp.LscpException; |
38 |
|
39 |
import static org.jsampler.JSI18n.i18n; |
40 |
|
41 |
|
42 |
/** |
43 |
* This class contains some helper function. |
44 |
* @author Grigor Iliev |
45 |
*/ |
46 |
public class HF { |
47 |
// GUI HELPER FUNCIONS |
48 |
|
49 |
/** |
50 |
* Returns more meaningful, non-<code>null</code> message. |
51 |
* @return More meaningful, non-<code>null</code> message. |
52 |
*/ |
53 |
public static String |
54 |
getErrorMessage(Exception e) { |
55 |
String msg = e.getMessage(); |
56 |
|
57 |
if(e instanceof LSException) { |
58 |
LSException x = (LSException)e; |
59 |
} else if(e instanceof LscpException) { |
60 |
|
61 |
} else { msg = (msg != null ? msg : i18n.getError("unknownError")); } |
62 |
|
63 |
return msg; |
64 |
} |
65 |
|
66 |
/** |
67 |
* Shows a dialog with the specified error message. |
68 |
* @param msg The error message to be shown. |
69 |
*/ |
70 |
public static void |
71 |
showErrorMessage(String msg) { showErrorMessage(msg, CC.getMainFrame()); } |
72 |
|
73 |
/** |
74 |
* Shows a dialog with the specified error message. |
75 |
* @param frame The parent <code>Frame</code> for the dialog. |
76 |
* @param msg The error message to be shown. |
77 |
*/ |
78 |
public static void |
79 |
showErrorMessage(String msg, Frame frame) { |
80 |
JOptionPane.showMessageDialog ( |
81 |
frame, msg, |
82 |
i18n.getError("error"), |
83 |
JOptionPane.ERROR_MESSAGE |
84 |
); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Shows a dialog with the specified error message. |
89 |
* @param dlg The parent <code>Dialog</code> from which the dialog is displayed. |
90 |
* @param msg The error message to be shown. |
91 |
*/ |
92 |
public static void |
93 |
showErrorMessage(String msg, Dialog dlg) { |
94 |
JOptionPane.showMessageDialog ( |
95 |
dlg, msg, |
96 |
i18n.getError("error"), |
97 |
JOptionPane.ERROR_MESSAGE |
98 |
); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
103 |
* @param e The <code>Exception</code> from which the error message is obtained. |
104 |
*/ |
105 |
public static void |
106 |
showErrorMessage(Exception e) { showErrorMessage(e, CC.getMainFrame()); } |
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 prefix The prefix to be added to the error message. |
112 |
*/ |
113 |
public static void |
114 |
showErrorMessage(Exception e, String prefix) { |
115 |
showErrorMessage(e, CC.getMainFrame(), prefix); |
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 |
*/ |
123 |
public static void |
124 |
showErrorMessage(Exception e, Frame frame) { |
125 |
showErrorMessage(e, frame, ""); |
126 |
} |
127 |
|
128 |
/** |
129 |
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
130 |
* @param e The <code>Exception</code> from which the error message is obtained. |
131 |
* @param frame The parent <code>Frame</code> for the dialog. |
132 |
* @param prefix The prefix to be added to the error message. |
133 |
*/ |
134 |
public static void |
135 |
showErrorMessage(Exception e, Frame frame, String prefix) { |
136 |
String msg = prefix + getErrorMessage(e); |
137 |
|
138 |
CC.getLogger().log(Level.INFO, msg, e); |
139 |
|
140 |
JOptionPane.showMessageDialog ( |
141 |
frame, msg, |
142 |
i18n.getError("error"), |
143 |
JOptionPane.ERROR_MESSAGE |
144 |
); |
145 |
} |
146 |
|
147 |
/** |
148 |
* Shows a dialog with error message obtained by {@link #getErrorMessage} method. |
149 |
* @param e The <code>Exception</code> from which the error message is obtained. |
150 |
* @param dlg The parent <code>Dialog</code> from which the dialog is displayed. |
151 |
*/ |
152 |
public static void |
153 |
showErrorMessage(Exception e, Dialog dlg) { |
154 |
String msg = getErrorMessage(e); |
155 |
|
156 |
CC.getLogger().log(Level.INFO, msg, e); |
157 |
|
158 |
JOptionPane.showMessageDialog ( |
159 |
dlg, msg, |
160 |
i18n.getError("error"), |
161 |
JOptionPane.ERROR_MESSAGE |
162 |
); |
163 |
} |
164 |
|
165 |
/** |
166 |
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
167 |
* @param parent The parent <code>Component</code> for the dialog. |
168 |
* @param message The message to display. |
169 |
*/ |
170 |
public static boolean |
171 |
showYesNoDialog(Component parent, String message) { |
172 |
return showYesNoDialog(parent, message, ""); |
173 |
} |
174 |
|
175 |
/** |
176 |
* Brings up a question dialog with Yes, No options, empty title and the specified message. |
177 |
* @param parent The parent <code>Component</code> for the dialog. |
178 |
* @param message The message to display. |
179 |
* @param title The dialog's title. |
180 |
*/ |
181 |
public static boolean |
182 |
showYesNoDialog(Component parent, String message, String title) { |
183 |
Object[] options = { i18n.getButtonLabel("yes"), i18n.getButtonLabel("no") }; |
184 |
int n = JOptionPane.showOptionDialog ( |
185 |
parent, |
186 |
message, title, |
187 |
JOptionPane.YES_NO_OPTION, |
188 |
JOptionPane.QUESTION_MESSAGE, |
189 |
null, |
190 |
options, options[0] |
191 |
); |
192 |
|
193 |
return n == 0; |
194 |
} |
195 |
|
196 |
/** |
197 |
* Sets the default font to be used in the GUI. |
198 |
* @param fontName The name of the font to be used as default. |
199 |
*/ |
200 |
public static void |
201 |
setUIDefaultFont(String fontName) { |
202 |
if(fontName == null) return; |
203 |
|
204 |
java.util.Enumeration keys = UIManager.getDefaults().keys(); |
205 |
while(keys.hasMoreElements()) { |
206 |
Object key = keys.nextElement(); |
207 |
Object value = UIManager.get(key); |
208 |
if(value instanceof FontUIResource) { |
209 |
Font f = (FontUIResource)value; |
210 |
FontUIResource fr = |
211 |
new FontUIResource(fontName, f.getStyle(), f.getSize()); |
212 |
UIManager.put(key, fr); |
213 |
} |
214 |
} |
215 |
} |
216 |
/////// |
217 |
} |