001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.gui.se.util;
017
018 import java.util.MissingResourceException;
019 import java.util.ResourceBundle;
020
021 import org.qedeq.base.trace.Trace;
022
023
024 /**
025 * This class reads entries from property files and
026 * gives typed get and set methods.
027 *
028 * @version $Revision: 1.5 $
029 * @author Michael Meyling
030 */
031 public final class DataDictionary {
032
033 /** This class. */
034 private static final Class CLASS = DataDictionary.class;
035
036 /** Resource access. */
037 private final ResourceBundle bundle;
038
039 /** The one and only instance. */
040 private static DataDictionary instance = null;
041
042 /**
043 * Get instance of config access. Method {@link #setup} must have been called before.
044 *
045 * @return singleton, which is responsible the config access
046 * @throws IllegalStateException if {@link #setup} wasn't called before this method call
047 */
048 public static DataDictionary getInstance() {
049 if (instance == null) {
050 throw new IllegalStateException(DataDictionary.class.getName()
051 + " not initialized, call init before!");
052 }
053 return instance;
054 }
055
056 /**
057 * Set resource name for this context. Must be called at first. Couldn't be called again.
058 *
059 * @param baseName name of resource file
060 * @throws IllegalStateException {@link #setup} was called once before
061 */
062 public static void init(final String baseName) {
063 if (instance != null) {
064 throw new IllegalStateException(DataDictionary.class.getName()
065 + " is already initialized!");
066 }
067 synchronized (DataDictionary.class) {
068 if (instance == null) {
069 instance = new DataDictionary("org.qedeq.gui.se.util");
070 } else {
071 throw new IllegalStateException(DataDictionary.class.getName()
072 + " is already initialized!");
073 }
074 }
075 }
076
077 /**
078 * Don't use me outside of this class.
079 *
080 * @param baseName Get bundle for this name.
081 */
082 private DataDictionary(final String baseName) {
083 bundle = ResourceBundle.getBundle(baseName);
084 }
085
086 /**
087 * This method returns a string from the resource bundle.
088 *
089 * @param key Key.
090 * @return String value.
091 */
092 public String getString(final String key) {
093 String value = null;
094 try {
095 value = getResourceBundle().getString(key);
096 } catch (MissingResourceException e) {
097 Trace.fatal(CLASS, this, "getString", "Couldn't find value for: "
098 + key, e);
099 }
100 if (value == null) {
101 value = "Could not find resource: " + key + " ";
102 }
103 return value;
104 }
105
106 /**
107 * Returns the resource bundle associated with this application.
108 *
109 * @return resource bundle
110 */
111 public final ResourceBundle getResourceBundle() {
112 return this.bundle;
113 }
114
115 /**
116 * Returns a mnemonic from the resource bundle. Typically used as
117 * keyboard shortcuts in menu items.
118 *
119 * @param key Key.
120 * @return Mnemonic for key.
121 */
122 public final char getMnemonic(final String key) {
123 return (getString(key)).charAt(0);
124 }
125
126 }
|