001: /*
002: * ResourceMgr.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.resource;
013:
014: import java.awt.event.KeyEvent;
015: import java.io.File;
016: import java.io.InputStream;
017: import java.net.URL;
018: import java.text.MessageFormat;
019: import java.text.SimpleDateFormat;
020: import java.util.Locale;
021: import java.util.MissingResourceException;
022: import java.util.ResourceBundle;
023:
024: import javax.swing.ImageIcon;
025:
026: import workbench.log.LogMgr;
027: import workbench.util.NumberStringCache;
028: import workbench.util.StringUtil;
029: import workbench.util.VersionNumber;
030:
031: /**
032: * @author support@sql-workbench.net.kellerer
033: */
034: public class ResourceMgr {
035: public static final String TXT_PRODUCT_NAME = "SQL Workbench/J";
036:
037: public static final String TXT_OK = "LblOK";
038: public static final String TXT_CANCEL = "LblCancel";
039:
040: public static final String IMG_SAVE = "Save";
041:
042: public static final String MNU_TXT_WORKSPACE = "MnuTxtWorkspace";
043: public static final String MNU_TXT_FILE = "MnuTxtFile";
044: public static final String MNU_TXT_MACRO = "MnuTxtMacro";
045: public static final String MNU_TXT_SQL = "MnuTxtSQL";
046: public static final String MNU_TXT_EDIT = "MnuTxtEdit";
047: public static final String MNU_TXT_DATA = "MnuTxtData";
048: public static final String MNU_TXT_COPY_SELECTED = "MnuTxtCopySelected";
049:
050: public static final String MNU_TXT_VIEW = "MnuTxtView";
051: public static final String MNU_TXT_TOOLS = "MnuTxtTools";
052: public static final String MNU_TXT_HELP = "MnuTxtHelp";
053: public static final String MNU_TXT_OPTIONS = "MnuTxtOptions";
054: private static ResourceBundle resources;
055:
056: private ResourceMgr() {
057: }
058:
059: public static String getBuildInfo() {
060: return getString("TxtBuild") + " "
061: + getBuildNumber().toString() + " ("
062: + getString("TxtBuildDate") + ")";
063: }
064:
065: public static String replaceModifierText(String msg) {
066: msg = StringUtil.replace(msg, "%shift%", KeyEvent
067: .getKeyModifiersText(KeyEvent.SHIFT_MASK));
068: msg = StringUtil.replace(msg, "%control%", KeyEvent
069: .getKeyModifiersText(KeyEvent.CTRL_MASK));
070: return msg;
071: }
072:
073: public static java.util.Date getBuildDate() {
074: String builddate = getString("TxtBuildDate");
075: // running from the dev environment --> build date is now!
076: if ("@BUILD_DATE@".equals(builddate))
077: return new java.util.Date();
078: SimpleDateFormat format = new SimpleDateFormat(
079: "yyyy-MM-dd HH:mm");
080: java.util.Date result = null;
081: try {
082: result = format.parse(builddate);
083: } catch (Exception e) {
084: LogMgr.logError("ResourceMgr.getBuildDate()",
085: "Err when parsing build date!", e);
086: result = new java.util.Date();
087: }
088: return result;
089: }
090:
091: public static boolean isDevBuild() {
092: String nr = getString("TxtBuildNumber");
093: char c = nr.charAt(0);
094: return (c == '[' || c == '@');
095: }
096:
097: public static VersionNumber getBuildNumber() {
098: String nr = getString("TxtBuildNumber");
099: return new VersionNumber(nr);
100: }
101:
102: public static String getDefaultTabLabel() {
103: return getString("LblTabStatement");
104: }
105:
106: public static String getFormattedString(String key, int value1) {
107: return MessageFormat.format(getString(key), NumberStringCache
108: .getNumberString(value1));
109: }
110:
111: public static String getFormattedString(String key, int value1,
112: int value2) {
113: return MessageFormat.format(getString(key), NumberStringCache
114: .getNumberString(value1), NumberStringCache
115: .getNumberString(value2));
116: }
117:
118: public static String getFormattedString(String key,
119: Object... values) {
120: return MessageFormat.format(getString(key), values);
121: }
122:
123: public static String getString(String aKey) {
124: return getString(aKey, false);
125: }
126:
127: public static String getString(String aKey, boolean replaceModifiers) {
128: try {
129: String value = getResources().getString(aKey);
130: if (replaceModifiers) {
131: return replaceModifierText(value);
132: }
133: return value;
134: } catch (MissingResourceException e) {
135: LogMgr.logWarning("ResourceMgr", "String with key=" + aKey
136: + " not found in resource file!", e);
137: return aKey;
138: }
139: }
140:
141: public static String getPlainString(String aKey) {
142: String value = getString(aKey).replaceAll("\\&", "");
143: return value;
144: }
145:
146: public static String getAcceleratorChar(String aKey) {
147: try {
148: String label = getString(aKey);
149: int pos = label.indexOf('&');
150: if (pos == -1)
151: return null;
152:
153: char c = label.charAt(pos + 1);
154: StringBuilder b = new StringBuilder(1);
155: b.append(c);
156: return b.toString();
157: } catch (MissingResourceException e) {
158: return null;
159: } catch (Exception e) {
160: return null;
161: }
162: }
163:
164: public static String getDescription(String aKey) {
165: return getDescription(aKey, false);
166: }
167:
168: /**
169: * Returns the description associcate with the given key.
170: * This is used for Tooltips which are associated with a
171: * certain menu text etc.
172: */
173: public static String getDescription(String aKey,
174: boolean replaceModifiers) {
175: String value = getString("d_" + aKey);
176: if (replaceModifiers) {
177: value = replaceModifierText(value);
178: }
179: return value;
180: }
181:
182: public static InputStream getDefaultSettings() {
183: InputStream in = ResourceMgr.class
184: .getResourceAsStream("default.properties");
185:
186: return in;
187: }
188:
189: public static ImageIcon getBlankImage() {
190: return retrieveImage("blank16", ".gif");
191: }
192:
193: public static ImageIcon getImage(String aKey) {
194: return retrieveImage(aKey + "16", ".gif");
195: }
196:
197: public static ImageIcon getPicture(String aName) {
198: return retrieveImage(aName, ".gif");
199: }
200:
201: public static ImageIcon getPng(String aName) {
202: return retrieveImage(aName, ".png");
203: }
204:
205: private static ImageIcon retrieveImage(String filename,
206: String extension) {
207: URL imageIconUrl = ResourceMgr.class.getClassLoader()
208: .getResource(
209: "workbench/resource/images/" + filename
210: + extension);
211: if (imageIconUrl != null) {
212: return new ImageIcon(imageIconUrl);
213: } else {
214: imageIconUrl = ResourceMgr.class.getClassLoader()
215: .getResource(filename);
216: if (imageIconUrl != null) {
217: return new ImageIcon(imageIconUrl);
218: }
219: }
220: return null;
221: }
222:
223: /**
224: * For testing purposes
225: */
226: static ResourceBundle getResourceBundle(Locale l) {
227: return ResourceBundle.getBundle("language/wbstrings", l);
228: }
229:
230: public static ResourceBundle getResources() {
231: if (resources == null) {
232: Locale l = Settings.getInstance().getLanguage();
233: resources = getResourceBundle(l);
234: boolean setDefaultLocale = Settings.getInstance()
235: .getBoolProperty("workbench.gui.setdefaultlocale",
236: true);
237: if (setDefaultLocale) {
238: Locale.setDefault(l);
239: }
240: }
241: return resources;
242: }
243:
244: }
|