/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot.jaxodraw;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.ResourceBundle;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;

import javax.swing.JOptionPane;


/** Responsible for importing/exporting user preferences. */
public final class JaxoPrefs {
    /** The file where user-selected preferences will be stored. */
    public static final String PREF_FILENAME = ".Jaxorc";

    // Strings: 0-99

    /** The preferred HTML browser. */
    public static final int PREF_BROWSER = 0;

    /** The preferred text editor. */
    public static final int PREF_EDITOR = 1;

    /** The preferred postscript viewer. */
    public static final int PREF_PSVIEWER = 2;

    /** The preferred Look and Feel. */
    public static final int PREF_LOOKNFEEL = 3;

    /** The preferred language. */
    public static final int PREF_LANGUAGE = 4;

    /** The preferred grid type. */
    public static final int PREF_GRIDTYPE = 5;

    /** The preferred Object color. */
    public static final int PREF_COLOR = 6;

    /** The preferred Postscript font family. */
    public static final int PREF_PSFAMILY = 7;

    /** The preferred Text color. */
    public static final int PREF_TEXTCOLOR = 8;

    /** The preferred latex path. */
    public static final int PREF_LATEXPATH = 9;

    /** The preferred dvips path. */
    public static final int PREF_DVIPSPATH = 10;

    // ints: 100-199

    /** The preferred screen size - x dimension. */
    public static final int PREF_SCREENSIZEX = 100;

    /** The preferred screen size - y dimension. */
    public static final int PREF_SCREENSIZEY = 101;

    /** The preferred grid size. */
    public static final int PREF_GRIDSIZE = 102;

    /** The preferred amplitude for photon and gluon objects. */
    public static final int PREF_AMPLITUDE = 104;

    /** The preferred Postscript font style. */
    public static final int PREF_PSSTYLE = 105;

    /** The preferred Postscript font size. */
    public static final int PREF_PSSIZE = 106;

    /** The preferred TeX font size. */
    public static final int PREF_TEXSIZE = 107;

    /** The preferred TeX alignment. */
    public static final int PREF_TEXALIGN = 108;

    /** The preferred postscript text rotation angle. */
    public static final int PREF_PSROTANGLE = 109;

    /** The preferred magnification factor for the zoom. */
    public static final int PREF_ZOOMFACTOR = 110;

    /** The preferred x-size of the zoom window. */
    public static final int PREF_ZOOMSIZEX = 111;

    /** The preferred y-size of the zoom window. */
    public static final int PREF_ZOOMSIZEY = 112;

    /** The preferred LaTeX text rotation angle. */
    public static final int PREF_TEXROTANGLE = 113;

    /** The default action. */
    public static final int PREF_DEFAULTACTION = 114;

    // floats: 200-299

    /** The preferred line width. */
    public static final int PREF_LINEWIDTH = 200;

    /** The preferred separation of double line objects. */
    public static final int PREF_DLSEP = 201;

    // booleans: 300-399

    /** A boolean preference indicating wether to show the tool bar or not. */
    public static final int PREF_SHOWTOOL = 300;

    /** A boolean preference indicating wether to show the status bar or not.*/
    public static final int PREF_SHOWSTATUS = 301;

    /** A boolean preference indicating wether to use antialias or not.*/
    public static final int PREF_ANTIALIAS = 302;

    /** A boolean preference indicating wether to draw arrows or not.*/
    public static final int PREF_ARROW = 303;

    /** A boolean preference indicating wether the grid is on or off.*/
    public static final int PREF_GRIDONOFF = 304;

      /** A boolean preference indicating wether to show the editor bar or not. */
    public static final int PREF_SHOWEDITOR = 305;

      /** A boolean preference indicating wether to show the editor bar or not. */
    public static final int PREF_SHOWBROWSER = 306;



    /** An array of integers identifying the preferences. */
    private static final int[] PREFS =
    {
        PREF_BROWSER, PREF_EDITOR, PREF_PSVIEWER, PREF_LOOKNFEEL, PREF_LANGUAGE,
        PREF_GRIDTYPE, PREF_COLOR, PREF_PSFAMILY, PREF_TEXTCOLOR,
        PREF_LATEXPATH, PREF_DVIPSPATH, PREF_DEFAULTACTION, PREF_SCREENSIZEX,
        PREF_SCREENSIZEY, PREF_GRIDSIZE, PREF_AMPLITUDE, PREF_PSSTYLE,
        PREF_PSSIZE, PREF_TEXSIZE, PREF_TEXALIGN, PREF_PSROTANGLE,
        PREF_ZOOMFACTOR, PREF_ZOOMSIZEX, PREF_ZOOMSIZEY, PREF_TEXROTANGLE,
        PREF_LINEWIDTH, PREF_DLSEP, PREF_SHOWTOOL, PREF_SHOWSTATUS,
        PREF_ANTIALIAS, PREF_ARROW, PREF_GRIDONOFF, PREF_SHOWEDITOR, PREF_SHOWBROWSER 
    };

