Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MultipleFormSupport |
|
| 1.8333333333333333;1.833 |
1 | // Copyright 2006 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.form; |
|
16 | ||
17 | import org.apache.tapestry.IForm; |
|
18 | import org.apache.tapestry.IMarkupWriter; |
|
19 | import org.apache.tapestry.IRequestCycle; |
|
20 | ||
21 | /** |
|
22 | * A {@link FormSupport} implementation that can work when a form is |
|
23 | * included multiple times in a given page ( due to it being in a loop |
|
24 | * or in a component that's included many times in the page). |
|
25 | * <p/> |
|
26 | * This is achieved by prefixing the ids of all form elements with the |
|
27 | * form's id. |
|
28 | * |
|
29 | * @since 4.1.1 |
|
30 | */ |
|
31 | public class MultipleFormSupport extends FormSupportImpl |
|
32 | { |
|
33 | /** |
|
34 | * The prefix to use for the form elements. On render, this is the |
|
35 | * clientId of the form. On rewind, it's computed from the posted data. |
|
36 | */ |
|
37 | private String _prefix; |
|
38 | ||
39 | public MultipleFormSupport(IMarkupWriter writer, IRequestCycle cycle, IForm form) |
|
40 | { |
|
41 | 7 | super(writer, cycle, form); |
42 | 7 | _prefix = form.getClientId() + ":"; |
43 | 7 | } |
44 | ||
45 | /** |
|
46 | * Constructs a unique identifier (within the page). The identifier consists of the component's |
|
47 | * id, with an index number added to ensure uniqueness. |
|
48 | */ |
|
49 | ||
50 | public String getElementId(IFormComponent component, String baseId) |
|
51 | { |
|
52 | 0 | return super.getElementId(component, _prefix + baseId); |
53 | } |
|
54 | ||
55 | public String peekClientId(IFormComponent comp) |
|
56 | { |
|
57 | 0 | String id = comp.getSpecifiedId(); |
58 | 0 | if (id == null) |
59 | 0 | return null; |
60 | ||
61 | 0 | return peekClientId(comp, id); |
62 | } |
|
63 | ||
64 | protected String peekClientId(IFormComponent comp, String baseId) |
|
65 | { |
|
66 | 0 | return _elementIdAllocator.peekNextId(_prefix + baseId); |
67 | } |
|
68 | ||
69 | public String rewind() |
|
70 | { |
|
71 | 0 | findIdPrefix(); |
72 | 0 | return super.rewind(); |
73 | } |
|
74 | ||
75 | private void findIdPrefix() |
|
76 | { |
|
77 | 0 | String allocatedFormIds = _cycle.getParameter(FORM_IDS); |
78 | 0 | if (allocatedFormIds==null) |
79 | 0 | return; |
80 | 0 | int pos = allocatedFormIds.indexOf(':'); |
81 | 0 | if (pos>=0) |
82 | 0 | _prefix = allocatedFormIds.substring(0, pos + 1); |
83 | 0 | } |
84 | } |