001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.function.v1;
009:
010: //base classes
011: import java.math.BigDecimal;
012:
013: //project specific classes
014: import org.jfolder.common.SelfArgumentlessConstructor;
015: import org.jfolder.common.StandardDataTypes;
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.function.BaseCommonFunctionCompatibilityTag;
018: import org.jfolder.common.function.WebPageTagInstanceViewContext;
019: import org.jfolder.common.function.WebPageTagInstanceViewContextInfo;
020: import org.jfolder.common.tagging.AppraiseConceptTagContext;
021: import org.jfolder.common.tagging.ConceptTagCharacteristic;
022: import org.jfolder.common.tagging.ConceptTagConstraint;
023: import org.jfolder.common.tagging.ConceptTagConstraintHolder;
024: import org.jfolder.common.tagging.ConceptTagFlagsHelper;
025: import org.jfolder.common.tagging.ConceptTagHelper;
026: import org.jfolder.common.tagging.DetermineClassConceptTagContext;
027: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
028: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
029: import org.jfolder.common.tagging.DynamicConceptTagConstraintContext;
030: import org.jfolder.common.tagging.InitializeConceptTagContext;
031: import org.jfolder.common.tagging.ReturnableConceptTag;
032: import org.jfolder.common.tagging.SelectionCriteriaForReturnableConceptTag;
033: import org.jfolder.common.tagging.StaticConceptTagConstraintContext;
034: import org.jfolder.common.tagging.StudioConceptTagCreParInfoContext;
035: import org.jfolder.common.tagging.StudioConceptTagInstanceInfoContext;
036: import org.jfolder.common.tagging.StudioConceptTagTypeInfoContext;
037: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
038: import org.jfolder.common.utils.misc.CommonExpressions;
039: import org.jfolder.common.utils.misc.MiscHelper;
040:
041: //other classes
042:
043: public class CommonFunctionMathSubtractTag extends
044: BaseCommonFunctionCompatibilityTag implements
045: SelfArgumentlessConstructor {
046:
047: //characteristics
048: private ConceptTagCharacteristic minuendCtchar = null;
049: private ConceptTagCharacteristic subtrahendCtchar = null;
050:
051: //minuend details
052: private final static String MINUEND_CTCON = "minuend";
053: private final static String MINUEND_TITLE = "Minuend";
054: private final static String MINUEND_SHORT_DESCRIPTION = "The number that will change as a result of the subtraction "
055: + "('Minuend' is a fancy term for this)";
056:
057: //subtrahend details
058: private final static String SUBTRAHEND_CTCON = "subtrahend";
059: private final static String SUBTRAHEND_TITLE = "Subtrahend";
060: private final static String SUBTRAHEND_SHORT_DESCRIPTION = "The number doing the subtracting "
061: + "('Subtrahend' is a fancy term for this)";
062:
063: public void initialize(InitializeConceptTagContext inIctc) {
064:
065: //
066: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
067: .newInstance(inIctc);
068:
069: //constrain "minuend"
070: ConceptTagConstraint minuendCtcon = ConceptTagConstraint
071: .newInstance(MINUEND_CTCON, rootCtconh);
072: minuendCtcon.setTitle(MINUEND_TITLE);
073: minuendCtcon.setShortDescription(MINUEND_SHORT_DESCRIPTION);
074: //
075: StaticConceptTagConstraintContext minuendSctcc = StaticConceptTagConstraintContext
076: .newInstance(minuendCtcon);
077: minuendSctcc.setRegularExpression(CommonExpressions.DECIMAL);
078: //
079: DynamicConceptTagConstraintContext minuendDctcc = DynamicConceptTagConstraintContext
080: .newInstance(minuendCtcon);
081: minuendDctcc.getValidator().allowReturnOfOnlyDecimal();
082: minuendDctcc.getValidator().filterDirectChildOfRootAndHtml();
083: //
084: minuendSctcc.addDefaultValue("0");
085:
086: //constrain "subtrahend"
087: ConceptTagConstraint subtrahendCtcon = ConceptTagConstraint
088: .newInstance(SUBTRAHEND_CTCON, rootCtconh);
089: subtrahendCtcon.setTitle(SUBTRAHEND_TITLE);
090: subtrahendCtcon
091: .setShortDescription(SUBTRAHEND_SHORT_DESCRIPTION);
092: //
093: StaticConceptTagConstraintContext subtrahendSctcc = StaticConceptTagConstraintContext
094: .newInstance(subtrahendCtcon);
095: subtrahendSctcc.setRegularExpression(CommonExpressions.DECIMAL);
096: //
097: DynamicConceptTagConstraintContext subtrahendDctcc = DynamicConceptTagConstraintContext
098: .newInstance(subtrahendCtcon);
099: subtrahendDctcc.getValidator().allowReturnOfOnlyDecimal();
100: subtrahendDctcc.getValidator().filterDirectChildOfRootAndHtml();
101: //
102: subtrahendSctcc.addDefaultValue("0");
103:
104: //
105: ConceptTagFlagsHelper
106: .pushNonIterativeCommonFunctionDepth(inIctc);
107: inIctc.initialize();
108: ConceptTagFlagsHelper
109: .popNonIterativeCommonFunctionDepth(inIctc);
110:
111: //initialize characteristics
112: this .minuendCtchar = minuendCtcon.getCharacteristic();
113: this .subtrahendCtchar = subtrahendCtcon.getCharacteristic();
114: }
115:
116: public void appraise(AppraiseConceptTagContext inActc) {
117:
118: if (inActc instanceof StudioConceptTagInstanceInfoContext) {
119:
120: StudioConceptTagInstanceInfoContext sctiic = (StudioConceptTagInstanceInfoContext) inActc;
121:
122: sctiic.setInstanceTitle("Subtract");
123: sctiic
124: .setInstanceDescription("Subtracts one number from another");
125: } else if (inActc instanceof StudioConceptTagTypeInfoContext) {
126:
127: StudioConceptTagTypeInfoContext scttic = (StudioConceptTagTypeInfoContext) inActc;
128:
129: scttic.setTypeTitle("Subtract");
130: scttic
131: .setTypeDescription("Subtracts one number from another");
132: } else if (inActc instanceof WebPageTagInstanceViewContext) {
133:
134: WebPageTagInstanceViewContext wptivc = (WebPageTagInstanceViewContext) inActc;
135:
136: WebPageTagInstanceViewContextInfo info = wptivc
137: .getInfoAsNonHtml();
138: //
139: //
140: info.setNonHtmlContent("Subtract(", ")");
141: //
142: //
143: //
144: WebPageTagInstanceViewContextInfo minuendInfo = info
145: .createNonHtmlInfo();
146: //
147: minuendInfo.setNonHtmlContent("", " - ");
148: //
149: WebPageTagInstanceViewContext.displayView(
150: this .minuendCtchar, minuendInfo, wptivc, "???");
151: //
152: //
153: //
154: //
155: WebPageTagInstanceViewContextInfo subtrahendInfo = info
156: .createNonHtmlInfo();
157: //
158: WebPageTagInstanceViewContext.displayView(
159: this .subtrahendCtchar, subtrahendInfo, wptivc,
160: "???");
161: //
162: //wptivc.getInfo().setStartContent("");
163: //wptivc.getInfo().setMiddleContent("");
164: //wptivc.getInfo().setEndContent("");
165: } else if (inActc instanceof SelectionCriteriaForReturnableConceptTag) {
166:
167: SelectionCriteriaForReturnableConceptTag scfrct = (SelectionCriteriaForReturnableConceptTag) inActc;
168:
169: scfrct.canReturnDecimal();
170: scfrct.declareFormatAsPlain();
171: } else if (inActc instanceof StudioConceptTagCreParInfoContext) {
172:
173: StudioConceptTagCreParInfoContext scpc = (StudioConceptTagCreParInfoContext) inActc;
174: }
175: }
176:
177: public void determineValueAndClass(
178: DetermineValueAndClassConceptTagContext inDvacctc) {
179:
180: try {
181: ValueAndClassForConceptTag vacfct = null;
182:
183: //minuend
184: BigDecimal minuend = null;
185: if (this .minuendCtchar.isStatic(0, inDvacctc)) {
186: minuend = new BigDecimal(this .minuendCtchar
187: .getValueAsString(0, inDvacctc));
188: } else {
189: ValueAndClassForConceptTag minuendVac = this .minuendCtchar
190: .getDetermineValueAndClass(0, inDvacctc);
191: minuend = MiscHelper.fromNumberToBigDecimal(minuendVac
192: .getValue());
193: }
194:
195: //subtrahend
196: BigDecimal subtrahend = null;
197: if (this .subtrahendCtchar.isStatic(0, inDvacctc)) {
198: subtrahend = new BigDecimal(this .subtrahendCtchar
199: .getValueAsString(0, inDvacctc));
200: } else {
201: ValueAndClassForConceptTag subtrahendVac = this .subtrahendCtchar
202: .getDetermineValueAndClass(0, inDvacctc);
203: subtrahend = MiscHelper
204: .fromNumberToBigDecimal(subtrahendVac
205: .getValue());
206: }
207:
208: BigDecimal difference = minuend.subtract(subtrahend);
209:
210: vacfct = ValueAndClassForConceptTag.newDecimal(difference);
211:
212: inDvacctc.setDetermineValueAndClass(vacfct);
213: } catch (DetermineValueAndClassConceptTagException dvaccte) {
214: inDvacctc.setDetermineException(dvaccte);
215: }
216: }
217:
218: public void determineClass(DetermineClassConceptTagContext inDcctc) {
219: inDcctc.setDetermineClass(StandardDataTypes.getDecimalClass());
220: }
221:
222: public Object createUninitializedSelf() {
223: return new CommonFunctionMathSubtractTag();
224: }
225:
226: /*
227: public void getTagSummary(TagSummaryDescriptor tsd) {
228: //TO DO: placeholder, put meaningful description
229: if (tsd.getMimeType().equals(TagSummaryMimeType.TEXT_HTML)) {
230: tsd.appendData("( ");
231: for (int i = 0; i < this.numberTags.size(); i++) {
232: if (i != 0) {
233: tsd.appendData(" - ");
234: }
235: ((Tag)(this.numberTags.get(i))).getTagSummary(tsd);
236: }
237: tsd.appendData(" )");
238: }
239: }
240: */
241: }
|