/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DisplayProject.table;
import java.awt.Component;
import java.text.ParseException;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.text.DocumentFilter;
import org.apache.log4j.Logger;
import DisplayProject.Constants;
import DisplayProject.DataField;
import DisplayProject.DataFieldFormatter;
import DisplayProject.actions.WidgetState;
/**
* This class formats a <code>JTable</code> cell using a <code>JFormattedTextField</code> as the editor.
* In most generated code this will actually be a <code>DataField</code>.
*
* @author Peter
*/
@SuppressWarnings("serial")
public class FormattedCellEditor extends DefaultCellEditor implements ArrayFieldTableCellEditor, StateColourForRowDisplayer
{
private static final Logger logger = Logger.getLogger(FormattedCellEditor.class);
// PM:30 Oct 2008:added capability to allow specific row dependent colours and states
protected StateColourForRow stateColour;
protected DataField dataFieldOriginal; // Stored to do the formatting of the text
protected DataField dataFieldCloned; // Stored to do the drawing of the component
JFormattedTextField df;
public FormattedCellEditor(JCheckBox checkBox) {
super(checkBox);
}
public FormattedCellEditor(JComboBox comboBox) {
super(comboBox);
}
public FormattedCellEditor(final JTextField textField) {
super(textField);
if (textField instanceof DataField){
this.dataFieldOriginal = (DataField)textField;
this.dataFieldCloned = ((DataField)textField).cloneComponentForEditor();
}
setClickCountToStart(1);
// CraigM:16/09/2008 - This is no longer needed as the DataField now calls setCaretPosition.
// textField.addFocusListener(new FocusAdapter()
// {
// public void focusGained(FocusEvent e)
// {
// logger.debug("Focus Gained. Scheduling select all later");
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// textField.selectAll();
// }
// });
// }
// });
}
public FormattedCellEditor(AbstractFormatter formatter) {
super(new JFormattedTextField(formatter));
df = (JFormattedTextField)getComponent();
df.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
public FormattedCellEditor(AbstractFormatterFactory factory) {
super(new JFormattedTextField(factory));
df = (JFormattedTextField)getComponent();
}
public FormattedCellEditor(DocumentFilter filter) {
super(new JFormattedTextField(filter));
df = (JFormattedTextField)getComponent();
((DataFieldFormatter)df.getFormatter()).setFilter(filter);
((DataFieldFormatter)df.getFormatter()).install(df);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
/*
* PM:31/10/08
* This is a big change
* We now use a clone of the DataField
* This may cause missing events
*/
// This null check is for the visual editors
if (this.dataFieldCloned == null) {
this.dataFieldCloned = new DataField();
this.dataFieldOriginal = new DataField();
}
this.dataFieldCloned.setLastValue(null);
this.dataFieldCloned.setValue(value);
this.dataFieldOriginal.setTable(table);
this.dataFieldOriginal.setTableColumn(column);
this.dataFieldOriginal.setTableRow(row);
this.dataFieldCloned.setTable(table);
this.dataFieldCloned.setTableColumn(column);
this.dataFieldCloned.setTableRow(row);
this.dataFieldCloned.requestFocusInWindow();
this.dataFieldCloned.setComponentPopupMenu(table.getComponentPopupMenu());
this.dataFieldCloned.setMaskType(this.dataFieldOriginal.getMaskType()); // CraigM:13/01/2009 - Set the mask type
// PM:31 Oct 2008:Draw the row highlight
this.dataFieldCloned.setForeground(table.getSelectionForeground());
this.dataFieldCloned.setBackground(table.getSelectionBackground());
this.dataFieldCloned.setCaretColor(table.getSelectionForeground());
// PM:3 Nov 2008:set the state being aware of a StateColourForRow plugin
if (this.stateColour != null){
this.dataFieldCloned.setEditable(!this.stateColour.isReadOnly(row));
this.dataFieldCloned.setEnabled(this.stateColour.isEnabled(row));
}
this.dataFieldCloned.setComponentPopupMenu(table.getComponentPopupMenu());
// TF:14/12/2009:DET-143:Set the tool tip information as well
this.dataFieldCloned.setToolTipText(this.dataFieldOriginal.getToolTipText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dataFieldCloned.requestFocus();
}
});
return this.dataFieldCloned;
}
public Object getCellEditorValue()
{
logger.debug("getCellEditorValue()");
// JFormattedTextField df = (JFormattedTextField)getComponent();
Object o = this.dataFieldCloned.getValue();
return o;
}
public boolean stopCellEditing()
{
logger.debug("stopCellEditing()");
// TF:01/12/2009:DET-134:Changed this method to only commit the edits if we're in update mode.
if (this.getState() != Constants.FS_UPDATE) {
this.cancelCellEditing();
return true;
}
DataField df = this.dataFieldCloned;
try
{
// Force the textfield to commit it itself. Even though a focus lost event
// will usually cause this, when used inside a jtable, this event is NOT
// processed until after the cell editors getCellEditorValue is called.
df.commitEdit();
}
catch (ParseException e)
{
// Unable to format so stop editing
return false;
}
if (!df.getInputVerifier().verify(df)) {
return false;
}
fireEditingStopped();
resetTableData(this.dataFieldOriginal, this.dataFieldCloned);
return true;
}
public void cancelCellEditing()
{
logger.debug("cancelCellEditing()");
DataField df = (DataField)getComponent();
resetTableData(this.dataFieldOriginal, this.dataFieldCloned);
df.disableEvents();
super.cancelCellEditing();
}
private void resetTableData(DataField original, DataField cloned){
original.setTable(null);
original.setTableColumn(-1);
original.setTableRow(-1);
cloned.setTable(null);
cloned.setTableColumn(-1);
cloned.setTableRow(-1);
cloned.setEditable(original.isEditable());
cloned.setEnabled(original.isEnabled());
cloned.setVisible(original.isVisible());
}
public JFormattedTextField getFormattedEditor(){
return (JFormattedTextField)getComponent();
}
public StateColourForRow getStateColour() {
return stateColour;
}
public void setStateColour(StateColourForRow stateColour) {
this.stateColour = stateColour;
}
/**
* Return the state of the underlying template field, that is one of the Constants.FS_ constants
* @return
*/
public int getState() {
// TF:21/01/2009:DET-72: Added this method to expose the state of the underlying template
// TF:04/12/2009:DET-139:Corrected this to be the true widget state, ie the worst state out of this widget and its parents
// return WidgetState.get(this.dataFieldOriginal);
return WidgetState.getWorstState(this.dataFieldOriginal);
}
}