01: package org.osbl.client;
02:
03: import org.osbl.client.wings.shell.Client;
04: import org.conform.*;
05:
06: import java.util.MissingResourceException;
07:
08: class LocalizationModifier implements Modifier {
09: private final static String KEY_MICRO_HELP_POSTFIX = ".help";
10:
11: private BeanMetaProvider beanMetaProvider;
12:
13: public LocalizationModifier(BeanMetaProvider beanMetaProvider) {
14: this .beanMetaProvider = beanMetaProvider;
15: }
16:
17: public void modify(BeanMeta beanMeta) throws ModifierException {
18: beanMeta.setLabel(getLabel(beanMeta.getName()));
19: beanMeta.setMicroHelp(getMicroHelp(beanMeta.getName()));
20:
21: for (int i = 0; i < beanMeta.getProperties().length; i++) {
22: PropertyMeta propertyMeta = beanMeta.getProperties()[i];
23: PropertyMeta inheritPropertyMeta = getInheritPropertyMeta(propertyMeta);
24: localize(propertyMeta,
25: inheritPropertyMeta != null ? inheritPropertyMeta
26: : propertyMeta);
27: }
28: }
29:
30: private PropertyMeta getInheritPropertyMeta(
31: PropertyMeta propertyMeta) {
32: Class beanClass = (Class) propertyMeta
33: .getAttribute("inheritBean");
34: if (beanClass == null)
35: return null;
36:
37: String propertyName = (String) propertyMeta
38: .getAttribute("inheritProperty");
39: BeanMeta inheritBeanMeta = beanMetaProvider
40: .getBeanMeta(beanClass);
41: return inheritBeanMeta.getProperty(propertyName);
42: }
43:
44: private void localize(PropertyMeta toPropertyMeta,
45: PropertyMeta fromPropertyMeta) {
46: String toKey = buildKey(toPropertyMeta.getBeanMeta().getName(),
47: toPropertyMeta.getName());
48: String fromKey = buildKey(fromPropertyMeta.getBeanMeta()
49: .getName(), fromPropertyMeta.getName());
50:
51: String label = getLabel(toKey);
52: if (label == null)
53: label = getLabel(fromKey);
54: toPropertyMeta.setLabel(label);
55:
56: String microHelp = getMicroHelp(toKey);
57: if (microHelp == null)
58: microHelp = getMicroHelp(fromKey);
59: toPropertyMeta.setMicroHelp(microHelp);
60: }
61:
62: private String buildKey(String className, String propertyName) {
63: return new StringBuilder().append(className).append(".")
64: .append(propertyName).toString();
65: }
66:
67: private String getLabel(String key) {
68: try {
69: String message = Client.getInstance().getResourceProvider()
70: .getMessage(key);
71: return message.equals(key) ? null : message;
72: } catch (MissingResourceException mre) {
73: return null;
74: }
75: }
76:
77: private String getMicroHelp(String key) {
78: try {
79: key = key + KEY_MICRO_HELP_POSTFIX;
80: String message = Client.getInstance().getResourceProvider()
81: .getMessage(key);
82: return message.endsWith(".help") ? null : message;
83: } catch (MissingResourceException mre) {
84: return null;
85: }
86: }
87: }
|