    /** The number of preferences defined. */
    private static final int NO_OF_PREFS = PREFS.length;

    /** An array of strings identifying the preferences. */
    private static String[] prefKEYS =
    {
        "prefBROWSER", "prefEDITOR", "prefPSVIEWER", "prefLOOKNFEEL",
        "prefLANGUAGE", "prefGRIDTYPE", "prefCOLOR", "prefPSFAMILY",
        "prefTEXTCOLOR", "prefLATEXPATH", "prefDVIPSPATH", "prefDEFAULTACTION",
        "prefSCREENSIZEX", "prefSCREENSIZEY", "prefGRIDSIZE", "prefAMPLITUDE",
        "prefPSSTYLE", "prefPSSIZE", "prefTEXSIZE", "prefTEXALIGN",
        "prefPSROTANGLE", "prefZOOMFACTOR", "prefZOOMSIZEX", "prefZOOMSIZEY",
        "prefTEXROTANGLE", "prefLINEWIDTH", "prefDLSEP", "prefSHOWTOOL",
        "prefSHOWSTATUS", "prefANTIALIAS", "prefARROW", "prefGRIDONOFF", "prefSHOWEDITOR", 
        "prefSHOWBROWSER" 
    };

    //  Default values
    private static final String BROWSER_DEFAULT = "";
    private static final String EDITOR_DEFAULT = "";
    private static final String PSVIEWER_DEFAULT = "";
    private static final String LOOKNFEEL_DEFAULT = "Metal";
    private static final String LANGUAGE_DEFAULT = "english";
    private static final String GRIDTYPE_DEFAULT = "rectangular";
    private static final String COLOR_DEFAULT =
        JaxoColor.getColorName(JaxoColor.BLACK);
    private static final String PSFAMILY_DEFAULT = "Default";
    private static final String TEXTCOLOR_DEFAULT =
        JaxoColor.getColorName(JaxoColor.BLACK);
    private static final String LATEXPATH_DEFAULT = "latex";
    private static final String DVIPSPATH_DEFAULT = "dvips";
    private static final String DEFAULTACTION_DEFAULT = "0";
    private static final String SCREENSIZEX_DEFAULT = "800";
    private static final String SCREENSIZEY_DEFAULT = "600";
    private static final String GRIDSIZE_DEFAULT = "15";
    private static final String AMPLITUDE_DEFAULT = "15";
    private static final String PSSTYLE_DEFAULT = "2";
    private static final String PSSIZE_DEFAULT = "18";
    private static final String TEXSIZE_DEFAULT = "6";
    private static final String TEXALIGN_DEFAULT = "2";
    private static final String PSROTANGLE_DEFAULT = "0";
    private static final String ZOOMFACTOR_DEFAULT = "2";
    private static final String ZOOMSIZEX_DEFAULT = "160";
    private static final String ZOOMSIZEY_DEFAULT = "100";
    private static final String TEXROTANGLE_DEFAULT = "0";
    private static final String LINEWIDTH_DEFAULT = "1.0";
    private static final String DLSEP_DEFAULT = "2.0";
    private static final String SHOWTOOL_DEFAULT = "false";
    private static final String SHOWEDITOR_DEFAULT = "false";
    private static final String SHOWBROWSER_DEFAULT = "false";
    private static final String SHOWSTATUS_DEFAULT = "true";
    private static final String ANTIALIAS_DEFAULT = "true";
    private static final String ARROW_DEFAULT = "true";
    private static final String GRIDONOFF_DEFAULT = "false";

