📄 resourcebundlesupport.java
字号:
final String name = getString(key);
return createIcon(name, false, false);
}
/**
* Returns the mnemonic stored at the given resourcebundle key. The mnemonic should be
* either the symbolic name of one of the KeyEvent.VK_* constants (without the 'VK_') or
* the character for that key.
* <p/>
* For the enter key, the resource bundle would therefore either contain "ENTER" or
* "\n".
* <pre>
* a.resourcebundle.key=ENTER
* an.other.resourcebundle.key=\n
* </pre>
*
* @param key the resourcebundle key
* @return the mnemonic
*/
public Integer getMnemonic(final String key) {
final String name = getString(key);
return createMnemonic(name);
}
/**
* Returns the keystroke stored at the given resourcebundle key.
* <p/>
* The keystroke will be composed of a simple key press and the plattform's
* MenuKeyMask.
* <p/>
* The keystrokes character key should be either the symbolic name of one of the
* KeyEvent.VK_* constants or the character for that key.
* <p/>
* For the 'A' key, the resource bundle would therefore either contain "VK_A" or
* "a".
* <pre>
* a.resourcebundle.key=VK_A
* an.other.resourcebundle.key=a
* </pre>
*
* @param key the resourcebundle key
* @return the mnemonic
* @see Toolkit#getMenuShortcutKeyMask()
*/
public KeyStroke getKeyStroke(final String key) {
return getKeyStroke(key, getMenuKeyMask());
}
/**
* Returns the keystroke stored at the given resourcebundle key.
* <p/>
* The keystroke will be composed of a simple key press and the given
* KeyMask. If the KeyMask is zero, a plain Keystroke is returned.
* <p/>
* The keystrokes character key should be either the symbolic name of one of the
* KeyEvent.VK_* constants or the character for that key.
* <p/>
* For the 'A' key, the resource bundle would therefore either contain "VK_A" or
* "a".
* <pre>
* a.resourcebundle.key=VK_A
* an.other.resourcebundle.key=a
* </pre>
*
* @param key the resourcebundle key
* @return the mnemonic
* @see Toolkit#getMenuShortcutKeyMask()
*/
public KeyStroke getKeyStroke(final String key, final int mask) {
final String name = getString(key);
return KeyStroke.getKeyStroke(createMnemonic(name).intValue(), mask);
}
/**
* Returns a JMenu created from a resource bundle definition.
* <p/>
* The menu definition consists of two keys, the name of the menu and the mnemonic for
* that menu. Both keys share a common prefix, which is extended by ".name" for the name
* of the menu and ".mnemonic" for the mnemonic.
* <p/>
* <pre>
* # define the file menu
* menu.file.name=File
* menu.file.mnemonic=F
* </pre>
* The menu definition above can be used to create the menu by calling <code>createMenu
* ("menu.file")</code>.
*
* @param keyPrefix the common prefix for that menu
* @return the created menu
*/
public JMenu createMenu(final String keyPrefix) {
final JMenu retval = new JMenu();
retval.setText(getString(keyPrefix + ".name"));
retval.setMnemonic(getMnemonic(keyPrefix + ".mnemonic").intValue());
return retval;
}
/**
* Returns a URL pointing to a resource located in the classpath. The resource is looked
* up using the given key.
* <p/>
* Example: The load a file named 'logo.gif' which is stored in a java package named
* 'org.jfree.resources':
* <pre>
* mainmenu.logo=org/jfree/resources/logo.gif
* </pre>
* The URL for that file can be queried with: <code>getResource("mainmenu.logo");</code>.
*
* @param key the key for the resource
* @return the resource URL
*/
public URL getResourceURL(final String key) {
final String name = getString(key);
final URL in = ObjectUtilities.getResource(name, ResourceBundleSupport.class);
if (in == null) {
Log.warn("Unable to find file in the class path: " + name + "; key=" + key);
}
return in;
}
/**
* Attempts to load an image from classpath. If this fails, an empty image icon is
* returned.
*
* @param resourceName the name of the image. The name should be a global resource
* name.
* @param scale true, if the image should be scaled, false otherwise
* @param large true, if the image should be scaled to 24x24, or false for 16x16
* @return the image icon.
*/
private ImageIcon createIcon(final String resourceName, final boolean scale,
final boolean large) {
final URL in = ObjectUtilities.getResource(resourceName, ResourceBundleSupport.class);;
if (in == null) {
Log.warn("Unable to find file in the class path: " + resourceName);
return new ImageIcon(createTransparentImage(1, 1));
}
final Image img = Toolkit.getDefaultToolkit().createImage(in);
if (img == null) {
Log.warn("Unable to instantiate the image: " + resourceName);
return new ImageIcon(createTransparentImage(1, 1));
}
if (scale) {
if (large) {
return new ImageIcon(img.getScaledInstance(24, 24, Image.SCALE_SMOOTH));
}
return new ImageIcon(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH));
}
return new ImageIcon(img);
}
/**
* Creates the Mnemonic from the given String. The String consists of the name of the VK
* constants of the class KeyEvent without VK_*.
*
* @param keyString the string
* @return the mnemonic as integer
*/
private Integer createMnemonic(final String keyString) {
if (keyString == null) {
throw new NullPointerException("Key is null.");
}
if (keyString.length() == 0) {
throw new IllegalArgumentException("Key is empty.");
}
int character = keyString.charAt(0);
if (keyString.startsWith("VK_")) {
try {
final Field f = KeyEvent.class.getField(keyString);
final Integer keyCode = (Integer) f.get(null);
character = keyCode.intValue();
}
catch (Exception nsfe) {
// ignore the exception ...
}
}
return new Integer(character);
}
/**
* Returns the plattforms default menu shortcut keymask.
*
* @return the default key mask.
*/
private int getMenuKeyMask() {
try {
return Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
}
catch (UnsupportedOperationException he) {
// headless exception extends UnsupportedOperation exception,
// but the HeadlessException is not defined in older JDKs...
return InputEvent.CTRL_MASK;
}
}
/**
* Creates a transparent image. These can be used for aligning menu items.
*
* @param width the width.
* @param height the height.
* @return the created transparent image.
*/
private BufferedImage createTransparentImage(final int width, final int height) {
final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
Arrays.fill(data, 0x00000000);
img.setRGB(0, 0, width, height, data, 0, width);
return img;
}
/**
* Creates a transparent icon. The Icon can be used for aligning menu items.
*
* @param width the width of the new icon
* @param height the height of the new icon
* @return the created transparent icon.
*/
public Icon createTransparentIcon(final int width, final int height) {
return new ImageIcon(createTransparentImage(width, height));
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param parameter the parameter for the message
* @return the formated string
*/
public String formatMessage(final String key, final Object parameter) {
return formatMessage(getString(key), new Object[]{parameter});
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param par1 the first parameter for the message
* @param par2 the second parameter for the message
* @return the formated string
*/
public String formatMessage(final String key,
final Object par1,
final Object par2) {
return formatMessage(getString(key), new Object[]{par1, par2});
}
/**
* Formats the message stored in the resource bundle (using a MessageFormat).
*
* @param key the resourcebundle key
* @param parameters the parameter collection for the message
* @return the formated string
*/
public String formatMessage(final String key, final Object[] parameters) {
final MessageFormat format = new MessageFormat(getString(key));
format.setLocale(getLocale());
return format.format(parameters);
}
/**
* Returns the current locale for this resource bundle.
* @return the locale.
*/
public Locale getLocale() {
return locale;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -