|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.Resources
public class Resources
A standard way to associate resource
bundles with classes.
Often when creating GUI components, it is desirable to have the
text string and messages which appear to come from a resource bundle. This class helps to facilitate this.
To use this class, you need to follow a certain convention with the design of your objects. The basic convention is if you create a "x.y.Class", there should be a "x.y.msgs.Class" property file to go with it.
For example, lets say you were working on a class called:
"com.ccg.swing.Foo" and the source file was located at:
"$HOME/com/ccg/swing/Foo.java", the resource file to associate with
this class should then exist as:
"$HOME/com/ccg/swing/msgs/Foo.properties". You can create different
variants of the Foo resources for the languages you want to support
(if you understand how resource bundles
work).
If you follow this procedure, then the following code fragment could be used to load the resource bundle for your class:
ResourceBundlerb = com.ccg.swing.Resources.getBundle(com.ccg.swing.Foo.class);
| Constructor Summary | |
|---|---|
Resources(Class cl)
Create a object with a assoicated ResourceBundle. |
|
Resources(Lookup l)
Create a resource repository using a lookup object. |
|
Resources(ResourceBundle rb)
Create a object with a assoicated ResourceBundle. |
|
| Method Summary | |
|---|---|
JComponent |
createButtonGrid(String tag,
Object callback)
Creates an entire "grid" of buttons AND registers callbacks. |
static JComponent |
createButtonGrid(TagLookup tl,
Object callback,
ImageHolder ih)
Creates an entire "grid" of buttons AND registers callbacks. |
JDialog |
createDialog(String tag,
JComponent top,
Component owner,
Object callback)
Creates a JDialog box from resources. |
JPanel |
createLabeledFields(String tag,
JComponent[] fields)
Layout a set of components with labels from our resources. |
static ResourceBundle |
getBundle(Class cl)
Fetch a resource bundle that is
associated with the class. |
static ResourceBundle |
getBundle(Class cl,
Locale locale)
Fetch a resource bundle that is
associated with the class. |
static ResourceBundle |
getBundle(Class cl,
Locale locale,
ClassLoader loader)
Fetch a resource bundle that is
associated with the class. |
static String |
getBundleName(Class c)
Determine the resource bundle name to associate with a class. |
ImageHolder |
getImageHolder()
Get the ImageHolder used to fetch image data from. |
static int |
getKeyCode(String s)
Convert a resource string to a keycode mnemonic. |
static Object |
getObject(ResourceBundle rb,
String key,
Object def)
Safely retrieve a Object. |
static String |
getString(ResourceBundle rb,
String key,
String def)
Safely retrieve a String. |
String |
getString(String keyName)
Fetch a string resource by key value. |
String |
getString(String keyName,
String defValue)
Fetch a string resource by key value. |
static TagLookup |
getTagLookup(ResourceBundle rb,
String tag)
Get a TagLookup object from your ResourceBundle. |
TagLookup |
getTagLookup(String tag)
Get a TagLookup object for a particular resource. |
void |
setButtonValues(String tag,
AbstractButton b)
Set the properties for a JButton object. |
static void |
setButtonValues(TagLookup tl,
AbstractButton b,
ImageHolder ih)
Set the properties for a JButton object. |
void |
setFrameValues(String tag,
JFrame jf)
Set the common basic properties shared by all JFrames. |
static void |
setFrameValues(TagLookup tl,
JFrame jf,
ImageHolder ih)
Set the common basic properties shared by all JFrames. |
void |
setImageHolder(ImageHolder val)
Set the ImageHolder used to fetch image data from. |
void |
setMenuItemValues(String tag,
JMenuItem jc)
Set the common basic properties shared by all JComponents. |
static void |
setMenuItemValues(TagLookup tl,
JMenuItem mi,
ImageHolder ih)
Set the common basic properties shared by all JComponents. |
void |
setValues(String tag,
JComponent jc)
Set the common basic properties shared by all JComponents. |
static void |
setValues(TagLookup tl,
JComponent jc)
Set the common basic properties shared by all JComponents. |
int |
showConfirmDialog(Component parent,
Object contents,
String tag,
Object[] args,
int optionType)
Present a confirmation dialog to the user and wait for user response. |
String |
showInputDialog(Component parent,
String tag,
Object[] args)
Present a input dialog to the user and wait for user response. |
void |
showMessage(Component parent,
String tag,
Object[] args)
Present a message to the user and wait for them to acknowledge it. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Resources(Lookup l)
lookup object.
l - Standard Lookup object used to retreive
resource information from.getTagLookup(java.util.ResourceBundle, java.lang.String)public Resources(Class cl)
ResourceBundle.
Use this method to create a resource
collection using a standard ResourceBundle which is associated with a specific class.
For example, if your class were: com.ccg.swing.ColorChoices, then you could invoke this method via:
Resources r = new Resources(com.ccg.swing.ColorChoices.class);
NOTE: To use the example shown above, the resource file "com/ccg/swing/msgs/ColorChoices.properties" must exist in the CLASSPATH.
cl - The class which you want to load the resources for - must not
be null.getBundle(Class)public Resources(ResourceBundle rb)
ResourceBundle.
Use this method to create a resource
collection using a standard ResourceBundle from which to pull information from.
This constructor allows one to use ANY resource bundle object.
rb - The resources to use - must not be null.getBundle(Class)| Method Detail |
|---|
public static ResourceBundle getBundle(Class cl,
Locale locale,
ClassLoader loader)
resource bundle that is
associated with the class.
cl - The class you want to retrieve the resources for (must not be
null).locale - The locale you want to retreive the resource bundle for.loader - The class loader to use to retreive the resources.ResourceBundle.getBundle(String,Locale,ClassLoader),
getBundleName(java.lang.Class)
public static ResourceBundle getBundle(Class cl,
Locale locale)
resource bundle that is
associated with the class.
cl - The class you want to retrieve the resources for (must not be
null).locale - The locale you want to retreive the resource bundle for.ResourceBundle.getBundle(String,Locale),
getBundleName(java.lang.Class)public static ResourceBundle getBundle(Class cl)
resource bundle that is
associated with the class.
cl - The class you want to retrieve the resources for (must not be
null).ResourceBundle.getBundle(String),
getBundleName(java.lang.Class)public static String getBundleName(Class c)
This method takes a class name and changes it into a string in the form where we expect the resource property files to exist. For example, if you passed this method 'com.ccg.swing.Resources', it will return a string of "com.ccg.swing.msgs.Resources" - you can try this out yourself with the below sample program:
import com.ccg.swing.*;
public class T {
static public void main(String args[]) {
System.out.println(Resources.getBundleName(Resources.class));
}
}
The ".msgs" prefix was chosen to be consistent with another implementation of this feature.
class - The class you want to fetch the name of the associated
resource bundle for (must not be null)
getBundle(Class)
public static String getString(ResourceBundle rb,
String key,
String def)
String.
This method will fetch a string from a resource bundle without throwing an
exception. Normally, if you request a string from a resource
bundle, you get a MissingResource exception if what you want isn't found. This
method catches this exception and returns your default value if
what you want isn't found.
rb - The ResourceBundle to fetch the string
value from (must not be null).key - The key of the resource you want to retrieve (must not be null).def - The default value to return (if resource doesn't have what you
want). This can be null if you want.
getObject(java.util.ResourceBundle, java.lang.String, java.lang.Object)
public static Object getObject(ResourceBundle rb,
String key,
Object def)
Object.
This method will fetch a object from a resource bundle without throwing an
exception. Normally, if you request a object from a resource
bundle, you get a MissingResource exception if what you want isn't found. This
method catches this exception and returns your default value if
what you want isn't found.
rb - The ResourceBundle to fetch the object
value from (must not be null).key - The key of the resource you want to retrieve (must not be null).def - The default value to return (if resource doesn't have what you
want). This can be null if you want.
getObject(java.util.ResourceBundle, java.lang.String, java.lang.Object)
public static TagLookup getTagLookup(ResourceBundle rb,
String tag)
TagLookup object from your ResourceBundle.
It can often be easier to work with a TagLookup object instead of a ResourceBundle. The main advantages include:
rb - The underlying ResourceBundle you want
to retrieve your values from.tag - The "tag" identifier you would like to prefixed each "key"
value you specify with (you can pass null if a prefix doesn't
help you).
TagLookup object.TagLookup
public static void setValues(TagLookup tl,
JComponent jc)
JComponents.
This method will search the TagLookup object
specified (probably created from the getTagLookup method) for the "tagged" properties and then set
the components properties. The following properties are
recognized:
tl - The TagLookup object to use for fetching values.jc - The JComponent to be affected.getTagLookup(java.util.ResourceBundle, java.lang.String)
public void setValues(String tag,
JComponent jc)
JComponents.
This method will apply various properties to a JComponent which are identified with a particular TAG
in the corresponding set of resources.. The following properties
are recognized:
tag - The TAG identifier for the component to be updated (used to
retrieve values mentioned above). null is OK if no TAG is used
in the resource file.jc - The JComponent to be affected.
public static void setMenuItemValues(TagLookup tl,
JMenuItem mi,
ImageHolder ih)
JComponents.
This method will search the TagLookup object
specified (probably created from the getTagLookup method) for the "tagged" properties and then set
the components properties. The following properties are
recognized:
ImageHolder).
tl - The TagLookup object to use for fetching values.jc - The JComponent to be affected.getTagLookup(java.util.ResourceBundle, java.lang.String)
public void setMenuItemValues(String tag,
JMenuItem jc)
JComponents.
This method will apply various properties to a JMenuItem object which are identified with a
particular TAG in the corresponding set of resources. The
following properties are recognized:
ImageHolder).
tag - The TAG identifier for the component to be updated (used to
retrieve values mentioned above). null is OK if no TAG is used
in the resource file.jc - The JComponent to be affected.
public static void setFrameValues(TagLookup tl,
JFrame jf,
ImageHolder ih)
JFrames.
This method will search the TagLookup object
specified (probably created from the getTagLookup method) for the "tagged" properties and then set
the components properties. The following properties are
recognized:
NOTE: Both width/height must be specified for either of them to be applied.
tl - The TagLookup object to use for fetching values.jf - The JFrame to be affected.ih - This is optional, if you pass a non-null value here, we will
try and look for the image icons specified.getTagLookup(java.util.ResourceBundle, java.lang.String)
public void setFrameValues(String tag,
JFrame jf)
JFrames.
This method will set certain properties for a JFrame object. The following properties are recognized:
tag - The TAG identifier for the frame (used to retrieve values
mentioned above). null is OK if no TAG is used in the resource
file.jf - The JFrame to be affected.
public static void setButtonValues(TagLookup tl,
AbstractButton b,
ImageHolder ih)
JButton object.
This method will search the TagLookup object
specified (probably created from the getTagLookup method) for the "tagged" properties and then set
the buttons properties. The following properties are
recognized:
setValues(com.ccg.util.TagLookup, javax.swing.JComponent) may be specified (like colors, fonts, etc).
ImageHolder.ImageHolder.ImageHolder.ImageHolder.
tl - The TagLookup object to use for fetching values.jc - The JComponent to be affected.ih - This is optional, if you pass a non-null value here, we will
try and look for the image icons specified.getTagLookup(java.util.ResourceBundle, java.lang.String)
public void setButtonValues(String tag,
AbstractButton b)
JButton object.
This method will search the TagLookup object
specified (probably created from the getTagLookup method) for the "tagged" properties and then set
the buttons properties. The following properties are
recognized:
setValues(com.ccg.util.TagLookup, javax.swing.JComponent) may be specified (like colors, fonts, etc).
ImageHolder.ImageHolder.ImageHolder.ImageHolder.
tag - The "TAG" which identifies the button in the assoiciated resource properties.jc - The button to be affected.
public static JComponent createButtonGrid(TagLookup tl,
Object callback,
ImageHolder ih)
callbacks.
This method will build an entire grid of buttons from a resource file and install the necessary callbacks so that the buttons will invoke various methods on your object. See the MyClass.java and msgs/MyClass.properties for a working example of how to do this.
Basically, it boils down to:
called back when the button is pressed for each
button that is to appear.
attributes
for each button (at a minimum, you'll probably want to specify
the "TAG.N.text" attribute).
tl - Where to look for the definitions that define the button grid
and the buttons which populate it.callback - The object which has the methods the buttons are allowed to
invoke when pressed (see ActionCallback.create(com.ccg.util.Lookup, javax.swing.AbstractButton, java.lang.Object) for
more details - or check out the example mentioned above).ih - A ImageHolder to retrieve icon images for
the buttons (when this becomes supported).
setButtonValues(com.ccg.util.TagLookup, javax.swing.AbstractButton, com.ccg.awt.ImageHolder)
public JComponent createButtonGrid(String tag,
Object callback)
callbacks.
This method will build an entire grid of buttons from a resource file and install the necessary callbacks so that the buttons will invoke various methods on your object. See the MyClass.java and msgs/MyClass.properties for a working example of how to do this.
Basically, it boils down to:
called back when the button is pressed for each
button that is to appear.
attributes
for each button (at a minimum, you'll probably want to specify
the "TAG.N.text" attribute).
tag - The "TAG" which identifies the button in the associated resource properties.callback - The object which has the methods the buttons are allowed to
invoke when pressed (see ActionCallback.create(com.ccg.util.Lookup, javax.swing.AbstractButton, java.lang.Object) for
more details - or check out the example mentioned above).
setButtonValues(String,AbstractButton)public static int getKeyCode(String s)
Some components allow one to assign a keyboard "mnemonic", this method is used to convert the string values (which might appear in a resource file) into their respective mnemonic value).
s - String to convert into a key code mnemonic
public TagLookup getTagLookup(String tag)
TagLookup object for a particular resource.
tag - The "tag" prefix to use when fetching information (or null if
you don't want a tag prefix).
TagLookup object which you can use to retrieve
information with.public void setImageHolder(ImageHolder val)
ImageHolder used to fetch image data from.
val - New ImageHolder value to assign.getImageHolder()public ImageHolder getImageHolder()
ImageHolder used to fetch image data from.
setImageHolder(com.ccg.awt.ImageHolder)
public JPanel createLabeledFields(String tag,
JComponent[] fields)
This method provides a handy means to label components using a resource bundle. This implementation places the labels to the left of the fields. The labels and other decorations are pulled from the resources associated with this object (by applying a "tag" to the name). It supports the following:
ResourceBundle - allowing
one to localize their application.
The following shows a section of a resource bundle that has be "tagged" with "po" that could be used with this method:
po.title=Pizza Order Properties po.width=7 po.0.label=Name: po.0.tooltip=Enter you name here po.1.label=Hungry?: po.1.tooltip=Are you hungry or not? po.2.label=Pizza Fetch: po.2.tooltip=Press here to go order your pizza # DEPRECATED form of specifying labels # po.labels=Name:,Hungry?:,Pizza Fetch:
The following demonstrates how this method could be used to then label and layout a set of fields:
private JTextField _Text;
private JCheckBox _Choice;
private JButton _Button;
private JComponent _All;
public Component getComponent(Resources r) {
if (_All == null) {
JComponent[] fields = {
_Text = new JTextField("Frankie"),
_Choice = new JCheckBox(),
_Button = new JButton("Order")
};
_All = r.createLabeledFields("po",fields);
}
return _All;
}
tag - The TAG ID which will be used to look for text strings that we
use to create the panel.fields - Array of components corresponding to the labels (you can pass
null, have null entries in your array, of not match the length
of your labels - we'll deal with it as best we can).
panel with your labels and fields installed.LayoutUtilspublic String getString(String keyName)
string resource by key value.
This method looks up the value of a string resource using a key name. If the resource isn't found, null is returned.
key - The "key" used to lookup the resource with - must not be null.
getString(String,String)
public String getString(String keyName,
String defValue)
string resource by key value.
This method looks up the value of a string resource using a key name. If the resource isn't found, the default value (which you pass) is returned.
key - The "key" used to lookup the resource with - must not be null.default_val - The default value to return if resource wasn't found.
getString(String,String)
public JDialog createDialog(String tag,
JComponent top,
Component owner,
Object callback)
JDialog box from resources.
This method will build a JDialog box for
you. It does so in the following manner:
JDialog object using the root
frame or dialog of the 'owner' passed as its parent.
createButtonGrid method and the
'callback' object you pass (this button grid - if created is then
added to the bottom of the dialog box).
createButtonGrid(String,Object) for the properties recognized
in the button installation.
tag - The "TAG" which identifies the button in the associated resource properties.top - You component to add to the center of the
dialog box created - if you pass null, it isn't added.parent - The component which is the "owner" or
"parent" of this object. We will search up its parent chain
for a frame or dialog object to which it belongs to (or is).callback - The object which has the methods the buttons are allowed to
invoke when pressed (see ActionCallback.create(com.ccg.util.Lookup, javax.swing.AbstractButton, java.lang.Object) for
more details - or check out the example mentioned above).
JDialog box - (packed, but not yet shown)createButtonGrid(String,Object)
public int showConfirmDialog(Component parent,
Object contents,
String tag,
Object[] args,
int optionType)
This method is a wrapper around the showConfirmDialog method in the JOptionPane class. This
method allows one to "localize" the message which is displayed to
construct the dialog.
This method uses the TAG to select properties from the resource bundle. The following are recognized:
Assume the following set of properties:
quit.title=Really Quit? quit.text=The time is {0} are you sure you want to quit?The following code fragment could be used to display a popup confirmation:
void actionPerformed(ActionEvent ae) {
if (ae.getSource() instanceof Component) {
Resources r = getResources();
// subsitute for {0} in resource strings
Object[] args = { new java.util.Date() };
int rc =
r.showConfirmDialog((Component) ae.getSource(),null,"quit",args,
JOptionPane.YES_NO_OPTION);
if (rc == JOptionPane.YES_OPTION) System.exit(0);
}
}
parent - Parent component for the dialog (see JOptionPane).message - The contents to be placed in the dialog area that
appears. This can be MANY different things (such as a simple
String, a full JPanel, etc) - see JOptionPane for more
details). It is legal to pass null - in which case we will
lookup the "TAG.text" string from the resource and use that
value.tag - The "tag" used to lookup settingsargs - Optional set of arguments to be subsituted (using standard
MessageFormat rules) into strings retrieved from the
resource. You can pass null if you don't need subsitution.optionType - Specifiews what type of question you want to ask (OK/Cancel,
Yes/No/Cancel, etc). See JOptionPane for more
details. You will probably pass JOptionPane.OK_CANCEL_OPTION -
but you can use any of them.
JOptionPane
class (indicating what option the user selected).JOptionPane
public String showInputDialog(Component parent,
String tag,
Object[] args)
This method is a wrapper around the showInputDialog method in the JOptionPane class. This
method allows one to "localize" the message which is displayed to
construct the dialog.
This method uses the TAG to select properties from the resource bundle. The following are recognized:
Assume the following set of properties:
name.title=Name Input name.text=Please enter/edit your full name below: name.init={0}The following code fragment could be used to prompt the user for their name:
Resources r = getResources();
// subsitute for {0} in resource strings
Object[] args = { "Mek Blankenbaker" };
String val =
r.showConfirmDialog(_MyComponent,"name",args);
if (val == null) System.out.println("User canceled");
else System.out.println("User name:"+val);
parent - Parent component for the dialog (see JOptionPane).tag - The "tag" used to lookup settingsargs - Optional set of arguments to be subsituted (using standard
MessageFormat rules) into strings retrieved from the
resource. You can pass null if you don't need subsitution -
but this is the only way to insert values from your code into
the message formed.
JOptionPane
public void showMessage(Component parent,
String tag,
Object[] args)
This method is a wrapper around the showInputDialog method in the JOptionPane class. This
method allows one to "localize" the message which is displayed to
construct the dialog.
This method uses the TAG to select properties from the resource bundle. The following are recognized:
JOptionPane.INFORMATION_MESSAGE,
w=JOptionPane.WARNING_MESSAGE, e=JOptionPane.ERROR_MESSAGE, q=JOptionPane.QUESTION_MESSAGE, p=JOptionPane.PLAIN_MESSAGE. If omitted we default to "i".Assume the following set of properties:
E100.title=Bad {0} E100.text=The "{1}" error occurred for {0} at {2}. E100.type=errorThe following code fragment could be used to prompt the user for their name:
public void evaluate(String name) {
try {
process(name);
} catch (Exception e) {
Resources r = getResources();
// set subsitution args
Object[] args = { "Frank", e.getMessage(), new java.util.Date() };
r.showMessage(_MyComponent,"E100",args);
}
}
parent - Parent component for the dialog (see JOptionPane).tag - The "tag" used to lookup settingsargs - Optional set of arguments to be subsituted (using standard
MessageFormat rules) into strings retrieved from the
resource. You can pass null if you don't need subsitution -
but this is the only way to insert values from your code into
the message formed.JOptionPane
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||