Package eva.gui

Source Code of eva.gui.EVAApplication

/**
*
*/
package eva.gui;

import java.lang.Thread.UncaughtExceptionHandler;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import java.awt.event.WindowEvent;
import java.io.*;

import javax.swing.*;

import eva.gui.mvc.tools.AbstractWindowListener;
import eva.gui.tools.JExceptionDialog;

/**
* @author SimonWagner
* @version 0.2
*
*/
public class EVAApplication {

  private final static String propertiesFilePath = "settings.conf";
  private static EVAApplication app;
  public final static String SCHEDULER_VERSION = "0.2.1-84";
  public final static String FRAMEWORK_VERSION = "0.2.1-85";
  public static URL UPDATE_CHECK_URL;
 
  static {
    try {
      UPDATE_CHECK_URL = new URL("file:///D:/Desktop/update.xml");
    } catch (MalformedURLException e) {
      UPDATE_CHECK_URL = null;
    }
  }
 
  private Properties properties;

  /**
   * @param args
   */
  public static void main(String[] args) {
    app = new EVAApplication();
   
    app.createExceptionHandler();
   
    if(!app.loadProperties()) {
      System.exit(2); //exit if loading settings has failed!
    }
   
    /*
    //load default system style
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      System.err.println("could not load system look and feel");
      e.printStackTrace();
    }*/
   
    //start checking for an update
    UpdateChecker.doAsyncUpdatesCheck(UPDATE_CHECK_URL);
   
    //show the main window
    app.showMainFrame();
   
  }
 
  private void createExceptionHandler() {
   
    UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
      @Override
      public void uncaughtException(Thread thread, Throwable throwable) {
        JExceptionDialog.showExceptionDialog(throwable);
      }   
    };
   
    Thread.setDefaultUncaughtExceptionHandler(h);
    Thread.currentThread().setUncaughtExceptionHandler(h);
  }

  private void showMainFrame() {
    MainFrame mainFrame = new MainFrame();
   
    mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mainFrame.setLocationByPlatform(true);
    mainFrame.setSize(800,600);
    mainFrame.setTitle("EVA Version 2");
    mainFrame.addWindowListener(new AbstractWindowListener() {
      @Override
      public void windowClosed(WindowEvent event) {
        getApplication().saveProperties();
      }
    });
    mainFrame.setVisible(true);
  }
 
  public synchronized boolean loadProperties() {
    URI propertiesPath = getApplicationPath().resolve(propertiesFilePath);   
    File propertiesFile = new File(propertiesPath);

    properties = new Properties();
    try {
      properties.loadFromXML(new FileInputStream(propertiesFile));
      return true;
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Fehler beim Lesen der Einstellungen", JOptionPane.ERROR_MESSAGE);
      return false;
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Fehler beim Lesen der Einstellungen", JOptionPane.ERROR_MESSAGE);
      return false;
    }
  }
 
  public synchronized boolean saveProperties() {
    URI propertiesPath = getApplicationPath().resolve(propertiesFilePath);   
    File propertiesFile = new File(propertiesPath);

    try {
      if(properties != null) {
        properties.storeToXML(new FileOutputStream(propertiesFile),"");
        return true;
      }
      else {
        return false;
      }
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Fehler beim Speichern der Einstellungen", JOptionPane.ERROR_MESSAGE);
      return false;
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Fehler beim Speichern der Einstellungen", JOptionPane.ERROR_MESSAGE);
      return false;
    }
  }
 
  /**
   * @return the properties
   */
  public synchronized Properties getProperties() {
    return properties;
  }
 
  public synchronized void setProperties(Properties properties) {
    this.properties = properties;
  }

  public synchronized static EVAApplication getApplication() {
    return app;
  }
 
  public static URI getApplicationPath() {   
    try {
      return EVAApplication.class.getProtectionDomain().getCodeSource().getLocation().toURI();
    } catch (URISyntaxException e) {
      return null;
    }
  }

}
TOP

Related Classes of eva.gui.EVAApplication

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.