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.util.FacesVersion;
23  
24  import javax.faces.component.UIComponentBase;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.ComponentSystemEvent;
28  import javax.faces.event.ListenerFor;
29  import javax.faces.event.PostAddToViewEvent;
30  import javax.faces.event.PreRenderViewEvent;
31  
32  /**
33   * {@link org.apache.myfaces.tobago.internal.taglib.component.ScriptTagDeclaration}
34   *
35   * @since 3.0.0
36   */
37  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
38  public abstract class AbstractUIScript extends UIComponentBase {
39  
40    @Override
41    public void processEvent(final ComponentSystemEvent event) {
42  
43      super.processEvent(event);
44  
45      if (event instanceof PreRenderViewEvent) {
46        addComponentResource();
47      } else if (event instanceof PostAddToViewEvent) {
48        if (FacesVersion.supports21() || !FacesVersion.isMyfaces()) {
49          // MyFaces core is removing the component resources in head if the view will be recreated before rendering.
50          // The view will be recreated because of expressions. For example  expressins in the ui:include src attribute
51          // The PostAddToViewEvent will not be broadcasted in this case again.
52          // A subscription to the PreRenderViewEvent avoids this problem
53          // NOTE: PreRenderViewEvent can not used in myfaces prior 2.0.3 using PostAddToView for all myfaces 2.0 versions
54          getFacesContext().getViewRoot().subscribeToEvent(PreRenderViewEvent.class, this);
55        } else {
56          addComponentResource();
57        }
58      }
59    }
60  
61    private void addComponentResource() {
62      final FacesContext facesContext = getFacesContext();
63      final UIViewRoot root = facesContext.getViewRoot();
64      root.addComponentResource(facesContext, this);
65    }
66  
67    public abstract String getFile();
68    public abstract void setFile(String file);
69  
70    public abstract String getType();
71    public abstract void setType(String type);
72  }