01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.samples;
18:
19: import org.apache.cocoon.environment.Redirector;
20: import org.apache.cocoon.environment.SourceResolver;
21: import org.apache.cocoon.environment.Request;
22: import org.apache.cocoon.environment.ObjectModelHelper;
23: import org.apache.cocoon.forms.acting.AbstractFormsAction;
24: import org.apache.cocoon.forms.formmodel.Field;
25: import org.apache.cocoon.forms.formmodel.Form;
26: import org.apache.cocoon.forms.formmodel.Repeater;
27: import org.apache.avalon.framework.parameters.Parameters;
28:
29: import java.util.Map;
30: import java.util.Date;
31:
32: /**
33: * An action that creates an instance of a specific example form included with
34: * Cocoon Forms, and adds some rows to its repeater widget. This example is
35: * meant to illustrate how you can prepopulate a Form instance before its
36: * initial display.
37: *
38: * @version $Id: InitForm1Action.java 449149 2006-09-23 03:58:05Z crossley $
39: */
40: public class InitForm1Action extends AbstractFormsAction {
41: public Map act(Redirector redirector, SourceResolver resolver,
42: Map objectModel, String source, Parameters parameters)
43: throws Exception {
44: String formSource = parameters.getParameter("form-definition");
45: String formAttribute = parameters
46: .getParameter("attribute-name");
47:
48: Form form = formManager.createForm(resolver
49: .resolveURI(formSource));
50:
51: Field birthDate = (Field) form.getChild("birthdate");
52: birthDate.setValue(new Date());
53:
54: Repeater repeater = (Repeater) form.getChild("contacts");
55: repeater.addRow();
56: Field field = (Field) repeater.getWidget(0, "firstname");
57: field.setValue("Jules");
58:
59: repeater.addRow();
60: field = (Field) repeater.getWidget(1, "firstname");
61: field.setValue("Lucien");
62:
63: Request request = ObjectModelHelper.getRequest(objectModel);
64: request.setAttribute(formAttribute, form);
65:
66: return null;
67: }
68: }
|