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;
|
---|
8 | /**
|
---|
9 | * Class containing the methods used to access the messages property bundle
|
---|
10 | * @author tgoecke
|
---|
11 | *
|
---|
12 | */
|
---|
13 | public class PropertyBundle{
|
---|
14 |
|
---|
15 | private static ResourceBundle properties;
|
---|
16 | private static final String path = "de.dass_it.vanhelsing.gui.messages";
|
---|
17 | /**
|
---|
18 | * reads the messages property file
|
---|
19 | */
|
---|
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 | }
|
---|
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 | */
|
---|
33 | public static String getProperty(String key){
|
---|
34 | try{
|
---|
35 | if (properties == null){
|
---|
36 | initialize();
|
---|
37 | }
|
---|
38 | key = key.toLowerCase();
|
---|
39 | return properties.getString(key);
|
---|
40 | } catch (Exception e) {
|
---|
41 | System.err.println(key+" / "+e.toString());
|
---|
42 | }
|
---|
43 | return null;
|
---|
44 | }
|
---|
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 | */
|
---|
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();
|
---|
64 | if (result.size()>0) return (String[]) result.toArray();
|
---|
65 | return result.toArray(new String[0]);
|
---|
66 | }
|
---|
67 | public static ArrayList<String> getTypeAttributes(String partialKey){
|
---|
68 | Enumeration<String> keySet = properties.getKeys();
|
---|
69 | String key;
|
---|
70 | ArrayList<String> result = new ArrayList<String>();
|
---|
71 | String [] keyComponents;
|
---|
72 | while(keySet.hasMoreElements()){
|
---|
73 | key = keySet.nextElement();
|
---|
74 | if (key.startsWith(partialKey)){
|
---|
75 | if (key.contains("type")){
|
---|
76 | keyComponents = key.split("\\.");
|
---|
77 | result.add(keyComponents[2]);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | result.trimToSize();
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|