]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/TextTableCellEditor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / TextTableCellEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.spreadsheet.ui;
13
14 import java.awt.Component;
15 import java.awt.Toolkit;
16 import java.awt.event.KeyEvent;
17 import java.awt.geom.AffineTransform;
18 import java.util.EventObject;
19
20 import javax.swing.DefaultCellEditor;
21 import javax.swing.JOptionPane;
22 import javax.swing.JTable;
23 import javax.swing.JTextField;
24 import javax.swing.SwingUtilities;
25
26 import org.simantics.databoard.Bindings;
27 import org.simantics.databoard.binding.mutable.Variant;
28 import org.simantics.spreadsheet.CellEditor;
29 import org.simantics.spreadsheet.ClientModel;
30 import org.simantics.spreadsheet.Spreadsheets;
31 import org.simantics.spreadsheet.util.SpreadsheetUtils;
32
33 class TextTableCellEditor extends DefaultCellEditor implements SpreadsheetCellEditor {
34
35     final int row;
36     final int column;
37     final CellEditor<?> editor;
38     final String initial;
39     
40     private static final long serialVersionUID = 8039248821751464832L;
41
42     JTextField ftf;
43     ClientModel clientModel;
44         
45     public TextTableCellEditor(final int row, final int column,final String initial, final CellEditor<?> editor, ClientModel clientModel) {
46         
47         super(new JTextField());
48         
49         assert(editor != null);
50
51         this.row = row;
52         this.column = column;
53         this.initial = initial == null ? "" : initial;
54         this.editor = editor;
55         this.clientModel = clientModel;
56         
57         ftf = (JTextField)getComponent();
58
59         ftf.setHorizontalAlignment(JTextField.TRAILING);
60         
61     }
62
63     @Override
64     public boolean isCellEditable(EventObject event) {
65         if (super.isCellEditable(event)) {
66             if (event instanceof KeyEvent) {
67                 KeyEvent ke = (KeyEvent) event;
68                 int kc = ke.getKeyCode();
69                 switch (kc) {
70                 case KeyEvent.VK_ESCAPE:
71                     return false;
72                 case KeyEvent.VK_F2:
73                     return true;
74                 }
75                 if (ke.isActionKey())
76                     return false;
77             }
78             return true;
79         }
80         return false;
81     }
82
83     //Override to invoke setValue on the formatted text field.
84     public Component getTableCellEditorComponent(JTable table,
85             Object value, boolean isSelected,
86             int row, int column) {
87         JTextField ftf =
88             (JTextField)super.getTableCellEditorComponent(
89                 table, value, isSelected, row, column);
90         ftf.setText(initial);
91         ftf.selectAll();
92         ftf.setFont(ftf.getFont().deriveFont(AffineTransform.getScaleInstance(0.8, 0.8)));
93         return ftf;
94     }
95
96     //Override to ensure that the value remains an Integer.
97     public Object getCellEditorValue() {
98         JTextField ftf = (JTextField)getComponent();
99         return ftf.getText();
100     }
101
102     /** 
103      * Lets the user know that the text they entered is 
104      * bad. Returns true if the user elects to revert to
105      * the last good value.  Otherwise, returns false, 
106      * indicating that the user wants to continue editing.
107      */
108     protected boolean userSaysRevert() {
109         Toolkit.getDefaultToolkit().beep();
110         ftf.selectAll();
111         Object[] options = {"Edit",
112                             "Revert"};
113         int answer = JOptionPane.showOptionDialog(
114             SwingUtilities.getWindowAncestor(ftf),
115             "The value must be an integer between "
116 //            + minimum + " and "
117 //            + maximum + ".\n"
118             + "You can either continue editing "
119             + "or revert to the last valid value.",
120             "Invalid Text Entered",
121             JOptionPane.YES_NO_OPTION,
122             JOptionPane.ERROR_MESSAGE,
123             null,
124             options,
125             options[1]);
126             
127         if (answer == 1) { //Revert!
128             ftf.setText(ftf.getText());
129             return true;
130         }
131         return false;
132     }
133
134     @Override
135     public void commit() {
136         
137         String str = (String)getCellEditorValue();
138
139         String cellName = Spreadsheets.cellName(row, column);
140         Object expressionO = clientModel.getPossiblePropertyAt(cellName, ClientModel.CONTENT_EXPRESSION);
141         String expression = null;
142         if(expressionO instanceof String) {
143                 expression = (String)expressionO;
144         } else if(expressionO instanceof Variant) {
145                 expression = ((Variant)expressionO).getValue().toString();
146         }
147         
148                 if(expression == null) {
149                         Variant content = SpreadsheetUtils.getSafeClientVariant(clientModel, cellName, ClientModel.CONTENT);
150                         if(content != null)
151                                 expression = content.getValue().toString();
152                         else
153                                 expression = "";
154                 } else {
155                         expression = "=" + expression;
156                 }
157                 //Don't update if the expression hasn't changed
158         if (expression.equals(str))
159                 return;
160         
161         if (str.startsWith("=")) {
162                         editor.edit(null, Spreadsheets.cellName(row, column), ClientModel.CONTENT_EXPRESSION, str, Bindings.STRING, null);
163                 //Update cell expression
164                         clientModel.setProperty(cellName, ClientModel.CONTENT_EXPRESSION, str);
165                 } else {
166                         editor.edit(null, Spreadsheets.cellName(row, column), Variant.ofInstance(str), null);
167                         //Update cell expression
168                         clientModel.setProperty(cellName, ClientModel.CONTENT_EXPRESSION, null); //If not an expression, then the expression property is null
169                 }
170         
171     }
172     
173 }