📄 basici18n.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/BasicI18n.java,v $// $RCSfile: BasicI18n.java,v $// $Revision: 1.3.2.3 $// $Date: 2005/07/29 14:43:14 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.Serializable;import java.lang.reflect.Field;import java.text.MessageFormat;import java.util.Enumeration;import java.util.Hashtable;import java.util.Locale;import java.util.MissingResourceException;import java.util.Properties;import java.util.ResourceBundle;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.border.Border;import javax.swing.border.TitledBorder;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.FileUtils;/** * Basic implementation of internationalization support. * <P> * This class backs the I18n interface by I18n.property files that it * expects to find in each package. * <P> * This class provides a mechanism for creating the I18n.property * files that it expects to use in the application. If the * 'i18n.create' flag is set in the Debug class, then this class will * track what resource bundle property files were searched for, and * what values were returned for requested keys. The * dumpCreatedResourceBundles() method will cause the resource bundles * to be written out, where they can be modified to change the strings * displayed in the GUI. If the 'i18n.default' flag is set, then * 'I18N.properties' files will be created. If that flag is not set, * then the name of the files will be modified for the locale * currently set for the application (i.e. I18N_en_US.properties for * the US locale). You can add the I18nFileCreateMenuItem to an * application to trigger the dumpCreatedResourceBundles() method. */public class BasicI18n implements I18n, Serializable { /** * Debug string, 'i18n' */ public static final String DEBUG = "i18n"; /** * Debug string that causes the BasicI18n class to keep track of * resource property files and strings it looks for, as well as * their defaults. These files can be created by calling * dumpCreatedResourceBundles(). */ public static final String DEBUG_CREATE = "i18n.create"; /** * If set, created ResourceBundle files will not have the local * suffixes added to them. Otherwise, the locale set in I18n will * be used and appended. */ public static final String DEBUG_CREATE_DEFAULT = "i18n.default"; /** * All properties files containing string should be contained in a * file called I18N.properties. This string defines the I18N. */ public static final String ResourceFileNamePrefix = "I18N"; private Locale loc; protected transient Hashtable createHash = null; // Constructors: // ///////////// /** * Create a BasicI18n object from the default locale. */ public BasicI18n() { this(Locale.getDefault()); } /** * Create a BasicI18n object with given locale. */ public BasicI18n(Locale loc) { this.loc = loc; } public String get(Object requestor, String field, String defaultString) { return get(requestor, field, TEXT, defaultString); } public String get(Object requestor, String field, int type, String defaultString) { return get(requestor.getClass(), field, type, defaultString); } public String get(Class requestor, String field, String defaultString) { return get(requestor, field, TEXT, defaultString); } public String get(Class requestor, String field, int type, String defaultString) { String ret = getInternal(requestor, field, type); if (Debug.debugging(DEBUG_CREATE)) { setForBundleCreation(requestor, field, type, defaultString); } if (ret == null) { return defaultString; } return ret; } protected Hashtable getCreateHash() { if (createHash == null) { createHash = new Hashtable(); } return createHash; } // Methods making it easier to use MessageFormat: // ////////////////////////////////////////////// public String get(Object requestor, String field, String defaultString, Object param1) { return get(requestor.getClass(), field, TEXT, defaultString, new Object[] { param1 }); } public String get(Object requestor, String field, int type, String defaultString, Object param1) { return get(requestor.getClass(), field, type, defaultString, new Object[] { param1 }); } public String get(Class requestor, String field, String defaultString, Object param1) { return get(requestor, field, TEXT, defaultString, new Object[] { param1 }); } public String get(Class requestor, String field, int type, String defaultString, Object param1) { return get(requestor, field, type, defaultString, new Object[] { param1 }); } public String get(Object requestor, String field, String defaultString, Object param1, Object param2) { return get(requestor.getClass(), field, TEXT, defaultString, new Object[] { param1, param2 }); } public String get(Object requestor, String field, int type, String defaultString, Object param1, Object param2) { return get(requestor.getClass(), field, type, defaultString, new Object[] { param1, param2 }); } public String get(Class requestor, String field, String defaultString, Object param1, Object param2) { return get(requestor, field, TEXT, defaultString, new Object[] { param1, param2 }); } public String get(Class requestor, String field, int type, String defaultString, Object param1, Object param2) { return get(requestor, field, type, defaultString, new Object[] { param1, param2 }); } public String get(Object requestor, String field, String defaultString, Object[] params) { return get(requestor.getClass(), field, TEXT, defaultString, params); } public String get(Object requestor, String field, int type, String defaultString, Object[] params) { return get(requestor.getClass(), field, type, defaultString, params); } public String get(Class requestor, String field, String defaultString, Object[] params) { return get(requestor, field, TEXT, defaultString, params); } public String get(Class requestor, String field, int type, String defaultString, Object[] params) { return MessageFormat.format(get(requestor, field, type, defaultString), params); } // Methods fill setting the textual properties of common Swing // components: // /////////////////////////////////////////////////////////////////////// public void set(Object requestor, String field, JLabel comp) { set(requestor, field, (JComponent) comp); comp.setText(getTEXT(requestor, field, comp.getText())); comp.setDisplayedMnemonic(getMNEMONIC(requestor, field, comp.getDisplayedMnemonic(), false)); } public void set(Object requestor, String field, JButton comp) { set(requestor, field, (JComponent) comp); comp.setText(getTEXT(requestor, field, comp.getText())); comp.setMnemonic(getMNEMONIC(requestor, field, comp.getMnemonic(), false)); } public void set(Object requestor, String field, JMenu comp) { set(requestor, field, (JComponent) comp); comp.setText(getTEXT(requestor, field, comp.getText())); comp.setMnemonic(getMNEMONIC(requestor, field, comp.getMnemonic(), true)); } public void set(Object requestor, String field, JMenuItem comp) { set(requestor, field, (JComponent) comp); comp.setText(getTEXT(requestor, field, comp.getText())); comp.setMnemonic(getMNEMONIC(requestor, field, comp.getMnemonic(), true)); } public void set(Object requestor, String field, JDialog comp) { comp.setTitle(getTITLE(requestor, field, comp.getTitle())); } public void set(Object requestor, String field, JFrame comp) { comp.setTitle(getTITLE(requestor, field, comp.getTitle())); } public void set(Object requestor, String field, JComponent comp) { setTOOLTIP(requestor, field, comp); Border b = comp.getBorder(); if (b instanceof TitledBorder) { TitledBorder t = (TitledBorder) b; t.setTitle(getTITLE(requestor, field, t.getTitle())); } } // Methods for filling in strings using reflection: // //////////////////////////////////////////////// public void set(Object requestor, String field) { Class c = requestor.getClass(); Field f = null; try { f = c.getField(field); } catch (NoSuchFieldException e) { // We'll try again below. } catch (SecurityException e) { RuntimeException r = new MissingResourceException("SecurityException trying to reflect on field field", c.getName(), field); r.initCause(e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -