001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.binding.value.support; 017 018 import java.text.MessageFormat; 019 import java.util.Arrays; 020 import java.util.HashSet; 021 022 import org.springframework.binding.value.ValueModel; 023 024 /** 025 * A value model that takes a set of "argument" ValueModels, formats their 026 * values into strings, and then inserts these formatted strings into the 027 * provided pattern at the appropriate places. Any changes to the "argument" 028 * ValueModels will cause this value model to also update. 029 * 030 * @author Oliver Hutchison 031 * @see MessageFormat 032 */ 033 public class MessageFormatValueModel extends AbstractDerivedValueModel { 034 035 private final String pattern; 036 037 private final ValueModel[] argumentValueModels; 038 039 private String value; 040 041 /** 042 * Constructs a new MessageFormatValueModel with a single argument. 043 * @param pattern the pattern 044 * @param argumentValueModel the single value model holding the 045 * value to be formatted and substituted 046 */ 047 public MessageFormatValueModel(String pattern, ValueModel argumentValueModel) { 048 this(pattern, new ValueModel[] {argumentValueModel}); 049 } 050 051 /** 052 * Constructs a new MessageFormatValueModel with a group of arguments. 053 * @param pattern the pattern 054 * @param argumentValueModels an array of value models holding the 055 * value to be formatted and substituted 056 */ 057 public MessageFormatValueModel(String pattern, ValueModel[] argumentValueModels) { 058 super((ValueModel[])new HashSet(Arrays.asList(argumentValueModels)).toArray(new ValueModel[0])); 059 this.pattern = pattern; 060 this.argumentValueModels = argumentValueModels; 061 // prime the initial value 062 sourceValuesChanged(); 063 } 064 065 protected void sourceValuesChanged() { 066 String oldValue = value; 067 value = MessageFormat.format(pattern, getArgumentValues()); 068 fireValueChange(oldValue, value); 069 } 070 071 public Object getValue() { 072 return value; 073 } 074 075 private Object[] getArgumentValues() { 076 Object[] values = new Object[argumentValueModels.length]; 077 for (int i = 0; i < values.length; i++) { 078 values[i] = argumentValueModels[i].getValue(); 079 } 080 return values; 081 } 082 }