001 package org.apache.myfaces.tobago.renderkit;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022 import org.apache.myfaces.tobago.component.ComponentUtil;
023 import org.apache.myfaces.tobago.component.UILayout;
024 import org.apache.myfaces.tobago.config.ThemeConfig;
025 import org.apache.myfaces.tobago.context.ResourceManagerUtil;
026
027 import javax.faces.component.UIComponent;
028 import javax.faces.component.ValueHolder;
029 import javax.faces.context.FacesContext;
030 import javax.faces.convert.Converter;
031 import javax.faces.convert.ConverterException;
032 import java.io.IOException;
033
034 public class RenderUtil {
035
036 private static final Log LOG = LogFactory.getLog(RenderUtil.class);
037
038 public static final String COMPONENT_IN_REQUEST = "org.apache.myfaces.tobago.component";
039
040 public static boolean contains(Object[] list, Object value) {
041 if (list == null) {
042 return false;
043 }
044 for (Object aList : list) {
045 if (aList != null && aList.equals(value)) {
046 return true;
047 }
048 }
049 return false;
050 }
051
052 public static void encodeChildren(FacesContext facesContext,
053 UIComponent panel)
054 throws IOException {
055 // UIComponent layout = panel.getFacet("layout");
056 UILayout layout = UILayout.getLayout(panel);
057 if (layout != null) {
058 layout.encodeChildrenOfComponent(facesContext, panel);
059 } else {
060 for (Object o : panel.getChildren()) {
061 UIComponent child = (UIComponent) o;
062 encode(facesContext, child);
063 }
064 }
065 }
066
067 public static void encode(FacesContext facesContext, UIComponent component) throws IOException {
068 if (component.isRendered()) {
069 if (LOG.isDebugEnabled()) {
070 LOG.debug("rendering " + component.getRendererType() + " " + component);
071 }
072
073 LayoutRenderer layoutRenderer = (LayoutRenderer)
074 ComponentUtil.getRenderer(facesContext, UILayout.getLayout(component));
075 layoutRenderer.prepareRender(facesContext, component);
076
077 component.encodeBegin(facesContext);
078 if (component.getRendersChildren()) {
079 component.encodeChildren(facesContext);
080 } else {
081 for (Object o : component.getChildren()) {
082 UIComponent kid = (UIComponent) o;
083 encode(facesContext, kid);
084 }
085 }
086 component.encodeEnd(facesContext);
087 }
088 }
089
090
091 public static String addMenuCheckToggle(String clientId, String onClick) {
092 if (onClick != null) {
093 onClick = " ; " + onClick;
094 } else {
095 onClick = "";
096 }
097
098 onClick = "menuCheckToggle('" + clientId + "')" + onClick;
099
100 return onClick;
101 }
102
103 public static String getFormattedValue(
104 FacesContext facesContext, UIComponent component) {
105 Object value = null;
106 if (component instanceof ValueHolder) {
107 value = ((ValueHolder) component).getLocalValue();
108 if (value == null) {
109 value = ((ValueHolder) component).getValue();
110 }
111 }
112 return getFormattedValue(facesContext, component, value);
113 }
114
115 public static String getFormattedValue(
116 FacesContext context, UIComponent component, Object currentValue)
117 throws ConverterException {
118
119 if (currentValue == null) {
120 return "";
121 }
122
123 if (!(component instanceof ValueHolder)) {
124 return currentValue.toString();
125 }
126
127 Converter converter = ((ValueHolder) component).getConverter();
128
129 if (converter == null) {
130 if (currentValue instanceof String) {
131 return (String) currentValue;
132 }
133 Class converterType = currentValue.getClass();
134 converter = context.getApplication().createConverter(converterType);
135 }
136
137 if (converter == null) {
138 return currentValue.toString();
139 } else {
140 return converter.getAsString(context, component, currentValue);
141 }
142 }
143
144 public static int calculateStringWidth2(FacesContext facesContext, UIComponent component, String text) {
145 int width = 0;
146 int defaultCharWidth = 0;
147 try {
148 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth");
149 } catch (NullPointerException e) {
150 if (LOG.isDebugEnabled()) {
151 LOG.debug("no value for \"fontWidth\" found in theme-config");
152 }
153 }
154
155 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font2.widths");
156
157 for (char c : text.toCharArray()) {
158 int charWidth;
159 if (c >= 32 && c < 128) {
160 int begin = (c - 32) * 2;
161 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16);
162 } else {
163 charWidth = defaultCharWidth;
164 }
165 width += charWidth;
166 }
167
168 return width;
169 }
170
171 public static int calculateStringWidth(FacesContext facesContext, UIComponent component, String text) {
172 int width = 0;
173 int defaultCharWidth = 0;
174 try {
175 defaultCharWidth = ThemeConfig.getValue(facesContext, component, "fontWidth");
176 } catch (NullPointerException e) {
177 if (LOG.isDebugEnabled()) {
178 LOG.debug("no value for \"fontWidth\" found in theme-config");
179 }
180 }
181
182 String fontWidths = ResourceManagerUtil.getProperty(facesContext, "tobago", "tobago.font.widths");
183
184 for (char c : text.toCharArray()) {
185 int charWidth;
186 if (c >= 32 && c < 128) {
187 int begin = (c - 32) * 2;
188 charWidth = Integer.parseInt(fontWidths.substring(begin, begin + 2), 16);
189 } else {
190 charWidth = defaultCharWidth;
191 }
192 width += charWidth;
193 }
194
195 return width;
196 }
197 }