/*
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 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 org.apache.log4j.Logger;
import DisplayProject.PasswordDataField;
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 PasswordCellEditor extends DefaultCellEditor implements ArrayFieldTableCellEditor, StateColourForRowDisplayer
{
private static final Logger logger = Logger.getLogger(PasswordCellEditor.class);
// PM:30 Oct 2008:added capability to allow specific row dependent colours and states
protected StateColourForRow stateColour;
protected PasswordDataField passwordDataFieldOriginal; // Stored to do the formatting of the text
protected PasswordDataField passwordDataFieldCloned; // Stored to do the drawing of the component
JFormattedTextField df;
public PasswordCellEditor(JCheckBox checkBox) {
super(checkBox);
}
public PasswordCellEditor(JComboBox comboBox) {
super(comboBox);
}
public PasswordCellEditor(final JTextField textField) {
super(textField);
if (textField instanceof PasswordDataField){
this.passwordDataFieldOriginal = (PasswordDataField)textField;
this.passwordDataFieldCloned = ((PasswordDataField)textField).cloneComponentForEditor();
}
setClickCountToStart(1);
// CraigM:16/09/2008 - This is no longer needed as the PasswordDataField 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 PasswordCellEditor(AbstractFormatter formatter) {
super(new JFormattedTextField(formatter));
df = (JFormattedTextField)getComponent();
df.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
public PasswordCellEditor(AbstractFormatterFactory factory) {
super(new JFormattedTextField(factory));
df = (JFormattedTextField)getComponent();
}
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 PasswordDataField
* This may cause missing events
*/
// This null check is for the visual editors
if (this.passwordDataFieldCloned == null) {
this.passwordDataFieldCloned = new PasswordDataField();
this.passwordDataFieldOriginal = new PasswordDataField();
}
this.passwordDataFieldCloned.setLastValue(null);
this.passwordDataFieldCloned.setValue(value);
this.passwordDataFieldOriginal.setTable(table);
this.passwordDataFieldOriginal.setTableColumn(column);
this.passwordDataFieldOriginal.setTableRow(row);
this.passwordDataFieldCloned.setTable(table);
this.passwordDataFieldCloned.setTableColumn(column);
this.passwordDataFieldCloned.setTableRow(row);
this.passwordDataFieldCloned.requestFocusInWindow();
this.passwordDataFieldCloned.setComponentPopupMenu(table.getComponentPopupMenu());
// PM:31 Oct 2008:Draw the row highlight
this.passwordDataFieldCloned.setForeground(table.getSelectionForeground());
this.passwordDataFieldCloned.setBackground(table.getSelectionBackground());
this.passwordDataFieldCloned.setCaretColor(table.getSelectionForeground());
// PM:3 Nov 2008:set the state being aware of a StateColourForRow plugin
if (this.stateColour != null){
this.passwordDataFieldCloned.setEditable(!this.stateColour.isReadOnly(row));
this.passwordDataFieldCloned.setEnabled(this.stateColour.isEnabled(row));
}
// TF:14/12/2009:DET-143:Allowed the tool tip information to show
this.passwordDataFieldCloned.setToolTipText(this.passwordDataFieldOriginal.getToolTipText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
passwordDataFieldCloned.requestFocus();
}
});
return this.passwordDataFieldCloned;
}
public Object getCellEditorValue()
{
logger.debug("getCellEditorValue()");
// JFormattedTextField df = (JFormattedTextField)getComponent();
Object o = this.passwordDataFieldCloned.getValue();
return o;
}
public boolean stopCellEditing()
{
logger.debug("stopCellEditing()");
PasswordDataField df = this.passwordDataFieldCloned;
if (df.isNullValueAllowed() & df.isValueNull()) {
fireEditingStopped();
resetTableData(this.passwordDataFieldOriginal, this.passwordDataFieldCloned);
return true;
}
fireEditingStopped();
resetTableData(this.passwordDataFieldOriginal, this.passwordDataFieldCloned);
return true;
}
public void cancelCellEditing()
{
logger.debug("cancelCellEditing()");
PasswordDataField df = (PasswordDataField)getComponent();
resetTableData(this.passwordDataFieldOriginal, this.passwordDataFieldCloned);
df.disableEvents();
super.cancelCellEditing();
}
private void resetTableData(PasswordDataField original, PasswordDataField 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
return WidgetState.get(this.passwordDataFieldOriginal);
}
}