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  package org.apache.myfaces.flow;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import javax.el.ValueExpression;
25  import javax.faces.context.FacesContext;
26  import javax.faces.flow.SwitchCase;
27  import javax.faces.flow.SwitchNode;
28  
29  /**
30   *
31   * @since 2.2
32   * @author Leonardo Uribe
33   */
34  public class SwitchNodeImpl extends SwitchNode implements Freezable
35  {
36      private String _defaultOutcome;
37      private ValueExpression _defaultOutcomeEL;
38      private String _id;
39      
40      private List<SwitchCase> _cases;
41      private List<SwitchCase> _unmodifiableCases;
42  
43      private boolean _initialized;
44  
45      public SwitchNodeImpl(String switchNodeId)
46      {
47          this._id = switchNodeId;
48          _cases = new ArrayList<SwitchCase>();
49          _unmodifiableCases = Collections.unmodifiableList(_cases);
50      }
51      
52      @Override
53      public List<SwitchCase> getCases()
54      {
55          return _unmodifiableCases;
56      }
57      
58      public void addCase(SwitchCase switchCase)
59      {
60          checkInitialized();
61          _cases.add(switchCase);
62      }
63  
64      @Override
65      public String getDefaultOutcome(FacesContext context)
66      {
67          if (_defaultOutcomeEL != null)
68          {
69              return (String) _defaultOutcomeEL.getValue(context.getELContext());
70          }
71          return _defaultOutcome;
72      }
73  
74      @Override
75      public String getId()
76      {
77          return _id;
78      }
79  
80      public void freeze()
81      {
82          _initialized = true;
83          
84          for (SwitchCase switchCase : _cases)
85          {
86              if (switchCase instanceof Freezable)
87              {
88                  ((Freezable)switchCase).freeze();
89              }
90          }
91      }
92      
93      private void checkInitialized() throws IllegalStateException
94      {
95          if (_initialized)
96          {
97              throw new IllegalStateException("Flow is inmutable once initialized");
98          }
99      }
100     
101     public void setDefaultOutcome(String defaultOutcome)
102     {
103         checkInitialized();
104         this._defaultOutcome = defaultOutcome;
105         this._defaultOutcomeEL = null;
106     }
107     
108     public void setDefaultOutcome(ValueExpression defaultOutcome)
109     {
110         checkInitialized();
111         this._defaultOutcomeEL = defaultOutcome;
112         this._defaultOutcome = null;
113     }
114 
115     public void setId(String id)
116     {
117         checkInitialized();
118         this._id = id;
119     }
120 }