package net.xoetrope.swing;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFormattedTextField;
import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.XAttributedComponent;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DateFormatter;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;
import net.xoetrope.debug.DebugLogger;
import net.xoetrope.xui.build.BuildProperties;
/**
* <p>A wrapper for the Swing JTextField class</p>
* <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
* $Revision: 2.8 $
*/
public class XEdit extends JFormattedTextField implements XTextHolder, XAttributedComponent
{
/* boolean to indicate whether to antialias the text. */
protected boolean antiAlias;
/**
* Construct a new edit
*/
public XEdit()
{
}
/**
* Render the text
* @param g the graphcis context
*/
public void paintComponent( Graphics g )
{
Object oldHint = null;
if ( !BuildProperties.BUILD_JDK_118 ) {
Graphics2D g2d = (Graphics2D)g;
oldHint = g2d.getRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING );
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF );
}
super.paintComponent( g );
if ( !BuildProperties.BUILD_JDK_118 )
((Graphics2D)g).setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, oldHint );
}
/**
* Set one or more attributes of the component.
* <OL>
* <LI>alignment, value=(Left|Right|Center|Leading|Trailing)</LI>
* <LI>border, value=0, to tun off the border</LI>
* <LI>margin, value=the size in pixels of the margin (the space between the border and the text)</LI>
* <LI>tooltip, value=the tooltip text</LI>
* <LI>format, value=integer|curreny|date|decimal or a mask for a mask format</LI>
* <LI>editable, value=(true|false) set the edit to being editable</LI>
* <LI>antialias, value=(true|false) antialias the text</LI>
* </OL>
* @param attribName the name of the attribute
* @param attribValue the value of the attribute
* @return 0 for success, non zero for failure or to require some further action
*/
public int setAttribute( String attribName, Object attribValue )
{
String attribNameLwr = attribName.toLowerCase();
String attribValueStr = (String)attribValue;
String attribValueLwr = null;
if ( attribValue != null )
attribValueLwr = attribValueStr.toLowerCase();
if ( attribNameLwr.equals( "alignment" ) || attribNameLwr.equals( "align" ))
setHorizontalAlignment( XAlignmentHelper.getAlignmentOption( attribValueStr ));
else if ( attribNameLwr.equals( "border" ) ) {
if ( attribValueLwr.equals( "0" ))
setBorder( new EmptyBorder( 0, 0, 0, 0 ));
}
else if ( attribNameLwr.equals( "margin" ) ) {
int margin = new Integer( attribValueLwr ).intValue();
setMargin( new Insets( margin, margin, margin, margin ));
}
else if ( attribNameLwr.equals( "tooltip" ))
setToolTipText( attribValueStr );
else if ( attribNameLwr.equals( "editable" ))
setEditable( attribValueStr.equals( "true" ));
else if ( attribNameLwr.equals( "format" )) {
JFormattedTextField.AbstractFormatter formatter = null;
Format numberFormat = null;
Object value = null;
if ( attribValueLwr.equals( "integer" )) {
numberFormat = NumberFormat.getIntegerInstance();
formatter = new NumberFormatter( (NumberFormat)numberFormat );
value = new Integer( 0 );
}
else if ( attribValueLwr.equals( "currency" )) {
numberFormat = NumberFormat.getCurrencyInstance( Locale.getDefault());
formatter = new NumberFormatter( (NumberFormat)numberFormat );
}
else if ( attribValueLwr.equals( "decimal")) {
numberFormat = DecimalFormat.getNumberInstance();
formatter = new NumberFormatter( (NumberFormat)numberFormat );
value = new Double( 0.0 );
}
else if ( attribValueLwr.equals( "date")) {
numberFormat = DateFormat.getDateInstance();
formatter = new DateFormatter( (DateFormat)numberFormat );
value = new Date();
}
else {
try {
formatter = new MaskFormatter( attribValueStr );
}
catch ( ParseException e ) {
if ( BuildProperties.DEBUG )
DebugLogger.logError( "Invalid mask format specified: " + attribValue );
}
}
if ( formatter != null ) {
setFormatter( formatter );
if ( value != null )
super.setValue( value );
}
}
else if ( attribNameLwr.equals( "antialias" ))
antiAlias = attribValue.equals( "true" );
else
return -1;
return 0;
}
}