KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > RepeaterAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */

16 package org.apache.cocoon.forms.formmodel;
17
18 import org.apache.cocoon.forms.FormContext;
19
20 /**
21  * An action that acts on a repeater.
22  *
23  * @see RepeaterActionDefinitionBuilder
24  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
25  * @version $Id: RepeaterAction.java 326838 2005-10-20 06:26:53Z sylvain $
26  */

27 public class RepeaterAction extends Action {
28     
29     private Repeater repeater;
30     
31
32     public RepeaterAction(RepeaterActionDefinition definition) {
33         super(definition);
34     }
35     
36     /**
37      * Get the repeater on which this action acts.
38      */

39     public Repeater getRepeater() {
40         if (this.repeater == null) {
41             String JavaDoc name = ((RepeaterActionDefinition)getDefinition()).getRepeaterName();
42             Widget widget;
43             if (name != null) {
44                 // Get the corresponding sibling
45
widget = ((ContainerWidget)getParent()).getChild(name);
46             } else {
47                 // Get the grand-parent (parent is the repeater row).
48
widget = getParent().getParent();
49             }
50          
51             if (widget == null || !(widget instanceof Repeater)) {
52                 throw new RuntimeException JavaDoc(name != null ?
53                     "Cannot find sibling repeater named '" + name + "'." :
54                     "Parent widget is not a repeater");
55             }
56             
57             this.repeater = (Repeater)widget;
58         }
59         
60         return this.repeater;
61     }
62     
63     public static class Move extends RepeaterAction {
64         private int from;
65         private int to;
66         
67         public Move(RepeaterActionDefinition definition) {
68             super(definition);
69         }
70         
71         public void readFromRequest(FormContext formContext) {
72             String JavaDoc fullName = getFullName();
73             String JavaDoc fromStr = formContext.getRequest().getParameter(fullName + ".from");
74             if (fromStr != null) {
75                 from = Integer.parseInt(fromStr);
76                 to = Integer.parseInt(formContext.getRequest().getParameter(fullName + ".to"));
77             } else {
78                 from = -1;
79                 to = -1;
80             }
81             super.readFromRequest(formContext);
82         }
83         
84         public int getFrom() {
85             return from;
86         }
87         
88         public int getTo() {
89             return to;
90         }
91     };
92 }
93
Popular Tags