Package Start

Source Code of Start.Start

/**
* The main method of the IDE. Here the GUI is created
* and other program instances are blocked
*/

package Start;

import GUI.GUI;
import GUI.Output;
import GUI.WorkspaceDialog;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import Compiler.CompilerUtils;
import Compiler.Pseuco;

public class Start {
 
  public static final String version = "v1.2.1 beta";
  public static String workspace = "";
  public static final boolean debug = false;
  private static LockApplication lock = new LockApplication();
  public static HashMap<String, String> config;
 
  private static BufferedReader reader = null;

  /**
   * @param args
   */
  public static void main(String[] args) {
    Pseuco.debugger.startRefresher();
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          if (!lock.isLocked()) {
            setLookAndFeel();
            if (loadConfig())
              new WorkspaceDialog();
            else {
              if (new File(Start.workspace).exists())
                  new GUI();
              else
                new WorkspaceDialog();
            }
          }
          else
            System.exit(0);
        } catch (Exception e) {
          if (Start.debug) e.printStackTrace();
        }
      }
    });
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          CompilerUtils.del(Pseuco.includeDirFile);
          lock.releaseLock();
        } catch (Exception e) {}     
      }
    });
  }
 
  protected static void setLookAndFeel() {
    NimbusLookAndFeel laf = new NimbusLookAndFeel();
    try {
      UIManager.setLookAndFeel(laf);
    } catch (UnsupportedLookAndFeelException e) {
      e.printStackTrace(Output.err);
    }
    UIDefaults nimbUID = laf.getDefaults();
    nimbUID.put("Tree.drawHorizontalLines", true);
    nimbUID.put("Tree.drawVerticalLines", true);
  }

  private static boolean loadConfig() throws IOException {
    config = loadConfigFile();
    if (config.containsKey("Workspace"))
      Start.workspace = config.get("Workspace");
    if (config.containsKey("AskAgain"))
      return Boolean.valueOf(config.get("AskAgain"));
    return true;
  }
 
  private static HashMap<String, String> loadConfigFile() throws IOException{
    HashMap<String, String> map = new HashMap<String, String>();
    if (new File(".config").exists()){
      reader = new BufferedReader(new FileReader(".config"));
      String line;
      while ((line = reader.readLine()) != null) {
        String[] single = line.split("=", 2);
        if (single.length == 2){
          map.put(single[0], single[1]);
        }
      }
    }
    return map;
  }
 
  public static void saveConfig(){
    try {
      BufferedWriter writer = new BufferedWriter(new FileWriter(".config"));
      for(String key: config.keySet()){
        writer.write(key);
        writer.write("=");
        writer.write(config.get(key));
        writer.write(System.getProperty("line.separator"));
      }
      writer.close();
    } catch (IOException e) {
      if (Start.debug) e.printStackTrace();
    }
  }

}
TOP

Related Classes of Start.Start

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.