1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* @(#)JeksCellEditor.java 08/23/99
*
* Copyright (c) 1998-2001 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Visit eTeks web site for up-to-date versions of this file and other
* Java tools and tutorials : http://www.eteks.com/
*/
package com.eteks.jeks;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.DefaultCellEditor;
import com.eteks.parser.CompilationException;
/**
* Cell editor for computed and default cells. This editor enables to edit the definition
* of an expression if the edited value is an instance of <code>JeksExpression</code>.
*
* @version 1.0
* @author Emmanuel Puybaret
* @since Jeks 1.0
*/
public class JeksCellEditor extends DefaultCellEditor
{
private JeksExpressionParser parser;
private JTable table;
private Object cellValue;
/**
* Creates a cell editor.
* @param parser the parser used to parse and compile the cells starting
* with an assignment operator.
*/
public JeksCellEditor (JeksExpressionParser parser)
{
super (new JTextField ()); // No defaut super constructor
final JTextField textField = new JTextField ();
editorComponent = textField;
// Reassign an other delegate that checks stopCellEditing ()
// before firing stop event
delegate = new EditorDelegate ()
{
public void setValue (Object value)
{
textField.setText ((value != null) ? value.toString() : "");
}
public Object getCellEditorValue ()
{
return textField.getText();
}
public void actionPerformed (ActionEvent event)
{
if (JeksCellEditor.this.stopCellEditing ())
super.actionPerformed (event);
}
};
textField.addActionListener(delegate);
this.parser = parser;
}
/**
* Returns the table model value edited by this editor.
*/
public Object getCellEditorValue ()
{
return cellValue;
}
public boolean stopCellEditing ()
{
String data = (String)super.getCellEditorValue ();
cellValue = getModelValue (data,
table.getModel (),
new JeksCell (table.getEditingRow (),
table.convertColumnIndexToModel (table.getEditingColumn ())));
if (cellValue == null)
return false;
else
return super.stopCellEditing ();
}
/**
* Returns the component used to edit cell values.
* Overrides <code>getTableCellEditorComponent ()</code> to use the value returned
* by <code>getEditedValue (value)</code> in the editor instead of the table model
* <code>value</code> itself.
*/
public Component getTableCellEditorComponent (JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
cellValue = value;
value = getEditedValue (value);
this.table = table;
return super.getTableCellEditorComponent (table, value, isSelected, row, column);
}
/**
* Returns <code>value</code> as an editable object. If <code>value</code> is an
* instance of <code>JeksExpression</code> the definition of the expression is returned,
* if <code>value</code> is a number or a date its localized string representation
* is returned, otherwise the object <code>value</code> itself is returned.
* @param value a value of the table model.
*/
public Object getEditedValue (Object value)
{
if (parser != null)
return parser.getEditedValue (value);
else
return value;
}
/**
* Returns an object of the same wrapping class as the string
* <code>data</code>. This method tries to create an instance
* of one of the following classes in this order: <code>Long</code>,
* <code>Double</code>, <code>Date</code>, <code>Boolean</code> or the string itself.
* If the string starts with an assignment operator (equal sign =), it tries
* to compile the expression to create a <code>JeksExpression</code> instance.
* @param data a string.
* @param model the edited table model.
* @param editingCell the current editing cell.
* @return an instance of either classes : <code>Long</code>, <code>Double</code>,
* <code>Date</code>, <code>Boolean</code>, <code>String</code>
* or <code>JeksExpression</code>.
*/
public Object getModelValue (String data,
TableModel model,
JeksCell editingCell)
{
try
{
if (parser != null)
{
Object modelValue = parser.getModelValue (data);
if (modelValue instanceof JeksExpression)
try
{
((JeksExpression)modelValue).checkCircularity (model, editingCell);
}
catch (CircularityException ex)
{
JeksExpressionSyntax syntax = (JeksExpressionSyntax)parser.getSyntax ();
if (JOptionPane.showConfirmDialog (null,
syntax.getMessage (JeksExpressionSyntax.MESSAGE_CIRCULARITY_ERROR_CONFIRM),
syntax.getMessage (JeksExpressionSyntax.MESSAGE_CIRCULARITY_ERROR_TITLE),
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
return null;
else
((JeksExpression)modelValue).invalidateValue (ex);
}
return modelValue;
}
else
return data;
}
catch (CompilationException ex)
{
JeksExpressionSyntax syntax = (JeksExpressionSyntax)parser.getSyntax ();
String errorMessage = syntax.getExceptionMessage (ex);
JOptionPane.showMessageDialog (null, errorMessage, syntax.getMessage (JeksExpressionSyntax.MESSAGE_COMPILATION_ERROR_TITLE),
JOptionPane.ERROR_MESSAGE);
// TODO find a way to show the error to user
// ((JTextField)getComponent ()).setCaretPosition (ex.getCharacterIndex ());
return null;
}
}
}
|