View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import javax.enterprise.context.SessionScoped;
23  import javax.inject.Named;
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  @SessionScoped
30  @Named
31  public class ForEachController implements Serializable {
32  
33    private List<River> rivers;
34    private String name;
35    private String length;
36    private String discharge;
37  
38    public ForEachController() {
39      reset();
40    }
41  
42    public List<River> getRivers() {
43      return rivers;
44    }
45  
46    public String getName() {
47      return name;
48    }
49  
50    public void setName(final String name) {
51      this.name = name;
52    }
53  
54    public String getLength() {
55      return length;
56    }
57  
58    public void setLength(final String length) {
59      this.length = length;
60    }
61  
62    public String getDischarge() {
63      return discharge;
64    }
65  
66    public void setDischarge(final String discharge) {
67      this.discharge = discharge;
68    }
69  
70    public String addNewRiver() {
71      rivers.add(new River(name, Integer.valueOf(length), Integer.valueOf(discharge)));
72      resetInputFields();
73      return null;
74    }
75  
76    public String reset() {
77      rivers = new ArrayList<>(Arrays.asList(
78          new River("Nile", 6853, 2830),
79          new River("Amazon", 6437, 209000),
80          new River("Yangtze", 6300, 30166)));
81      resetInputFields();
82      return null;
83    }
84  
85    private void resetInputFields() {
86      name = null;
87      length = null;
88      discharge = null;
89    }
90  
91    public class River implements Serializable {
92      private String name;
93      private int length;
94      private int discharge;
95  
96      public River(final String name, final int length, final int discharge) {
97        this.name = name;
98        this.length = length;
99        this.discharge = discharge;
100     }
101 
102     public String getName() {
103       return name;
104     }
105 
106     public int getLength() {
107       return length;
108     }
109 
110     public int getDischarge() {
111       return discharge;
112     }
113   }
114 }