    /** An array of strings identifying the default preferences. */
    private static String[] prefDEFAULTS =
    {
        BROWSER_DEFAULT, EDITOR_DEFAULT, PSVIEWER_DEFAULT, LOOKNFEEL_DEFAULT,
        LANGUAGE_DEFAULT, GRIDTYPE_DEFAULT, COLOR_DEFAULT, PSFAMILY_DEFAULT,
        TEXTCOLOR_DEFAULT, LATEXPATH_DEFAULT, DVIPSPATH_DEFAULT,
        DEFAULTACTION_DEFAULT, SCREENSIZEX_DEFAULT, SCREENSIZEY_DEFAULT,
        GRIDSIZE_DEFAULT, AMPLITUDE_DEFAULT, PSSTYLE_DEFAULT, PSSIZE_DEFAULT,
        TEXSIZE_DEFAULT, TEXALIGN_DEFAULT, PSROTANGLE_DEFAULT,
        ZOOMFACTOR_DEFAULT, ZOOMSIZEX_DEFAULT, ZOOMSIZEY_DEFAULT,
        TEXROTANGLE_DEFAULT, LINEWIDTH_DEFAULT, DLSEP_DEFAULT, SHOWTOOL_DEFAULT,
        SHOWSTATUS_DEFAULT, ANTIALIAS_DEFAULT, ARROW_DEFAULT, GRIDONOFF_DEFAULT,
        SHOWEDITOR_DEFAULT, SHOWBROWSER_DEFAULT 
    };

    // The preferences, initialized to default values
    private static String[] preferences = prefDEFAULTS;

    /*
        {
            BROWSER_DEFAULT, EDITOR_DEFAULT, PSVIEWER_DEFAULT, LOOKNFEEL_DEFAULT,
            LANGUAGE_DEFAULT, GRIDTYPE_DEFAULT, COLOR_DEFAULT, PSFAMILY_DEFAULT,
            TEXTCOLOR_DEFAULT, LATEXPATH_DEFAULT, DVIPSPATH_DEFAULT,
            SCREENSIZEX_DEFAULT, SCREENSIZEY_DEFAULT, GRIDSIZE_DEFAULT,
            AMPLITUDE_DEFAULT, PSSTYLE_DEFAULT, PSSIZE_DEFAULT, TEXSIZE_DEFAULT,
            TEXALIGN_DEFAULT, PSROTANGLE_DEFAULT, ZOOMFACTOR_DEFAULT,
            ZOOMSIZEX_DEFAULT, ZOOMSIZEY_DEFAULT, LINEWIDTH_DEFAULT, DLSEP_DEFAULT,
            SHOWTOOL_DEFAULT, SHOWSTATUS_DEFAULT, ANTIALIAS_DEFAULT, ARROW_DEFAULT,
        };
    */

    //////////////////////////////////////////////////////////////////////////////
    //   TO ADD PREFS, ONLY MODIFY FIELDS ABOVE THIS LINE
    //////////////////////////////////////////////////////////////////////////////

    /** Sets verbose error (exception) messaging. */
    private static boolean verbose = true;

    /** An array of strings identifying the recently opened files. */
    private static String[] recentKEYS =
    {"recentFile1", "recentFile2", "recentFile3", "recentFile4"};

    /** The recently opened files. */
    private static String[] recentFile = {"", "", "", ""};

    /** Empty private constructor to prevent the class from being
     * explicitly instantiated.
     */
    private JaxoPrefs() {
        // empty on purpose
    }

    /** Returns the number of preferences defined by JaxoPrefs.
     * @return The number of preferences defined by JaxoPrefs.
     */
    public static int getNofPrefs() {
        return NO_OF_PREFS;
    }

    /** Returns the verbose error (exception) messaging.
     * @return True, if verbose error (exception) messaging is on, false
     * otherwise.
     */
    public static boolean verbose() {
        return verbose;
    }

    /**
     * Sets the verbose error messaging.
     * @param verb True, if verbose error messaging is to be switched
     * on, false otherwise.
     */
    public static void setVerbose(boolean verb) {
        verbose = verb;
    }

    /**
     * Returns the absolute path of a recently opened file.
     * @param i The index of the file, 0<= i <=3
     * @return The absolute path of the file.
     */
    public static String getRecentFile(int i) {
        String str = null;

        if ((i >= 0) && (i < 4)) {
            str = recentFile[i];
        }

        return str;
    }

    /**
     * Sets a recently visited file.
     * @param i The index of the file to be set, 0<= i <= 3
     * @param str The absolute path of the file to be set.
     */
    public static void setRecentFile(int i, String str) {
        if ((i >= 0) && (i < 4)) {
            recentFile[i] = str;
        }
    }

