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_SCRIPT_BLOCKS = "org.apache.myfaces.tobago.scriptBlocks";
39    private static final String TOBAGO_STYLE_FILES = "org.apache.myfaces.tobago.styleFiles";
40    private static final String TOBAGO_STYLE_BLOCKS = "org.apache.myfaces.tobago.styleBlocks";
41    private static final String TOBAGO_ONLOAD_SCRIPTS = "org.apache.myfaces.tobago.onloadScripts";
42    private static final String TOBAGO_ONUNLOAD_SCRIPTS = "org.apache.myfaces.tobago.onunloadScripts";
43    private static final String TOBAGO_ONEXIT_SCRIPTS = "org.apache.myfaces.tobago.onexitScripts";
44    private static final String TOBAGO_ONSUBMIT_SCRIPTS = "org.apache.myfaces.tobago.onsubmitScripts";
45    private static final String TOBAGO_POPUPS = "org.apache.myfaces.tobago.popups";
46    private static final String TOBAGO_MENU_ACCELERATORS = "org.apache.myfaces.tobago.menuAccelerators";
47    private static final String TOBAGO_FOCUS_ID = "org.apache.myfaces.tobago.focusId";
48    private static final String TOBAGO_ACTION_ID = "org.apache.myfaces.tobago.actionId";
49  
50    private FacesContextUtils() {
51    }
52  
53    public static boolean isAjax(FacesContext context) {
54      return context.getAttributes().containsKey(TOBAGO_AJAX);
55    }
56  
57    public static void setAjax(FacesContext context, boolean ajax) {
58      context.getAttributes().put(TOBAGO_AJAX, ajax);
59    }
60  
61    public static void setFocusId(FacesContext context, String focusId) {
62      context.getAttributes().put(TOBAGO_FOCUS_ID, focusId);
63    }
64  
65    public static String getFocusId(FacesContext context) {
66      return (String) context.getAttributes().get(TOBAGO_FOCUS_ID);
67    }
68  
69    public static void setActionId(FacesContext context, String actionId) {
70      context.getAttributes().put(TOBAGO_ACTION_ID, actionId);
71    }
72  
73    public static String getActionId(FacesContext context) {
74      return (String) context.getAttributes().get(TOBAGO_ACTION_ID);
75    }
76  
77  
78    public static String getAjaxComponentId(FacesContext context) {
79      return (String) context.getAttributes().get(TOBAGO_AJAX_COMPONENT_ID);
80    }
81  
82    public static void setAjaxComponentId(FacesContext context, String ajaxComponentId) {
83      context.getAttributes().put(TOBAGO_AJAX_COMPONENT_ID, ajaxComponentId);
84    }
85  
86    public static String getEnctype(FacesContext context) {
87      return (String) context.getAttributes().get(TOBAGO_ENCTYPE);
88    }
89  
90    public static void setEnctype(FacesContext context, String enctype) {
91      context.getAttributes().put(TOBAGO_ENCTYPE, enctype);
92    }
93  
94    public static List<String> getScriptFiles(FacesContext context) {
95      List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
96      if (list == null) {
97        return Collections.emptyList();
98      }
99      return list;
100   }
101   
102   public static void addScriptFile(FacesContext context, String file) {
103     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
104     if (list == null) {
105       list = SetUniqueList.decorate(new ArrayList());
106       context.getAttributes().put(TOBAGO_SCRIPT_FILES, list);
107     }
108     list.add(file);
109 
110   }
111 
112   public static Set<String> getScriptBlocks(FacesContext context) {
113     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_SCRIPT_BLOCKS);
114      if (set == null) {
115       return Collections.emptySet();
116     }
117     return set;
118   }
119 
120   public static void addScriptBlock(FacesContext context, String script) {
121     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_SCRIPT_BLOCKS);
122     if (set == null) {
123       set = new ListOrderedSet();
124       context.getAttributes().put(TOBAGO_SCRIPT_BLOCKS, set);
125     }
126     set.add(script);
127   }
128 
129 
130   public static Set<String> getStyleFiles(FacesContext context) {
131     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
132     if (set == null) {
133       return Collections.emptySet();
134     }
135     return set;
136   }
137 
138   public static void addStyleFile(FacesContext context, String script) {
139     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
140     if (set == null) {
141       set = new ListOrderedSet();
142       context.getAttributes().put(TOBAGO_STYLE_FILES, set);
143     }
144     set.add(script);
145   }
146 
147   public static Set<String> getStyleBlocks(FacesContext context) {
148     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_BLOCKS);
149     if (set == null) {
150       return Collections.emptySet();
151     }
152     return set;
153   }
154 
155   public static void addStyleBlock(FacesContext context, String script) {
156     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_BLOCKS);
157     if (set == null) {
158       set = new ListOrderedSet();
159       context.getAttributes().put(TOBAGO_STYLE_BLOCKS, set);
160     }
161     set.add(script);
162   }
163 
164   public static List<String> getOnloadScripts(FacesContext context) {
165     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
166     if (list == null) {
167       return Collections.emptyList();
168     }
169     return list;
170   }
171 
172 
173   public static void addOnloadScript(FacesContext context, String file) {
174     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
175     if (list == null) {
176       list = SetUniqueList.decorate(new ArrayList());
177       context.getAttributes().put(TOBAGO_ONLOAD_SCRIPTS, list);
178     }
179     list.add(file);
180   }
181 
182   public static void addOnloadScript(FacesContext context, int position, String file) {
183     List<String> list = (List<String>) context.getAttributes().get(TOBAGO_ONLOAD_SCRIPTS);
184     if (list == null) {
185       list = SetUniqueList.decorate(new ArrayList());
186       context.getAttributes().put(TOBAGO_ONLOAD_SCRIPTS, list);
187     }
188     list.add(position, file);
189   }
190 
191   public static Set<String> getOnunloadScripts(FacesContext context) {
192     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONUNLOAD_SCRIPTS);
193     if (set == null) {
194       return Collections.emptySet();
195     }
196     return set;
197   }
198 
199   public static void addOnunloadScript(FacesContext context, String script) {
200     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONUNLOAD_SCRIPTS);
201     if (set == null) {
202       set = new ListOrderedSet();
203       context.getAttributes().put(TOBAGO_ONUNLOAD_SCRIPTS, set);
204     }
205     set.add(script);
206   }
207 
208   public static Set<String> getOnexitScripts(FacesContext context) {
209     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONEXIT_SCRIPTS);
210     if (set == null) {
211       return Collections.emptySet();
212     }
213     return set;
214   }
215 
216   public static void addOnexitScript(FacesContext context, String script) {
217     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONEXIT_SCRIPTS);
218     if (set == null) {
219       set = new ListOrderedSet();
220       context.getAttributes().put(TOBAGO_ONEXIT_SCRIPTS, set);
221     }
222     set.add(script);
223   }
224 
225   public static Set<String> getOnsubmitScripts(FacesContext context) {
226     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONSUBMIT_SCRIPTS);
227     if (set == null) {
228       return Collections.emptySet();
229     }
230     return set;
231   }
232 
233   public static void addOnsubmitScript(FacesContext context, String script) {
234     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_ONSUBMIT_SCRIPTS);
235     if (set == null) {
236       set = new ListOrderedSet();
237       context.getAttributes().put(TOBAGO_ONSUBMIT_SCRIPTS, set);
238     }
239     set.add(script);
240   }
241 
242   public static void clearMenuAcceleratorScripts(FacesContext context) {
243     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
244     set.clear();
245   }
246 
247   public static Set<String> getMenuAcceleratorScripts(FacesContext context) {
248     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
249     if (set == null) {
250       return Collections.emptySet();
251     }
252     return set;
253   }
254 
255   public static void addMenuAcceleratorScript(FacesContext context, String script) {
256     Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_MENU_ACCELERATORS);
257     if (set == null) {
258       set = new ListOrderedSet();
259       context.getAttributes().put(TOBAGO_MENU_ACCELERATORS, set);
260     }
261     set.add(script);
262   }
263 
264   public static Set<AbstractUIPopup> getPopups(FacesContext context) {
265     Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
266     if (set == null) {
267       return Collections.emptySet();
268     }
269     return set;
270   }
271 
272   public static void addPopup(FacesContext context, AbstractUIPopup popup) {
273     Set<AbstractUIPopup> set = (Set<AbstractUIPopup>) context.getAttributes().get(TOBAGO_POPUPS);
274     if (set == null) {
275       set = new ListOrderedSet();
276       context.getAttributes().put(TOBAGO_POPUPS, set);
277     }
278     set.add(popup);
279   }
280 }