001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ResourceBundleLookupExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.strings;
030:
031: import java.util.ResourceBundle;
032:
033: import org.jfree.report.ResourceBundleFactory;
034: import org.jfree.report.function.AbstractExpression;
035: import org.jfree.report.function.ExpressionUtilities;
036:
037: /**
038: * Performs a resource-bundle lookup using the value read from the defined field as key in the resource-bundle. This
039: * expression behaves like the Resource-field.
040: *
041: * @author Thomas Morgner
042: */
043: public class ResourceBundleLookupExpression extends AbstractExpression {
044: /**
045: * The field from where to read the key value.
046: */
047: private String field;
048:
049: /**
050: * The name of the used resource bundle. If null, the default resource-bundle will be used instead.
051: */
052: private String resourceIdentifier;
053:
054: /**
055: * Default Constructor.
056: */
057: public ResourceBundleLookupExpression() {
058: }
059:
060: /**
061: * Returns the name of the datarow-column from where to read the resourcebundle key value.
062: *
063: * @return the field.
064: */
065: public String getField() {
066: return field;
067: }
068:
069: /**
070: * Defines the name of the datarow-column from where to read the resourcebundle key value.
071: *
072: * @param field the field.
073: */
074: public void setField(final String field) {
075: this .field = field;
076: }
077:
078: /**
079: * Returns the name of the resource-bundle. If none is defined here, the default resource-bundle is used instead.
080: *
081: * @return the resource-bundle identifier.
082: */
083: public String getResourceIdentifier() {
084: return resourceIdentifier;
085: }
086:
087: /**
088: * Defines name of the resource-bundle. If none is defined here, the default resource-bundle is used instead.
089: *
090: * @param resourceIdentifier the resource-bundle identifier.
091: */
092: public void setResourceIdentifier(final String resourceIdentifier) {
093: this .resourceIdentifier = resourceIdentifier;
094: }
095:
096: /**
097: * Returns the current value for the data source.
098: *
099: * @return the value.
100: */
101: public Object getValue() {
102: final Object key = getDataRow().get(getField());
103: if (key == null) {
104: return null;
105: }
106:
107: final ResourceBundleFactory resourceBundleFactory = getResourceBundleFactory();
108: final ResourceBundle bundle;
109: if (resourceIdentifier == null) {
110: bundle = ExpressionUtilities.getDefaultResourceBundle(this);
111: } else {
112: bundle = resourceBundleFactory
113: .getResourceBundle(resourceIdentifier);
114: }
115: return bundle.getObject(String.valueOf(key));
116: }
117: }
|