package edu.princeton.toy; import java.awt.*; import java.io.*; import javax.swing.*; /** * TMain is the class which contains the main function for the X-TOY application. It also parses * the version file. * * @author btsang * @version 7.1 */ public class TMain { private static final TMain INSTANCE = new TMain(); private Image frameIcon; private String version; private String buildDate; /** * Constructs a new TMain. */ private TMain() { version = "(Error)"; buildDate = "(Error)"; } /** * Parses the version file, launches a splash window until all the example, image, and parameter * managers are ready. Then launches an untitled frame. */ public static void main(String arguments[]) { System.out.println("Visual X-TOY"); // Load the version try { StringBuffer buffer = new StringBuffer(); Reader reader = TResourceLoader.getReader("version"); int character = reader.read(); while (character != '\n' && character != -1) { buffer.append((char)character); character = reader.read(); } if (character == '\n') { INSTANCE.version = buffer.toString(); character = reader.read(); buffer.delete(0, buffer.length()); while (character != '\n' && character != -1) { buffer.append((char)character); character = reader.read(); } if (character == '\n') { INSTANCE.buildDate = buffer.toString(); character = reader.read(); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("Version: " + INSTANCE.version); System.out.println("Build Date: " + INSTANCE.buildDate); System.out.println("OS Name: " + System.getProperty("os.name")); System.out.println("User Home: " + System.getProperty("user.home")); System.out.println("Class Version: " + System.getProperty("java.class.version")); System.out.println("JVM Version: " + System.getProperty("java.version")); // Test to make sure that this version wasn't compiled with Java >= 1.4 when this JVM // is < 1.4. try { // This is an interesting test because it uses the fact that in Java >= 1.4, appending // a StringBuffer to a StringBuffer is handled by a special method which treats the // appendee as a StringBuffer. Whereas previous versions of Java did not have this // method and would treat the StringBuffer as just any old Object, inefficiently // calling the toString() method of it. StringBuffer buffer1 = new StringBuffer("foo"); StringBuffer buffer2 = new StringBuffer("bar"); // This would cause a NoSuchMethodException if a >= 1.4 compiler tried to use the // special method which a < 1.4 JVM would not have. buffer1.append(buffer2); } catch (NoSuchMethodError e) { e.printStackTrace(); JOptionPane.showMessageDialog( null, "You are executing a version of Visual X-TOY that requires Java 1.4 or better,\n" + "but your version of java is " + System.getProperty("java.version") + ".\n" + "Please upgrade your JVM or use the Java 1.3 compatible version of Visual X-TOY.", "JVM Error", JOptionPane.ERROR_MESSAGE ); System.exit(-1); } try { // Show the spash screen TImageManager.prepare(TImageManager.STARTUP_UNSCALED_IMAGE_GROUP); TSplashWindow splashWindow = new TSplashWindow(); splashWindow.setVisible(true); splashWindow.requestFocus(); splashWindow.toFront(); // Wait for the resources to load while (!TExampleManager.isReady() || !TConfigurationManager.isReady()) { Thread.sleep(500); } TImageManager.prepare(TImageManager.REGULAR_UNSCALED_IMAGE_GROUP); { // Have the JFileChooser do some warm ups (if this is not done, the first showing // of the dialog will make it look like the button that launched it froze the // entire program for about 20 seconds) JFileChooser chooser = new JFileChooser(); File currentDirectory = TOptionsFrame.getCurrentDirectory(); if (currentDirectory != null) chooser.setCurrentDirectory(currentDirectory); chooser.rescanCurrentDirectory(); } // Figure out which icon to show String osName = System.getProperty("os.name").toLowerCase(); if (osName.indexOf("windows") != -1) { INSTANCE.frameIcon = TImageManager.getImage( TImageManager.REGULAR_UNSCALED_IMAGE_GROUP, TImageManager.REGULAR_UNSCALED_FRAME_ICON_WINDOWS ); } else { INSTANCE.frameIcon = TImageManager.getImage( TImageManager.REGULAR_UNSCALED_IMAGE_GROUP, TImageManager.REGULAR_UNSCALED_FRAME_ICON_OTHER_OS ); } // Create a frame and show it TFrame.createInitialFrame(); // Get rid of the splash screen splashWindow.setVisible(false); splashWindow.dispose(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } /** * Returns the version, as parsed from the version file. * * @return The version of this application. */ public static String getVersion() { return INSTANCE.version; } /** * Returns the build date, as parsed from the version file. * * @return The build date of this application. */ public static String getBuildDate() { return INSTANCE.buildDate; } /** * Returns the frame icon that should be used in this OS. * * @return The image for the frame icon that should be used in this OS. */ public static Image getFrameIcon() { return INSTANCE.frameIcon; } }