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.util;
21  
22  import org.apache.commons.collections.list.SetUniqueList;
23  import org.apache.commons.collections.set.ListOrderedSet;
24  
25  import javax.faces.context.FacesContext;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Set;
30  
31  @SuppressWarnings("unchecked")
32  public final class FacesContextUtils {
33  
34    private static final String TOBAGO_AJAX = "org.apache.myfaces.tobago.ajax";
35    private static final String TOBAGO_AJAX_COMPONENT_ID = "org.apache.myfaces.tobago.ajaxComponentId";
36    private static final String TOBAGO_ENCTYPE = "org.apache.myfaces.tobago.enctype";
37    private static final String TOBAGO_SCRIPT_FILES = "org.apache.myfaces.tobago.scriptFiles";
38    private static final String TOBAGO_STYLE_FILES = "org.apache.myfaces.tobago.styleFiles";
39    private static final String TOBAGO_FOCUS_ID = "org.apache.myfaces.tobago.focusId";
40  
41    private FacesContextUtils() {
42    }
43  
44    public static boolean isAjax(final FacesContext context) {
45      return context.getAttributes().containsKey(TOBAGO_AJAX);
46    }
47  
48    public static void setAjax(final FacesContext context, final boolean ajax) {
49      context.getAttributes().put(TOBAGO_AJAX, ajax);
50    }
51  
52    public static void setFocusId(final FacesContext context, final String focusId) {
53      context.getAttributes().put(TOBAGO_FOCUS_ID, focusId);
54    }
55  
56    public static String getFocusId(final FacesContext context) {
57      return (String) context.getAttributes().get(TOBAGO_FOCUS_ID);
58    }
59  
60    public static String getAjaxComponentId(final FacesContext context) {
61      return (String) context.getAttributes().get(TOBAGO_AJAX_COMPONENT_ID);
62    }
63  
64    public static void setAjaxComponentId(final FacesContext context, final String ajaxComponentId) {
65      context.getAttributes().put(TOBAGO_AJAX_COMPONENT_ID, ajaxComponentId);
66    }
67  
68    public static String getEnctype(final FacesContext context) {
69      return (String) context.getAttributes().get(TOBAGO_ENCTYPE);
70    }
71  
72    public static void setEnctype(final FacesContext context, final String enctype) {
73      context.getAttributes().put(TOBAGO_ENCTYPE, enctype);
74    }
75  
76    public static List<String> getScriptFiles(final FacesContext context) {
77      final List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
78      if (list == null) {
79        return Collections.emptyList();
80      }
81      return list;
82    }
83    
84    public static void addScriptFile(final FacesContext context, final String file) {
85      List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
86      if (list == null) {
87        list = SetUniqueList.decorate(new ArrayList());
88        context.getAttributes().put(TOBAGO_SCRIPT_FILES, list);
89      }
90      list.add(file);
91    }
92  
93    public static Set<String> getStyleFiles(final FacesContext context) {
94      final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
95      if (set == null) {
96        return Collections.emptySet();
97      }
98      return set;
99    }
100 
101   public static void addStyleFile(final FacesContext context, final String script) {
102     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
103     if (set == null) {
104       set = new ListOrderedSet();
105       context.getAttributes().put(TOBAGO_STYLE_FILES, set);
106     }
107     set.add(script);
108   }
109 }