1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.spreadsheet.ui;
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;
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;
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;
33 class TextTableCellEditor extends DefaultCellEditor implements SpreadsheetCellEditor {
37 final CellEditor<?> editor;
40 private static final long serialVersionUID = 8039248821751464832L;
43 ClientModel clientModel;
45 public TextTableCellEditor(final int row, final int column,final String initial, final CellEditor<?> editor, ClientModel clientModel) {
47 super(new JTextField());
49 assert(editor != null);
53 this.initial = initial == null ? "" : initial;
55 this.clientModel = clientModel;
57 ftf = (JTextField)getComponent();
59 ftf.setHorizontalAlignment(JTextField.TRAILING);
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();
70 case KeyEvent.VK_ESCAPE:
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) {
88 (JTextField)super.getTableCellEditorComponent(
89 table, value, isSelected, row, column);
92 ftf.setFont(ftf.getFont().deriveFont(AffineTransform.getScaleInstance(0.8, 0.8)));
96 //Override to ensure that the value remains an Integer.
97 public Object getCellEditorValue() {
98 JTextField ftf = (JTextField)getComponent();
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.
108 protected boolean userSaysRevert() {
109 Toolkit.getDefaultToolkit().beep();
111 Object[] options = {"Edit",
113 int answer = JOptionPane.showOptionDialog(
114 SwingUtilities.getWindowAncestor(ftf),
115 "The value must be an integer between "
116 // + minimum + " and "
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,
127 if (answer == 1) { //Revert!
128 ftf.setText(ftf.getText());
135 public void commit() {
137 String str = (String)getCellEditorValue();
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();
148 if(expression == null) {
149 Variant content = SpreadsheetUtils.getSafeClientVariant(clientModel, cellName, ClientModel.CONTENT);
151 expression = content.getValue().toString();
155 expression = "=" + expression;
157 //Don't update if the expression hasn't changed
158 if (expression.equals(str))
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);
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