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.release.demos;
016:
017: import java.io.Serializable;
018: import java.math.BigDecimal;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Date;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.Random;
028: import org.apache.commons.lang.RandomStringUtils;
029: import org.araneaframework.example.main.TemplateBaseWidget;
030: import org.araneaframework.example.main.release.features.ExampleData;
031: import org.araneaframework.framework.LocalizationContext.LocaleChangeListener;
032: import org.araneaframework.uilib.form.BeanFormWidget;
033: import org.araneaframework.uilib.form.FormElement;
034: import org.araneaframework.uilib.form.control.DateControl;
035: import org.araneaframework.uilib.form.control.FloatControl;
036: import org.araneaframework.uilib.form.control.NumberControl;
037: import org.araneaframework.uilib.form.control.SelectControl;
038: import org.araneaframework.uilib.form.control.TextControl;
039: import org.araneaframework.uilib.form.data.LongData;
040: import org.araneaframework.uilib.support.DisplayItem;
041:
042: /**
043: * @author Taimo Peelo (taimo@araneaframework.org)
044: */
045: public class FriendlyUpdateDemoWidget extends TemplateBaseWidget
046: implements LocaleChangeListener {
047: private BeanFormWidget companyForm;
048: private BeanFormWidget invoiceForm;
049: private FormElement firmElement;
050:
051: static List allCountries = new ArrayList();
052:
053: static {
054: for (Iterator i = Arrays.asList(Locale.getISOCountries())
055: .iterator(); i.hasNext();) {
056: allCountries.add(new Locale("en", (String) i.next())
057: .getDisplayCountry(Locale.ENGLISH));
058: }
059: }
060:
061: protected void init() throws Exception {
062: setViewSelector("release/demos/friendlyUpdateDemo");
063: getL10nCtx().addLocaleChangeListener(this );
064:
065: companyForm = buildCompanyForm();
066: invoiceForm = buildInvoiceForm();
067:
068: addWidget("companyForm", companyForm);
069: addWidget("invoiceForm", invoiceForm);
070: }
071:
072: private BeanFormWidget buildInvoiceForm() {
073: BeanFormWidget result = new BeanFormWidget(Invoice.class);
074: result.addBeanElement("id", "ufriendly.component.invoice.id",
075: new TextControl(new Long(10), new Long(10)), true);
076: result.addBeanElement("date",
077: "ufriendly.component.invoice.date", new DateControl(),
078: true);
079: result.addBeanElement("sum", "ufriendly.component.invoice.sum",
080: new FloatControl(), true);
081:
082: return result;
083: }
084:
085: private BeanFormWidget buildCompanyForm() {
086: BeanFormWidget result = new BeanFormWidget(Firm.class);
087: result.addElement("arkNumber",
088: "ufriendly.component.centralfirmid",
089: new NumberControl(), new LongData(), true);
090: result.addBeanElement("registryAddress",
091: "ufriendly.component.registryaddress",
092: new TextControl(), false);
093: result.addBeanElement("postalAddress",
094: "ufriendly.component.postaladdress", new TextControl(),
095: false);
096: result.addBeanElement("bankAccount",
097: "ufriendly.component.bankaccount", new TextControl(),
098: false);
099: result.addBeanElement("vatNumber",
100: "ufriendly.component.vatidentifier",
101: new NumberControl(), false);
102: firmElement = result.addBeanElement("firmType",
103: "ufriendly.component.firmtype", buildFirmTypeSelect(),
104: false);
105:
106: return result;
107: }
108:
109: private Map firms = new HashMap();
110:
111: private void handleEventFetchData() throws Exception {
112: if (companyForm.convertAndValidate()) {
113: Thread.currentThread().sleep(6000);
114: Firm firm = new Firm();
115: firm = (Firm) companyForm.writeToBean(firm);
116:
117: // present ?
118: Firm present = (Firm) firms.get(firm.getArkNumber());
119: if (present == null) {
120: present = generateFirm(firm.getArkNumber());
121: }
122:
123: firms.put(present.getArkNumber(), present);
124: companyForm.readFromBean(present);
125: }
126: }
127:
128: private void handleEventResetCompanyForm() throws Exception {
129: companyForm.readFromBean(new Firm());
130: }
131:
132: private Firm generateFirm(Long arkNumber) {
133: Firm result = new Firm();
134: result.setArkNumber(arkNumber);
135:
136: Random rnd = new Random();
137:
138: result.setBankAccount(RandomStringUtils.randomNumeric(16));
139:
140: String registryCountry = (String) allCountries.get(rnd
141: .nextInt(allCountries.size()));
142: String postalCountry = (String) allCountries.get(rnd
143: .nextInt(allCountries.size()));
144:
145: String registryStreet = ExampleData.fungi[rnd
146: .nextInt(ExampleData.fungi.length)];
147: String postalStreet = ExampleData.fungi[rnd
148: .nextInt(ExampleData.fungi.length)];
149:
150: String registryhouse = String.valueOf(rnd.nextInt(500));
151: String postalhouse = String.valueOf(rnd.nextInt(200));
152:
153: result.setRegistryAddress(registryStreet + "-" + registryhouse
154: + ", " + registryCountry);
155: result.setPostalAddress(postalStreet + "-" + postalhouse + ", "
156: + postalCountry);
157: result.setVatNumber(new Long(Math.abs(rnd.nextLong())));
158: result.setFirmType(new Integer(rnd.nextInt(3) + 1));
159:
160: return result;
161: }
162:
163: private SelectControl buildFirmTypeSelect() {
164: SelectControl result = new SelectControl();
165:
166: result.addItem(new DisplayItem("0", t("select.choose")));
167: result.addItem(new DisplayItem("1",
168: t("ufriendly.public.limited")));
169: result.addItem(new DisplayItem("2", t("ufriendly.npo")));
170: result.addItem(new DisplayItem("3",
171: t("ufriendly.private.limited")));
172:
173: return result;
174: }
175:
176: public void onLocaleChange(Locale oldLocale, Locale newLocale) {
177: Object value = firmElement.getControl().getRawValue();
178: SelectControl c = buildFirmTypeSelect();
179: c.setRawValue(value);
180: firmElement.setControl(c);
181: }
182:
183: public static class Firm implements Serializable {
184: private Long arkNumber;
185: private String registryAddress;
186: private String postalAddress;
187: private String bankAccount;
188: private Long vatNumber;
189: private Integer firmType;
190:
191: public Long getArkNumber() {
192: return arkNumber;
193: }
194:
195: public void setArkNumber(Long arkNumber) {
196: this .arkNumber = arkNumber;
197: }
198:
199: public String getRegistryAddress() {
200: return registryAddress;
201: }
202:
203: public void setRegistryAddress(String registryAddress) {
204: this .registryAddress = registryAddress;
205: }
206:
207: public String getPostalAddress() {
208: return postalAddress;
209: }
210:
211: public void setPostalAddress(String postalAddress) {
212: this .postalAddress = postalAddress;
213: }
214:
215: public String getBankAccount() {
216: return bankAccount;
217: }
218:
219: public void setBankAccount(String bankAccount) {
220: this .bankAccount = bankAccount;
221: }
222:
223: public Long getVatNumber() {
224: return vatNumber;
225: }
226:
227: public void setVatNumber(Long vatNumber) {
228: this .vatNumber = vatNumber;
229: }
230:
231: public Integer getFirmType() {
232: return firmType;
233: }
234:
235: public void setFirmType(Integer firmType) {
236: this .firmType = firmType;
237: }
238: }
239:
240: public static class Invoice implements Serializable {
241: private String id;
242: private Date date;
243: private BigDecimal sum;
244:
245: public String getId() {
246: return id;
247: }
248:
249: public void setId(String id) {
250: this .id = id;
251: }
252:
253: public Date getDate() {
254: return date;
255: }
256:
257: public void setDate(Date date) {
258: this .date = date;
259: }
260:
261: public BigDecimal getSum() {
262: return sum;
263: }
264:
265: public void setSum(BigDecimal sum) {
266: this.sum = sum;
267: }
268: }
269: }
|