    /**
     * Returns the i'th preference as a string.
     * @param i The index of the preference.
     * @return The i'th preference.
     */
    public static String getPref(int i) {
        String str = null;

        if (getArrayIndex(i) < getNofPrefs()) {
            str = preferences[getArrayIndex(i)];
        }

        return str;
    }

    /**
     * Returns the i'th preference as a string.
     * @param i The index of the preference.
     * @return The i'th preference.
     */
    public static String getStringPref(int i) {
        String str = null;

        if ((i >= 0) && (i < 100)) {
            str = preferences[getArrayIndex(i)];
        }

        return str;
    }

    /**
     * Returns the i'th preference as an integer, or -1 if the
     * i'th preference does not correspond to an integer.
     * @param i The index of the preference.
     * @return The i'th preference.
     */
    public static int getIntPref(int i) {
        int jj = -1;

        if ((i >= 100) && (i < 200)) {
            String str = preferences[getArrayIndex(i)];
            Integer jjInt = new Integer(str);
            jj = jjInt.intValue();
        }

        return jj;
    }

    /**
     * Returns the i'th preference as a float, or 0.f if the
     * i'th preference does not correspond to a float.
     * @param i The index of the preference.
     * @return The i'th preference.
     */
    public static float getFloatPref(int i) {
        float jj = 0.f;

        if ((i >= 200) && (i < 300)) {
            String str = preferences[getArrayIndex(i)];
            Float jjFloat = new Float(str);
            jj = jjFloat.floatValue();
        }

        return jj;
    }

    /**
     * Returns the i'th preference as a boolean, or true, if the i'th
     * preference does not correspond to a boolean.
     * @param i The index of the preference.
     * @return The i'th preference.
     */
    public static boolean getBooleanPref(int i) {
        boolean jj = true;

        if ((i >= 300) && (i < 400)) {
            String str = preferences[getArrayIndex(i)];
            Boolean jjBool = Boolean.valueOf(str);
            jj = jjBool.booleanValue();
        }

        return jj;
    }

    /**
     * Sets the i'th preference to the string str.
     * @param i The index of the preference to be set.
     * @param str the String representation of the preference
     * to be set.
     */
    public static void setPref(int i, String str) {
        preferences[getArrayIndex(i)] = str;
    }

    /**
     * Sets the i'th preference to the int value newPref.
     * @param i The index of the preference to be set.
     * @param newPref The preference to be set.
     */
    public static void setIntPref(int i, int newPref) {
        Integer j = new Integer(newPref);
        preferences[getArrayIndex(i)] = j.toString();
    }

    /**
     * Sets the i'th preference to the float value of newPref.
     * @param i The index of the preference to be set.
     * @param newPref The preference to be set.
     */
    public static void setFloatPref(int i, float newPref) {
        Float j = new Float(newPref);
        preferences[getArrayIndex(i)] = j.toString();
    }

    /**
     * Sets the i'th preference to the boolean value of newPref.
     * @param i The index of the preference to be set.
     * @param newPref The preference to be set.
     */
    public static void setBooleanPref(int i, boolean newPref) {
        Boolean j = Boolean.valueOf(newPref);
        preferences[getArrayIndex(i)] = j.toString();
    }

