/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tessgui;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import javax.swing.UIManager;

/**
 *
 * @author  marius (http://www.spinu.it)
 * @email   marius@spinu.it
 */
public class Main {

    private static Hashtable preferences = new Hashtable();
    public static final String TESS_PATH = "TesseractPath";
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            //cambiaDimensioneFont();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        String tesseractPath = "";       
        // verifico l'esistenza delle cartelle locali e remote 
        readPreferences();
        tesseractPath = (String) preferences.get(TESS_PATH);
        
        File tessLog = new File(tesseractPath+"\\tesseract.exe");       
        if (tessLog.canWrite()) {
            System.out.println("Path valido: " + tesseractPath);            
            TessFrame tf = new TessFrame(tesseractPath);   
            tf.setTitle("Tesseract 2.00 GUI - v0.2");
            centerOnParent(tf);
            tf.setVisible(true);
        }
        else {
            System.out.println("Path valido: " + tesseractPath);            
            TessFrame tf = new TessFrame("verificare PATH");            
            centerOnParent(tf);
            tf.setVisible(true);
        }
    }
    
    public static void readPreferences() {
        Enumeration myEnum;
        PropertyResourceBundle rb;
        String key, value;
        Properties properties = System.getProperties();
        myEnum = properties.propertyNames();
        
        while (myEnum.hasMoreElements()) {
            key = myEnum.nextElement().toString();
            if (key.startsWith("Pianificatore")) {
                preferences.put(key, properties.getProperty(key));
            }
        }
        
        try {
            rb = new PropertyResourceBundle(new FileInputStream("tessgui.properties"));
            myEnum = rb.getKeys();
            while (myEnum.hasMoreElements()) {
                key = myEnum.nextElement().toString();
                value = (String) preferences.get(key);
                if (value == null) {
                    preferences.put(key, rb.getString(key));
                }
            }
        } catch (FileNotFoundException ex) {
            //ex.printStackTrace();
            System.out.println("File proprietà non trovato");
        } catch (IOException ex1) {
            //ex1.printStackTrace();
        }
    }
    
    public static void centerOnParent(Window window) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle r = ge.getMaximumWindowBounds();
        Dimension dim = new Dimension(r.width, r.height); //getToolkit().getScreenSize();
        Rectangle abounds = window.getBounds();
        window.setLocation( (dim.width - abounds.width) / 2,
                (dim.height - abounds.height) / 2);
        
    }

}
