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.framework;
16:
17: import java.io.Serializable;
18: import org.apache.commons.collections.Closure;
19: import org.araneaframework.Environment;
20: import org.araneaframework.InputData;
21: import org.araneaframework.OutputData;
22: import org.araneaframework.Path;
23: import org.araneaframework.core.Assert;
24: import org.araneaframework.core.StandardEnvironment;
25: import org.araneaframework.framework.core.BaseFilterWidget;
26: import org.araneaframework.http.util.EnvironmentUtil;
27:
28: /**
29: * @author Taimo Peelo (taimo@araneaframework.org)
30: * @since 1.1
31: */
32: public class StandardConfirmationFilterWidget extends BaseFilterWidget
33: implements ConfirmationContext {
34: private static final long serialVersionUID = 1L;
35: private Closure closure;
36: private String message;
37:
38: // PUBLIC API
39: public void confirm(Closure onConfirmClosure, String message) {
40: Assert.isInstanceOfParam(Serializable.class, onConfirmClosure,
41: "onConfirmClosure");
42: Assert.notNullParam(this , message, "message");
43: this .closure = onConfirmClosure;
44: this .message = message;
45: }
46:
47: // IMPLEMENTATION
48:
49: protected void removeConfirmation() {
50: this .closure = null;
51: this .message = null;
52: }
53:
54: public String getConfirmationMessage() {
55: return message;
56: }
57:
58: protected boolean isActive() {
59: return closure != null;
60: }
61:
62: protected void event(Path path, InputData input) throws Exception {
63: if (isActive()) {
64: String confirmationResult = (String) input.getGlobalData()
65: .get(ConfirmationContext.CONFIRMATION_RESULT_KEY);
66:
67: if ("true".equalsIgnoreCase(confirmationResult)) {
68: closure.execute(null);
69: removeConfirmation();
70: } else if ("false".equalsIgnoreCase(confirmationResult)) {
71: removeConfirmation();
72: }
73: }
74:
75: super .event(path, input);
76: }
77:
78: protected void render(OutputData output) throws Exception {
79: SystemFormContext systemFormContext = EnvironmentUtil
80: .requireSystemFormContext(getEnvironment());
81: systemFormContext.addField(
82: ConfirmationContext.CONFIRMATION_RESULT_KEY, "");
83: super .render(output);
84: }
85:
86: protected Environment getChildWidgetEnvironment() {
87: return new StandardEnvironment(super
88: .getChildWidgetEnvironment(),
89: ConfirmationContext.class, this);
90: }
91: }
|