/**
* Lets you edit an ISO date in a table.
*/
package com.mindprod.vercheck;
import com.mindprod.common11.BigDate;
import javax.swing.CellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import java.awt.Color;
import java.awt.Font;
/**
* editor a date in ISO format yyyy-mm-dd. similar to ISODateRenderer
*/
class ISODateEditor extends DefaultCellEditor implements TableCellEditor, CellEditor
{
private final Color background;
private final Color foreground;
private final Font font;
private final int horizontalAlignment;
/**
* constructor
*
* @param foreground foreground colour
* @param background background colour
* @param font font
* @param horizontalAlignment e.g. JTextField.LEFT
*/
@SuppressWarnings( { "SameParameterValue" } )
public ISODateEditor( final Color foreground,
final Color background,
final Font font,
final int horizontalAlignment )
{
super( new JTextField() );
this.foreground = foreground;
this.background = background;
this.font = font;
this.horizontalAlignment = horizontalAlignment;
}
/**
* Returns the value contained in the editor GUI component
*
* @return the value contained in the editor
*/
public Object getCellEditorValue()
{
String yyyy_mm_dd = ( String ) delegate.getCellEditorValue();
yyyy_mm_dd = yyyy_mm_dd.trim();
if ( yyyy_mm_dd.length() > 0 && BigDate.isValid( yyyy_mm_dd ) )
{
return new BigDate( yyyy_mm_dd );
}
else
{
Audio.BAD_DATE.play();
return null;
}
}
/**
* Render the date value. Takes a BigDate and produces an JTextField with a corresponding yyyy-mm-dd String
* value. Sets an initial <code>value</code> for the editor. This will cause the editor to <code>stopEditing</code>
* and lose any partially edited value if the editor is editing when this method is called. <p>
* <p/>
* Returns the component that should be added to the client's <code>Component</code> hierarchy. Once installed in
* the client's hierarchy this component will then be able to draw and receive user input.
*
* @param table the <code>JTable</code> that is asking the editor to edit; can be <code>null</code>
* @param value the value of the cell to be edited; it is up to the specific editor to interpret and draw the
* value. For example, if value is the string "true", it could be rendered as a string or it
* could be rendered as a check box that is checked. <code>null</code> is a valid value
* @param isSelected true if the cell is to be rendered with highlighting
* @param row the row of the cell being edited
* @param column the column of the cell being edited
* @return the component for editing, a JTextField Component.
*/
public JTextField getTableCellEditorComponent( JTable table, Object value,
boolean isSelected,
int row, int column )
{
JTextField template = ( JTextField ) super.getTableCellEditorComponent( table, value,
isSelected, row, column );
assert value == null || value instanceof BigDate : "not a BigDate: " + value.toString();
template.setFont( font );
template.setForeground( foreground );
template.setBackground( background );
template.setHorizontalAlignment( horizontalAlignment );
if ( value == null )
{
template.setText( "" );
}
else
{
BigDate bdValue = ( BigDate ) value;
if ( bdValue.getOrdinal() == BigDate.NULL_ORDINAL )
{
template.setText( "" );
}
else
{
template.setText( bdValue.toString() );
}
}
return template;
}
}