~andre-dau/jhelioviewer/deadlock-fix

« back to all changes in this revision

Viewing changes to base/src/org/helioviewer/base/jhv/JHVGlobals.java

  • Committer: Andre Dau
  • Date: 2010-06-10 01:53:09 UTC
  • Revision ID: andre@sporran.nascom.nasa.gov-20100610015309-4rqe1s53op1uda7h
Put JHVDirectory and JHVGlobals back to jhv project and changed the way LogSettings is initialized. Fixed a bug where a NullPointerException would occur when closing JHV while playing a movie.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.helioviewer.base.jhv;
2
 
 
3
 
import java.awt.EventQueue;
4
 
import java.io.File;
5
 
import java.lang.reflect.Method;
6
 
import java.util.Arrays;
7
 
import java.util.Date;
8
 
 
9
 
import org.helioviewer.base.logging.Log;
10
 
import javax.swing.JOptionPane;
11
 
 
12
 
/**
13
 
 * Intended to be a class for static functions and fields relevant to the
14
 
 * application as a whole.
15
 
 * 
16
 
 * @author caplins
17
 
 */
18
 
public class JHVGlobals {
19
 
 
20
 
    public static final Date startTimestamp = new Date();
21
 
 
22
 
    /** The maximum amount of memory the JVM will use for the heap. */
23
 
    public static final long MAX_JVM_HEAP_SIZE = Runtime.getRuntime().maxMemory();
24
 
 
25
 
    /** The the maximum amount of memory the BufferManager object will use. */
26
 
    public static final long MAX_BUFFER_MANAGER_SIZE = (MAX_JVM_HEAP_SIZE * 8) / 10;
27
 
 
28
 
    private final static String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" };
29
 
 
30
 
    /** Constructor is private to prevent instantiation. */
31
 
    private JHVGlobals() {
32
 
    }
33
 
 
34
 
    /**
35
 
     * Attempts to create the necessary directories if they do not exist. It
36
 
     * gets its list of directories to create from the JHVDirectory class.
37
 
     * 
38
 
     * @throws SecurityException
39
 
     */
40
 
    public static void createDirs() throws SecurityException {
41
 
        JHVDirectory[] dirs = JHVDirectory.values();
42
 
        for (JHVDirectory dir : dirs) {
43
 
            File f = dir.getFile();
44
 
            if (!f.exists()) {
45
 
                f.mkdirs();
46
 
            }
47
 
        }
48
 
    }
49
 
 
50
 
    /**
51
 
     * A central error handler. Displays an error message in a JOptionDialog and
52
 
     * exits if the flag is set.
53
 
     * 
54
 
     * @param _title
55
 
     *            title of the error message.
56
 
     * @param _msg
57
 
     *            the message which has to be displayed.
58
 
     * @param _exitImmediately
59
 
     *            the program exits when the value true will be passed.
60
 
     */
61
 
    public static void err(final String _title, final Object _msg, final boolean _exitImmediately) {
62
 
        EventQueue.invokeLater(new Runnable() {
63
 
            public void run() {
64
 
                JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (_msg == null ? "No error details available." : _msg.toString())), (_exitImmediately ? "Fatal Error!" : "Error!"), JOptionPane.ERROR_MESSAGE);
65
 
                if (_exitImmediately)
66
 
                    System.exit(-1);
67
 
            }
68
 
        });
69
 
    }
70
 
 
71
 
    /**
72
 
     * A central warning handler. Displays a warning message in a JOptionDialog.
73
 
     * 
74
 
     * @param _title
75
 
     *            title of the warning message.
76
 
     * @param _msg
77
 
     *            the message which has to be displayed.
78
 
     */
79
 
    public static void warn(final String _title, final Object _msg) {
80
 
        final String msg = _msg.toString();
81
 
 
82
 
        EventQueue.invokeLater(new Runnable() {
83
 
            public void run() {
84
 
                JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (msg == null || msg.equals("") ? "No warning details available." : msg)), "Warning!", JOptionPane.WARNING_MESSAGE);
85
 
            }
86
 
        });
87
 
    }
88
 
 
89
 
    /**
90
 
     * Opens the specified web page in the default web browser
91
 
     * 
92
 
     * @param url
93
 
     *            A web address (URL) of a web page (e.g
94
 
     *            "http://www.jhelioviewer.org/")
95
 
     */
96
 
    public static void openURL(String url) {
97
 
        Log.info("Opening URL " + url);
98
 
        String functionCall = "openURL(" + url + ")";
99
 
        String functionCallEntry = ">> " + functionCall;
100
 
        Log.trace(">> " + functionCall);
101
 
 
102
 
        try { // attempt to use Desktop library from JDK 1.6+ (even if on 1.5)
103
 
            Log.debug(functionCallEntry + " > Try to use java.awt.Desktop class from JDK 1.6+");
104
 
            Class<?> d = Class.forName("java.awt.Desktop");
105
 
            d.getDeclaredMethod("browse", new Class[] { java.net.URI.class }).invoke(d.getDeclaredMethod("getDesktop").invoke(null), new Object[] { java.net.URI.create(url) });
106
 
        } catch (Exception ignore) { // library not available or failed
107
 
            Log.debug(functionCallEntry + " > Loading class java.awt.Desktop failed. Try other methods to open URL.");
108
 
            String osName = System.getProperty("os.name");
109
 
            Log.trace(functionCallEntry + " > OS: " + osName);
110
 
            try {
111
 
                if (osName.startsWith("Mac OS")) {
112
 
                    Log.debug(functionCallEntry + " > Open URL assuming MacOS");
113
 
                    Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
114
 
                    Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
115
 
                    openURL.invoke(null, new Object[] { url });
116
 
 
117
 
                } else if (osName.startsWith("Windows")) {
118
 
                    Log.debug(functionCallEntry + " > Open URL assuming Windows");
119
 
                    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
120
 
                } else { // assume Unix or Linux
121
 
                    Log.debug(functionCallEntry + " > Open URL assuming Unix");
122
 
                    boolean found = false;
123
 
                    for (String browser : browsers) {
124
 
                        if (!found) {
125
 
                            found = Runtime.getRuntime().exec(new String[] { "which", browser }).waitFor() == 0;
126
 
                            if (found) {
127
 
                                Log.debug(functionCallEntry + " > Found browser " + browser);
128
 
                                Runtime.getRuntime().exec(new String[] { browser, url });
129
 
                            }
130
 
                        }
131
 
                    }
132
 
                    if (!found) {
133
 
                        throw new Exception(Arrays.toString(browsers));
134
 
                    }
135
 
                }
136
 
            } catch (Exception e) {
137
 
                Log.error("Error attempting to launch web browser", e);
138
 
                JOptionPane.showMessageDialog(null, "Error attempting to launch web browser\n" + e.toString());
139
 
            }
140
 
        }
141
 
        Log.trace("<< " + functionCall);
142
 
    }
143
 
 
144
 
}