/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2009 Pentaho Corporation.. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.core.modules.gui.base.parameters;
import java.awt.Color;
import java.text.Format;
import java.util.Locale;
import java.util.TimeZone;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.pentaho.reporting.engine.classic.core.modules.gui.base.ParameterReportControllerPane;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterAttributeNames;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterContext;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterDefinitionEntry;
import org.pentaho.reporting.engine.classic.core.util.beans.BeanException;
import org.pentaho.reporting.engine.classic.core.util.beans.ConverterRegistry;
/**
* Todo: Document me!
* <p/>
* Date: 14.05.2009
* Time: 20:30:10
*
* @author Thomas Morgner.
*/
public class TextFieldParameterComponent extends JTextField implements ParameterComponent
{
private class TextUpdateHandler implements ChangeListener
{
private TextUpdateHandler()
{
}
public void stateChanged(final ChangeEvent e)
{
initialize();
}
}
private ParameterUpdateContext updateContext;
private String parameterName;
private TextComponentEditHandler handler;
/**
* Constructs a new <code>TextField</code>. A default model is created,
* the initial string is <code>null</code>,
* and the number of columns is set to 0.
*/
public TextFieldParameterComponent(final ParameterDefinitionEntry entry,
final ParameterContext parameterContext,
final ParameterUpdateContext updateContext)
{
this.updateContext = updateContext;
this.parameterName = entry.getName();
final String formatString = entry.getParameterAttribute(ParameterAttributeNames.Core.NAMESPACE,
ParameterAttributeNames.Core.DATA_FORMAT, parameterContext);
final Locale locale = parameterContext.getResourceBundleFactory().getLocale();
final TimeZone timeZone = parameterContext.getResourceBundleFactory().getTimeZone();
final Format format =
TextComponentEditHandler.createFormat(formatString, locale, timeZone, entry.getValueType());
handler =
new TextComponentEditHandler(entry.getValueType(), entry.getName(), this, updateContext, format);
setColumns(60);
getDocument().addDocumentListener(handler);
addActionListener(handler);
updateContext.addChangeListener(new TextUpdateHandler());
}
public JComponent getUIComponent()
{
return this;
}
public void initialize()
{
handler.setAdjustingToExternalInput(true);
try
{
final Object value = updateContext.getParameterValue(parameterName);
if (value != null)
{
try
{
setText(ConverterRegistry.toAttributeValue(value));
}
catch (BeanException e)
{
// ignore illegal values, set them as plain text.
setText(value.toString());
setBackground(ParameterReportControllerPane.ERROR_COLOR);
}
}
else
{
setText(null);
}
}
finally
{
handler.setAdjustingToExternalInput(false);
}
}
}