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 org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.araneaframework.example.main.TemplateBaseWidget;
020: import org.araneaframework.framework.MessageContext;
021: import org.araneaframework.uilib.form.FormWidget;
022: import org.araneaframework.uilib.form.constraint.AndConstraint;
023: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
024: import org.araneaframework.uilib.form.constraint.OrConstraint;
025: import org.araneaframework.uilib.form.control.ButtonControl;
026: import org.araneaframework.uilib.form.control.TextControl;
027: import org.araneaframework.uilib.form.data.StringData;
028:
029: /**
030: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
031: * @since {since}
032: */
033: public class FormComplexConstraintDemoWidget extends TemplateBaseWidget {
034: private static final long serialVersionUID = 1L;
035: private static final Log log = LogFactory
036: .getLog(FormComplexConstraintDemoWidget.class);
037: private FormWidget searchForm;
038:
039: protected void init() throws Exception {
040: setViewSelector("sample/searchForm");
041:
042: searchForm = new FormWidget();
043:
044: //Adding form controls
045: searchForm.addElement("clientFirstName", "#Client first name",
046: new TextControl(), new StringData(), false);
047: searchForm.addElement("clientLastName", "#Client last name",
048: new TextControl(), new StringData(), false);
049:
050: searchForm.addElement("clientPersonalId",
051: "#Client personal id", new TextControl(),
052: new StringData(), false);
053:
054: searchForm.addElement("clientAddressTown", "#Town",
055: new TextControl(), new StringData(), false);
056: searchForm.addElement("clientAddressStreet", "#Street",
057: new TextControl(), new StringData(), false);
058: searchForm.addElement("clientAddressHouse", "#House",
059: new TextControl(), new StringData(), false);
060:
061: searchForm.addElement("search", "#Search", new ButtonControl(),
062: null, false);
063:
064: //
065: //Complex constraint
066: //
067:
068: //First searching scenario
069: AndConstraint clientNameConstraint = new AndConstraint();
070: clientNameConstraint.addConstraint(new NotEmptyConstraint(
071: searchForm.getElementByFullName("clientFirstName")));
072: clientNameConstraint.addConstraint(new NotEmptyConstraint(
073: searchForm.getElementByFullName("clientLastName")));
074:
075: //Second searching scenario
076: NotEmptyConstraint clientPersonalIdConstraint = new NotEmptyConstraint(
077: searchForm.getElementByFullName("clientPersonalId"));
078:
079: //Third searching scenario
080: AndConstraint clientAddressConstraint = new AndConstraint();
081: clientAddressConstraint.addConstraint(new NotEmptyConstraint(
082: searchForm.getElementByFullName("clientAddressTown")));
083: clientAddressConstraint
084: .addConstraint(new NotEmptyConstraint(searchForm
085: .getElementByFullName("clientAddressStreet")));
086: clientAddressConstraint.addConstraint(new NotEmptyConstraint(
087: searchForm.getElementByFullName("clientAddressHouse")));
088:
089: //Combining scenarios
090: OrConstraint searchConstraint = new OrConstraint();
091: searchConstraint.addConstraint(clientPersonalIdConstraint);
092: searchConstraint.addConstraint(clientNameConstraint);
093: searchConstraint.addConstraint(clientAddressConstraint);
094:
095: //Setting custom error message
096: searchConstraint
097: .setCustomErrorMessage("Not enough data! Please fill in either (client first and last name) or (client personal id) or (client town, street and number)");
098:
099: //Setting constraint
100: searchForm.setConstraint(searchConstraint);
101:
102: //Putting the widget
103: addWidget("searchForm", searchForm);
104: }
105:
106: public void handleEventSearch() throws Exception {
107: if (searchForm.convertAndValidate()) {
108: getMessageCtx().showMessage(MessageContext.INFO_TYPE,
109: "Search allowed!");
110: }
111: }
112:
113: public void handleEventReturn(String eventParameter)
114: throws Exception {
115: getFlowCtx().cancel();
116: }
117: }
|