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.demo;
016:
017: import java.io.Serializable;
018: import java.util.Calendar;
019: import java.util.Date;
020: import org.apache.commons.collections.Predicate;
021: import org.apache.commons.lang.time.DateUtils;
022: import org.araneaframework.example.main.TemplateBaseWidget;
023: import org.araneaframework.uilib.event.ProxyOnClickEventListener;
024: import org.araneaframework.uilib.flowcontext.transitionhandler.CancelConfirmingTransitionHandler;
025: import org.araneaframework.uilib.form.FormElement;
026: import org.araneaframework.uilib.form.FormWidget;
027: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
028: import org.araneaframework.uilib.form.control.ButtonControl;
029: import org.araneaframework.uilib.form.control.CheckboxControl;
030: import org.araneaframework.uilib.form.control.DateControl;
031: import org.araneaframework.uilib.form.control.DateTimeControl;
032: import org.araneaframework.uilib.form.control.FloatControl;
033: import org.araneaframework.uilib.form.control.TextControl;
034: import org.araneaframework.uilib.form.control.TimeControl;
035: import org.araneaframework.uilib.form.data.BigDecimalData;
036: import org.araneaframework.uilib.form.data.BooleanData;
037: import org.araneaframework.uilib.form.data.DateData;
038: import org.araneaframework.uilib.form.data.StringData;
039:
040: /**
041: * Demonstrates the basic usage of {@link FlowEventConfirmationContext} by registering a cancel()
042: * precondition and appropriate doConfirm closure. onConfirm is left undefined, b/c defined doConfirm
043: * does not interact with application end-user.
044: *
045: * @author Taimo Peelo (taimo@araneaframework.org)
046: */
047: public class DemoFlowEventConfirmationWidget extends TemplateBaseWidget {
048: private static final long serialVersionUID = 1L;
049: private FormWidget form;
050: private boolean nested = false;
051:
052: public DemoFlowEventConfirmationWidget() {
053: }
054:
055: private DemoFlowEventConfirmationWidget(boolean isNested) {
056: this .nested = isNested;
057: }
058:
059: private void registerCancelConfirmationHandler() {
060: getFlowCtx().setTransitionHandler(
061: new CancelConfirmingTransitionHandler(
062: new UnsavedFlowDataPredicate(),
063: "Form contains unsaved data. Continue?"));
064: }
065:
066: /**
067: * Builds the form.
068: */
069: protected void init() throws Exception {
070: setViewSelector("demo/flowEventConfirm");
071:
072: registerCancelConfirmationHandler();
073:
074: form = new FormWidget();
075:
076: FormElement el = form.createElement("common.Textbox",
077: new TextControl(), new StringData(), false);
078: form.addElement("textbox1", el);
079:
080: form.addElement("checkbox1", "Checkbox", new CheckboxControl(),
081: new BooleanData(), false);
082: form.addElement("dateTime", "common.datetime",
083: new DateTimeControl(), new DateData(), false);
084: form.addElement("time", "common.time", new TimeControl(),
085: new DateData(), false);
086: form.addElement("date", "common.date", new DateControl(),
087: new DateData(), false);
088: form.addElement("number", "common.float", new FloatControl(),
089: new BigDecimalData(), false);
090: // require the number input field to be filled. It could have been achieved already
091: // on formelement creation by setting mandatory attribute to true
092: form.getElement("number").setConstraint(
093: new NotEmptyConstraint());
094: // sets initial value of form element
095:
096: form.setValueByFullName("dateTime", DateUtils.truncate(
097: new Date(), Calendar.MINUTE));
098:
099: // now we construct a button, that is also Control. Reason why we cannot just add it
100: // to form is obvious, we want to add a specific listener to button before.
101: ButtonControl button = new ButtonControl();
102: button.addOnClickEventListener(new ProxyOnClickEventListener(
103: this , "testSimpleForm"));
104: // add the button to form. As the button does not hold any value, Data will be null.
105: form.addElement("button", "common.Submit", button, null, false);
106:
107: form.markBaseState();
108:
109: // the usual, add the created widget to main widget.
110: addWidget("form", form);
111: }
112:
113: public void handleEventTestSimpleForm() throws Exception {
114: form.convertAndValidate();
115: form.markBaseState();
116: }
117:
118: public void handleEventNextFlow() throws Exception {
119: getFlowCtx().start(new DemoFlowEventConfirmationWidget(true));
120: }
121:
122: public void handleEventReturn() throws Exception {
123: if (isNested())
124: getFlowCtx().cancel();
125: }
126:
127: public boolean isNested() {
128: return nested;
129: }
130:
131: private class UnsavedFlowDataPredicate implements Predicate,
132: Serializable {
133: private static final long serialVersionUID = 1L;
134:
135: public boolean evaluate(Object obj) {
136: form.convert();
137: return form.isStateChanged();
138: }
139: }
140: }
|