KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > resources > ITrackerResourceBundle


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.resources;
20
21 import java.util.*;
22
23 import cowsultants.itracker.ejb.client.exceptions.*;
24 import cowsultants.itracker.ejb.client.models.*;
25
26 public class ITrackerResourceBundle extends ResourceBundle {
27     private HashMap data = new HashMap();
28     private Object JavaDoc[][] dataArray = null;
29     private Locale locale;
30
31     public ITrackerResourceBundle() {
32     }
33
34     public ITrackerResourceBundle(Locale locale, Object JavaDoc[][] data) {
35         setLocale(locale);
36         setContents(data);
37     }
38
39     public ITrackerResourceBundle(Locale locale, LanguageModel[] items) {
40         setLocale(locale);
41         setContents(items);
42     }
43
44     public Object JavaDoc[][] getContents() {
45         // Only load the array if it is requested for some reason.
46
if(dataArray == null) {
47             int i = 0;
48             Object JavaDoc[][] dataArray = new Object JavaDoc[2][data.size()];
49             for(Iterator iter = data.keySet().iterator(); iter.hasNext(); i++) {
50                 dataArray[0][i] = iter.next();
51                 dataArray[1][i] = data.get(dataArray[0][i]);
52             }
53         }
54         return dataArray;
55     }
56
57     public void setContents(LanguageModel[] content) {
58         if(content != null ) {
59             synchronized (data) {
60                 data.clear();
61                 for(int i = 0; i < content.length; i++) {
62                     data.put(content[i].getResourceKey(), content[i].getResourceValue());
63                 }
64             }
65         }
66     }
67
68     public void setContents(Object JavaDoc[][] content) {
69         if(content != null && content.length == 2 && content[0].length == content[1].length) {
70             synchronized (data) {
71                 data.clear();
72                 for(int i = 0; i < content[0].length; i++) {
73                     data.put(content[0][i], content[1][i]);
74                 }
75             }
76         }
77     }
78
79     public Locale getLocale() {
80         return (this.locale == null ? super.getLocale() : this.locale);
81     }
82
83     public void setLocale(Locale locale) {
84         this.locale = locale;
85     }
86
87     public boolean isDirty(String JavaDoc key) {
88         Object JavaDoc value = handleGetObject(key);
89         if(value != null && value instanceof DirtyKey) {
90             return true;
91         }
92         return false;
93     }
94
95     public void updateValue(String JavaDoc key, Object JavaDoc value) {
96         synchronized (data) {
97             data.put(key, value);
98         }
99     }
100
101     public void updateValue(LanguageModel model) {
102         if(model != null) {
103             synchronized (data) {
104                 data.put(model.getResourceKey(), model.getResourceValue());
105             }
106         }
107     }
108
109     public void removeValue(String JavaDoc key, boolean markDirty) {
110         if(key != null) {
111             synchronized (data) {
112                 if(markDirty) {
113                     data.put(key, new DirtyKey());
114                 } else {
115                     data.remove(key);
116                 }
117             }
118         }
119     }
120
121     /**
122       * Implementation of ResourceBundle.handleGetObject. Returns
123       * the request key from the internal data map.
124       */

125     public final Object JavaDoc handleGetObject(String JavaDoc key) {
126         Object JavaDoc value = data.get(key);
127         if(value instanceof DirtyKey) {
128             throw new ITrackerDirtyResourceException("The requested key has been marked dirty.", "ITrackerResourceBundle_" + locale.toString(), key);
129         }
130         return value;
131     }
132
133     /**
134       * Implementation of ResourceBundle.getKeys. Since it returns an enumeration,
135       * It creates a new Hashtable, and returns that collections enumerator.
136       */

137     public Enumeration getKeys() {
138         Hashtable table = new Hashtable(data);
139         return table.keys();
140     }
141
142     public class DirtyKey {
143     }
144 }
145
Popular Tags