001: package net.xoetrope.builder.editor.helper;
002:
003: import net.xoetrope.xui.XProjectManager;
004: import net.xoetrope.xui.data.XBaseModel;
005: import net.xoetrope.xui.data.XModel;
006:
007: /**
008: * A utility class to help construct a HTML like table structure
009: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
010: * @version $Revision: 1.4 $
011: */
012: public class XTableModelHelper {
013: public XTableModelHelper() {
014: }
015:
016: /**
017: * Creates a new dataset within the model with the specified name. The new
018: * XModel is appended to the root and returned
019: * @param name The name to be given to the new XModel
020: * @return The newly created XModel
021: */
022: public static XModel createDataSet(String name) {
023: return createDataSet(name, XProjectManager.getModel());
024: }
025:
026: public static XModel createDataSet(String name, XModel model) {
027: XModel dset = (XModel) model.get(name);
028: if (dset == null) {
029: dset = new XBaseModel();
030: dset.setId(name);
031: dset.setTagName("dataset");
032: model.append(dset);
033: }
034: return dset;
035: }
036:
037: public static XModel createDataItem(String name, String value,
038: XModel model) {
039: XBaseModel child = new XBaseModel();
040: child.setTagName("data");
041: child.setId(name);
042: child.setAttribValue(XBaseModel.VALUE_ATTRIBUTE, value);
043: model.append(child);
044: return child;
045: }
046:
047: /**
048: * Checks that the base Datasets node exists within the model and if not
049: * creates one. The newly created XModel is returned.
050: * @return The newly created XModel
051: */
052: private static XModel getDatasetsNode() {
053: XModel dsets = null;
054: XModel root = XProjectManager.getModel();
055: if (root.getNumChildren() == 0) {
056: dsets = (XModel) root.get("base");
057: root.setId("Datasets");
058: root.setTagName("Datasets");
059: }
060: return dsets;
061: }
062:
063: /**
064: * Adds a new table XModel node to the model node
065: * The XModel which will have the newly created table XModel
066: * appended to it
067: * @param model The XModel which will have the newly created model appended
068: * to it
069: * @param name The name of the new XModel
070: * @return The newly created table XModel
071: */
072: public static XModel createTable(XModel model, String name) {
073: XBaseModel base = (XBaseModel) XProjectManager.getModel();
074:
075: XBaseModel table = new XBaseModel();
076: table.setTagName("table");
077: table.setId("items");
078: model.append(table);
079:
080: return table;
081: }
082:
083: /**
084: * Creates a new 'th' node and returns it
085: * @param model The model which will have the newly created XModel appended to
086: * it
087: * @return The newly created th node
088: */
089: public static XModel addHeader(XModel model) {
090: XBaseModel heading = new XBaseModel();
091: heading.setTagName("th");
092: model.append(heading);
093: return heading;
094: }
095:
096: /**
097: * Add a new 'tr' node to the passed XModel
098: * @param model The model which will have the newly created XModel appended to
099: * it
100: * @return The newly created th node
101: */
102: public static XModel addRow(XModel model) {
103: XBaseModel row = new XBaseModel();
104: row.setTagName("tr");
105: model.append(row);
106: return row;
107: }
108:
109: /**
110: * Add data to the specified model.
111: * @param model The node which we are adding the data to
112: * @param name The name of the new data item
113: * @param value The value of the new data item
114: * @return The newly created data item
115: */
116: public static XModel addData(XModel model, String name, String value) {
117: XBaseModel item = new XBaseModel();
118: item.setTagName("td");
119: item.setId(name);
120: item.set(value);
121: model.append(item);
122: return item;
123: }
124: }
|