📄 basici18n.java
字号:
throw r; } if (f == null) { try { f = c.getDeclaredField(field); } catch (NoSuchFieldException e) { RuntimeException r = new MissingResourceException("Can't find field via reflection", c.getName(), field); r.initCause(e); throw r; } catch (SecurityException e) { RuntimeException r = new MissingResourceException("SecurityException trying to reflect on field field", c.getName(), field); r.initCause(e); throw r; } } // Try to set it accessible: try { f.setAccessible(true); } catch (SecurityException e) { Debug.message(DEBUG, "Coudn't set field " + field + " accessible"); } // Ok, now try to get the data: Class type = f.getType(); Object fd = null; try { fd = f.get(requestor); } catch (IllegalArgumentException e) { RuntimeException r = new MissingResourceException("Couldn't get field", c.getName(), field); r.initCause(e); throw r; } catch (IllegalAccessException e) { RuntimeException r = new MissingResourceException("Couldn't access field", c.getName(), field); r.initCause(e); throw r; } // Now do the calls: if (JLabel.class.isInstance(type)) { set(requestor, field, (JLabel) fd); } else if (JButton.class.isInstance(type)) { set(requestor, field, (JButton) fd); } else if (JMenu.class.isInstance(type)) { set(requestor, field, (JMenu) fd); } else if (JMenuItem.class.isInstance(type)) { set(requestor, field, (JMenuItem) fd); } else if (JDialog.class.isInstance(type)) { set(requestor, field, (JDialog) fd); } else if (JFrame.class.isInstance(type)) { set(requestor, field, (JFrame) fd); } else if (JComponent.class.isInstance(type)) { set(requestor, field, (JComponent) fd); } else { Debug.message(DEBUG, "Couldn't assign data for unknown type: " + type); } } public void fill(Object requestor) { } // // // // Implemenation Methods: // ///////////////////////// // ///////////////////////// /** * Set a tooltip on the given component if it has one. */ protected void setTOOLTIP(Object requestor, String field, JComponent comp) { String tooltip = get(requestor, field, TOOLTIP, comp.getToolTipText()); if (tooltip != null) { comp.setToolTipText(tooltip); } else { Debug.message(DEBUG, "No tooltip for: " + getKeyRef(requestor, field)); } } /** * Get text for the given component. */ protected String getTEXT(Object requestor, String field, String defaultString) { String text = get(requestor, field, TEXT, defaultString); if (text == null) { throw new MissingResourceException("No TEXT resource", requestor.getClass() .getName(), field); } return text; } /** * Get title for the given component. */ protected String getTITLE(Object requestor, String field, String defaultString) { String title = get(requestor, field, TITLE, defaultString); if (title == null) { throw new MissingResourceException("No TITLE resource", requestor.getClass() .getName(), field); } return title; } /** * Get text for the given component. */ protected int getMNEMONIC(Object requestor, String field, int defaultInt, boolean verbose) { String mn = get(requestor, field, MNEMONIC, Character.toString((char) defaultInt)); if (mn == null) { if (verbose) { Debug.message(DEBUG, "No MNEMONIC resource for " + getKeyRef(requestor, field)); } return defaultInt; } // Now parse this string into something useful: // For now, don't deal with virtual keys, though that is an // obvious // extension: return Character.getNumericValue(mn.charAt(0)); } /** * Obtain a String from the underlying data for the given * class/field/type. * * @return null if the data can't be found -- defaulting happens * above here. */ protected String getInternal(Class requestor, String field, int type) { ResourceBundle bundle = null; Package pckg = requestor.getPackage(); String bString = (pckg == null ? "" : (requestor.getPackage().getName() + ".")) + ResourceFileNamePrefix; try { bundle = ResourceBundle.getBundle(bString, loc); } catch (MissingResourceException e) { Debug.message(DEBUG, "Could not locate resource: " + bString.replace('.', '/') + ".properties"); return null; } String key = shortClassName(requestor) + "." + field; switch (type) { case TEXT: // Do nothing. break; case TITLE: key += ".title"; break; case TOOLTIP: key += ".tooltip"; break; case MNEMONIC: key += ".mnemonic"; break; } try { return bundle.getString(key); } catch (MissingResourceException e) { Debug.message(DEBUG, "Could not locate string in resource: " + (pckg == null ? "" : (requestor.getPackage().getName().replace('.', '/') + ".")) + "properties for key: " + key); return null; } } /** * Obtain a String from the underlying data for the given * class/field/type. * * @return null if the data can't be found -- defaulting happens * above here. */ protected void setForBundleCreation(Class requestor, String field, int type, String defaultString) { ResourceBundle bundle = null; Package pckg = requestor.getPackage(); String bString = (pckg == null ? "" : (requestor.getPackage().getName() + ".")) + ResourceFileNamePrefix; String propertyFileNameKey = null; Properties propertyFileProperties = null; // OK, first see what the property file should be. StringBuffer sbuf = new StringBuffer(bString.replace('.', '/')); // Scope the desired resource bundle property file if the // default isn't wanted. if (!Debug.debugging(DEBUG_CREATE_DEFAULT)) { sbuf.append("_" + loc.toString()); } sbuf.append(".properties"); propertyFileNameKey = sbuf.toString().intern(); // See if we already have a properties file with the key-value // pairs for this particular resource bundle. propertyFileProperties = (Properties) getCreateHash().get(propertyFileNameKey); // If not, create it. if (propertyFileProperties == null) { propertyFileProperties = new Properties(); getCreateHash().put(propertyFileNameKey, propertyFileProperties); } try { bundle = ResourceBundle.getBundle(bString, loc); } catch (MissingResourceException e) { } String resourceKey = shortClassName(requestor) + "." + field; switch (type) { case TEXT: // Do nothing. break; case TITLE: resourceKey += ".title"; break; case TOOLTIP: resourceKey += ".tooltip"; break; case MNEMONIC: resourceKey += ".mnemonic"; break; } try { if (bundle != null) { String resourceValue = bundle.getString(resourceKey); propertyFileProperties.put(resourceKey, resourceValue); } else { propertyFileProperties.put(resourceKey, defaultString); } } catch (MissingResourceException e) { propertyFileProperties.put(resourceKey, defaultString); } } /** * If called, will bring up a JFileChooser so you can pick a * location to dump the resource files that were searched for, * with values that were found or the defaults provided in the * code. This will only do something if the BasicI18n class was * accessed with the 'i18n.create' flag set in the Debug class. */ public void dumpCreatedResourceBundles() { String location = FileUtils.getFilePathToSaveFromUser("Choose Location"); if (location != null) { dumpCreatedResourceBundles(location); } } /** * Will dump the resource bundle property files searched for by * this class, along with the contents that were returned or with * the defaults provided by the application. * * @param location directory location to use as the root for * resource bundle file hierarchy. */ public void dumpCreatedResourceBundles(String location) { Hashtable bundleHash = getCreateHash(); for (Enumeration enumeration = bundleHash.keys(); enumeration.hasMoreElements();) { String key = (String) enumeration.nextElement(); Properties props = (Properties) bundleHash.get(key); try { File propFile = new File(location + "/" + key); File parentDir = new File(propFile.getParent()); parentDir.mkdirs(); propFile.createNewFile(); FileOutputStream fos = new FileOutputStream(propFile); props.store(fos, "I18N Resource File"); fos.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private static String shortClassName(Class c) { String name = c.getName(); return name.substring(name.lastIndexOf(".") + 1); } private static String getKeyRef(Object requestor, String field) { return requestor.getClass().getName() + "." + field; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -