📄 resource.java
字号:
*
* @param key
* @param arg1
* @param arg2
* @param arg3
* @param arg4
* @return
*/
public static String getString(String key, Object arg1, Object arg2, Object arg3, Object arg4)
{
return MessageFormat.format(getString(key), new Object[]{arg1, arg2, arg3, arg4});
}
/**
* Gets a String from the resource and inserts five optional arguments.
*
* @param key
* @param arg1
* @param arg2
* @param arg3
* @param arg4
* @param arg5
* @return
*/
public static String getString(String key, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)
{
return MessageFormat.format(getString(key), new Object[]{arg1, arg2, arg3, arg4, arg5});
}
/**
* Sets a button's text and mnemonic values using the specified resource
* key. The button text is scanned for &. If found the character after it is
* used as menmonic.
*
* @param button
* the button (e.g. a menu or menu item) to localize
* @param key
* the resource string to find
*/
public static final void localize(AbstractButton button, String key) {
String text = getString(key);
int pos = text.indexOf('&');
if (pos != -1)
{
char mnemonic = text.charAt(pos+1);
button.setMnemonic(mnemonic);
text = text.substring(0, pos) + text.substring(pos+1);
}
button.setText(text);
}
/**
* Builds the Language Menu.
*
* @return JMenu
*/
public static JMenu buildLanguageMenu()
{
ActionListener listener = new LangListener();
JMenu langMenu = new JMenu();
localize(langMenu, "language.menu");
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem item_sys = new JRadioButtonMenuItem();
localize(item_sys, "language.system");
item_sys.addActionListener(listener);
item_sys.setSelected(locale == null);
item_sys.setActionCommand("system");
langMenu.add(item_sys);
group.add(item_sys);
langMenu.addSeparator();
Locale[] locales = getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
Locale item = locales[i];
JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(item.getLanguage());
menuItem.addActionListener(listener);
if (locale != null)
{
menuItem.setSelected(item.getLanguage().equals(locale.getLanguage()));
}
menuItem.setActionCommand(item.getLanguage());
langMenu.add(menuItem);
group.add(menuItem);
}
return langMenu;
}
/**
* Inner class LangListener. Handles the actions from the Language Menu.
*/
private static class LangListener implements ActionListener
{
/*
* (non-Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
if (action.equals("system"))
{
locale = null;
}
else
{
locale = new Locale(action, "", "");
}
JOptionPane.showMessageDialog(null, Resource.getString("msg.new.language"), Resource.getString("msg.infomessage"), JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* Returns the available Locales for pjxresources.
*
* @return Locale[]
*/
private static Locale[] getAvailableLocales() {
Set locales = new HashSet();
String defLang = Locale.getDefault().getLanguage();
try {
// we know we have an english resource, so first find this one to find the location
URL url = ClassLoader.getSystemResource(PJX_RESOURCE_PREFIX + "_en.properties");
if (url != null) {
URLConnection urlc = null;
urlc = url.openConnection();
// If the resources are located in a JAR file, we need this
// version to get the available locales.
if (urlc != null && urlc instanceof JarURLConnection) {
JarURLConnection jurlc = (JarURLConnection) urlc;
addAvailableLocalesFromJar(locales, jurlc);
}
// .. else if the resources are in the file system, we use the
// default version to get the available locales.
else {
File enFile = new File(url.getFile());
File dir = enFile.getParentFile();
addAvailableLocalesFromFileSystem(locales, dir);
}
} else {
System.err.println("Couldn't find \"" + PJX_RESOURCE_PREFIX
+ "\"*.properties");
}
// also look into the current working directory for additional resource files
File workDirFile = new File(workdir);
addAvailableLocalesFromFileSystem(locales, workDirFile);
} catch (Exception e) {
System.out.println(e);
}
return (Locale[])locales.toArray(new Locale[0]);
}
/**
* Adds available Locales from the file system.
*
* @param locales
* @param dir
*/
private static void addAvailableLocalesFromFileSystem(Set locales, File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile() && file.getName().startsWith(PJX_RESOURCE_PREFIX))
{
try {
String code = file.getName();
int pos = code.indexOf('_');
if (pos != -1) {
code = code.substring(pos + 1);
}
pos = code.indexOf('.');
if (pos != -1)
{
code = code.substring(0, pos);
}
Locale locale = new Locale(code, "");
locales.add(locale);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
}
/**
* Adds available Locales from a Jar file location
*
* @param locales
* @param jurlc
*/
private static void addAvailableLocalesFromJar(Set locales, JarURLConnection jurlc) {
JarFile jarf = null;
try {
jarf = jurlc.getJarFile();
} catch (Exception e) {
System.out.println(e);
}
if (jarf != null) {
for (Enumeration en = jarf.entries(); en.hasMoreElements();) {
JarEntry jare = (JarEntry) en.nextElement();
String name = jare.getName();
if (name.startsWith(PJX_RESOURCE_PREFIX)) {
String code = name.substring(0,
name.length() - ".properties".length());
int pos = code.indexOf('_');
if (pos != -1) {
code = code.substring(pos + 1);
}
pos = code.indexOf('.');
if (pos != -1)
{
code = code.substring(0, pos);
}
Locale locale = new Locale(code, "");
locales.add(locale);
}
}
}
}
/**
* Returns a resource (e.g. from the jar file) as an URL.
*
* @param resource the name of the resource
* @return URL
*/
public static URL getResourceURL(String resource)
{
try
{
String filename = workdir + filesep + resource;
File file = new File(filename);
if (file.exists() && file.canRead())
{
return file.toURL();
}
}
catch(Exception e)
{
// ignore it, it was just a try to get this resource from the filesystem
}
// for the classloader we need to replace all backslashes to forward slashes.
// this is only necessary on windows systems and doesn't harm others
resource = resource.replace('\\', '/');
// ok, not founde in the filesystem, now try the classloader
return Resource.class.getClassLoader().getResource(resource);
}
/**
* Returns a resource (e.g. from the jar file) as an URL.
*
* @param resourceName the name of the resource
* @return URL
*/
public static URL getLocalizedResourceURL(String path, String resourceName)
{
Locale usedLocale = null;
if (locale != null)
{
usedLocale = locale;
}
else
{
usedLocale = Locale.getDefault();
}
String localizedResource = path + filesep + usedLocale.getLanguage() + filesep + resourceName;
URL url = getResourceURL(localizedResource);
if (url != null)
{
return url;
}
// there is no localized version of this file, try the default version
return getResourceURL(path + filesep + resourceName);
}
/**
* Loads an image as ImageIcon.
*
* @param iconName
* @return ImageIcon
*/
public static ImageIcon loadIcon(String iconName)
{
return new ImageIcon(getResourceURL(iconName));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -