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.internal.component;
21  
22  import org.apache.myfaces.tobago.component.OnComponentCreated;
23  import org.apache.myfaces.tobago.event.FacesEventWrapper;
24  import org.apache.myfaces.tobago.model.Wizard;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.ValueBinding;
29  import javax.faces.event.AbortProcessingException;
30  import javax.faces.event.FacesEvent;
31  import java.io.IOException;
32  
33  public abstract class AbstractUIWizard extends AbstractUIPanel implements OnComponentCreated {
34  
35    public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Wizard";
36  
37    // todo: how to use a auto-generated controller?
38    private Wizard controller;
39  
40    private String var;
41  
42    private String outcome;
43    private String title;
44    private Boolean allowJumpForward;
45  
46    @Override
47    public void processDecodes(FacesContext facesContext) {
48      if (var != null) {
49        facesContext.getExternalContext().getRequestMap().put(var, getController());
50      }
51      super.processDecodes(facesContext);
52    }
53  
54    @Override
55    public void decode(FacesContext facesContext) {
56      super.decode(facesContext);
57      if (var != null) {
58        facesContext.getExternalContext().getRequestMap().remove(var);
59      }
60    }
61  
62    @Override
63    public void queueEvent(FacesEvent event) {
64      super.queueEvent(new FacesEventWrapper(event, this));
65    }
66  
67    @Override
68    public void broadcast(FacesEvent event) throws AbortProcessingException {
69      if (event instanceof FacesEventWrapper) {
70        FacesContext facesContext = FacesContext.getCurrentInstance();
71        if (var != null) {
72          facesContext.getExternalContext().getRequestMap().put(var, getController());
73        }
74        FacesEvent originalEvent = ((FacesEventWrapper) event).getWrappedFacesEvent();
75        originalEvent.getComponent().broadcast(originalEvent);
76        if (var != null) {
77          facesContext.getExternalContext().getRequestMap().remove(var);
78        }
79      } else {
80        super.broadcast(event);
81      }
82    }
83  
84    @Override
85    public void encodeBegin(FacesContext facesContext) throws IOException {
86      if (var != null) {
87        facesContext.getExternalContext().getRequestMap().put(var, getController());
88      }
89      super.encodeBegin(facesContext);
90    }
91  
92    @Override
93    public void encodeEnd(FacesContext facesContext) throws IOException {
94      super.encodeEnd(facesContext);
95      if (var != null) {
96        facesContext.getExternalContext().getRequestMap().remove(var);
97      }
98    }
99  
100   public void onComponentCreated(FacesContext context, UIComponent parent) {
101     Wizard wizard = getController();
102     wizard.register();
103     if (getOutcome() != null) {
104       getController().getCurrentStep().setOutcome(getOutcome());
105     }
106     if (getTitle() != null) {
107       getController().getCurrentStep().setTitle(getTitle());
108     }
109   }
110 
111   @Override
112   public Object saveState(FacesContext facesContext) {
113     Object[] state = new Object[3];
114     state[0] = super.saveState(facesContext);
115     state[1] = var;
116     state[2] = controller;
117     return state;
118   }
119 
120   @Override
121   public void restoreState(FacesContext facesContext, Object state) {
122     Object[] values = (Object[]) state;
123     super.restoreState(facesContext, values[0]);
124     var = (String) values[1];
125     controller = (Wizard) values[2];
126   }
127 
128   public Wizard getController() {
129     if (controller != null) {
130       return controller;
131     }
132     ValueBinding vb = getValueBinding("controller"); // xxx const
133     if (vb != null) {
134       return (Wizard) vb.getValue(getFacesContext());
135     } else {
136       return null;
137     }
138   }
139 
140   public void setController(Wizard controller) {
141     this.controller = controller;
142   }
143 
144   public String getVar() {
145     return var;
146   }
147 
148   public void setVar(String var) {
149     this.var = var;
150   }
151 
152   public java.lang.String getOutcome() {
153     if (outcome != null) {
154       return outcome;
155     }
156     javax.faces.el.ValueBinding vb = getValueBinding("outcome");
157     if (vb != null) {
158       return (java.lang.String) vb.getValue(getFacesContext());
159     }
160     return null;
161   }
162 
163 
164   public void setOutcome(String outcome) {
165     this.outcome = outcome;
166   }
167 
168   public String getTitle() {
169     if (title != null) {
170       return title;
171     }
172     javax.faces.el.ValueBinding vb = getValueBinding("title");
173     if (vb != null) {
174       return (java.lang.String) vb.getValue(getFacesContext());
175     }
176     return null;
177   }
178 
179 
180   public void setTitle(String title) {
181     this.title = title;
182   }
183 
184   public boolean isAllowJumpForward() {
185     if (allowJumpForward != null) {
186       return allowJumpForward;
187     }
188     javax.faces.el.ValueBinding vb = getValueBinding("allowJumpForward");
189     if (vb != null) {
190       Boolean bool = (Boolean) vb.getValue(getFacesContext());
191       if (bool != null) {
192         return bool;
193       }
194     }
195     return false;
196   }
197 
198   public void setAllowJumpForward(Boolean allowJumpForward) {
199     this.allowJumpForward = allowJumpForward;
200 //    getController().removeForwardSteps();
201   }
202 }