You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2008/04/18 11:27:47 UTC
svn commit: r649423 - in
/myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago:
application/ compat/ util/
Author: bommel
Date: Fri Apr 18 02:27:41 2008
New Revision: 649423
URL: http://svn.apache.org/viewvc?rev=649423&view=rev
Log:
(TOBAGO-650) Support for validatorMessage, requiredMessage, converterMessage and label attribute for EditableValueHolder
Added:
myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/
- copied from r647199, myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/application/
myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java (with props)
myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java (with props)
myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java (with props)
Modified:
myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/compat/FacesUtils.java
Added: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java?rev=649423&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java (added)
+++ myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java Fri Apr 18 02:27:41 2008
@@ -0,0 +1,63 @@
+package org.apache.myfaces.tobago.application;
+
+import org.apache.myfaces.tobago.util.MessageUtils;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+import javax.faces.webapp.UIComponentTag;
+import java.util.Locale;
+
+@SuppressWarnings("deprecation")
+public class LabelValueBindingFacesMessage extends FacesMessage {
+ private Locale locale;
+ private Object[] args;
+
+ public LabelValueBindingFacesMessage() {
+ super();
+ }
+
+ public LabelValueBindingFacesMessage(FacesMessage.Severity severity, String summary, String detail,
+ Locale locale, Object... args) {
+ super(severity, summary, detail);
+ this.locale = locale;
+ this.args = args;
+ }
+
+ public LabelValueBindingFacesMessage(String summary, String detail) {
+ super(summary, detail);
+ }
+
+ public LabelValueBindingFacesMessage(String summary) {
+ super(summary);
+ }
+
+ @Override
+ public String getDetail() {
+ String detail = super.getDetail();
+ if (args != null && args.length > 0) {
+ if (args.length == 1 && UIComponentTag.isValueReference(args[0].toString())) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ValueBinding value = facesContext.getApplication().createValueBinding(detail);
+ return MessageUtils.getFormatedMessage(detail, locale, value.getValue(facesContext));
+ }
+ return MessageUtils.getFormatedMessage(detail, locale, args);
+ }
+ return detail;
+ }
+
+ @Override
+ public String getSummary() {
+ String summary = super.getSummary();
+ if (args != null && args.length > 0) {
+ if (args.length == 1 && UIComponentTag.isValueReference(args[0].toString())) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ValueBinding value = facesContext.getApplication().createValueBinding(summary);
+ return MessageUtils.getFormatedMessage(summary, locale, value.getValue(facesContext));
+ }
+ return MessageUtils.getFormatedMessage(summary, locale, args);
+ }
+ return summary;
+ }
+
+}
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueBindingFacesMessage.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java?rev=649423&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java (added)
+++ myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java Fri Apr 18 02:27:41 2008
@@ -0,0 +1,41 @@
+package org.apache.myfaces.tobago.application;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+import javax.el.ValueExpression;
+
+public class LabelValueExpressionFacesMessage extends FacesMessage {
+ public LabelValueExpressionFacesMessage() {
+ super();
+ }
+
+ public LabelValueExpressionFacesMessage(FacesMessage.Severity severity, String summary, String detail) {
+ super(severity, summary, detail);
+ }
+
+ public LabelValueExpressionFacesMessage(String summary, String detail) {
+ super(summary, detail);
+ }
+
+ public LabelValueExpressionFacesMessage(String summary) {
+ super(summary);
+ }
+
+ @Override
+ public String getDetail() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ValueExpression value = facesContext.getApplication().getExpressionFactory().
+ createValueExpression(facesContext.getELContext(), super.getDetail(), String.class);
+ return (String) value.getValue(facesContext.getELContext());
+ }
+
+ @Override
+ public String getSummary() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ValueExpression value = facesContext.getApplication().getExpressionFactory().
+ createValueExpression(facesContext.getELContext(), super.getSummary(), String.class);
+ return (String) value.getValue(facesContext.getELContext());
+ }
+
+
+}
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/application/LabelValueExpressionFacesMessage.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/compat/FacesUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/compat/FacesUtils.java?rev=649423&r1=649422&r2=649423&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/compat/FacesUtils.java (original)
+++ myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/compat/FacesUtils.java Fri Apr 18 02:27:41 2008
@@ -55,6 +55,10 @@
private static int facesVersion;
+ public static boolean isJSF12() {
+ return facesVersion == 12;
+ }
+
public static boolean invokeOnComponent(FacesContext context, UIComponent component,
String clientId, ContextCallback callback) {
String thisClientId = component.getClientId(context);
Added: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java?rev=649423&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java (added)
+++ myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java Fri Apr 18 02:27:41 2008
@@ -0,0 +1,142 @@
+package org.apache.myfaces.tobago.util;
+
+/*
+ * 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.
+ */
+
+import org.apache.myfaces.tobago.compat.FacesUtils;
+import org.apache.myfaces.tobago.application.LabelValueExpressionFacesMessage;
+import org.apache.myfaces.tobago.application.LabelValueBindingFacesMessage;
+
+import javax.faces.context.FacesContext;
+import javax.faces.component.UIComponent;
+import javax.faces.application.FacesMessage;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.MissingResourceException;
+import java.text.MessageFormat;
+
+// TODO merge with MessageFactory
+public class MessageUtils {
+
+ private static final String DETAIL_SUFFIX = "_detail";
+
+ public static void addMessage(FacesContext facesContext, UIComponent component, FacesMessage.Severity severity,
+ String messageId, Object[] args) {
+ facesContext.addMessage(component.getClientId(facesContext),
+ getMessage(facesContext, facesContext.getViewRoot().getLocale(), severity, messageId, args));
+ }
+
+ public static FacesMessage getMessage(FacesContext facesContext, Locale locale,
+ FacesMessage.Severity severity, String messageId, Object args[]) {
+
+ String detail;
+ ResourceBundle appBundle = getApplicationBundle(facesContext, locale);
+ String summary = getBundleString(appBundle, messageId);
+ if (summary != null) {
+ detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
+ } else {
+ ResourceBundle defBundle = getDefaultBundle(facesContext, locale);
+ summary = getBundleString(defBundle, messageId);
+ if (summary != null) {
+ detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
+ } else {
+ //Try to find detail alone
+ detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
+ if (detail != null) {
+ summary = null;
+ } else {
+ detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
+ if (detail != null) {
+ summary = null;
+ } else {
+ //Neither detail nor summary found
+ facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
+ return new FacesMessage(severity, messageId, null);
+ }
+ }
+ }
+ }
+
+ if (FacesUtils.isJSF12()) {
+ if (args != null && args.length > 0) {
+ MessageFormat format;
+ if (summary != null) {
+ format = new MessageFormat(summary, locale);
+ summary = format.format(args);
+ }
+
+ if (detail != null) {
+ format = new MessageFormat(detail, locale);
+ detail = format.format(args);
+ }
+ }
+ return new LabelValueExpressionFacesMessage(severity, summary, detail);
+ } else {
+ return new LabelValueBindingFacesMessage(severity, summary, detail, locale, args);
+ }
+ }
+
+ private static String getBundleString(ResourceBundle bundle, String key) {
+ try {
+ return bundle == null ? null : bundle.getString(key);
+ } catch (MissingResourceException e) {
+ return null;
+ }
+ }
+
+ private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale) {
+ String bundleName = facesContext.getApplication().getMessageBundle();
+ return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
+ }
+
+ private static ResourceBundle getDefaultBundle(FacesContext facesContext, Locale locale) {
+ return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
+ }
+
+ private static ResourceBundle getBundle(FacesContext facesContext, Locale locale, String bundleName) {
+ try {
+ return ResourceBundle.getBundle(bundleName, locale, MessageUtils.class.getClassLoader());
+ } catch (MissingResourceException ignore2) {
+ try {
+ return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader());
+ } catch (MissingResourceException damned) {
+ facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
+ return null;
+ }
+ }
+ }
+
+ public static String getLabel(FacesContext facesContext, UIComponent component) {
+ Object label = component.getAttributes().get("label");
+ if (label != null) {
+ return label.toString();
+ }
+ if (FacesUtils.hasValueBindingOrValueExpression(component, "label")) {
+ return FacesUtils.getExpressionString(component, "label");
+ }
+ return component.getClientId(facesContext);
+ }
+
+ public static String getFormatedMessage(String message, Locale locale, Object... args) {
+ if (args != null && args.length > 0 && message != null) {
+ MessageFormat format = new MessageFormat(message, locale);
+ return format.format(args);
+ }
+ return message;
+ }
+
+}
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tobago/trunk/tobago-jsf-compat/src/main/java/org/apache/myfaces/tobago/util/MessageUtils.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL