~ubuntu-branches/ubuntu/utopic/figtree/utopic

« back to all changes in this revision

Viewing changes to src/figtree/application/Utils.java

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-02-21 08:17:38 UTC
  • Revision ID: james.westby@ubuntu.com-20110221081738-80pe2ulo8rg7up10
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Utils.java
 
3
 *
 
4
 * Copyright (c) 2009 JAM Development Team
 
5
 *
 
6
 * This package is distributed under the Lesser Gnu Public Licence (LGPL)
 
7
 *
 
8
 */
 
9
 
 
10
/*
 
11
 * Utils.java
 
12
 *
 
13
 * (c) 2002-2005 BEAST Development Core Team
 
14
 *
 
15
 * This package may be distributed under the
 
16
 * Lesser Gnu Public Licence (LGPL)
 
17
 */
 
18
 
 
19
package figtree.application;
 
20
 
 
21
import java.awt.*;
 
22
import java.io.File;
 
23
import java.io.PrintWriter;
 
24
import java.io.StringWriter;
 
25
 
 
26
/**
 
27
 *
 
28
 * @author Alexei Drummond
 
29
 * @author Andrew Rambaut
 
30
 *
 
31
 * @version $Id: Utils.java 965 2009-01-03 00:39:29Z rambaut $
 
32
 */
 
33
public class Utils {
 
34
 
 
35
 
 
36
        private static javax.swing.JFileChooser SAVE_FILE_CHOOSER = null;
 
37
 
 
38
        public static String getLoadFileName(String message) {
 
39
                java.io.File file = getLoadFile(message);
 
40
                if (file == null) return null;
 
41
                return file.getAbsolutePath();
 
42
        }
 
43
 
 
44
        public static String getSaveFileName(String message) {
 
45
                java.io.File file = getSaveFile(message);
 
46
                if (file == null) return null;
 
47
                return file.getAbsolutePath();
 
48
        }
 
49
 
 
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);
 
56
        chooser.show();
 
57
                if (chooser.getFile() == null) return null;
 
58
                java.io.File file = new java.io.File(chooser.getDirectory(), chooser.getFile());
 
59
                chooser.dispose();
 
60
                frame.dispose();
 
61
 
 
62
                return file;
 
63
        }
 
64
 
 
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());
 
74
 
 
75
        chooser.dispose();
 
76
                frame.dispose();
 
77
 
 
78
                return file;
 
79
        }
 
80
 
 
81
        /**
 
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
 
85
         * return as it is.
 
86
         * @return the trimmed filename
 
87
         */
 
88
        public static String trimExtensions(String fileName, String[] extensions) {
 
89
 
 
90
                String newName = null;
 
91
 
 
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());
 
96
                        }
 
97
                }
 
98
 
 
99
                if (newName == null) newName = fileName;
 
100
 
 
101
                return newName;
 
102
        }
 
103
 
 
104
        /**
 
105
         * @return a named image from file or resource bundle.
 
106
         */
 
107
        public static Image getImage(Object caller, String name) {
 
108
 
 
109
                java.net.URL url = caller.getClass().getResource(name);
 
110
                if (url != null) {
 
111
                        return Toolkit.getDefaultToolkit().createImage(url);
 
112
                } else {
 
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);
 
118
                                return i;
 
119
                        } else return null;
 
120
                }
 
121
        }
 
122
 
 
123
 
 
124
        /*public static void showExceptionDialog(JFrame parent, Throwable e, String title) {
 
125
 
 
126
                JOptionPane pane = new JOptionPane(e.getMessage(), JOptionPane.ERROR_MESSAGE);
 
127
                JDialog dialog = pane.createDialog(parent, title);
 
128
                dialog.show();
 
129
        }
 
130
 
 
131
        public static void showExceptionDialog(JFrame parent, Throwable e) {
 
132
 
 
133
                JExceptionDialog d = new JExceptionDialog(parent, true, "Exception", getStackTrace(e));
 
134
        }*/
 
135
 
 
136
        /**
 
137
         * Return the stack trace of an exception as a string.
 
138
         */
 
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();
 
144
        }
 
145
 
 
146
}