001: package net.xoetrope.optional.data;
002:
003: import net.xoetrope.xui.data.XModel;
004:
005: /**
006: * A helper to eliminate some typecasting with the XModel
007: * <p> Copyright (c) Xoetrope Ltd., 2002-2004</p>
008: * <p> $Revision: 1.2 $</p>
009: * <p> License: see License.txt</p>
010: */
011: public class XModelNodeHelper {
012: private XModel modelNode;
013:
014: /**
015: * Construct a new helper node
016: * @param node the model node on which to operate
017: */
018: public XModelNodeHelper(XModel node) {
019: modelNode = node;
020: }
021:
022: /**
023: * Do a get on the model and return its value as a model node
024: * @return the model node
025: */
026: public XModel get() {
027: return (XModel) modelNode.get();
028: }
029:
030: /**
031: * Get the model value for the specificed path and return its value as a model node
032: * @param path the path to the child node
033: * @return the model node
034: */
035: public XModel get(String path) {
036: return (XModel) modelNode.get(path);
037: }
038:
039: /**
040: * Do a get on the model and return its value as a model node
041: * @return the model node
042: */
043: public static XModel get(XModel modelNode) {
044: return (XModel) modelNode.get();
045: }
046:
047: /**
048: * Get the model value for the specificed path and return its value as a model node
049: * @param baseNode the base node from which to look for the specified path
050: * @param path the path to the child node
051: * @return the model node
052: */
053: public static XModel get(XModel baseNode, String path) {
054: XModel modelNode = baseNode;
055: if (modelNode == null)
056: modelNode = XModel.getInstance();
057: return (XModel) modelNode.get(path);
058: }
059:
060: /**
061: * Get the model value for the specificed path and return its value as a model node
062: * @param baseNode the base node from which to look for the specified path
063: * @param path the path to the child node
064: * @return the model node
065: */
066: public static String getString(XModel baseNode, String path) {
067: XModel targetNode = get(baseNode, path);
068: if (targetNode == null)
069: return null;
070:
071: return (String) targetNode.get();
072: }
073:
074: /**
075: * Set the model value for this node
076: * @param value the new value
077: */
078: public void set(Object value) {
079: modelNode.set(value);
080: }
081:
082: /**
083: * Set the model value for the specificed path
084: * @param path the path to the child node
085: * @param value the new value
086: */
087: public void set(String path, Object value) {
088: ((XModel) modelNode.get(path)).set(value);
089: }
090:
091: /**
092: * Set the model value for the specificed child of the base node
093: * @param baseNode the node whose child is being modified
094: * @param path the path to the child node
095: * @param value the new value
096: */
097: public static void set(XModel baseNode, String path, Object value) {
098: XModel modelNode = baseNode;
099: if (modelNode == null)
100: modelNode = XModel.getInstance();
101: ((XModel) modelNode.get(path)).set(value);
102: }
103: }
|