1
package org.helioviewer.base.jhv;
3
import java.awt.EventQueue;
5
import java.lang.reflect.Method;
6
import java.util.Arrays;
9
import org.helioviewer.base.logging.Log;
10
import javax.swing.JOptionPane;
13
* Intended to be a class for static functions and fields relevant to the
14
* application as a whole.
18
public class JHVGlobals {
20
public static final Date startTimestamp = new Date();
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();
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;
28
private final static String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" };
30
/** Constructor is private to prevent instantiation. */
31
private JHVGlobals() {
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.
38
* @throws SecurityException
40
public static void createDirs() throws SecurityException {
41
JHVDirectory[] dirs = JHVDirectory.values();
42
for (JHVDirectory dir : dirs) {
43
File f = dir.getFile();
51
* A central error handler. Displays an error message in a JOptionDialog and
52
* exits if the flag is set.
55
* title of the error message.
57
* the message which has to be displayed.
58
* @param _exitImmediately
59
* the program exits when the value true will be passed.
61
public static void err(final String _title, final Object _msg, final boolean _exitImmediately) {
62
EventQueue.invokeLater(new Runnable() {
64
JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (_msg == null ? "No error details available." : _msg.toString())), (_exitImmediately ? "Fatal Error!" : "Error!"), JOptionPane.ERROR_MESSAGE);
72
* A central warning handler. Displays a warning message in a JOptionDialog.
75
* title of the warning message.
77
* the message which has to be displayed.
79
public static void warn(final String _title, final Object _msg) {
80
final String msg = _msg.toString();
82
EventQueue.invokeLater(new Runnable() {
84
JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (msg == null || msg.equals("") ? "No warning details available." : msg)), "Warning!", JOptionPane.WARNING_MESSAGE);
90
* Opens the specified web page in the default web browser
93
* A web address (URL) of a web page (e.g
94
* "http://www.jhelioviewer.org/")
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);
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);
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 });
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) {
125
found = Runtime.getRuntime().exec(new String[] { "which", browser }).waitFor() == 0;
127
Log.debug(functionCallEntry + " > Found browser " + browser);
128
Runtime.getRuntime().exec(new String[] { browser, url });
133
throw new Exception(Arrays.toString(browsers));
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());
141
Log.trace("<< " + functionCall);