001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.rave.faces.lifecycle;
043:
044: import java.beans.Beans;
045: import java.util.Hashtable;
046: import java.util.Iterator;
047:
048: import javax.faces.FactoryFinder;
049: import javax.faces.component.UIViewRoot;
050: import javax.faces.context.FacesContext;
051: import javax.faces.event.PhaseEvent;
052: import javax.faces.event.PhaseId;
053: import javax.faces.event.PhaseListener;
054: import javax.faces.lifecycle.LifecycleFactory;
055:
056: public class CreatorLifeCycle {
057: private Hashtable listeners;
058:
059: public CreatorLifeCycle() {
060: if (!Beans.isDesignTime()) {
061: listeners = new Hashtable();
062: LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
063: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
064: String lifecycleId = FacesContext.getCurrentInstance()
065: .getExternalContext().getInitParameter(
066: "javax.faces.lifecycle.LIFECYCLE_ID"); //NOI18N
067: if (lifecycleId == null) {
068: lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
069: }
070: lifecycleFactory.getLifecycle(lifecycleId)
071: .addPhaseListener(new PhaseListener() {
072: public void afterPhase(PhaseEvent event) {
073: for (Iterator i = listeners.keySet()
074: .iterator(); i.hasNext();) {
075: LifeCycleListener listener = (LifeCycleListener) i
076: .next();
077: UIViewRoot viewRoot = (UIViewRoot) listeners
078: .get(listener);
079: if (viewRoot == FacesContext
080: .getCurrentInstance()
081: .getViewRoot()) {
082: LifeCycleEvent lifeCycleEvent = new LifeCycleEvent(
083: event.getFacesContext(),
084: event.getPhaseId());
085: if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {
086: listener
087: .postRestoreView(lifeCycleEvent);
088: } else if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES) {
089: listener
090: .postApplyRequestValues(lifeCycleEvent);
091: } else if (event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS) {
092: listener
093: .postProcessValidations(lifeCycleEvent);
094: } else if (event.getPhaseId() == PhaseId.UPDATE_MODEL_VALUES) {
095: listener
096: .postUpdateModelValues(lifeCycleEvent);
097: } else if (event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
098: listener
099: .postInvokeApplication(lifeCycleEvent);
100: } else if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
101: listener
102: .postRenderResponse(lifeCycleEvent);
103: }
104: }
105: }
106: }
107:
108: public void beforePhase(PhaseEvent event) {
109: for (Iterator i = listeners.keySet()
110: .iterator(); i.hasNext();) {
111: LifeCycleListener listener = (LifeCycleListener) i
112: .next();
113: UIViewRoot viewRoot = (UIViewRoot) listeners
114: .get(listener);
115: if (viewRoot == FacesContext
116: .getCurrentInstance()
117: .getViewRoot()) {
118: LifeCycleEvent lifeCycleEvent = new LifeCycleEvent(
119: event.getFacesContext(),
120: event.getPhaseId());
121: if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {
122: listener
123: .preRestoreView(lifeCycleEvent);
124: } else if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES) {
125: listener
126: .preApplyRequestValues(lifeCycleEvent);
127: } else if (event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS) {
128: listener
129: .preProcessValidations(lifeCycleEvent);
130: } else if (event.getPhaseId() == PhaseId.UPDATE_MODEL_VALUES) {
131: listener
132: .preUpdateModelValues(lifeCycleEvent);
133: } else if (event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
134: listener
135: .preInvokeApplication(lifeCycleEvent);
136: } else if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
137: listener
138: .preRenderResponse(lifeCycleEvent);
139: }
140: }
141: }
142: }
143:
144: public PhaseId getPhaseId() {
145: return PhaseId.ANY_PHASE;
146: }
147: });
148: }
149: }
150:
151: public void addLifeCycleListener(LifeCycleListener listener) {
152: if (!Beans.isDesignTime()) {
153: listeners.put(listener, FacesContext.getCurrentInstance()
154: .getViewRoot());
155: }
156: }
157:
158: public void removeLifeCycleListener(LifeCycleListener listener) {
159: if (!Beans.isDesignTime()) {
160: listeners.remove(listener);
161: }
162: }
163: }
|