1 package org.simantics.spreadsheet.ui;
3 import java.awt.event.FocusEvent;
4 import java.awt.event.FocusListener;
5 import java.awt.event.KeyEvent;
6 import java.awt.event.KeyListener;
8 import javax.swing.JTextField;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.databoard.binding.mutable.Variant;
12 import org.simantics.spreadsheet.CellEditor;
13 import org.simantics.spreadsheet.ClientModel;
14 import org.simantics.spreadsheet.Spreadsheets;
16 public class ExpressionTextListener implements FocusListener, KeyListener {
18 final private JTextField text;
19 final private CellEditor editor;
21 String undoPoint = null;
25 public ExpressionTextListener(JTextField text, CellEditor editor) {
30 void setCell(String text, int row, int column) {
31 this.text.setText(text);
32 this.undoPoint = text;
33 this.currentRow = row;
34 this.currentColumn = column;
38 if(undoPoint == null) {
42 String current = text.getText();
43 if(current.equals(undoPoint)) return;
44 if(currentRow == -1 || currentColumn == -1) return;
46 if (current.startsWith("=")) {
47 editor.edit(null, Spreadsheets.cellName(currentRow, currentColumn), ClientModel.CONTENT_EXPRESSION, current, Bindings.STRING, null);
49 editor.edit(null, Spreadsheets.cellName(currentRow, currentColumn), Variant.ofInstance(current), null);
57 text.setText(undoPoint);
61 public void keyTyped(KeyEvent e) {
65 public void keyReleased(KeyEvent e) {
66 if(e.getKeyCode() == KeyEvent.VK_ENTER) {
69 if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
75 public void keyPressed(KeyEvent e) {
79 public void focusGained(FocusEvent e) {
83 public void focusLost(FocusEvent e) {