KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > util > RepeaterAsList


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.util;
17
18 import java.util.AbstractList JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.cocoon.forms.formmodel.AbstractContainerWidget;
22 import org.apache.cocoon.forms.formmodel.Repeater;
23 import org.apache.cocoon.forms.formmodel.Widget;
24
25 /**
26  * A <code>List</code> view of a {@link Repeater}, each element of the list being a <code>Map</code>
27  * wrapping a repeater row, as defined by {@link ContainerWidgetAsMap}.
28  * <p>
29  * This implementation of list supports all methods, with the following restrictions:
30  * <ul>
31  * <li>values stored in the list must be <code>Map</code>s, that will be used with {@link ContainerWidgetAsMap#putAll(Map)}
32  * on the <code>Map</code> representation of the repeater rows,</li>
33  * <li>operations that involve testing equality with the list contents (e.g. <code>contains(Object)</code>) will
34  * not function properly, the <code>Map</code> wrapping the rows being created on demand.</li>
35  * </ul>
36  *
37  * @since 2.1.8
38  * @version $Id: RepeaterAsList.java 327865 2005-10-23 22:03:06Z sylvain $
39  */

40 public class RepeaterAsList extends AbstractList JavaDoc {
41     
42     private Repeater repeater;
43     private boolean lowerCase;
44
45     /**
46      * Create a <code>List<code> view around a repeater. The <code>keysToLowerCase</code> parameter
47      * specifies if <code>Map</code>s wrapping rows should convert input keys to lower case, as
48      * specified by {@link ContainerWidgetAsMap#ContainerWidgetAsMap(AbstractContainerWidget, boolean)}.
49      *
50      * @param repeater the repeater to wrap
51      * @param keysToLowerCase should we convert input keys to lower case?
52      */

53     public RepeaterAsList(Repeater repeater, boolean keysToLowerCase) {
54         this.repeater = repeater;
55         this.lowerCase = keysToLowerCase;
56     }
57     
58     /**
59      * Same as <code>RepeaterAsList(repeater, false)</code>.
60      */

61     public RepeaterAsList(Repeater repeater) {
62         this(repeater, false);
63     }
64
65     /**
66      * Get the repeater widget that is wrapped by this <code>List</code>.
67      *
68      * @return the wrapped {@link Repeater}
69      */

70     public Repeater getWidget() {
71         return this.repeater;
72     }
73     
74     /**
75      * Get a widget relative to the repeater wrapped by this <code>List</code>
76      *
77      * @param path a widget lookup path
78      * @return the widget pointed to by <code>path</code> or <code>null</code> if it doesn't exist.
79      * @see Widget#lookupWidget(String)
80      */

81     public Widget getWidget(String JavaDoc path) {
82         return this.repeater.lookupWidget(path);
83     }
84
85     public Object JavaDoc get(int index) {
86         return new ContainerWidgetAsMap(repeater.getRow(index), lowerCase);
87     }
88
89     public int size() {
90         return repeater.getSize();
91     }
92     
93     public Object JavaDoc set(int index, Object JavaDoc o) {
94         if (o == null) {
95             throw new NullPointerException JavaDoc("Cannot set null to a repeater");
96         }
97         if (!(o instanceof Map JavaDoc)) {
98             throw new IllegalArgumentException JavaDoc("Cannot set a '" + o.getClass().toString() + "' to a repeater");
99         }
100         Map JavaDoc result = new ContainerWidgetAsMap(repeater.getRow(index));
101         result.putAll((Map JavaDoc)o);
102         return result;
103     }
104     
105     public void add(int index, Object JavaDoc o) {
106         if (o == null) {
107             throw new NullPointerException JavaDoc("Cannot add null to a repeater");
108         }
109         if (!(o instanceof Map JavaDoc)) {
110             throw new IllegalArgumentException JavaDoc("Cannot add a '" + o.getClass().toString() + "' to a repeater");
111         }
112         Repeater.RepeaterRow row = repeater.addRow(index);
113         new ContainerWidgetAsMap(row).putAll((Map JavaDoc)o);
114     }
115     
116     public Object JavaDoc remove(int index) {
117         Map JavaDoc result = new ContainerWidgetAsMap(repeater.getRow(index));
118         repeater.removeRow(index);
119         return result;
120     }
121     
122     // Not mandated by the abstract class, but will speed up things
123
public void clear() {
124         repeater.clear();
125     }
126 }
Popular Tags