001: package net.xoetrope.optional.data;
002:
003: import java.util.ResourceBundle;
004: import java.util.Vector;
005:
006: import java.awt.Component;
007:
008: import net.xoetrope.xml.XmlElement;
009: import net.xoetrope.xui.XListHolder;
010: import net.xoetrope.xui.XProjectManager;
011: import net.xoetrope.xui.XResourceManager;
012: import net.xoetrope.xui.data.XCustomDataBinding;
013: import net.xoetrope.xui.data.XListBinding;
014: import net.xoetrope.xui.data.XListModelAdapter;
015: import net.xoetrope.xui.data.XModel;
016:
017: /**
018: * A list binding that supports localization of the list content
019: * <p>Copyright Xoetrope (c) 2001-2004</p>
020: * $Revision: 1.7 $
021: */
022: public class XLocalisedListBinding extends XListBinding implements
023: XCustomDataBinding {
024: protected ResourceBundle languageResourceBundle;
025: protected Vector keys;
026:
027: public XLocalisedListBinding() {
028: keys = new Vector();
029: }
030:
031: /**
032: * Used to setup custom components where the constructor. The properties of the
033: * binding are specified by the xmlElement and the date pointed to by the
034: * dataElement path is bound to the component
035: * @param c the component to bind to
036: * @param xmlElement the XML element which contains the binding configuration
037: */
038: public void setup(Component c, XmlElement xmlElement) {
039: languageResourceBundle = XResourceManager
040: .getResourceBundle(XProjectManager.getCurrentProject()
041: .getStartupParam("Language"));
042: srcPath = xmlElement.getAttribute("source");
043: String destElement = xmlElement.getAttribute("output");
044: outputPath = XModel.prefixOutputPath(destElement);
045: XModel srcModel = null;
046: XModel dstModel = null;
047: if (srcPath != null)
048: srcModel = (XModel) XProjectManager.getModel().get(srcPath);
049: if (destElement == null)
050: outputPath = XModel.prefixOutputPath(srcPath);
051: outputNode = dstModel;
052: if ((dstModel == null) && (destElement != null))
053: outputNode = (XModel) XProjectManager.getModel().get(
054: outputPath);
055: if ((srcModel == null) && (srcPath != null))
056: srcModel = (XModel) XProjectManager.getModel().get(srcPath);
057:
058: model = new XListModelAdapter(srcModel);
059: comp = (XListHolder) c;
060: useUnique = false;
061: bDirty = true;
062: }
063:
064: /**
065: * Construct a new data binding
066: * @param c the component to be bound
067: * @param dataElement the name of the data in the model
068: */
069: public XLocalisedListBinding(Component c, String dataElement) {
070: this (c, dataElement, null, null, null);
071: }
072:
073: /**
074: * Construct a new data binding
075: * @param c the component to be bound
076: * @param dataElement the name of the data in the model
077: * @param destElement the name of the destination data in the model
078: * @param srcModel the model node that acts as the data source for this node
079: * @param dstModel the model node to which the selection is saved
080: */
081: public XLocalisedListBinding(Component c, String dataElement,
082: String destElement, XModel srcModel, XModel dstModel) {
083: this (c, dataElement, destElement, srcModel, dstModel, true);
084: }
085:
086: /**
087: * Construct a new data binding
088: * @param c the component to be bound
089: * @param dataElement the name of the data in the model
090: * @param srcModel the model node that acts as the data source for this node
091: */
092: public XLocalisedListBinding(Component c, String dataElement,
093: String destElement) {
094: this (c, dataElement, destElement, null, null, true);
095: }
096:
097: /**
098: * Construct a new data binding, and set the languageResourceBundle for text
099: * to be translated
100: * @param c the component to be bound
101: * @param dataElement the name of the data in the model
102: * @param destElement the name of the destination data in the model
103: * @param srcModel the model node that acts as the data source for this node
104: * @param dstModel the model node to which the selection is saved
105: * @param saveToSource true to save the selected value as the value of the source node, false to save only to the output node
106: */
107: public XLocalisedListBinding(Component c, String dataElement,
108: String destElement, XModel srcModel, XModel dstModel,
109: boolean saveToSource) {
110: super (c, dataElement, destElement, srcModel, dstModel,
111: saveToSource);
112: if ((model.getModel().get() == null) && (outputNode != null))
113: model.getModel().set((String) outputNode.get());
114: keys = new Vector();
115: languageResourceBundle = XResourceManager
116: .getResourceBundle(XProjectManager.getCurrentProject()
117: .getStartupParam("Language"));
118: }
119:
120: /**
121: * Translate the text being added to the list.
122: * @param key the key to be look up in the resourcebundle
123: * @return the translated key or the key itself if no translation found
124: */
125: protected String translate(String key) {
126: String ret = key;
127: while (key.indexOf(' ') >= 0)
128: key = key.replace(' ', '_');
129:
130: try {
131: ret = languageResourceBundle.getString(key);
132: } catch (Exception ex) {
133: }
134:
135: return ret;
136: }
137:
138: /**
139: * translate the text at this point
140: * @param s the text to be translated
141: */
142: protected String addItem(String s) {
143: keys.add(s);
144: String item = translate(s);
145: comp.addItem(item);
146: return item;
147: }
148:
149: /**
150: * Get the untranslated key for a list item
151: * @param idx the list index
152: */
153: public String getKey(int idx) {
154: return (String) keys.elementAt(idx);
155: }
156: }
|