[857] | 1 | package de.dass_it.vanhelsing.gui;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Enumeration;
|
---|
| 5 | import java.util.Locale;
|
---|
| 6 | import java.util.ResourceBundle;
|
---|
| 7 | import javax.faces.context.FacesContext;
|
---|
[865] | 8 | /**
|
---|
| 9 | * Class containing the methods used to access the messages property bundle
|
---|
| 10 | * @author tgoecke
|
---|
| 11 | *
|
---|
| 12 | */
|
---|
[857] | 13 | public class PropertyBundle{
|
---|
| 14 |
|
---|
| 15 | private static ResourceBundle properties;
|
---|
| 16 | private static final String path = "de.dass_it.vanhelsing.gui.messages";
|
---|
[865] | 17 | /**
|
---|
| 18 | * reads the messages property file
|
---|
| 19 | */
|
---|
[857] | 20 | private static void initialize(){
|
---|
| 21 | Locale l = FacesContext.getCurrentInstance().getViewRoot().getLocale();
|
---|
| 22 |
|
---|
| 23 | if (l == null){
|
---|
| 24 | l = Locale.ENGLISH;
|
---|
| 25 | }
|
---|
| 26 | properties = ResourceBundle.getBundle(path, l);
|
---|
| 27 | }
|
---|
[865] | 28 | /**
|
---|
| 29 | * method returns a single value of a given key
|
---|
| 30 | * @param key key of
|
---|
| 31 | * @return value of the property identified by key. If an exception is thrown, null will be returned
|
---|
| 32 | */
|
---|
[857] | 33 | public static String getProperty(String key){
|
---|
| 34 | try{
|
---|
| 35 | if (properties == null){
|
---|
| 36 | initialize();
|
---|
| 37 | }
|
---|
[858] | 38 | key = key.toLowerCase();
|
---|
[857] | 39 | return properties.getString(key);
|
---|
| 40 | } catch (Exception e) {
|
---|
| 41 | System.err.println(key+" / "+e.toString());
|
---|
| 42 | }
|
---|
| 43 | return null;
|
---|
| 44 | }
|
---|
[865] | 45 | /**
|
---|
| 46 | * method to retrieve properties with a partial key. a partial key is the first n characters of a key
|
---|
| 47 | *
|
---|
| 48 | * @param partialKey first n characters of a property key
|
---|
| 49 | * @return an array of values whose keys started with partialKey. an empty array is returned if no matchings have been found
|
---|
| 50 | */
|
---|
[857] | 51 | public static String[] getTypeProperties(String partialKey){
|
---|
| 52 | Enumeration<String> keySet = properties.getKeys();
|
---|
| 53 | String key;
|
---|
| 54 | int i = 0;
|
---|
| 55 | ArrayList<String> result = new ArrayList<String>();
|
---|
| 56 |
|
---|
| 57 | while(keySet.hasMoreElements()){
|
---|
| 58 | key = keySet.nextElement();
|
---|
| 59 | if (key.startsWith(partialKey)){
|
---|
| 60 | result.add(getProperty(key));
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | result.trimToSize();
|
---|
[865] | 64 | if (result.size()>0) return (String[]) result.toArray();
|
---|
[857] | 65 | return result.toArray(new String[0]);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|