01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.uilib.flowcontext.transitionhandler;
16:
17: import java.io.Serializable;
18: import org.apache.commons.collections.Closure;
19: import org.apache.commons.collections.Predicate;
20: import org.araneaframework.Widget;
21: import org.araneaframework.core.Assert;
22: import org.araneaframework.framework.ConfirmationContext;
23: import org.araneaframework.framework.FlowContext;
24: import org.araneaframework.framework.FlowContext.TransitionHandler;
25: import org.araneaframework.framework.container.StandardFlowContainerWidget;
26:
27: /**
28: * {@link TransitionHandler} with some special confirmation handling for
29: * {@link FlowContext#TRANSITION_CANCEL} events. It attaches event listener to active
30: * flow when its cancellation request is received and asks user to confirm the action
31: * whenever the predicate <code>shouldConfirm</code> evaluates to <code>true</code>.
32: *
33: * @author Taimo Peelo (taimo@araneaframework.org)
34: * @since 1.1
35: */
36: public class CancelConfirmingTransitionHandler extends
37: StandardFlowContainerWidget.StandardTransitionHandler {
38: private static final long serialVersionUID = 1L;
39: private Predicate shouldConfirm;
40: private String confirmationMessage;
41:
42: public CancelConfirmingTransitionHandler(Predicate shouldConfirm,
43: String confirmationMessage) {
44: Assert.notNullParam(this , shouldConfirm, "shouldConfirm");
45: Assert
46: .isInstanceOf(Serializable.class, shouldConfirm,
47: "shouldConfirm Predicate must implement java.io.Serializable");
48: Assert.notNullParam(this , confirmationMessage,
49: "confirmationMessage");
50: this .shouldConfirm = shouldConfirm;
51: this .confirmationMessage = confirmationMessage;
52: }
53:
54: public void doTransition(int transitionType,
55: final Widget activeFlow, final Closure transition) {
56: if (transitionType == FlowContext.TRANSITION_CANCEL
57: && shouldConfirm.evaluate(activeFlow)) {
58: ConfirmationContext ctx = requireConfirmationContext(activeFlow);
59: Closure parameterizedTransition = new ParameterizedTransition(
60: transitionType, activeFlow, transition);
61: ctx.confirm(parameterizedTransition, confirmationMessage);
62: } else
63: super .doTransition(transitionType, activeFlow, transition);
64: }
65:
66: protected ConfirmationContext requireConfirmationContext(
67: Widget activeFlow) {
68: ConfirmationContext ctx = (ConfirmationContext) activeFlow
69: .getEnvironment().requireEntry(
70: ConfirmationContext.class);
71: return ctx;
72: }
73:
74: private final class ParameterizedTransition implements Closure,
75: Serializable {
76: private final Closure transition;
77: private final Widget activeFlow;
78: private int transitionType;
79:
80: private ParameterizedTransition(int transitionType,
81: Widget activeFlow, Closure transition) {
82: this .transitionType = transitionType;
83: this .transition = transition;
84: this .activeFlow = activeFlow;
85: }
86:
87: public void execute(Object obj) {
88: notifyScrollContext(transitionType, activeFlow);
89: transition.execute(activeFlow);
90: }
91: }
92: }
|