~andre-dau/jhelioviewer/deadlock-fix

« back to all changes in this revision

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