1
package org.helioviewer.jhv;
3
import java.awt.EventQueue;
5
import java.lang.reflect.Method;
6
import java.util.Arrays;
8
import org.helioviewer.base.logging.Log;
9
import javax.swing.JOptionPane;
12
* Intended to be a class for static functions and fields relevant to the
13
* application as a whole.
17
public class JHVGlobals {
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();
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;
25
private final static String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" };
27
/** Constructor is private to prevent instantiation. */
28
private JHVGlobals() {
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.
35
* @throws SecurityException
37
public static void createDirs() throws SecurityException {
38
JHVDirectory[] dirs = JHVDirectory.values();
39
for (JHVDirectory dir : dirs) {
40
File f = dir.getFile();
48
* A central error handler. Displays an error message in a JOptionDialog and
49
* exits if the flag is set.
52
* title of the error message.
54
* the message which has to be displayed.
55
* @param _exitImmediately
56
* the program exits when the value true will be passed.
58
public static void err(final String _title, final Object _msg, final boolean _exitImmediately) {
59
EventQueue.invokeLater(new Runnable() {
61
JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (_msg == null ? "No error details available." : _msg.toString())), (_exitImmediately ? "Fatal Error!" : "Error!"), JOptionPane.ERROR_MESSAGE);
69
* A central warning handler. Displays a warning message in a JOptionDialog.
72
* title of the warning message.
74
* the message which has to be displayed.
76
public static void warn(final String _title, final Object _msg) {
77
final String msg = _msg.toString();
79
EventQueue.invokeLater(new Runnable() {
81
JOptionPane.showMessageDialog(null, ((_title == null ? "" : _title + "\n") + (msg == null || msg.equals("") ? "No warning details available." : msg)), "Warning!", JOptionPane.WARNING_MESSAGE);
87
* Opens the specified web page in the default web browser
90
* A web address (URL) of a web page (e.g
91
* "http://www.jhelioviewer.org/")
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);
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);
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 });
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) {
122
found = Runtime.getRuntime().exec(new String[] { "which", browser }).waitFor() == 0;
124
Log.debug(functionCallEntry + " > Found browser " + browser);
125
Runtime.getRuntime().exec(new String[] { browser, url });
130
throw new Exception(Arrays.toString(browsers));
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());
138
Log.trace("<< " + functionCall);