]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/TextTableCellEditor.java
Database backwards compatibility fix for fail-safe import changes
[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.util.SpreadsheetUtils;
31
32 class TextTableCellEditor extends DefaultCellEditor implements SpreadsheetCellEditor {
33
34     final int row;
35     final int column;
36     final CellEditor<?> editor;
37     final String initial;
38     
39     private static final long serialVersionUID = 8039248821751464832L;
40
41     JTextField ftf;
42     ClientModel clientModel;
43         
44     public TextTableCellEditor(final int row, final int column,final String initial, final CellEditor<?> editor, ClientModel clientModel) {
45         
46         super(new JTextField());
47         
48         assert(editor != null);
49
50         this.row = row;
51         this.column = column;
52         this.initial = initial == null ? "" : initial;
53         this.editor = editor;
54         this.clientModel = clientModel;
55         
56         ftf = (JTextField)getComponent();
57
58         ftf.setHorizontalAlignment(JTextField.TRAILING);
59         
60     }
61
62     @Override
63     public boolean isCellEditable(EventObject event) {
64         if (super.isCellEditable(event)) {
65             if (event instanceof KeyEvent) {
66                 KeyEvent ke = (KeyEvent) event;
67                 int kc = ke.getKeyCode();
68                 switch (kc) {
69                 case KeyEvent.VK_ESCAPE:
70                     return false;
71                 case KeyEvent.VK_F2:
72                     return true;
73                 }
74                 if (ke.isActionKey())
75                     return false;
76             }
77             return true;
78         }
79         return false;
80     }
81
82     //Override to invoke setValue on the formatted text field.
83     public Component getTableCellEditorComponent(JTable table,
84             Object value, boolean isSelected,
85             int row, int column) {
86         JTextField ftf =
87             (JTextField)super.getTableCellEditorComponent(
88                 table, value, isSelected, row, column);
89         ftf.setText(initial);
90         ftf.selectAll();
91         ftf.setFont(ftf.getFont().deriveFont(AffineTransform.getScaleInstance(0.8, 0.8)));
92         return ftf;
93     }
94
95     //Override to ensure that the value remains an Integer.
96     public Object getCellEditorValue() {
97         JTextField ftf = (JTextField)getComponent();
98         return ftf.getText();
99     }
100
101     /** 
102      * Lets the user know that the text they entered is 
103      * bad. Returns true if the user elects to revert to
104      * the last good value.  Otherwise, returns false, 
105      * indicating that the user wants to continue editing.
106      */
107     protected boolean userSaysRevert() {
108         Toolkit.getDefaultToolkit().beep();
109         ftf.selectAll();
110         Object[] options = {"Edit",
111                             "Revert"};
112         int answer = JOptionPane.showOptionDialog(
113             SwingUtilities.getWindowAncestor(ftf),
114             "The value must be an integer between "
115 //            + minimum + " and "
116 //            + maximum + ".\n"
117             + "You can either continue editing "
118             + "or revert to the last valid value.",
119             "Invalid Text Entered",
120             JOptionPane.YES_NO_OPTION,
121             JOptionPane.ERROR_MESSAGE,
122             null,
123             options,
124             options[1]);
125             
126         if (answer == 1) { //Revert!
127             ftf.setText(ftf.getText());
128             return true;
129         }
130         return false;
131     }
132
133     @Override
134     public void commit() {
135         
136         String str = (String)getCellEditorValue();
137
138         String cellName = SpreadsheetUtils.cellName(row, column);
139         String expression = clientModel.getPossiblePropertyAt(cellName, ClientModel.CONTENT_EXPRESSION);
140                 if(expression == null) {
141                         Variant content = SpreadsheetUtils.getSafeClientVariant(clientModel, cellName, ClientModel.CONTENT);
142                         if(content != null)
143                                 expression = content.getValue().toString();
144                         else
145                                 expression = "";
146                 } else {
147                         expression = "=" + expression;
148                 }        
149         if (expression.equals(str))
150                 return;
151         
152         if (str.startsWith("=")) {
153                         editor.edit(null, SpreadsheetUtils.cellName(row, column), ClientModel.CONTENT_EXPRESSION, str, Bindings.STRING, null);
154                 } else {
155                         editor.edit(null, SpreadsheetUtils.cellName(row, column), Variant.ofInstance(str), null);
156                 }
157         
158     }
159     
160 }