001: package com.xoetrope.swing.table;
002:
003: import net.xoetrope.xui.data.XBaseModel;
004: import net.xoetrope.xui.data.XModel;
005: import net.xoetrope.xui.XProjectManager;
006: import net.xoetrope.xui.helper.*;
007:
008: /**
009: * <p>Title: </p>
010: * <p>Description: </p>
011: *
012: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
013: * the GNU Public License (GPL), please see license.txt for more details. If
014: * you make commercial use of this software you must purchase a commercial
015: * license from Xoetrope.</p>
016: * <p> $Revision: 1.3 $</p>
017: */
018:
019: public class XTableModelHelper {
020: private static XTableModelHelper modelHelper;
021:
022: private XTableModelHelper() {
023: }
024:
025: public static XTableModelHelper getInstance() {
026: if (modelHelper == null)
027: modelHelper = new XTableModelHelper();
028:
029: return modelHelper;
030: }
031:
032: /**
033: * Clones the current model node. Need to call cloneChildren which iterated
034: * the children and creates new instances of the children. The call to
035: * super.clone() does not create new instance of the child nodes.
036: * @return the newly created XModel.
037: */
038: public static XModel clone(XModel src) {
039: XBaseModel model = new XBaseModel();
040: cloneChildren(src, model);
041: return model;
042: }
043:
044: /**
045: * Iterates the children of the source model and transfers them to the target
046: * model. For each model the attributes also need to be iterated and added.
047: * @param source the model being cloned
048: * @param target the new model resuting from the clone operation.
049: */
050: private static void cloneChildren(XModel source, XModel target) {
051: target.setNumAttributes(source.getNumAttributes());
052: target.setId(source.getId());
053: for (int att = 0; att < source.getNumAttributes(); att++) {
054: target.setAttribValue(att, source.getAttribName(att),
055: source.getAttribValue(att));
056: }
057:
058: for (int i = 0; i < source.getNumChildren(); i++) {
059: XBaseModel child = (XBaseModel) source.get(i);
060: XBaseModel newChild = new XBaseModel(target);
061: target.append(newChild);
062: cloneChildren(child, newChild);
063: }
064: }
065:
066: /**
067: * merge the conents of one model with another. Calls cloneChildren which
068: * transfers the data.
069: * @param mainModel the model which is to be updated
070: * @param newmodel the model which contains the data to be merged with this model.
071: */
072: public static void merge(XModel mainModel, XModel newModel) {
073: cloneChildren(newModel, mainModel);
074: }
075:
076: public static String getAttrib(XModel model, String name) {
077: return model.getAttribValueAsString(model.getAttribute(name));
078: }
079:
080: public static void setAttrib(XModel model, String name, String value) {
081: model.setAttribValue(model.getAttribute(name), name, value);
082: }
083:
084: public static XBaseModel getParent(XBaseModel model, int level) {
085: XBaseModel parent = model;
086: for (int i = 0; i < level; i++)
087: parent = (XBaseModel) parent.getParent();
088:
089: return parent;
090: }
091:
092: public static int getPositionInModel(XBaseModel refmodel) {
093: for (int i = 0; i < refmodel.getParent().getNumChildren(); i++) {
094: if (refmodel.getParent().get(i).equals(refmodel)) {
095: return i;
096: }
097: }
098: return -1;
099: }
100:
101: public static String getValue(Object model) {
102: return (String) ((XModel) model).get();
103: }
104:
105: public static double getDoubleValue(XModel model) {
106: double ret = 0.0;
107: String value = (String) model.get();
108: try {
109: if (value != null)
110: ret = NumberFormatter.parseDouble(value);
111: } catch (NumberFormatException ex) {
112: }
113: return ret;
114: }
115:
116: public static double getDoubleValue(XBaseModel model, String att) {
117: return getDoubleValue(model, att, 0.0);
118: }
119:
120: public static double getDoubleValue(XBaseModel model, String att,
121: double defaultValue) {
122: try {
123: return model
124: .getAttribValueAsDouble(model.getAttribute(att));
125: } catch (Exception ex) {
126: return defaultValue;
127: }
128: }
129:
130: public static int getIntValue(XModel model) {
131: int ret = 0;
132: String value = (String) model.get();
133: try {
134: if (value != null)
135: ret = Integer.parseInt(value);
136: } catch (NumberFormatException ex) {
137: }
138: return ret;
139: }
140:
141: public static int getIntValue(XBaseModel model, String att) {
142: return getIntValue(model, att, 0);
143: }
144:
145: public static int getIntValue(XBaseModel model, String att,
146: int defaultValue) {
147: try {
148: return model.getAttribValueAsInt(model.getAttribute(att));
149: } catch (Exception ex) {
150: return defaultValue;
151: }
152: }
153:
154: public static double addModels(XModel model1, XModel model2) {
155: double value = getDoubleValue(model1);
156: value += getDoubleValue(model2);
157: return value;
158: }
159:
160: public static double multipyModels(XModel model1, XModel model2) {
161: double value = getDoubleValue(model1);
162: value *= getDoubleValue(model2);
163: return value;
164: }
165:
166: public static double multipyAttribs(XBaseModel model,
167: String attrib1, String attrib2) {
168: double value = getDoubleValue(model, attrib1);
169: value *= getDoubleValue(model, attrib2);
170: return value;
171: }
172:
173: public static void setTempVar(String name, String value) {
174: XModel tempmodel = (XModel) XProjectManager.getModel().get(
175: "temp/" + name);
176: tempmodel.set(value);
177: }
178:
179: public static String getTempVar(String name) {
180: XModel tempmodel = (XModel) XProjectManager.getModel().get(
181: "temp/" + name);
182: return (String) tempmodel.get();
183: }
184:
185: public static boolean getBooleanValue(XBaseModel model) {
186: Object o = model.get();
187: boolean include;
188: if (o instanceof Boolean)
189: include = ((Boolean) o).booleanValue();
190: else
191: include = o.toString().compareTo("true") == 0 ? true
192: : false;
193:
194: return include;
195: }
196:
197: public static boolean isChildNode(XBaseModel ref, XBaseModel parent) {
198: XBaseModel nextParent = (XBaseModel) ref.getParent();
199: while (nextParent != null) {
200: if (nextParent.equals(ref))
201: return true;
202: nextParent = (XBaseModel) nextParent.getParent();
203: }
204: return false;
205: }
206: }
|