]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ExpressionTextListener.java
Adopt spreadsheet changes made in Balas development
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / ExpressionTextListener.java
1 package org.simantics.spreadsheet.ui;
2
3 import java.awt.event.FocusEvent;
4 import java.awt.event.FocusListener;
5 import java.awt.event.KeyEvent;
6 import java.awt.event.KeyListener;
7
8 import javax.swing.JTextField;
9
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;
15
16 public class ExpressionTextListener implements FocusListener, KeyListener {
17
18         final private JTextField text;
19         final private CellEditor editor;
20         
21         String undoPoint = null;
22         int currentRow;
23         int currentColumn;
24         
25         public ExpressionTextListener(JTextField text, CellEditor editor) {
26                 this.text = text;
27                 this.editor = editor;
28         }
29         
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;
35         }
36         
37         void apply() {
38                 if(undoPoint == null) {
39                         // No selection
40                         return;
41                 }
42                 String current = text.getText();
43                 if(current.equals(undoPoint)) return;
44                 if(currentRow == -1 || currentColumn == -1) return;
45                 
46                 if (current.startsWith("=")) {
47                     editor.edit(null, Spreadsheets.cellName(currentRow, currentColumn), ClientModel.CONTENT_EXPRESSION, current, Bindings.STRING, null);
48                 } else {
49                     editor.edit(null, Spreadsheets.cellName(currentRow, currentColumn), Variant.ofInstance(current), null);
50                 }
51                 undoPoint = current;
52                 
53         }
54         
55         void cancel() {
56                 if(undoPoint != null)
57                         text.setText(undoPoint);
58         }
59         
60         @Override
61         public void keyTyped(KeyEvent e) {
62         }
63         
64         @Override
65         public void keyReleased(KeyEvent e) {
66                 if(e.getKeyCode() == KeyEvent.VK_ENTER) {
67                         apply();
68                 }
69                 if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
70                         cancel();
71                 }
72         }
73         
74         @Override
75         public void keyPressed(KeyEvent e) {
76         }
77         
78         @Override
79         public void focusGained(FocusEvent e) {
80         }
81
82         @Override
83         public void focusLost(FocusEvent e) {
84                 apply();
85         }
86         
87 }