/**
*
* @(#)Main.java	1.0
*
* Purpose : Main method for Example
* Usage   : Main 
*
* @author  Harry Pfohl
* @version 1.0, 01/10/2008
* @since   1.0
*/
package template;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;

import javax.swing.JApplet;
import javax.swing.JPanel;

import de.cbse.jeasy.JEFrame;
import de.cbse.jeasy.JEGlobal;
import de.cbse.jeasy.JENode;
import de.cbse.jeasy.JEObject;
import de.cbse.jeasy.JEPanel;
import de.cbse.jeasy.JESaxParser;
import de.cbse.jeasy.JEStart;
import de.cbse.jeasy.JEUtil;
import de.cbse.jeasy.JEWindow;

/**
* @author   hpl
*/
public class Main extends JApplet {
	/**
	 * Comment for serialVersionUID
	 */
	private static final long serialVersionUID = 5872688067039585620L;

	String repository = "/JEasyRepository.xml";
	String projectFile = "";
	String starter = "";
	static int debug = 0;
	CallBack jcb;
	FileInputStream in = null;
	InputStreamReader dinr = null;
	BufferedReader din = null;
	static JEStart jestart = null;
	public static boolean appletFlag = true;
	public static String ipServer;
	public static String autoFile;
	public static boolean simulation;
	private static Main j;

	private Main() {
		// JEUtil.setDevelopMode(true);
		// JEUtil.setLogLevel(JEUtil.WARNING + JEUtil.ERROR + JEUtil.EVENT);
		// JEUtil.setLogLevel(JEUtil.WARNING + JEUtil.ERROR);
	}

	/**
	 * Sets the applet flag which is used to determine if the program is running as an applet or as an application.
	 */
	public void setAppletFlag(boolean flag) {
		appletFlag = flag;
	}

