/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
*
* $Id: Run.java,v 1.36 2009/02/04 14:25:16 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package jsynoptic.ui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JFrame;
import jsynoptic.base.Plugin;
import jsynoptic.builtin.Builtin;
import jsynoptic.data.ExpressionDataSourceProvider;
import simtools.data.DataSourcePool;
import simtools.ui.BasicMessageWriter;
import simtools.ui.CustomizedLocale;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import simtools.ui.UserProperties;
/**
* This is the main class for jsynoptic, the entry point of the program java
* jsynoptic.Run [options]
*
* Please compile this class with javac -target 1.1, so as to generate code
* compatible with older JVMs. Then, a check is done at start-up to determine if
* the VM version is sufficient.
*
* @author Nicolas Brodu
*
* @version 1.0 2001
*/
public class Run {
/** Resources */
public static MenuResourceBundle resources = ResourceFinder.getMenu(Run.class);
public static BasicMessageWriter messageWriter = ResourceFinder.getMessages(Run.class);
public static String productName = resources.getStringValue("productName");
public static String fullProductVersion = resources.getStringValue("productVersion");
protected static UserProperties userProperties = new UserProperties("jsynoptic", true);
public static Vector plugins = new Vector();
public static JSynopticSplashSreen splashSreen = new JSynopticSplashSreen();
public static void loadPlugin(String name) {
for (int i = 0; i < plugins.size(); ++i) {
if (plugins.get(i).getClass().getName().equals(name)) {
return; // already loaded
}
}
Plugin p;
try {
p = Plugin.load(name);
} catch (Throwable t) {
p = null;
t.printStackTrace();
}
if (p == null) {
System.err.println(messageWriter.print1args("cannotLoadPlugin", name));
} else {
plugins.add(p);
}
}
public static UserProperties getProperties() {
return userProperties;
}
public static void setUserProperties(UserProperties userProperties){
Run.userProperties = userProperties;
}
// This part cannot be translated, as Localization evolved since older JVMs.
static void checkJavaVersion(boolean batch) {
double javaVersion = 0.0;
try {
javaVersion = Double.parseDouble(System.getProperty("java.specification.version"));
} catch (Throwable t) {
javaVersion = 0.0;
}
if (javaVersion < 1.4) {
System.err.println("Java version 1.4 or later is required to run this program.");
System.err.println("Please download it from http://java.sun.com/");
if (!batch) {
// Display AWT window => may run with older JVM
final Frame f = new Frame("System Configuration Error");
f.add(new Label("Java version 1.4 or later is required to run this program."), BorderLayout.NORTH);
f.add(new Label("Please download it from http://java.sun.com/"), BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
synchronized (f) {
f.notifyAll();
}
}
});
synchronized (f) {
try {
f.wait();
} catch (InterruptedException e1) {
}
}
}
System.exit(1);
}
}
/**
* Program main entry point.
*/
public static void main(String[] args) {
boolean batch = false;
// Display message and stop now when running with older JVM
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-printToFile") || args[i].equals("-f") || args[i].equals("-printTo")
|| args[i].equals("-r") || args[i].equals("-print") || args[i].equals("-p")
|| args[i].equals("-export") || args[i].equals("-e")
|| args[i].equals("-batch")
|| args[i].equals("-b")
|| args[i].equals("-transform") // We should be able to process synoptics transformations in GUI mode. However, does not work for
// now (V2.3.0)...
|| args[i].equals("-t")
){
batch = true;
}
}
// Check the java version at run-time.
// This became necessary because java 1.3 doesn't crash immediately,
// and makes the user think there is an aweful bug at startup
checkJavaVersion(batch);
// Read properties => this contain the user preference, including
// language setup
userProperties.read();
// language may be "fr", or "en_US", or whatever. If unsupported, it
// will default to best match
String language = userProperties.getString("jsynoptic.language", "");
// Let command-line -D JVM define take precedence
if ((System.getProperty("language") == null) || System.getProperty("language").equals("")) {
if (!language.equals("")) {
int sep = language.indexOf('_');
if (sep == -1) {
CustomizedLocale.set(new Locale(language));
} else {
String lang = language.substring(0, sep);
String country = language.substring(sep + 1);
sep = country.indexOf('_');
if (sep == -1) {
CustomizedLocale.set(new Locale(lang, country));
} else {
CustomizedLocale.set(new Locale(lang, country.substring(0, sep), country.substring(sep + 1)));
}
}
}
}
// initialize a few variables
plugins.add(new Builtin());
Vector commands = new Vector();
String defaultPlugins = userProperties.getString("jsynoptic.plugins", "");
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-help") || args[i].equals("-h") || args[i].equals("--help")) {
int numLines = resources.getIntValue("helpMsgNumLines");
for (int j = 0; j < numLines; j++) {
System.out.println(messageWriter.print0args("helpMsg" + j));
}
System.exit(0);
}
if (args[i].equals("-plugin") || args[i].equals("-g")) {
if (i + 1 >= args.length) {
System.err.println(messageWriter.print0args("pluginHelp"));
System.exit(0);
}
loadPlugin(args[++i]);
continue;
}
if (args[i].equals("-paper") || args[i].equals("-a")) {
batch = true;
if (i + 1 >= args.length) {
System.err.println(messageWriter.print0args("paperHelp"));
System.exit(0);
}
commands.add("paper " + args[++i]);
continue;
}
if (args[i].equals("-orientation") || args[i].equals("-o")) {
batch = true;
if (i + 1 >= args.length) {
System.err.println(messageWriter.print0args("orientationHelp"));
System.exit(0);
}
commands.add("orientation " + args[++i]);
continue;
}
if (args[i].equals("-print") || args[i].equals("-p")) {
batch = true;
commands.add("printToPrinter");
continue;
}
if (args[i].equals("-printTo") || args[i].equals("-r")) {
batch = true;
if (i + 1 >= args.length) {
System.err.println(messageWriter.print0args("printToHelp"));
System.exit(0);
}
commands.add("printToPrinter " + args[++i]);
continue;
}
if (args[i].equals("-printToFile") || args[i].equals("-f")) {
batch = true;
if (i + 1 >= args.length) {
System.err.println(messageWriter.print0args("printToFileHelp"));
System.exit(0);
}
commands.add("printToFile " + args[++i]);
continue;
}
if (args[i].equals("-export") || args[i].equals("-e")) {
batch = true;
String exportDirectory = "";
if (i + 1 < args.length) {
exportDirectory = args[++i];
}
commands.add("export " + exportDirectory);
continue;
}
if (args[i].equals("-transform") || args[i].equals("-t")) {
batch = true; // TODO should be functional in GUI mode too
String actionType = "";
if (i + 1 < args.length) {
actionType = args[++i];
}
commands.add("transform " + actionType);
continue;
}
if (args[i].equals("-batch") || args[i].equals("-b")) {
batch = true;
continue;
}
commands.add("load " + args[i]);
}
if (batch) {
System.out.println(messageWriter.print0args("waitLoading"));
splashSreen.setVisible(false);
}
try {
// Compute a list of plugins to load
String appClassPath = userProperties.getString("jsynoptic.class.path", "");
String appNativePath = userProperties.getString("jsynoptic.native.path", "");
if (appClassPath.equals("") || appNativePath.equals("")) {
PluginLookup.addPluginPaths();
userProperties.setString("jsynoptic.class.path", PluginLookup.getClassPath());
userProperties.setString("jsynoptic.native.path", PluginLookup.getNativePath());
} else {
StringTokenizer st = new StringTokenizer(appClassPath, ",;:");
while (st.hasMoreTokens()) {
PluginLookup.addClassPath(st.nextToken());
}
st = new StringTokenizer(appNativePath, ", ;:\t|");
while (st.hasMoreTokens()) {
PluginLookup.addNativePath(new File(st.nextToken()));
}
}
// Load plugins
if (defaultPlugins.equals("")) {
if (splashSreen.isVisible()) {
splashSreen.setProgressBarPosition(20);
splashSreen.setMessage(messageWriter.print0args("lookupPlugins"));
}
for (Iterator it = PluginLookup.lookup().iterator(); it.hasNext();) {
String plugin = (String) it.next();
if (!defaultPlugins.equals("")) {
defaultPlugins += ",";
}
defaultPlugins += plugin;
loadPlugin(plugin);
}
if (!defaultPlugins.equals("")) {
userProperties.setString("jsynoptic.plugins", defaultPlugins);
}
} else {
StringTokenizer st = new StringTokenizer(defaultPlugins, ", ;:\t|");
int n = st.countTokens();
int i = 0;
if (splashSreen.isVisible()) {
splashSreen.setMessage(messageWriter.print0args("loadingPlugins"));
}
while (st.hasMoreTokens()) {
loadPlugin(st.nextToken());
if (splashSreen.isVisible()) {
splashSreen.setProgressBarPosition(20 + 40 * i / n);
}
i++;
}
}
// Plugins are loaded, add main app features
DataSourcePool.global.addProvider(new ExpressionDataSourceProvider());
if (batch) {
// Start batch process
new JSynopticBatch(plugins, commands);
} else {
if (splashSreen.isVisible()){
splashSreen.setProgressBarPosition(50);
splashSreen.setMessage(messageWriter.print0args("buildingGUI"));
}
// Create JSynoptic frame
new JSynopticFrame( new JFrame("Jsynoptic"), commands);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}