1 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 [][] dataArray = null; 29 private Locale locale; 30 31 public ITrackerResourceBundle() { 32 } 33 34 public ITrackerResourceBundle(Locale locale, Object [][] 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 [][] getContents() { 45 if(dataArray == null) { 47 int i = 0; 48 Object [][] dataArray = new Object [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 [][] 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 key) { 88 Object value = handleGetObject(key); 89 if(value != null && value instanceof DirtyKey) { 90 return true; 91 } 92 return false; 93 } 94 95 public void updateValue(String key, Object 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 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 125 public final Object handleGetObject(String key) { 126 Object 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 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 |