4
* Copyright (c) 2009 JAM Development Team
6
* This package is distributed under the Lesser Gnu Public Licence (LGPL)
13
* (c) 2002-2005 BEAST Development Core Team
15
* This package may be distributed under the
16
* Lesser Gnu Public Licence (LGPL)
19
package figtree.application;
23
import java.io.PrintWriter;
24
import java.io.StringWriter;
28
* @author Alexei Drummond
29
* @author Andrew Rambaut
31
* @version $Id: Utils.java 965 2009-01-03 00:39:29Z rambaut $
36
private static javax.swing.JFileChooser SAVE_FILE_CHOOSER = null;
38
public static String getLoadFileName(String message) {
39
java.io.File file = getLoadFile(message);
40
if (file == null) return null;
41
return file.getAbsolutePath();
44
public static String getSaveFileName(String message) {
45
java.io.File file = getSaveFile(message);
46
if (file == null) return null;
47
return file.getAbsolutePath();
50
@SuppressWarnings({"deprecation"})
51
public static File getLoadFile(String message) {
52
// No file name in the arguments so throw up a dialog box...
53
java.awt.Frame frame = new java.awt.Frame();
54
java.awt.FileDialog chooser = new java.awt.FileDialog(frame, message,
55
java.awt.FileDialog.LOAD);
57
if (chooser.getFile() == null) return null;
58
java.io.File file = new java.io.File(chooser.getDirectory(), chooser.getFile());
65
public static File getSaveFile(String message) {
66
// No file name in the arguments so throw up a dialog box...
67
java.awt.Frame frame = new java.awt.Frame();
68
java.awt.FileDialog chooser = new java.awt.FileDialog(frame, message,
69
java.awt.FileDialog.SAVE);
70
chooser.setVisible(true);
71
java.io.File file = null;
72
if(chooser.getDirectory() != null && chooser.getFile() != null)
73
file = new java.io.File(chooser.getDirectory(), chooser.getFile());
82
* This function takes a file name and an array of extensions (specified
83
* without the leading '.'). If the file name ends with one of the extensions
84
* then it is returned with this trimmed off. Otherwise the file name is
86
* @return the trimmed filename
88
public static String trimExtensions(String fileName, String[] extensions) {
90
String newName = null;
92
for (int i = 0; i < extensions.length; i++) {
93
String ext = "." + extensions[i];
94
if (fileName.endsWith(ext)) {
95
newName = fileName.substring(0, fileName.length() - ext.length());
99
if (newName == null) newName = fileName;
105
* @return a named image from file or resource bundle.
107
public static Image getImage(Object caller, String name) {
109
java.net.URL url = caller.getClass().getResource(name);
111
return Toolkit.getDefaultToolkit().createImage(url);
113
if (caller instanceof Component) {
114
Component c = (Component)caller;
115
Image i = c.createImage(100,20);
116
Graphics g = c.getGraphics();
117
g.drawString("Not found!", 1, 15);
124
/*public static void showExceptionDialog(JFrame parent, Throwable e, String title) {
126
JOptionPane pane = new JOptionPane(e.getMessage(), JOptionPane.ERROR_MESSAGE);
127
JDialog dialog = pane.createDialog(parent, title);
131
public static void showExceptionDialog(JFrame parent, Throwable e) {
133
JExceptionDialog d = new JExceptionDialog(parent, true, "Exception", getStackTrace(e));
137
* Return the stack trace of an exception as a string.
139
private static String getStackTrace(Throwable e) {
140
StringWriter sw = new StringWriter();
141
PrintWriter pw = new PrintWriter(sw);
142
e.printStackTrace(pw);
143
return sw.toString();