	/**
	 * Method main.
	 * 
	 * @param argv
	 * @throws IOException
	 */
	public static void main(String argv[]) throws IOException {
		j = new Main();
		j.setAppletFlag(false);
		int i = 0;
		int argCount = argv.length;
		if (argCount == 0) {
			System.out.println("No parameters !!!" + '\n');
		}
		j.jcb = CallBack.getInstance();
		String s = "";
		while (i < argCount) {
			if (argv[i].charAt(0) == '-') {
				if (argv[i].length() < 2) {
					System.err.println("Missing argument after '-' !");
				} else
					switch (argv[i].charAt(1)) {
					case 'P':
						s = argv[i].substring(2);
						System.out.println("Property : " + s);
						break;
					default:
						System.err.println("Parameter '" + argv[i].substring(1) + "' not defined !!!");
					}
			}
			i++;
		}
		j.init();
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				j.start();
			}
		});
	}

	public void init() {
		if (appletFlag) {
			projectFile = getParam("ProjectFile", "");
		}
		String vers = System.getProperty("java.version");
		System.out.println("Java Version: " + vers);
		if (vers.compareTo("1.4") < 0) {
			System.out.println("!!!WARNING: please use JDK 1.5 or higher !!!");
		}
	}

	public String getParam(String p, String d) {
		String s = getParameter(p);
		return s == null ? d : s;
	}

	public int getParam(String p, int d) {
		try {
			d = Integer.parseInt(getParameter(p));
		} catch (NumberFormatException e) {}
		return d;
	}

	public void start() {
		JENode element = null;
		try {
			// input stream
			// System.out.println("Start opening xml file "+JEFile);
			InputStream in = this.getClass().getResourceAsStream(repository);
			if (in == null) {
				in = new FileInputStream(repository.substring(1));
				if (in == null) {
					System.err.println("Could not open " + repository.substring(1));
					System.exit(-1);
				}
			}
			JEUtil.readJEObj(in);
		} catch (Exception ioe) {
			JEUtil.log('E', "JEasyRepository.start()", "" + element, "" + ioe);
		}
		jestart = JEStart.getInstance();
		jestart.setApplet(appletFlag);
		if (!appletFlag) {
			jestart.createSplashScreen();
			jestart.showSplashScreen();
		}
		JEGlobal jeglobal = (JEGlobal) JEUtil.getJO(jestart.getGlobalId());
		jcb.setLookAndFeel("METAL");
		System.out.println("Start building Swing components");
		if (appletFlag) {
			System.out.println("Start Applet");
			run(jestart.getStartIdApplet());
			JEObject je = (JEObject) JEUtil.getJO(jestart.getStartIdApplet());
			if ((je.getType()).equals("PA")) {
				// Panel is shown inside the Browsers window
				getContentPane().setLayout(new BorderLayout());
				JEPanel jep = (JEPanel) JEUtil.getJO(jestart.getStartIdApplet());
				JPanel jp = (JPanel) jep.getComponent();
				getContentPane().add(jp, BorderLayout.CENTER);
				System.out.println("Start Applet in Content");
			}
		} else {
			run(jestart.getStartIdApplication());
		}
		System.out.println("Start application");
		JEObject jo = new JEObject();
		jo = (JEObject) JEUtil.getJO(jestart.getStartIdApplication());
		// When starting as a frame
		if (jo.getType().equals(JEObject.FRAME)) {
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			JEFrame frame = (JEFrame) jo;
			frame.getJFrame().setBounds(5, 5, screenSize.width - 10, screenSize.height - 40);
			frame.setVisible(true);
			JEObject.setRootPane(frame.getFrameRootPane());
			JEUtil.registerJECallBack(jcb, jo.getId());
		}
		// When starting as a window
		else if (jo.getType().equals(JEObject.WINDOW)) {
			JEWindow window = (JEWindow) jo;
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			window.getJWindow().setBounds(5, 5, screenSize.width - 10, screenSize.height - 40);
			window.setVisible(true);
			JEObject.setRootPane(window.getJWindow().getRootPane());
			JEUtil.registerJECallBack(jcb, jo.getId());
			JEUtil.registerJECallBack(jcb, window.getOwnerId());
		}
		jcb.initJECallBack();
		try {
			// JGoodies
			//			String lafShortName = "PlasticXP";
			//			String lafClassName;
			//			if ("Windows".equalsIgnoreCase(lafShortName)) {
			//				lafClassName = Options.JGOODIES_WINDOWS_NAME;
			//			} else if ("Plastic".equalsIgnoreCase(lafShortName)) {
			//				lafClassName = Options.PLASTIC_NAME;
			//			} else if ("Plastic3D".equalsIgnoreCase(lafShortName)) {
			//				lafClassName = Options.PLASTIC3D_NAME;
			//			} else if ("PlasticXP".equalsIgnoreCase(lafShortName)) {
			//				lafClassName = Options.PLASTICXP_NAME;
			//			} else {
			//				lafClassName = lafShortName;
			//			}
			//			System.out.println("L&f chosen: " + lafClassName);
			//			settings = JGoodiesSettings.createDefault();
			//			settings.setSelectedLookAndFeel(lafClassName);
			//	        settings.setSelectedTheme(PlasticLookAndFeel.createMyDefaultTheme());
			//			settings.configureJGoodiesUI();
			// end JGoodies
			//			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			//			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			//			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			//			JETheme jet = (JETheme) JEUtil.getJO(JEIds.TH_JEASY);
			//			DefaultMetalTheme dmt = jet.getDefaultMetalTheme();
			//			MetalLookAndFeel.setCurrentTheme(dmt);
			//			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			//			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			//			jeglobal.updateTreeUIAllIF();
			//			jeglobal.updateTreeUIAllDL();
			//			jeglobal.updateTreeUIAllFR();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void run(String startId) {
		JEObject jo = new JEObject();
		try {
			jo = (JEObject) JEUtil.getJO(startId);
			jo.setupComponent();
			if (!appletFlag) {
				jestart.hideSplash();
			}
		} catch (NullPointerException npe) {
			JEUtil.log('E', "JEasyRepository.run()", "", "" + npe);
			npe.printStackTrace();
		}
	}
}