KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > context > effects > CurrentStyle


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.context.effects;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.w3c.dom.Element JavaDoc;
41
42 import javax.faces.component.UIComponent;
43 import javax.faces.context.FacesContext;
44 import javax.faces.el.ValueBinding;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * Effects can change a components style. This class keeps track of these
51  * changes
52  */

53 public class CurrentStyle {
54
55     /**
56      * Name of field used to send CSS Updated
57      */

58     public static String JavaDoc CSS_UPDATE_FIELD = "icefacesCssUpdates";
59     /**
60      * String uploaded
61      */

62     private String JavaDoc cssString;
63
64     /**
65      * Last string uplodaed
66      */

67     private String JavaDoc lastCssString;
68
69     /**
70      * Constant for visible = true
71      */

72     private static String JavaDoc DISPLAY_ON = "display:block;";
73
74     /**
75      * Constant for visible =false
76      */

77     private static String JavaDoc DISPLAY_OFF = "display:none;";
78
79     private static final Log log = LogFactory.getLog(CurrentStyle.class);
80
81     /**
82      * @param cssString
83      */

84     public CurrentStyle(String JavaDoc cssString) {
85         this.cssString = cssString;
86     }
87
88     public String JavaDoc getCssString() {
89         return cssString;
90     }
91
92     public String JavaDoc getLastCssString() {
93         return lastCssString;
94     }
95
96     public void setLastCssString(String JavaDoc lastCssString) {
97         this.lastCssString = lastCssString;
98     }
99
100     /**
101      * Apply CSS changes to the rendered componenent
102      *
103      * @param facesContext
104      * @param uiComponent
105      */

106     public static void apply(FacesContext facesContext,
107                              UIComponent uiComponent) {
108         apply(facesContext, uiComponent, null, null);
109     }
110
111     /**
112      * Apply css changes to rendered component
113      *
114      * @param facesContext
115      * @param uiComponent
116      * @param targetElement
117      * @param style
118      */

119     public static void apply(FacesContext facesContext, UIComponent uiComponent,
120                              Element JavaDoc targetElement, String JavaDoc style) {
121         if(targetElement == null) {
122             DOMContext domContext =
123                     DOMContext.getDOMContext(facesContext, uiComponent);
124             Object JavaDoc node = domContext.getRootNode();
125             if (node == null || !(node instanceof Element JavaDoc)) {
126                 return;
127             }
128             Element JavaDoc root = (Element JavaDoc) node;
129             targetElement = root;
130         }
131         String JavaDoc jspStyle = (String JavaDoc) uiComponent.getAttributes().get("style");
132         if (log.isTraceEnabled()) {
133             if (jspStyle != null) {
134                 log.trace("Existing style [" + jspStyle + "]");
135             }
136         }
137
138         if (style != null) {
139             if (jspStyle == null) {
140                 jspStyle = "";
141             }
142             jspStyle += style;
143         }
144
145         Boolean JavaDoc visibility =
146                 (Boolean JavaDoc) uiComponent.getAttributes().get("visible");
147         // default to true if visibility is null
148
boolean visible = true;
149         if (visibility != null) {
150             visible = visibility.booleanValue();
151         }
152         CurrentStyle currentStyle =
153                 (CurrentStyle) uiComponent.getAttributes().get("currentStyle");
154         if (currentStyle != null) {
155             String JavaDoc appendedStyle = currentStyle.cssString;
156
157             currentStyle.lastCssString = currentStyle.cssString;
158             if (appendedStyle != null) {
159                 if (jspStyle == null) {
160                     jspStyle = appendedStyle;
161                 } else {
162                     jspStyle += ";" + appendedStyle;
163                 }
164             }
165
166         }
167
168         if (visible) {
169             if (jspStyle != null) {
170                 int startI = jspStyle.indexOf(DISPLAY_OFF);
171                 if (startI != -1) {
172                     String JavaDoc start = "";
173                     if (startI > 0) {
174                         start = jspStyle.substring(0, startI);
175                     }
176                     int endI = startI + DISPLAY_OFF.length();
177                     String JavaDoc end = "";
178                     if (endI < jspStyle.length()) {
179                         end = jspStyle.substring(endI);
180                     }
181                     jspStyle = start + end;
182                 }
183             }
184         } else {
185
186             if (jspStyle == null) {
187                 jspStyle = DISPLAY_OFF;
188             } else {
189                 jspStyle += DISPLAY_OFF;
190             }
191         }
192         if (log.isTraceEnabled()) {
193             if (jspStyle != null) {
194                 log.trace("JSP Style [" + jspStyle + "]");
195             }
196         }
197         if (targetElement != null) {
198             if(jspStyle != null && jspStyle.length() > 0)
199                 targetElement.setAttribute(HTML.STYLE_ATTR, jspStyle);
200             else
201                 targetElement.removeAttribute(HTML.STYLE_ATTR);
202         }
203     }
204
205     /**
206      * Parse cssUpdates from browser. Format id{property:value;property;value}id{property:value}
207      *
208      */

209     public static Map JavaDoc decode(FacesContext facesContext) {
210
211         Map JavaDoc parameters = facesContext.getExternalContext().getRequestParameterMap();
212         String JavaDoc cssUpdate = (String JavaDoc) parameters.get(CSS_UPDATE_FIELD);
213         Map JavaDoc requestMap = facesContext.getExternalContext().getRequestMap();
214         String JavaDoc oldCssUpdate = (String JavaDoc) requestMap.get(CSS_UPDATE_FIELD);
215
216         if (cssUpdate == null || cssUpdate.length() == 0) {
217             return null;
218         }
219         Map JavaDoc updates = null;
220         if (!cssUpdate.equals(oldCssUpdate)) {
221             updates = new HashMap JavaDoc();
222
223             int rightBrace = 0;
224             do {
225                 rightBrace = cssUpdate.indexOf("}");
226                 if (rightBrace != -1) {
227                     String JavaDoc update = cssUpdate.substring(0, ++rightBrace);
228
229                     cssUpdate = cssUpdate.substring(rightBrace);
230                     int leftBrace = update.indexOf("{");
231                     String JavaDoc id = update.substring(0, leftBrace);
232
233                     leftBrace++;
234                     String JavaDoc style = update.substring(leftBrace, update.length() - 1);
235                     if (log.isTraceEnabled()) {
236                         log.trace("Adding id[" + id + "] Style [" + style + "]");
237                     }
238                     updates.put(id, style);
239                 }
240             } while (rightBrace != -1);
241             facesContext.getExternalContext().getSessionMap().put(CurrentStyle.class.getName(), updates);
242             requestMap.put(CSS_UPDATE_FIELD, cssUpdate);
243         }
244         return updates;
245     }
246
247     /**
248      * Parse CSS updates for a componenet
249      *
250      * @param facesContext
251      * @param uiComponent
252      */

253     public static void decode(FacesContext facesContext,
254                               UIComponent uiComponent) {
255
256         decode(facesContext);
257         Map JavaDoc map = (Map JavaDoc) facesContext.getExternalContext().getSessionMap()
258                 .get(CurrentStyle.class.getName());
259         if (map == null) {
260                 return;
261         }
262         if (uiComponent == null) {
263             return;
264         }
265         String JavaDoc clientId = uiComponent.getClientId(facesContext);
266         String JavaDoc style = (String JavaDoc) map.get(clientId);
267         if (style == null) {
268             return;
269         }
270
271         if (log.isTraceEnabled()) {
272             log.trace("Decode Applying Style to [" + clientId + "] Css [" +
273                       style + "]");
274         }
275         CurrentStyle cs =
276                 (CurrentStyle) uiComponent.getAttributes().get("currentStyle");
277         if (cs != null) {
278             cs.cssString = style;
279         } else {
280             cs = new CurrentStyle(style);
281
282             uiComponent.getAttributes()
283                     .put("currentStyle", new CurrentStyle(style));
284         }
285
286         // sync the component visible attribute with the css display style attribute
287
Boolean JavaDoc value = Boolean.valueOf("true");
288         if (cs.cssString.endsWith(DISPLAY_OFF)) {
289             value = Boolean.valueOf("false");
290         }
291         ValueBinding vb = uiComponent.getValueBinding("visible");
292         if (vb == null) {
293             uiComponent.getAttributes().put("visible", value);
294         } else {
295             try {
296                 vb.setValue(facesContext, value);
297             } catch (Exception JavaDoc e) {
298
299                 if (log.isErrorEnabled()) {
300                     log.error("Exception setting visible. Value Binding [" +
301                               vb.getExpressionString() + "]", e);
302                     if (facesContext == null) {
303                         log.error("Faces Context is null");
304                     }
305                 }
306             }
307         }
308     }
309
310 }
311
Popular Tags