Return-Path: X-Original-To: apmail-empire-db-commits-archive@www.apache.org Delivered-To: apmail-empire-db-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 56F2E996E for ; Sat, 14 Apr 2012 14:58:07 +0000 (UTC) Received: (qmail 16497 invoked by uid 500); 14 Apr 2012 14:58:07 -0000 Delivered-To: apmail-empire-db-commits-archive@empire-db.apache.org Received: (qmail 16477 invoked by uid 500); 14 Apr 2012 14:58:07 -0000 Mailing-List: contact commits-help@empire-db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: empire-db-dev@empire-db.apache.org Delivered-To: mailing list commits@empire-db.apache.org Received: (qmail 16468 invoked by uid 99); 14 Apr 2012 14:58:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 14 Apr 2012 14:58:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 14 Apr 2012 14:57:55 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6F04A23889EC for ; Sat, 14 Apr 2012 14:57:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1326123 [2/4] - in /empire-db/trunk/empire-db-jsf2/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/empire/ main/java/org/apache/empire/jsf2/ main/java/org/apache/empire/jsf2/app/ main/java/org/apache/empi... Date: Sat, 14 Apr 2012 14:57:30 -0000 To: commits@empire-db.apache.org From: doebele@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120414145732.6F04A23889EC@eris.apache.org> Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuItemTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuItemTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuItemTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuItemTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; + +import javax.faces.component.UIComponent; +import javax.faces.component.UINamingContainer; +import javax.faces.component.html.HtmlOutcomeTargetLink; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.ObjectUtils; +import org.apache.empire.commons.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MenuItemTag extends LinkTag +{ + // Logger + private static final Logger log = LoggerFactory.getLogger(MenuItemTag.class); + + private static int renderCount = 1; + + private MenuListTag parentMenu = null; + private String menuId; + + public MenuItemTag() + { + super(); + } + + @Override + public String getFamily() + { + return UINamingContainer.COMPONENT_FAMILY; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + // Detect Parent Menu + parentMenu = getParentMenu(); + menuId = StringUtils.toString(getAttributes().get("menuId")); + if(!isRendered()) + return; + + // render components + ResponseWriter writer = context.getResponseWriter(); + writer.startElement("li", this); + writer.writeAttribute("id", getClientId(context), null); + writer.writeAttribute("class", getStyleClass(), null); + writer.writeAttribute("count", String.valueOf(renderCount++), null); + + // begin + super.encodeBegin(context); + } + + @Override + public boolean getRendersChildren() + { + return true; + } + + @Override + public void encodeChildren(FacesContext context) + throws IOException + { + if (isExpanded()) + { + UIComponent c = getChildren().get(0); + if (c instanceof HtmlOutcomeTargetLink) + { if (c.isRendered()) + { log.warn("Unexpected rendering of output link. Rendering is ignored."); + c.setRendered(false); + } + } + else + log.warn("Unexpected child element as first child of MenuItemTag!"); + // encode children + super.encodeChildren(context); + } + } + + @Override + public void encodeEnd(FacesContext context) + throws IOException + { + if(!isRendered()) + return; + // call base + super.encodeEnd(context); + // end of list item + ResponseWriter writer = context.getResponseWriter(); + writer.endElement("li"); + } + + /* + private void printChildTree(UIComponent comp, int level) + { + List cl = comp.getChildren(); + for (UIComponent c : cl) + { + boolean isRendered = c.isRendered(); + log.info("-{}- rendering {} "+String.valueOf(isRendered), level, c.getClass().getSimpleName()); + printChildTree(c, level+1); + } + } + */ + + @Override + protected String getLinkStyleClass() + { + return null; + } + + protected MenuListTag getParentMenu() + { + // walk upwards the parent component tree and return the first record component found (if + // any) + UIComponent parent = this; + while ((parent = parent.getParent()) != null) + { + if (parent instanceof MenuListTag) + { + return (MenuListTag) parent; + } + } + return null; + } + + private boolean isCurrent() + { + if (menuId==null || parentMenu==null || parentMenu.getCurrentId()==null) + return false; + // All present + return menuId.equals(parentMenu.getCurrentId()); + } + + private boolean isDisabled() + { + Object value = getAttributes().get("disabled"); + if (value!=null) + return ObjectUtils.getBoolean(value); + return false; + } + + private boolean isExpanded() + { + Object value = getAttributes().get("expanded"); + boolean auto = false; + if (value!=null) + { // is current? + auto = "auto".equals(value); + if (auto==false) + return ObjectUtils.getBoolean(value); + // check current + if (isCurrent()) + return true; + } + // Check parent + if (menuId==null || parentMenu==null || parentMenu.getCurrentId()==null) + return auto; + // All present + String currentId = parentMenu.getCurrentId(); + return currentId.startsWith(menuId+"."); + } + + @Override + public boolean isRendered() + { + Object value = getAttributes().get("currentOnly"); + boolean currentOnly = false; + if(value!=null) + currentOnly = ObjectUtils.getBoolean(value); + + // Check parent + if (currentOnly && menuId!=null && parentMenu!=null && parentMenu.getCurrentId()!=null) + { + return isCurrent(); + } + + return super.isRendered(); + } + + private String getStyleClass() + { + String styleClass = StringUtils.toString(getAttributes().get("styleClass")); + String menuClass = null; + if (parentMenu!=null) + { + // Menu Class + if (isCurrent()) + menuClass = parentMenu.getCurrentClass(); + else if (isExpanded()) + menuClass = parentMenu.getExpandedClass(); + else if (isDisabled()) + menuClass = parentMenu.getDisabledClass(); + else + menuClass = parentMenu.getEnabledClass(); + // Style Class + if (StringUtils.isEmpty(styleClass)) + styleClass = parentMenu.getItemStyleClass(); + } + // check + if (menuClass==null) + return styleClass; + if (StringUtils.isEmpty(styleClass)) + return menuClass; + // both supplied + return styleClass+" "+menuClass; + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuItemTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuListTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuListTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuListTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuListTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; +import java.util.Map; + +import javax.faces.component.NamingContainer; +import javax.faces.component.UIComponent; +import javax.faces.component.UINamingContainer; +import javax.faces.component.UIOutput; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.StringUtils; + +public class MenuListTag extends UIOutput implements NamingContainer +{ + // Logger + // private static final Logger log = LoggerFactory.getLogger(MenuTag.class); + + private String currentId = null; + private String currentClass = null; + private String enabledClass = null; + private String disabledClass = null; + private String expandedClass = null; + private String defaultItemClass = null; // e.g. "level{}" + private int level = 0; + + @Override + public String getFamily() + { + return UINamingContainer.COMPONENT_FAMILY; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + // call base + super.encodeBegin(context); + + Map map = getAttributes(); + initMenuAttributes(map); + + // render components + ResponseWriter writer = context.getResponseWriter(); + writer.startElement("ul", this); + // writeAttribute(writer, map, "id"); + writeAttribute(writer, map, "styleClass", "class"); + writeAttribute(writer, map, "style"); + } + + /* + @Override + public boolean getRendersChildren() + { + boolean test = super.getRendersChildren(); + return test; + } + */ + + @Override + public void encodeEnd(FacesContext context) + throws IOException + { + ResponseWriter writer = context.getResponseWriter(); + writer.endElement("ul"); + // call base + super.encodeEnd(context); + } + + private void initMenuAttributes(Map map) + { + currentId = StringUtils.toString(map.get("currentId")); + currentClass = StringUtils.toString(map.get("currentClass")); + enabledClass = StringUtils.toString(map.get("enabledClass")); + disabledClass = StringUtils.toString(map.get("disabledClass")); + expandedClass = StringUtils.toString(map.get("expandedClass")); + defaultItemClass = StringUtils.toString(map.get("defaultItemClass")); + + MenuListTag parent = getParentMenu(); + if (parent==null) + return; + + if (currentId==null) + currentId = parent.getCurrentId(); + if (currentClass==null) + currentClass = parent.getCurrentClass(); + if (enabledClass==null) + enabledClass = parent.getEnabledClass(); + if (disabledClass==null) + disabledClass = parent.getDisabledClass(); + if (expandedClass==null) + expandedClass = parent.getExpandedClass(); + if (defaultItemClass==null) + defaultItemClass = parent.defaultItemClass; + + // Copy parent Info + level = parent.level + 1; + } + + protected MenuListTag getParentMenu() + { + // walk upwards the parent component tree and return the first record component found (if + // any) + UIComponent parent = this; + while ((parent = parent.getParent()) != null) + { + if (parent instanceof MenuListTag) + { + return (MenuListTag) parent; + } + } + return null; + } + + public String getCurrentId() + { + return currentId; + } + + public String getCurrentClass() + { + return currentClass; + } + + public String getEnabledClass() + { + return enabledClass; + } + + public String getDisabledClass() + { + return disabledClass; + } + + public String getExpandedClass() + { + return expandedClass; + } + + public int getLevel() + { + return level; + } + + public String getItemStyleClass() + { + if (defaultItemClass!=null && defaultItemClass.indexOf("{}")>=0) + return StringUtils.replace(defaultItemClass, "{}", String.valueOf(level)); + // return default + return defaultItemClass; + } + + /** setters wozu? **/ + + public void setCurrentId(String currentId) + { + this.currentId = currentId; + } + + public void setCurrentClass(String currentClass) + { + this.currentClass = currentClass; + } + + public void setEnabledClass(String enabledClass) + { + this.enabledClass = enabledClass; + } + + public void setDisabledClass(String disabledClass) + { + this.disabledClass = disabledClass; + } + + public void setExpandedClass(String expandedClass) + { + this.expandedClass = expandedClass; + } + + protected void writeAttribute(ResponseWriter writer, Map map, String attribute, String targetName) + throws IOException + { + Object value = map.get(attribute); + if (value != null) + writer.writeAttribute(targetName, value, null); + } + protected void writeAttribute(ResponseWriter writer, Map map, String attribute) + throws IOException + { + writeAttribute(writer, map, attribute, attribute); + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/MenuListTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/NbspTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/NbspTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/NbspTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/NbspTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; + +import javax.faces.component.NamingContainer; +import javax.faces.component.UINamingContainer; +import javax.faces.component.UIOutput; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.ObjectUtils; + +public class NbspTag extends UIOutput implements NamingContainer +{ + // private int count = 1; + + @Override + public String getFamily() + { + return UINamingContainer.COMPONENT_FAMILY; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + super.encodeBegin(context); + // write + ResponseWriter writer = context.getResponseWriter(); + int count = getCountAttribute(); + while (count>0) + { + writer.write(" "); + count--; + } + } + + public int getCountAttribute() + { + Object value = getAttributes().get("count"); + if (value!=null) + return ObjectUtils.getInteger(value); + return 1; + } + + /* + public void setCount(int count) + { + this.count = count; + } + */ + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/NbspTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/RecordTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/RecordTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/RecordTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/RecordTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import javax.faces.component.NamingContainer; +import javax.faces.component.UIComponentBase; + +import org.apache.empire.commons.ObjectUtils; +import org.apache.empire.data.Record; +import org.apache.empire.data.RecordData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RecordTag extends UIComponentBase implements NamingContainer +{ + // Logger + private static final Logger log = LoggerFactory.getLogger(RecordTag.class); + + public RecordTag() + { + log.trace("component record created"); + } + + @Override + public String getFamily() + { + return "javax.faces.NamingContainer"; + } + + public Object getRecord() + { + return getAttributes().get("value"); + } + + public boolean isReadOnly() + { + // is it a record? + Object rec = getRecord(); + if (rec instanceof RecordData) + { // only a RecordData? + if (!(rec instanceof Record) || !((Record)rec).isValid()) + return true; + } + // check attribute + Object ro = getAttributes().get("readonly"); + if (ro != null) + return ObjectUtils.getBoolean(ro); + // ask record + return false; + } +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/RecordTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/SelectTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/SelectTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/SelectTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/SelectTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; +import java.util.Map; + +import javax.faces.component.NamingContainer; +import javax.faces.component.UIComponent; +import javax.faces.component.UIInput; +import javax.faces.component.UISelectItem; +import javax.faces.component.html.HtmlSelectOneMenu; +import javax.faces.context.FacesContext; + +import org.apache.empire.commons.ObjectUtils; +import org.apache.empire.commons.OptionEntry; +import org.apache.empire.commons.Options; +import org.apache.empire.commons.StringUtils; +import org.apache.empire.jsf2.app.FacesUtils; +import org.apache.empire.jsf2.app.TextResolver; +import org.apache.empire.jsf2.utils.TagRenderHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SelectTag extends UIInput implements NamingContainer +{ + // Logger + private static final Logger log = LoggerFactory.getLogger(SelectTag.class); + + public SelectTag() + { + log.trace("component select created"); + } + + @Override + public String getFamily() + { + return "javax.faces.NamingContainer"; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + // add label and input components when the view is loaded for the first time + UIInput inputComponent = null; + if (getChildCount() > 0) + { + inputComponent = getInputComponent(); + } + if (inputComponent == null) + { + inputComponent = createSelectOneMenu(FacesUtils.getTextResolver(context)); + this.getChildren().add(inputComponent); + } + // render components + inputComponent.encodeAll(context); + // default + super.encodeBegin(context); + } + + @Override + public void updateModel(FacesContext context) + { + // check read only + if (!isDisabled()) + { + UIInput inputComponent = getInputComponent(); + + Object value = inputComponent == null ? "" : inputComponent.getValue(); + if (value == null) + value = ""; + setValue(value); + } + super.updateModel(context); + } + + @Override + public void validate(FacesContext context) + { + // nothing submitted (AJAX part request, e.g. calendar component) or readonly (won't be set + // in updateModel())? + UIInput inputComponent = getInputComponent(); + if (inputComponent == null) + { + return; + } + // component itself already checked validity, was it successful? + if (!inputComponent.isValid() || isDisabled()) + { + return; + } + // nothing to do + super.validate(context); + } + + private UIInput getInputComponent() + { + if (getChildren().size() == 0) + { + return null; + } + + return (UIInput) getChildren().get(0); + } + + private Options getOptionList() + { + Object options = getAttributes().get("options"); + if (!(options instanceof Options)) + return new Options(); + return ((Options) options); + } + + private boolean isAllowNull() + { + Object allowNull = getAttributes().get("allowNull"); + return ObjectUtils.getBoolean(allowNull); + } + + private String getNullText() + { + Object nullText = getAttributes().get("nullText"); + return StringUtils.toString(nullText, ""); + } + + private boolean isDisabled() + { + Object disabled = getAttributes().get("disabled"); + return ObjectUtils.getBoolean(disabled); + } + + private UIInput createSelectOneMenu(TextResolver textResolver) + { + + HtmlSelectOneMenu input = new HtmlSelectOneMenu(); + // css style + String userStyle = StringUtils.toString(getAttributes().get("styleClass")); + String cssStyle = TagRenderHelper.getTagStyleClass("eSelect", null, null, userStyle); + input.setStyleClass(cssStyle); + // other attributes + copyAttributes(input); + // Options + Options options = getOptionList(); + if (isAllowNull()) + { // Empty entry + options = new Options(options); + addSelectItem(input, textResolver, new OptionEntry("", getNullText())); + } + for (OptionEntry e : options) + { // Option entries + addSelectItem(input, textResolver, e); + } + // input.setReadonly(isReadOnly()); + if (isDisabled()) + input.setDisabled(true); + // input.setLabel(getLabelString()); + // input.setRequired(col.isRequired() && !col.isAutoGenerated()); + // input.setId(this.getId() + INPUT_SUFFIX); + input.setValue(getValue()); + return input; + } + + private void copyAttributes(HtmlSelectOneMenu input) + { + Map attr = getAttributes(); + Object value; + if ((value = attr.get("style")) != null) + input.setStyle(StringUtils.toString(value)); + if ((value = attr.get("tabindex")) != null) + input.setTabindex(StringUtils.toString(value)); + if ((value = attr.get("onchange")) != null) + input.setOnchange(StringUtils.toString(value)); + } + + private void addSelectItem(UIComponent input, TextResolver textResolver, OptionEntry e) + { + UISelectItem selectItem = new UISelectItem(); + // set value + selectItem.setItemValue(e.getValueString()); + // set text + String text = e.getText(); + text = textResolver.resolveText(text); + selectItem.setItemLabel(text); + // add item + input.getChildren().add(selectItem); + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/SelectTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/TitleTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/TitleTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/TitleTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/TitleTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; +import java.util.Map; + +import javax.faces.component.NamingContainer; +import javax.faces.component.UIOutput; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.StringUtils; +import org.apache.empire.data.Column; +import org.apache.empire.exceptions.InvalidArgumentException; +import org.apache.empire.jsf2.utils.TagRenderHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TitleTag extends UIOutput implements NamingContainer +{ + // Logger + private static final Logger log = LoggerFactory.getLogger(TitleTag.class); + + private TagRenderHelper helper = new TagRenderHelper(this, "eTitle"); + + public TitleTag() + { + log.trace("component LabelTag created"); + } + + @Override + public String getFamily() + { + return "javax.faces.NamingContainer"; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + // add label and input components when the view is loaded for the first time + super.encodeBegin(context); + + // Create + Column column = helper.getColumn(); + if (column==null) + throw new InvalidArgumentException("column", column); + + // Tooltip title + String title = helper.getLabelTooltip(column); + + // render components + ResponseWriter writer = context.getResponseWriter(); + String tag = writeStartElement(title, writer); + renderLabel(column, writer); + if (tag != null) + writer.endElement(tag); + } + + /* Helpers */ + protected void renderLabel(Column column, ResponseWriter writer) throws IOException + { + String title=null; + // Check for short form + if (helper.hasFormat("short")) + { + title = StringUtils.toString(column.getAttribute(TagRenderHelper.COLATTR_ABBR_TITLE)); + if (title==null) + log.warn("No Abbreviation available for column {}. Using normal title.", column.getName()); + } + // Use normal title + if (title==null) + title=column.getTitle(); + // render now + title = helper.getDisplayText(title); + writer.append((StringUtils.isEmpty(title) ? " " : title)); + } + + protected String writeStartElement(String title, ResponseWriter writer) + throws IOException + { + Map map = getAttributes(); + String tag = StringUtils.toString(map.get("tag")); + // Check + if (tag == null && title == null && !map.containsKey("styleClass")) + return null; + // Write tag + if (StringUtils.isEmpty(tag)) + tag="span"; + writer.startElement(tag, this); + helper.writeAttribute(writer, "class", helper.getTagStyleClass()); + helper.writeAttribute(writer, "style", map.get("style")); + helper.writeAttribute(writer, "title", title); + return tag; + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/TitleTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ValueTag.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ValueTag.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ValueTag.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ValueTag.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.components; + +import java.io.IOException; +import java.util.Map; + +import javax.faces.component.UIOutput; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.StringUtils; +import org.apache.empire.jsf2.controls.FieldRenderer; +import org.apache.empire.jsf2.utils.TagRenderHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ValueTag extends UIOutput // implements NamingContainer +{ + + // Logger + private static final Logger log = LoggerFactory.getLogger(ValueTag.class); + + private TagRenderHelper helper = new TagRenderHelper(this, "eVal"); + + public ValueTag() + { + log.trace("component value created"); + } + + @Override + public String getFamily() + { + return "javax.faces.NamingContainer"; + } + + @Override + public void encodeBegin(FacesContext context) + throws IOException + { + + // add label and input components when the view is loaded for the first time + super.encodeBegin(context); + + helper.encodeBegin(); + FieldRenderer renderer = helper.getFieldRenderer(); + FieldRenderer.ValueInfo vi = helper.getValueInfo(context); + + // render components + ResponseWriter writer = context.getResponseWriter(); + String tag = writeStartElement(vi, writer); + renderer.renderValue(vi, writer); + if (tag != null) + writer.endElement(tag); + } + + protected String writeStartElement(FieldRenderer.ValueInfo vi, ResponseWriter writer) + throws IOException + { + Map map = getAttributes(); + String tag = StringUtils.toString(map.get("tag")); + String title = StringUtils.toString(map.get("title")); + // Check + if (tag == null && title == null && !map.containsKey("styleClass")) + return null; + // Write tag + if (StringUtils.isEmpty(tag)) + tag="span"; + writer.startElement(tag, this); + helper.writeAttribute(writer, "class", helper.getTagStyleClass()); + helper.writeAttribute(writer, "style", map.get("style")); + helper.writeAttribute(writer, "title", helper.getValueTooltip(title)); + return tag; + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ValueTag.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRenderer.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRenderer.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRenderer.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRenderer.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,439 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.controls; + +import java.io.IOException; +import java.util.List; +import java.util.Locale; + +import javax.faces.component.UIComponent; +import javax.faces.component.UIInput; +import javax.faces.context.FacesContext; +import javax.faces.context.ResponseWriter; + +import org.apache.empire.commons.Options; +import org.apache.empire.commons.StringUtils; +import org.apache.empire.data.Column; +import org.apache.empire.exceptions.ObjectNotValidException; +import org.apache.empire.exceptions.UnexpectedReturnValueException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class FieldRenderer +{ + /* + public static class ColumnValueValidator implements Validator, StateHolder + { + private static final Logger log = LoggerFactory.getLogger(ColumnValueValidator.class); + + private Column column; + + public ColumnValueValidator() + { + } + + public ColumnValueValidator(Column column) + { + this.column=column; + } + + @Override + public void validate(FacesContext context, UIComponent component, Object value) + throws ValidatorException + { + try { + log.info("ColumnValueValidator:validate for column "+column.getName()+" value is: "+String.valueOf(value)); + column.validate(value); + if (value.equals("test")) + throw new FieldIllegalValueException(column, String.valueOf(value)); + + } catch(Exception e) { + FacesMessage msg = new FacesMessage(e.getLocalizedMessage()); + throw new ValidatorException(msg); + } + } + + @Override + public Object saveState(FacesContext context) + { + /-- * + try + { // Serialization test + String columnId = ((DBColumn)column).getId(); + + DBColumn c = DBColumn.findById(columnId); + if (c==column) + log.info("success!"); + + // findByClass test + DBDatabase fdb = DBDatabase.findByClass(FinDB.class); + log.info(fdb.getId()); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(column); + // oos.writeObject(fdb); + oos.flush(); + String info = baos.toString(); + System.out.println(info); + byte[] bytes = baos.toByteArray(); + int size = bytes.length; + System.out.println("Size is "+String.valueOf(size)); + + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + ObjectInputStream ois = new ObjectInputStream(bais); + Object obj = ois.readObject(); + + if (obj instanceof FinDB) + { + System.out.println("Hurra Database!"); + } + if (obj instanceof Column) + { + if (column==(Column)obj) + System.out.println("Hurra Column!"); + } + } + catch (ClassNotFoundException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + * --/ + return column; + } + + @Override + public void restoreState(FacesContext context, Object state) + { + if (state instanceof Column) + column = (Column)state; + } + + @Override + public boolean isTransient() + { + return false; + } + + @Override + public void setTransient(boolean newTransientValue) + { + } + } + */ + + private static final Logger log = LoggerFactory.getLogger(FieldRenderer.class); + + // Special Input Column Attributes + public static final String NUMBER_TYPE_ATTRIBUTE = "numberType"; // "Integer", "Currency", "Percent" + public static final String NUMBER_GROUPSEP_ATTRIBUTE = "numberGroupSeparator"; // boolean + public static final String NUMBER_FRACTION_DIGITS = "numberFractionDigits"; // integer + public static final String MINVALUE_ATTRIBUTE = "minValue"; + public static final String MAXVALUE_ATTRIBUTE = "maxValue"; + public static final String CURRENCY_CODE_ATTRIBUTE = "currencyCode"; // "ISO 4217 code of the currency" + + public FieldRenderer() + { + log.info("FieldRenderer created"); + } + + /** + * This interface allows access to a value and its metainformation + * used with the renderData function + */ + public interface ValueInfo + { + Column getColumn(); + Options getOptions(); + Object getValue(); + Object getNullValue(); + String getFormat(); // Custom Formatting options specific to each InputControl-type + Locale getLocale(); + String getText(String key); + /* + String getOnclick(); + String getOndblclick(); + String getCssClass(); + String getCssStyle(); + String getId(); + */ + } + + /** + * This interface extends the value information by information about the input control + * used with the renderInput function + */ + public interface InputInfo extends ValueInfo + { + int getHSize(); + int getVSize(); + boolean isRequired(); + boolean isDisabled(); // readOnly + String getInputId(); + String getTabindex(); + String getStyleClass(String addlStyle); + // perform action + void setValue(Object value); + void validate(Object value); + + // String getAccesskey(); + /* + String getName(); + boolean isValid(); // Indicates whether the value supplied is valid + String getOnchange(); + String getOnfocus(); + String getOnblur(); + */ + } + + private String name; + + protected FieldRenderer(String name) + { + this.name = name; + } + + public final String getName() + { + return name; + } + + /* Value */ + public void renderValue(ValueInfo vi, ResponseWriter writer) + throws IOException + { + String text = formatValue(vi); + writer.append((StringUtils.isEmpty(text) ? " " : text)); + } + + /* Input */ + public void renderInput(UIComponent comp, InputInfo ii, FacesContext context, boolean encode) + throws IOException + { + int count = comp.getChildCount(); + if (count<1) + createInputComponents(comp, ii, context, comp.getChildren()); + // Encode all + if (!encode) + return; + for (UIComponent child : comp.getChildren()) + { + child.encodeAll(context); + } + } + + public Object getInputValue(UIComponent comp, InputInfo ii, boolean submitted) + { + UIInput input = getInputComponent(comp); + if (input==null) + throw new ObjectNotValidException(this); + + // Get value from Input + return (submitted) ? input.getSubmittedValue() : input.getValue(); + } + + /* validate + public boolean validateValue(UIComponent comp, InputInfo ii, FacesContext context) + { + UIInput input = getInputComponent(comp); + if (input==null) + throw new ObjectNotValidException(this); + + input.validate(context); + if (input.isValid()==false) + return false; + /-* + Object value = getInputValue(comp, ii, context, false); + try { + ii.getColumn().validate(value); + Object xxx = input.getLocalValue(); + // Wert geändert? + Object previous = ii.getValue(); + if (ObjectUtils.compareEqual(value, previous)==false) + { + comp.queueEvent(new ValueChangeEvent(comp, previous, value)); + // Wert setzen + ii.setValue(value, true); + } + return true; + + } catch(Exception e) { + // Add Error Messgae + String text = e.getLocalizedMessage(); + FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, text, text); + context.addMessage(comp.getClientId(), msg); + // Invalid + ii.setValue(value, false); + return false; + } + *-/ + return true; + } + */ + + /* Input helpers */ + protected abstract void createInputComponents(UIComponent parent, InputInfo ii, FacesContext context, List compList); + + protected UIInput getInputComponent(UIComponent parent) + { + // default implementation + int count = parent.getChildCount(); + if (count!=1) + return null; + // the input + UIComponent inp = parent.getChildren().get(0); + if (!(inp instanceof UIInput)) + throw new UnexpectedReturnValueException(inp, "comp.getChildren().get(0)"); + // OK + return (UIInput)inp; + } + + protected void copyAttributes(UIComponent parent, InputInfo ii, UIInput input, String additonalStyle) + { + String inputId = ii.getInputId(); + if (StringUtils.isNotEmpty(inputId)) + input.getAttributes().put("id", inputId); + + String styleClass = ii.getStyleClass(additonalStyle); + input.getAttributes().put("styleClass", styleClass); + + copyAttribute(parent, input, "style"); + copyAttribute(parent, input, "tabindex"); + copyAttribute(parent, input, "onchange"); + + // validator + // input.addValidator(new ColumnValueValidator(ii.getColumn())); + + // IceFaces special + /* + if (input instanceof IceExtended) + { // partialSubmit + Object v = parent.getAttributes().get("partialSubmit"); + if (v!=null) + ((IceExtended)input).setPartialSubmit(ObjectUtils.getBoolean(v)); + } + */ + } + + protected final void copyAttributes(UIComponent parent, InputInfo ii, UIInput input) + { + copyAttributes(parent, ii, input, null); + } + + protected void copyAttribute(UIComponent parent, UIInput input, String name) + { + Object value = parent.getAttributes().get(name); + if (value!=null) + input.getAttributes().put(name, value); + } + + /** + * Returns the value formated as a string + * this is a simple default implementation that does no type-secific formatting + * Derived classes may override formatString an provide further formmatting + * see TextInputControl for details + * + * @param value the value to be formatted + * @param vi Meta-information about the value + * + * @return the formatted value + */ + protected String formatValue(Object value, ValueInfo vi, boolean hasError) + { + // Lookup and Print value + Options options = vi.getOptions(); + if (options != null && !options.isEmpty() && !hasFormatOption(vi, "nolookup")) + { // Check for Options + String text = options.get(value); + if (text != null) + return vi.getText(text); + // Error + log.error("The element '" + String.valueOf(value) + "' is not part of the supplied option list."); + } + // value + if (value==null) + value = vi.getNullValue(); + // Convert to String + String s = StringUtils.valueOf(value); + if (hasFormatOption(vi, "noencode")) + return s; + // Encode Html + return escapeHTML(s); + } + + /** + * Returns the value formated as a string + * This is a shortcut for formatString(vi.getValue(), vi) + * Derived classes may override formatString + */ + protected final String formatValue(ValueInfo vi) + { + boolean hasError = false; // ((vi instanceof InputInfo) && !((InputInfo)vi).isValid()); + return formatValue(vi.getValue(), vi, hasError); + } + + /** + * escapes a String for html + * @param text + * @return the escaped html String + */ + protected String escapeHTML(String text) + { + // TODO + return text; + } + + /** + * checks if a particular formating option has been specified. + * @param vi the value info + * @param option the formating option to check + * @return true if the requested formating option has been specified or false otherwise + */ + protected boolean hasFormatOption(ValueInfo vi, String option) + { + String format = vi.getFormat(); + return (format!=null ? format.indexOf(option)>=0 : false); + } + + protected String getFormatOption(ValueInfo vi, String option) + { + // Is unit supplied with format + String format = vi.getFormat(); + if (format==null) + return null; + // Check for option + int beg = format.indexOf(option); + if (beg < 0) + return null; + // Find + beg = beg + option.length(); + int end = format.indexOf(';', beg+1); + if (end < beg) + return format.substring(beg); + // The cbValue + return format.substring(beg, end); + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRenderer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRendererManager.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRendererManager.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRendererManager.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRendererManager.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.controls; + +import java.util.HashMap; + +public final class FieldRendererManager +{ + static HashMap rendererMap = null; + + static { + + rendererMap = new HashMap(); + + registerRenderer(new TextFieldRenderer()); + registerRenderer(new SelectFieldRenderer()); + registerRenderer(new TextAreaFieldRenderer()); + /* + registerControl("checkbox", new CheckboxInputControl()); + registerControl("phone", new PhoneInputControl()); + registerControl("radio", new RadioInputControl()); + registerControl("email", new EMailInputControl()); + registerControl("hlink", new HLinkInputControl()); + registerControl("password", new PasswordInputControl()); + */ + } + + private FieldRendererManager() + { + // Default Constructor + } + + public static void registerRenderer(FieldRenderer renderer) + { + rendererMap.put(renderer.getName(), renderer); + } + + public static FieldRenderer getRenderer(String name) + { + return rendererMap.get(name); + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/FieldRendererManager.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/SelectFieldRenderer.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/SelectFieldRenderer.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/SelectFieldRenderer.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/SelectFieldRenderer.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.controls; + +import java.util.List; + +import javax.faces.component.UIComponent; +import javax.faces.component.UISelectItem; +import javax.faces.component.html.HtmlSelectOneMenu; +import javax.faces.context.FacesContext; + +import org.apache.empire.commons.OptionEntry; +import org.apache.empire.commons.Options; +import org.apache.empire.data.Column; +import org.apache.empire.exceptions.InternalException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SelectFieldRenderer extends FieldRenderer +{ + private static final Logger log = LoggerFactory.getLogger(SelectFieldRenderer.class); + + public static final String COLATTR_ABBR_OPTIONS = "ABBR_OPTIONS"; // Option list for abbreviations + + public static final String NAME = "select"; + + private Class inputComponentClass; + + public SelectFieldRenderer(Class inputComponentClass) + { + super(NAME); + this.inputComponentClass = inputComponentClass; + } + + public SelectFieldRenderer() + { + this(javax.faces.component.html.HtmlSelectOneMenu.class); + } + + @Override + protected void createInputComponents(UIComponent parent, InputInfo ii, FacesContext context, List compList) + { + HtmlSelectOneMenu input; + try { + input = inputComponentClass.newInstance(); + } catch (InstantiationException e1) { + throw new InternalException(e1); + } catch (IllegalAccessException e2) { + throw new InternalException(e2); + } + copyAttributes(parent, ii, input); + + Options options = ii.getOptions(); + if (ii.isRequired()==false) + { // Empty entry + options = new Options(options); + addSelectItem(input, ii, new OptionEntry("", getNullText(ii))); + } + for (OptionEntry e : options) + { // Option entries + addSelectItem(input, ii, e); + } + + input.setValue(ii.getValue()); + input.setDisabled(ii.isDisabled()); + + compList.add(input); + } + + private String getNullText(InputInfo ii) + { + String nullText = getFormatOption(ii, "null:"); + if (nullText==null) + nullText=""; + return nullText; + } + + private void addSelectItem(UIComponent input, InputInfo ii, OptionEntry e) + { + UISelectItem selectItem = new UISelectItem(); + // set value + selectItem.setItemValue(e.getValueString()); + // set text + String text = e.getText(); + text = ii.getText(text); + selectItem.setItemLabel(text); + // add item + input.getChildren().add(selectItem); + } + + @Override + protected String formatValue(Object value, ValueInfo vi, boolean hasError) + { + // Lookup and Print value + if (vi.getOptions()==null) + { + log.warn("Select field {} has no Option list attached!", vi.getColumn().getName()); + return super.formatValue(value, vi, hasError); + } + // Check for Abbreviation + if (hasFormatOption(vi, "short")) + { + Column column = vi.getColumn(); + if (column!=null) + { // Check for Abbreviation option list + Object attrValue = column.getAttribute(COLATTR_ABBR_OPTIONS); + if (attrValue instanceof Options) + { // Check for Options + String text = ((Options)attrValue).get(value); + if (text != null) + return vi.getText(text); + // Error + log.error("The element '" + String.valueOf(value) + "' is not part of the supplied option list."); + } + } + } + return super.formatValue(value, vi, hasError); + } + + /* + @Override + public void renderInput(ResponseWriter writer, ControlInfo ci) + { + boolean disabled = ci.getDisabled(); + + HtmlTag input = writer.startTag("select"); + input.addAttribute("id", ci.getId()); + input.addAttribute("class", ci.getCssClass()); + input.addAttribute("style", ci.getCssStyle()); + if (disabled) + { + input.addAttribute("disabled"); + } else + { + input.addAttribute("name", ci.getName()); + } + // Event Attributes + input.addAttribute("onclick", ci.getOnclick()); + input.addAttribute("onchange", ci.getOnchange()); + input.addAttribute("onfocus", ci.getOnfocus()); + input.addAttribute("onblur", ci.getOnblur()); + input.beginBody(true); + // Render List of Options + Options options = ci.getOptions(); + if (options!=null) + { // Render option list + Object current = ci.getValue(); + if (hasFormatOption(ci, "allownull") && options.contains(null)==false) + { // add an empty entry + addEmtpyEntry(writer, ObjectUtils.isEmpty(current)); + } + for (OptionEntry entry : options) + { + Object value = entry.getValue(); + boolean isCurrent = ObjectUtils.compareEqual(current, value); + if (isCurrent == false && disabled) + continue; // + // Add Option entry + HtmlTag option = writer.startTag("option"); + option.addAttributeNoCheck("value", value, true); + option.addAttribute("selected", isCurrent); + option.beginBody(ci.getTranslation(entry.getText())); + option.endTag(true); + } + } + else + { // No Option list available + log.error("No options available for select input control."); + } + // done + input.endTag(); + } + + private void addEmtpyEntry(HtmlWriter writer, boolean isCurrent) + { + // Add Option entry + HtmlTag option = writer.startTag("option"); + option.addAttributeNoCheck("value", "", false); + option.addAttribute("selected", isCurrent); + option.beginBody(""); + option.endTag(true); + } + */ + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/SelectFieldRenderer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextAreaFieldRenderer.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextAreaFieldRenderer.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextAreaFieldRenderer.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextAreaFieldRenderer.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.controls; + +import java.util.List; + +import javax.faces.component.UIComponent; +import javax.faces.component.html.HtmlInputTextarea; +import javax.faces.context.FacesContext; + +import org.apache.empire.exceptions.InternalException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TextAreaFieldRenderer extends FieldRenderer +{ + private static final Logger log = LoggerFactory.getLogger(TextAreaFieldRenderer.class); + + public static final String NAME = "textarea"; + + private Class inputComponentClass; + + public TextAreaFieldRenderer(Class inputComponentClass) + { + super(NAME); + this.inputComponentClass = inputComponentClass; + } + + public TextAreaFieldRenderer() + { + this(javax.faces.component.html.HtmlInputTextarea.class); + } + + @Override + protected void createInputComponents(UIComponent parent, InputInfo ii, FacesContext context, List compList) + { + HtmlInputTextarea input; + try { + input = inputComponentClass.newInstance(); + } catch (InstantiationException e1) { + throw new InternalException(e1); + } catch (IllegalAccessException e2) { + throw new InternalException(e2); + } + copyAttributes(parent, ii, input); + + int cols = ii.getHSize(); + if (cols>0) + input.setCols(cols); + + int rows = ii.getVSize(); + if (rows>0) + input.setRows(rows); + + input.setDisabled(ii.isDisabled()); + input.setValue(ii.getValue()); + + compList.add(input); + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextAreaFieldRenderer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextFieldRenderer.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextFieldRenderer.java?rev=1326123&view=auto ============================================================================== --- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextFieldRenderer.java (added) +++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextFieldRenderer.java Sat Apr 14 14:57:28 2012 @@ -0,0 +1,457 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.jsf2.controls; + +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.Currency; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import javax.faces.component.UIComponent; +import javax.faces.component.html.HtmlInputText; +import javax.faces.context.FacesContext; + +import org.apache.empire.commons.ObjectUtils; +import org.apache.empire.commons.Options; +import org.apache.empire.commons.StringUtils; +import org.apache.empire.data.Column; +import org.apache.empire.data.DataType; +import org.apache.empire.exceptions.InternalException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TextFieldRenderer extends FieldRenderer +{ + private static final Logger log = LoggerFactory.getLogger(TextFieldRenderer.class); + + public static final String NAME = "text"; + + public static final String FORMAT_UNIT = "unit:"; + + private Class inputComponentClass; + + public TextFieldRenderer(String name, Class inputComponentClass) + { + super(name); + this.inputComponentClass = inputComponentClass; + } + + public TextFieldRenderer(String name) + { + this(name, javax.faces.component.html.HtmlInputText.class); + } + + public TextFieldRenderer() + { + this(NAME, javax.faces.component.html.HtmlInputText.class); + } + + @Override + protected void createInputComponents(UIComponent parent, InputInfo ii, FacesContext context, List compList) + { + HtmlInputText input; + try { + input = inputComponentClass.newInstance(); + } catch (InstantiationException e1) { + throw new InternalException(e1); + } catch (IllegalAccessException e2) { + throw new InternalException(e2); + } + copyAttributes(parent, ii, input); + + int maxLength = 0; + DataType type = ii.getColumn().getDataType(); + switch(type) + { + case CHAR: + case TEXT: + maxLength = ((int) Math.round(ii.getColumn().getSize())); + break; + case DECIMAL: + maxLength = ((int) Math.round(ii.getColumn().getSize()))+1; + break; + default: + maxLength = 0; + } + if (maxLength>0) + input.setMaxlength(maxLength); + + /* + if (ii.getColumn().getName().equals("TITLE_DE")) + log.info(ii.getColumn().getName()); + */ + + input.setDisabled(ii.isDisabled()); // || ii.getColumn().isAutoGenerated() + input.setValue(ii.getValue()); + + compList.add(input); + } + + // ------- parsing ------- + + /* + @Override + protected Object parseValue(String value, Locale locale, Column column) + { + // Check Data Type + DataType type = column.getDataType(); + if (type==DataType.TEXT) + return value; + // Check other types + if (type==DataType.INTEGER) + { return parseInteger(value); + } + if (type==DataType.DECIMAL) + { return parseDecimal(value, getNumberFormat(column.getDataType(), locale, column)); + } + if (type==DataType.DATE || type==DataType.DATETIME) + { return parseDate(value, getDateFormat(column.getDataType(), locale, column)); + } + if (type==DataType.AUTOINC) + { // autoinc + log.error("Autoinc-value cannot be changed."); + return NO_VALUE; + } + // Default + return value; + } + + // ------- validation ------- + + @Override + protected Object validate(Object o, Locale locale, Column column, String s) + { + if (o instanceof Number) + { + Object min = column.getAttribute(FieldRenderer.MINVALUE_ATTRIBUTE); + Object max = column.getAttribute(FieldRenderer.MAXVALUE_ATTRIBUTE); + if (min!=null && max!=null) + { + Number n = (Number)o; + if (n.intValue()ObjectUtils.getInteger(max)) + { // Out of Range + return error(WebErrors.InputValueOutOfRange, new String[] { min.toString(), max.toString() }, s); + } + } + } + return o; + } + */ + + // ------- formatting ------- + + @Override + protected String formatValue(Object value, ValueInfo vi, boolean hasError) + { + // Lookup and Print value + Options options = vi.getOptions(); + if (options != null && !options.isEmpty()) + { // Check for Options + String text = options.get(value); + if (text != null) + return vi.getText(text); + // Error + log.error("The element '" + String.valueOf(value) + "' is not part of the supplied option list."); + } + // Check Value + if (value == null) + { // Try to use default value + if (value!=vi.getNullValue()) + return formatValue(vi.getNullValue(), vi, false); + // Empty String + return ""; + } + // Format Value + Column column = vi.getColumn(); + DataType dataType = getValueType(value, (column != null) ? column.getDataType() : DataType.UNKNOWN); + if (dataType == DataType.TEXT || dataType == DataType.UNKNOWN) + { // String + String s = String.valueOf(value); + if (hasFormatOption(vi, "noencode")) + return s; + // Encoded text + return escapeHTML(s); + } + if (dataType == DataType.INTEGER || dataType == DataType.AUTOINC) + { // Integer + NumberFormat nf = NumberFormat.getIntegerInstance(vi.getLocale()); + nf.setGroupingUsed(false); + return nf.format(value); + } + if (dataType == DataType.DECIMAL || dataType == DataType.FLOAT) + { // Dezimal oder Double + NumberFormat nf = getNumberFormat(dataType, vi.getLocale(), column); + return nf.format(value); + } + if (dataType == DataType.DATE || dataType == DataType.DATETIME) + { // Date or DateTime + if (dataType== DataType.DATETIME && hasFormatOption(vi, "notime")) + dataType = DataType.DATE; + // Now format the date according to the user's locale + DateFormat df = getDateFormat(dataType, vi.getLocale(), column); + return df.format(value); + } + /* + * if (dataType == DBDataType.BOOL) { + * } + */ + // Convert to String + return escapeHTML(String.valueOf(value)); + } + + protected String formatValue(ValueInfo vi, boolean appendUnit) + { + String text = super.formatValue(vi); + if (appendUnit && text!=null && text.length()>0) + { + String unit = getUnitString(vi); + if (unit != null) + { // Append unit + text += " " + unit; + } + } + return text; + } + + // ------- render ------- + + /* + @Override + public void renderInput(Response writer, ControlInfo ci) + { + HtmlTag input = writer.startTag("input"); + input.addAttribute("type", "text"); + input.addAttribute("id", ci.getId()); + input.addAttribute("class", ci.getCssClass()); + input.addAttribute("style", ci.getCssStyle()); + if (ci.getDisabled()==false) + { // Name of the field + input.addAttribute("name", ci.getName()); + // Get Max Length + int maxLength = getMaxInputLength(ci.getColumn()); + if (maxLength>0) + { + input.addAttribute("maxlength", maxLength); + input.addAttribute("size", String.valueOf(Math.min(maxLength, ci.getHSize()))); + } + } + else + { // Disabled text control + input.addAttribute("disabled"); + // Get Max Length + int maxLength = getMaxInputLength(ci.getColumn()); + if (maxLength>0) + { + input.addAttribute("size", String.valueOf(Math.min(maxLength, ci.getHSize()))); + } + } + // Value + input.addAttribute("value", formatValue(ci, ci.getDisabled())); + // Event Attributes + input.addAttribute("onclick", ci.getOnclick()); + input.addAttribute("onchange", ci.getOnchange()); + input.addAttribute("onfocus", ci.getOnfocus()); + input.addAttribute("onblur", ci.getOnblur()); + input.endTag(); + // Add Unit + if (ci.getDisabled()==false) + { + String unit = getUnitString(ci); + if (unit != null) + { writer.print(" "); + writer.print(unit); + } + } + } + + // ------- Input Helpers ------- + + protected int getMaxInputLength(Column col) + { + // cast to DBTableColumn + DataType type = col.getDataType(); + if (type==DataType.AUTOINC || + type==DataType.INTEGER) + return 10; + if (type==DataType.DOUBLE) + return 18; + if (type==DataType.DECIMAL) + { + double size = col.getSize(); + int len = (int)size; + size = (size - len)*10; // Ganzahlanteil + if (((int)size)>0) + len += ((int)size)+1; // Dezimaltrenner plus Nachkommastellen + return len; + } + if (type==DataType.BOOL) + return 1; + if (type==DataType.DATE) + return 10; + if (type==DataType.DATETIME) + return 16; + if (type==DataType.CLOB) + return 0; // unlimited (use 0x7FFFFFFF instead?) + // Default + return (int)col.getSize(); + } + */ + + protected DataType getValueType(Object value, DataType desiredType) + { + // Detect Data Type from Value + if (value instanceof String) + return DataType.TEXT; + if (value instanceof Number) + { // Check desired type + if (desiredType == DataType.AUTOINC || desiredType == DataType.INTEGER || + desiredType == DataType.FLOAT || desiredType == DataType.DECIMAL) + return desiredType; + // Detect type + if (value instanceof Integer || value instanceof Long || value instanceof Short) + return DataType.INTEGER; + if (value instanceof Float || value instanceof Double) + return DataType.FLOAT; + // default + return DataType.DECIMAL; + } + if (value instanceof Date) + { // Check desired type + if (desiredType == DataType.DATETIME || desiredType == DataType.DATE) + return desiredType; + // Detect type + if (value instanceof Timestamp) + return DataType.DATETIME; + // Just a date + return DataType.DATE; + } + if (value instanceof Boolean) + return DataType.BOOL; + // Default Datatype + return DataType.UNKNOWN; + } + + protected NumberFormat getNumberFormat(DataType dataType, Locale locale, Column column) + { + if (column==null) + return NumberFormat.getNumberInstance(locale); + // Column is supplied + String type = StringUtils.valueOf(column.getAttribute(FieldRenderer.NUMBER_TYPE_ATTRIBUTE)); + NumberFormat nf = null; + if (type.equalsIgnoreCase("Integer")) + nf = NumberFormat.getIntegerInstance(locale); + else + nf = NumberFormat.getNumberInstance(locale); + // Groups Separator? + Object groupSep = column.getAttribute(FieldRenderer.NUMBER_GROUPSEP_ATTRIBUTE); + if (groupSep!=null) + nf.setGroupingUsed(ObjectUtils.getBoolean(groupSep)); + // Fraction Digits? + Object fractDigit = column.getAttribute(FieldRenderer.NUMBER_FRACTION_DIGITS); + if (fractDigit!=null) + { int fractionDigits = ObjectUtils.getInteger(fractDigit); + nf.setMaximumFractionDigits(fractionDigits); + nf.setMinimumFractionDigits(fractionDigits); + } + // Number format + return nf; + } + + protected DateFormat getDateFormat(DataType dataType, Locale locale, Column column) + { + DateFormat df; + if (dataType==DataType.DATE) + df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); + else + df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale); + return df; + } + + private String getUnitString(ValueInfo vi) + { + // Is unit supplied as a format option + String format = getFormatOption(vi, FORMAT_UNIT); + if (format!=null) + return format; + // Is it a currency column + Column column = vi.getColumn(); + if (column!=null && column.getDataType()==DataType.DECIMAL) + { + String numberType = StringUtils.toString(column.getAttribute(FieldRenderer.NUMBER_TYPE_ATTRIBUTE)); + if (numberType!=null) + { + if (numberType.equalsIgnoreCase("Currency")) + { + String currencyCode = StringUtils.toString(column.getAttribute(FieldRenderer.CURRENCY_CODE_ATTRIBUTE)); + if (currencyCode!=null) + { // nf = NumberFormat.getCurrencyInstance(locale); + Currency currency = Currency.getInstance(currencyCode); + return (currency!=null) ? currency.getSymbol() : null; + } + } else if (numberType.equalsIgnoreCase("Percent")) + { + return "%"; + } + } + } + // No Unit supplied + return null; + } + + // ------- value parsing ------- + + protected Object parseInteger(String s) + { + // Try to convert + return Integer.parseInt(s); + } + + protected Object parseDecimal(String s, NumberFormat nf) + { + // Try to convert + for (int i=0; i='A') + throw new NumberFormatException("Not a number: "+s); + } + // Parse String + try { + return nf.parseObject(s); + } catch(ParseException pe) { + throw new NumberFormatException("Not a number: "+s+" Exception: "+pe.toString()); + } + } + + protected Object parseDate(String s, DateFormat df) + { + // Try to convert + try { + // Parse Date + df.setLenient(true); + return df.parseObject(s); + } catch(ParseException pe) { + throw new RuntimeException("Invalid date format: "+s, pe); + } + } + +} Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/controls/TextFieldRenderer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain