Java tutorial
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. Portions Copyrighted 2012 Daniel * Huss. * * The contents of this file are subject to the terms of either the GNU General Public License * Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, * the "License"). You may not use this file except in compliance with the License. You can obtain a * copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. * See the License for the specific language governing permissions and limitations under the * License. When distributing the software, include this License Header Notice in each file and * include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this particular file * as subject to the "Classpath" exception as provided by Sun in the GPL Version 2 section of the * License file that accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original Software is Sun * Microsystems, Inc. Portions Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, * indicate your decision by adding "[Contributor] elects to include this software in this * distribution under the [CDDL or GPL Version 2] license." If you do not indicate a single choice * of license, a recipient has the option to distribute your version of this file under either the * CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. * However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the * option applies only if the new code is made subject to such option by the copyright holder. */ package de.unentscheidbar.validation.swing; import java.io.Serializable; import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.ResourceBundle.Control; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; import org.apache.commons.lang3.ClassUtils; import de.unentscheidbar.validation.ValidationMessage; import de.unentscheidbar.validation.ValidationMessage.Id; @Immutable final class DefaultMessageTextGetter implements MessageTextGetter, Serializable { private static final long serialVersionUID = Validation.VERSION; static final MessageTextGetter INSTANCE = new DefaultMessageTextGetter(); /** * Reads and formats a message from a resource bundle that is named "Messages" and lives in the * same package as the class of the message ID (obtained using {@link #getClass()}). * <p> * Examples:<br/> * <ul> * <li>Message ID {@code org.netbeans.validation.SampleValidator$Id} uses resource bundle with * base name "org.netbeans.validation.Messages"</li> * <li>Class {@code org.netbeans.validation.AnotherValidatorId} also uses resource bundle with * base name "org.netbeans.validation.Messages"</li> * <li>Class {@code HelloWorld} (in default namespace) uses resource bundle with base name * "Messages"</li> * </ul> * </p> * <p> * Note: This method returs the string value of the message ID in case no bundle or message is * found instead of throwing {@link MissingResourceException}. * </p> * * @param message * The message containing an identifier and message details (formatting parameters). * @param locale * The locale to use when looking up resource bundles. If {@code null}, defaults to * the default display locale. * @return Formatted message. */ @Override public @Nonnull CharSequence getMessageText(ValidationMessage message, Locale locale) { try { return MessageFormat.format(getFormatString(message.id(), locale), message.details().toArray()); } catch (MissingResourceException e) { return message.id().toString(); } } private String getBundleName(ValidationMessage.Id messageId) { String bundleName = ClassUtils.getPackageName(messageId.getClass()); bundleName = bundleName.isEmpty() ? "Messages" : bundleName + ".Messages"; return bundleName; } private String getFormatString(ValidationMessage.Id messageId, Locale locale) throws MissingResourceException { ResourceBundle bundle = ResourceBundle.getBundle(getBundleName(messageId), locale, messageId.getClass().getClassLoader()); return String.valueOf(bundle.getObject(messageId.name())); } private static class StrictControl extends Control implements Serializable { @Override public Locale getFallbackLocale(String baseName, Locale locale) { return null; } } private static final DefaultMessageTextGetter.StrictControl STRICT_CONTROL = new StrictControl(); @Override public boolean hasText(Id messageId, Locale locale) { try { String bundleName = getBundleName(messageId); ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, messageId.getClass().getClassLoader(), STRICT_CONTROL); Object resource = bundle.getObject(messageId.name()); return resource != null; } catch (MissingResourceException e) { return false; } } }