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  import org.apache.myfaces.tobago.internal.component.AbstractUIPopup;
25  
26  import javax.faces.context.FacesContext;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.Set;
31  
32  @SuppressWarnings("unchecked")
33  public final class FacesContextUtils {
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_POPUPS = "org.apache.myfaces.tobago.popups";
40    private static final String TOBAGO_FOCUS_ID = "org.apache.myfaces.tobago.focusId";
41  
42    private FacesContextUtils() {
43    }
44  
45    public static boolean isAjax(final FacesContext context) {
46      return context.getAttributes().containsKey(TOBAGO_AJAX);
47    }
48  
49    public static void setAjax(final FacesContext context, final boolean ajax) {
50      context.getAttributes().put(TOBAGO_AJAX, ajax);
51    }
52  
53    public static void setFocusId(final FacesContext context, final String focusId) {
54      context.getAttributes().put(TOBAGO_FOCUS_ID, focusId);
55    }
56  
57    public static String getFocusId(final FacesContext context) {
58      return (String) context.getAttributes().get(TOBAGO_FOCUS_ID);
59    }
60  
61    public static String getAjaxComponentId(final FacesContext context) {
62      return (String) context.getAttributes().get(TOBAGO_AJAX_COMPONENT_ID);
63    }
64  
65    public static void setAjaxComponentId(final FacesContext context, final String ajaxComponentId) {
66      context.getAttributes().put(TOBAGO_AJAX_COMPONENT_ID, ajaxComponentId);
67    }
68  
69    public static String getEnctype(final FacesContext context) {
70      return (String) context.getAttributes().get(TOBAGO_ENCTYPE);
71    }
72  
73    public static void setEnctype(final FacesContext context, final String enctype) {
74      context.getAttributes().put(TOBAGO_ENCTYPE, enctype);
75    }
76  
77    public static List<String> getScriptFiles(final FacesContext context) {
78      final List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
79      if (list == null) {
80        return Collections.emptyList();
81      }
82      return list;
83    }
84    
85    public static void addScriptFile(final FacesContext context, final String file) {
86      List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
87      if (list == null) {
88        list = SetUniqueList.decorate(new ArrayList());
89        context.getAttributes().put(TOBAGO_SCRIPT_FILES, list);
90      }
91      list.add(file);
92  
93    }
94  
95    public static Set<String> getStyleFiles(final FacesContext context) {
96      final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
97      if (set == null) {
98        return Collections.emptySet();
99      }
100     return set;
101   }
102 
103   public static void addStyleFile(final FacesContext context, final String script) {
104     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
105     if (set == null) {
106       set = new ListOrderedSet();
107       context.getAttributes().put(TOBAGO_STYLE_FILES, set);
108     }
109     set.add(script);
110   }
111 
112   public static Set<AbstractUIPopup> getPopups(final FacesContext context) {
113     final Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
114     if (set == null) {
115       return Collections.emptySet();
116     }
117     return set;
118   }
119 
120   public static void addPopup(final FacesContext context, final AbstractUIPopup popup) {
121     Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
122     if (set == null) {
123       set = new ListOrderedSet();
124       context.getAttributes().put(TOBAGO_POPUPS, set);
125     }
126     set.add(popup);
127   }
128 }