    /** Imports user preferences from the specified file fileName.
     * @param fileName The preferences file.
     */
    public static void importPrefs(String fileName) {
        ResourceBundle lang =
            ResourceBundle.getBundle(getPref(JaxoPrefs.PREF_LANGUAGE));

        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(fileName));
            Preferences.importPreferences(is);
        } catch (FileNotFoundException e) {
            if (verbose()) {
                System.err.println(e);
            }

            JOptionPane.showMessageDialog(null,
                fileName + lang.getString("_does_not_exist!"));
        } catch (InvalidPreferencesFormatException ipfEx) {
            if (verbose()) {
                System.err.println(ipfEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString("Unknown_preferences_file_format!"));
        } catch (IOException ioEx) {
            if (verbose()) {
                System.err.println(ioEx);
            }
            JOptionPane.showMessageDialog(null,
                lang.getString("IOException:_Cannot_open_preferences_file!"));
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioEx) {
                    if (verbose()) {
                        System.err.println(ioEx);
                    }
                }
            }
        }

        Preferences pr = Preferences.userRoot().node(JaxoDraw.USER_HOME);

        for (int j = 0; j < PREFS.length; j++) {
            preferences[j] = pr.get(prefKEYS[j], prefDEFAULTS[j]);
        }

        // recent files
        for (int j = 0; j < 4; j++) {
            recentFile[j] = pr.get(recentKEYS[j], recentFile[j]);
        }
    }

    /** Exports user preferences to the specified file fileName.
     * @param fileName The preferences file.
     */
    public static void exportPrefs(String fileName) {
        ResourceBundle lang =
            ResourceBundle.getBundle(getPref(JaxoPrefs.PREF_LANGUAGE));

        Preferences pr = Preferences.userRoot().node(JaxoDraw.USER_HOME);

        for (int j = 0; j < PREFS.length; j++) {
            pr.put(prefKEYS[j], preferences[j]);
        }

        for (int j = 0; j < 4; j++) {
            pr.put(recentKEYS[j], recentFile[j]);
        }

        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(fileName));
            pr.exportNode(os);
        } catch (IOException ioEx) {
            if (verbose()) {
                System.err.println(ioEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString("IOException:_Cannot_save_preferences!"));
        } catch (BackingStoreException bsEx) {
            if (verbose()) {
                System.err.println(bsEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString(
                    "BackingStoreException:_Cannot_save_preferences!"));
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ioEx) {
                    if (verbose()) {
                        System.err.println(ioEx);
                    }
                }
            }
        }
    }

    /** Saves the four last visited files into the preferences file .Jaxorc. */
    public static void saveRecentFiles() {
        ResourceBundle lang =
            ResourceBundle.getBundle(getPref(JaxoPrefs.PREF_LANGUAGE));

        Preferences pr = Preferences.userRoot().node(JaxoDraw.USER_HOME);

        for (int j = 0; j < 4; j++) {
            pr.put(recentKEYS[j], recentFile[j]);
        }


        String fileName=jhplot.HPlotJa.PREFS_FILENAME;
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(fileName));
            pr.exportNode(os);
        } catch (IOException ioEx) {
            if (verbose()) {
                System.err.println(ioEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString("IOException:_Cannot_save_preferences!"));
        } catch (BackingStoreException bsEx) {
            if (verbose()) {
                System.err.println(bsEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString(
                    "BackingStoreException:_Cannot_save_preferences!"));
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ioEx) {
                    if (verbose()) {
                        System.err.println(ioEx);
                    }
                }
            }
        }
    }

    /** Imports the recently visited files from the .Jaxorc preferences file. */
    public static void readRecentFiles() {
        ResourceBundle lang =
            ResourceBundle.getBundle(getPref(JaxoPrefs.PREF_LANGUAGE));


        String fileName=jhplot.HPlotJa.PREFS_FILENAME;
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(fileName));
            Preferences.importPreferences(is);
        } catch (FileNotFoundException e) {
            if (verbose()) {
                System.err.println(e);
            }

            JOptionPane.showMessageDialog(null,
                fileName + lang.getString("_does_not_exist!"));
        } catch (InvalidPreferencesFormatException ipfEx) {
            if (verbose()) {
                System.err.println(ipfEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString("Unknown_preferences_file_format!"));
        } catch (IOException ioEx) {
            if (verbose()) {
                System.err.println(ioEx);
            }

            JOptionPane.showMessageDialog(null,
                lang.getString("IOException:_Cannot_open_preferences_file!"));
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioEx) {
                    if (verbose()) {
                        System.err.println(ioEx);
                    }
                }
            }
        }

        Preferences pr = Preferences.userRoot().node(JaxoDraw.USER_HOME);

        for (int j = 0; j < 4; j++) {
            recentFile[j] = pr.get(recentKEYS[j], recentFile[j]);
        }
    }

    
    
    public static void setSize(int sizeX, int sizeY) {
    	
    	String sx=Integer.toBinaryString(sizeX);
    	String sy=Integer.toBinaryString(sizeY);
    	
    //	JaxoPrefs.setPref(JaxoPrefs.PREF_SCREENSIZEX, sx);
    //    JaxoPrefs.setPref(JaxoPrefs.PREF_SCREENSIZEY, sy);

    	
    }
    
    
    
    private static int getArrayIndex(int i) {
        int k = -1;

        for (int j = 0; j < getNofPrefs(); j++) {
            if (PREFS[j] == i) {
                k = j;

                break;
            }
        }

        return k;
    }
}

 

Ads help maintain this website.