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.util.SpreadsheetUtils;
32 class TextTableCellEditor extends DefaultCellEditor implements SpreadsheetCellEditor {
36 final CellEditor<?> editor;
39 private static final long serialVersionUID = 8039248821751464832L;
42 ClientModel clientModel;
44 public TextTableCellEditor(final int row, final int column,final String initial, final CellEditor<?> editor, ClientModel clientModel) {
46 super(new JTextField());
48 assert(editor != null);
52 this.initial = initial == null ? "" : initial;
54 this.clientModel = clientModel;
56 ftf = (JTextField)getComponent();
58 ftf.setHorizontalAlignment(JTextField.TRAILING);
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();
69 case KeyEvent.VK_ESCAPE:
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) {
87 (JTextField)super.getTableCellEditorComponent(
88 table, value, isSelected, row, column);
91 ftf.setFont(ftf.getFont().deriveFont(AffineTransform.getScaleInstance(0.8, 0.8)));
95 //Override to ensure that the value remains an Integer.
96 public Object getCellEditorValue() {
97 JTextField ftf = (JTextField)getComponent();
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.
107 protected boolean userSaysRevert() {
108 Toolkit.getDefaultToolkit().beep();
110 Object[] options = {"Edit",
112 int answer = JOptionPane.showOptionDialog(
113 SwingUtilities.getWindowAncestor(ftf),
114 "The value must be an integer between "
115 // + minimum + " and "
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,
126 if (answer == 1) { //Revert!
127 ftf.setText(ftf.getText());
134 public void commit() {
136 String str = (String)getCellEditorValue();
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);
143 expression = content.getValue().toString();
147 expression = "=" + expression;
149 if (expression.equals(str))
152 if (str.startsWith("=")) {
153 editor.edit(null, SpreadsheetUtils.cellName(row, column), ClientModel.CONTENT_EXPRESSION, str, Bindings.STRING, null);
155 editor.edit(null, SpreadsheetUtils.cellName(row, column), Variant.ofInstance(str), null);