001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.web.sample;
016:
017: import java.io.Writer;
018: import java.math.BigDecimal;
019: import org.araneaframework.InputData;
020: import org.araneaframework.OutputData;
021: import org.araneaframework.core.ProxyActionListener;
022: import org.araneaframework.core.StandardActionListener;
023: import org.araneaframework.example.main.TemplateBaseWidget;
024: import org.araneaframework.http.HttpOutputData;
025: import org.araneaframework.uilib.form.FormElement;
026: import org.araneaframework.uilib.form.FormWidget;
027: import org.araneaframework.uilib.form.control.FloatControl;
028: import org.araneaframework.uilib.form.control.NumberControl;
029: import org.araneaframework.uilib.form.data.BigDecimalData;
030: import org.araneaframework.uilib.form.data.IntegerData;
031:
032: /**
033: * @author Alar Kvell (alar@araneaframework.org)
034: */
035: public class SampleActionFormWidget extends TemplateBaseWidget {
036:
037: private static final long serialVersionUID = 1L;
038:
039: private FormWidget form;
040: private FormElement price;
041: private FormElement quantity;
042: private FormElement vat;
043: private FormElement total;
044: private FormElement vatTotal;
045: private FormElement bigTotal;
046:
047: protected void init() throws Exception {
048: setViewSelector("sample/sampleActionForm");
049:
050: form = new FormWidget();
051:
052: price = form.addElement("price", "#Price", new FloatControl(),
053: new BigDecimalData(), false);
054: price.setDisabled(true);
055: price.setValue(round2(new BigDecimal(Math.random() * 5 + 5)));
056:
057: quantity = form.addElement("quantity", "#Quantity",
058: new NumberControl(), new IntegerData(), false);
059: quantity.setValue(new Integer(3));
060:
061: total = form.addElement("total", "#Total", new FloatControl(),
062: new BigDecimalData(), false);
063: total.setDisabled(true);
064:
065: vat = form.addElement("vat", "#VAT", new FloatControl(),
066: new BigDecimalData(), false);
067: vat.setValue(new BigDecimal("0.18"));
068:
069: vatTotal = form.addElement("vatTotal", "#not used",
070: new FloatControl(), new BigDecimalData(), false);
071: vatTotal.setDisabled(true);
072:
073: bigTotal = form.addElement("bigTotal", "#Total Sum",
074: new FloatControl(), new BigDecimalData(), false);
075: bigTotal.setDisabled(true);
076:
077: calculate();
078: addWidget("form", form);
079:
080: addActionListener("quantityChange",
081: new StandardActionListener() {
082: private static final long serialVersionUID = 1L;
083:
084: public void processAction(Object actionId,
085: String actionParam, InputData input,
086: OutputData output) throws Exception {
087: // TODO numberFormatException handling
088: quantity.setValue(new Integer(actionParam));
089: calculate();
090: writeFields(output);
091: }
092: });
093:
094: addActionListener("vatChange", new ProxyActionListener(this ));
095: }
096:
097: public void handleActionVatChange(String actionParam)
098: throws Exception {
099: // TODO numberFormatException handling
100: vat.setValue(round2(new BigDecimal(actionParam)));
101: calculate();
102: writeFields(getOutputData());
103: }
104:
105: protected void calculate() {
106: total.setValue(round2(calculateTotal((BigDecimal) price
107: .getValue(), (Integer) quantity.getValue())));
108: vatTotal.setValue(round2(calculateVatTotal((BigDecimal) total
109: .getValue(), (BigDecimal) vat.getValue())));
110: bigTotal.setValue(round2(calculateBigTotal((BigDecimal) total
111: .getValue(), (BigDecimal) vatTotal.getValue())));
112: }
113:
114: protected BigDecimal calculateTotal(BigDecimal price,
115: Integer quantity) {
116: return price.multiply(new BigDecimal(quantity.intValue()));
117: }
118:
119: protected BigDecimal calculateVatTotal(BigDecimal total,
120: BigDecimal vat) {
121: return total.multiply(vat);
122: }
123:
124: protected BigDecimal calculateBigTotal(BigDecimal total,
125: BigDecimal vatTotal) {
126: return total.add(vatTotal);
127: }
128:
129: protected void writeFields(OutputData output) throws Exception {
130: StringBuffer s = new StringBuffer();
131: s.append(quantity.getValue()).append("|");
132: s.append(vat.getValue()).append("|");
133: s.append(total.getValue()).append("|");
134: s.append(vatTotal.getValue()).append("|");
135: s.append(bigTotal.getValue());
136:
137: Writer out = ((HttpOutputData) output).getWriter();
138: out.write(s.toString());
139: }
140:
141: public static BigDecimal round2(BigDecimal num) {
142: return new BigDecimal(num.movePointRight(2).toBigInteger())
143: .movePointLeft(2);
144: }
145:
146: }
|