// $Id: DataDictionary.java,v 1.2 2004/04/26 05:29:53 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" 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.
//
// This program 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.

package com.meyling.principia.io;

import java.io.IOException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


/**
 * This class reads entries from property files and
 * gives typed get and set methods.
 *
 * @version $Revision: 1.2 $
 * @author  Michael Meyling
 */
public class DataDictionary {

    private final ResourceBundle bundle;

    /** the one and only instance. */
    private static DataDictionary instance = null;
    

    /**
     * Get instance of config access. Method {@link #setup} must have been called before.
     *
     * @return  singleton, which is responsible the config access
     * @throws  IllegalStateException   if {@link #setup} wasn't called before this method call
     */
    public final static DataDictionary getInstance() throws IllegalStateException {
        if (instance == null) {
            throw new IllegalStateException(DataDictionary.class.getName() 
                + " not initialized, call init before!");
        }
        return instance;
    }


    /**
     * Set resource name for this context. Must be called at first. Couldn't be called again.
     *
     * @param   baseName          name of resource file
     * @throws  IllegalStateException   {@link #setup} was called once before
     */
    public final static void init(final String baseName) throws IOException {
        if (instance != null) {
            throw new IllegalStateException(DataDictionary.class.getName() 
                + " is already initialized!");
        }
        synchronized (DataDictionary.class) {
            if (instance == null) {
                instance = new DataDictionary("com.meyling.principia.resources.pmii");
            } else {
                throw new IllegalStateException(DataDictionary.class.getName() 
                    + " is already initialized!");
            }
        }
    }


    /**
     * Don't use me outside of this class.
     */
    private DataDictionary(final String baseName) {
        bundle = ResourceBundle.getBundle(baseName);
    }


    /**
     * This method returns a string from the resource bundle.
     */
    public String getString(String key) {
        String value = null;
        try {
            value = getResourceBundle().getString(key);
        } catch (MissingResourceException e) {
            System.out.println("java.util.MissingResourceException: Couldn't find value for: " 
                + key);
        }
        if (value == null) {
            value = "Could not find resource: " + key + "  ";
        }
        return value;
    }


    /**
     * Returns the resource bundle associated with this application.
     * 
     * @return  resource bundle
     */
    public final ResourceBundle getResourceBundle() {
        return this.bundle;
    }


    /**
     * Returns a mnemonic from the resource bundle. Typically used as
     * keyboard shortcuts in menu items.
     */
    public final char getMnemonic(String key) {
        return (getString(key)).charAt(0);
    }




}
