+ if (!found) {
+ // Look into the package itself for resources
+ try {
+ resetMap(ResourceBundle
+ .getBundle(bname, locale, type.getClassLoader(),
+ new FixedResourceBundleControl()));
+ found = true;
+ } catch (MissingResourceException e) {
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ if (!found) {
+ // We have no bundle for this Bundle
+ System.err.println("No bundle found for: " + bname);
+ resetMap(null);
+ }
+ }
+
+ /**
+ * Reset the backing map to the content of the given bundle, or with default
+ * valiues if bundle is NULL.
+ *
+ * @param bundle
+ * the bundle to copy
+ */
+ protected void resetMap(ResourceBundle bundle) {
+ this.map.clear();
+ for (Field field : type.getDeclaredFields()) {
+ try {
+ Meta meta = field.getAnnotation(Meta.class);
+ if (meta != null) {
+ E id = Enum.valueOf(type, field.getName());
+
+ String value;
+ if (bundle != null) {
+ value = bundle.getString(id.name());
+ } else {
+ value = meta.def();
+ }
+
+ this.map.put(id.name(), value == null ? null : value.trim());
+ }
+ } catch (MissingResourceException e) {
+ }
+ }
+ }
+
+ /**
+ * Take a snapshot of the changes in memory in this {@link Bundle} made by
+ * the "set" methods ( {@link Bundle#setString(Enum, String)}...) at the
+ * current time.
+ *
+ * @return a snapshot to use with {@link Bundle#restoreSnapshot(Object)}
+ */
+ public Object takeSnapshot() {
+ return new HashMap<String, String>(changeMap);
+ }
+
+ /**
+ * Restore a snapshot taken with {@link Bundle}, or reset the current
+ * changes if the snapshot is NULL.
+ *
+ * @param snap
+ * the snapshot or NULL
+ */
+ @SuppressWarnings("unchecked")
+ public void restoreSnapshot(Object snap) {
+ if (snap == null) {
+ changeMap.clear();
+ } else {
+ if (snap instanceof Map) {
+ changeMap = (Map<String, String>) snap;
+ } else {
+ throw new RuntimeException(
+ "Restoring changes in a Bundle must be done on a changes snapshot, "
+ + "or NULL to discard current changes");
+ }