KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent;
19 import org.apache.cocoon.forms.event.ActionListener;
20
21 /**
22  * Abstract repeater action. Subclasses will typically just self-add an
23  * event handler that will act on the repeater.
24  *
25  * @see RepeaterActionDefinitionBuilder
26  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
27  * @version $Id: RepeaterActionDefinition.java 327125 2005-10-21 08:52:51Z sylvain $
28  */

29 public abstract class RepeaterActionDefinition extends ActionDefinition {
30
31     private String JavaDoc name = null;
32     
33     /**
34      * Builds an action whose target repeater is the parent of this widget
35      */

36     public RepeaterActionDefinition() {
37     }
38     
39     /**
40      * Builds an action whose target is a sibling of this widget
41      * @param repeaterName the name of the repeater
42      */

43     public RepeaterActionDefinition(String JavaDoc repeaterName) {
44         this.name = repeaterName;
45     }
46     
47     /**
48      * initialize this definition with the other, sort of like a copy constructor
49      */

50     public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
51         super.initializeFrom(definition);
52         
53         if(definition instanceof RepeaterActionDefinition) {
54             RepeaterActionDefinition other = (RepeaterActionDefinition)definition;
55             
56             this.name = other.name;
57             
58         } else {
59             throw new Exception JavaDoc("Definition to inherit from is not of the right type! (at "+getLocation()+")");
60         }
61     }
62
63     public Widget createInstance() {
64         return new RepeaterAction(this);
65     }
66     
67     /**
68      * Get the name of the repeater on which to act. If <code>null</code>, the repeater
69      * is the parent of the current widget (i.e. actions are in repeater rows). Otherwise,
70      * the repeater is a sibling of the current widget.
71      *
72      * @return the repeater name (can be <code>null</code>).
73      */

74     public String JavaDoc getRepeaterName() {
75         return this.name;
76     }
77     
78     //---------------------------------------------------------------------------------------------
79

80     /**
81      * The definition of a repeater action that deletes the selected rows of a sibling repeater.
82      * <p>
83      * The action listeners attached to this action, if any, are called <em>before</em> the rows
84      * are actually removed
85      */

86     public static class DeleteRowsActionDefinition extends RepeaterActionDefinition {
87
88         private String JavaDoc selectName;
89
90         public DeleteRowsActionDefinition(String JavaDoc repeaterName, String JavaDoc selectName) {
91             super(repeaterName);
92             this.selectName = selectName;
93         }
94
95         /**
96          * initialize this definition with the other, sort of like a copy constructor
97          */

98         public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
99             super.initializeFrom(definition);
100             
101             if(definition instanceof DeleteRowsActionDefinition) {
102                 DeleteRowsActionDefinition other = (DeleteRowsActionDefinition)definition;
103                 
104                 this.selectName = other.selectName;
105                 
106             } else {
107                 throw new Exception JavaDoc("Definition to inherit from is not of the right type! (at "+getLocation()+")");
108             }
109         }
110         
111         public boolean hasActionListeners() {
112             // we always want to be notified
113
return true;
114         }
115
116         public void fireActionEvent(ActionEvent event) {
117             // Call action listeners, if any
118
super.fireActionEvent(event);
119
120             // and actually delete the rows
121
Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater();
122             for (int i = repeater.getSize() - 1; i >= 0; i--) {
123                 Repeater.RepeaterRow row = repeater.getRow(i);
124                 if (Boolean.TRUE.equals(row.getChild(this.selectName).getValue())) {
125                     repeater.removeRow(i);
126                 }
127             }
128         }
129     }
130
131     //---------------------------------------------------------------------------------------------
132

133     /**
134      * The definition of a repeater action that adds a row to a sibling repeater.
135      */

136     public static class AddRowActionDefinition extends RepeaterActionDefinition {
137         
138         public AddRowActionDefinition(String JavaDoc repeaterName) {
139             super(repeaterName);
140             
141             this.addActionListener(new ActionListener() {
142                 public void actionPerformed(ActionEvent event) {
143                     Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater();
144                     repeater.addRow();
145                 }
146             });
147         }
148     }
149
150     //---------------------------------------------------------------------------------------------
151

152     /**
153      * The definition of a repeater action that insert rows before the selected rows in a sibling repeater,
154      * or at the end of the repeater if no row is selected.
155      */

156     public static class InsertRowsActionDefinition extends RepeaterActionDefinition {
157         
158         private String JavaDoc selectName;
159         
160         /**
161          * initialize this definition with the other, sort of like a copy constructor
162          */

163         public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
164             super.initializeFrom(definition);
165             
166             if(definition instanceof InsertRowsActionDefinition) {
167                 InsertRowsActionDefinition other = (InsertRowsActionDefinition)definition;
168                 
169                 this.selectName = other.selectName;
170                 
171             } else {
172                 throw new Exception JavaDoc("Definition to inherit from is not of the right type! (at "+getLocation()+")");
173             }
174         }
175         
176         public InsertRowsActionDefinition(String JavaDoc repeaterName, String JavaDoc selectWidgetName) {
177             super(repeaterName);
178             this.selectName = selectWidgetName;
179             
180             this.addActionListener(new ActionListener() {
181                 public void actionPerformed(ActionEvent event) {
182                     Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater();
183                     boolean foundSelection = false;
184                     for (int i = repeater.getSize() - 1; i >= 0; i--) {
185                         Repeater.RepeaterRow row = repeater.getRow(i);
186                         Widget selectWidget = row.getChild(selectName);
187                         if (Boolean.TRUE.equals(selectWidget.getValue())) {
188                             // Add a row
189
repeater.addRow(i);
190                             foundSelection = true;
191                         }
192                     }
193                     
194                     if (!foundSelection) {
195                         // Add a row at the end
196
repeater.addRow();
197                     }
198                 }
199             });
200         }
201     }
202     
203     public static class MoveRowActionDefinition extends RepeaterActionDefinition {
204
205         public MoveRowActionDefinition(String JavaDoc repeaterName) {
206             super(repeaterName);
207             this.addActionListener(new ActionListener() {
208                 public void actionPerformed(ActionEvent event) {
209                     RepeaterAction.Move move = (RepeaterAction.Move)event.getSource();
210                     Repeater repeater = move.getRepeater();
211                     repeater.moveRow(move.getFrom(), move.getTo());
212                 };
213             });
214         }
215         public Widget createInstance() {
216             return new RepeaterAction.Move(this);
217         }
218     }
219 }
220
Popular Tags