View Javadoc

1   /*
2    *  File: DoubleCellEditor.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.editor;
12  
13  import java.text.ParseException;
14  
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.events.FocusEvent;
17  import org.eclipse.swt.events.FocusListener;
18  import org.eclipse.swt.events.KeyEvent;
19  import org.eclipse.swt.events.KeyListener;
20  import org.eclipse.swt.graphics.Point;
21  import org.eclipse.swt.widgets.Control;
22  import org.eclipse.swt.widgets.Text;
23  
24  import de.jaret.util.ui.DoubleField;
25  import de.jaret.util.ui.table.JaretTable;
26  import de.jaret.util.ui.table.model.IColumn;
27  import de.jaret.util.ui.table.model.IRow;
28  
29  /***
30   * Cell Editor for editing double values.
31   * <p>
32   * Key bindings: CR, TAB: accept input and leave, ESC leave and reset to value when starting editing. Cursor up/down
33   * will roll the value.
34   * </p>
35   * 
36   * @author Peter Kliem
37   * @version $Id: DoubleCellEditor.java 394 2007-05-01 10:51:25Z olk $
38   */
39  public class DoubleCellEditor extends CellEditorBase implements ICellEditor, FocusListener {
40      /*** text control wrapped by the doublefield. */
41      protected Text _text;
42      /*** old value for restauration. */
43      protected double _oldVal;
44      /*** doublefield managing the input. */
45      protected DoubleField _doubleField;
46  
47      /***
48       * Default constructor.
49       */
50      public DoubleCellEditor() {
51      }
52  
53      /***
54       * Convert the value retrieved from the model.
55       * 
56       * @param row row of the cell
57       * @param column column of the cell
58       * @return double value (defaulting to 0.0)
59       */
60      protected double convertValue(IRow row, IColumn column) {
61          Object value = column.getValue(row);
62          return value != null ? (Double) value : 0.0;
63      }
64  
65      /***
66       * Store the value in the model. If any error occurs, it will be silently ignored!
67       * 
68       * @param row row
69       * @param column column
70       */
71      protected void storeValue(IRow row, IColumn column) {
72          double value;
73          try {
74              value = _doubleField.getValue();
75              _column.setValue(_row, value);
76          } catch (ParseException e) {
77              // ignore
78          }
79      }
80  
81      /***
82       * Create and setup the control.
83       * 
84       * @param table parent for the control
85       */
86      private void createControl(JaretTable table) {
87          if (_text == null) {
88              _table = table;
89              _text = new Text(table, SWT.BORDER | SWT.RIGHT);
90              _doubleField = new DoubleField();
91              _doubleField.setText(_text);
92  
93              _text.addKeyListener(new KeyListener() {
94  
95                  public void keyPressed(KeyEvent event) {
96                      if (event.keyCode == SWT.TAB) {
97                          event.doit = false;
98                          stopEditing(true);
99                          _table.forceFocus();
100                         _table.focusRight();
101                     } else if (event.keyCode == SWT.CR) {
102                         event.doit = false;
103                         stopEditing(true);
104                         _table.forceFocus();
105                         _table.focusDown();
106                     } else if (event.keyCode == SWT.ESC) {
107                         event.doit = false;
108                         stopEditing(false);
109                         _column.setValue(_row, _oldVal);
110                         _table.forceFocus();
111                     }
112                 }
113 
114                 public void keyReleased(KeyEvent arg0) {
115                 }
116 
117             });
118 
119             _text.addFocusListener(this);
120         }
121     }
122 
123     /***
124      * {@inheritDoc}
125      */
126     public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
127         super.getEditorControl(table, row, column, typedKey);
128         createControl(table);
129         _oldVal = convertValue(row, column);
130         if (typedKey != 0) {
131             _text.setText("" + typedKey);
132             _text.setSelection(1);
133         } else {
134             double value = convertValue(row, column);
135             _doubleField.setValue(value);
136             _text.selectAll();
137         }
138         return _text;
139     }
140 
141     /***
142      * {@inheritDoc}
143      */
144     public int getPreferredHeight() {
145         if (_text == null) {
146             return -1;
147         }
148         Point size = _text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
149         return size.y;
150     }
151 
152     /***
153      * {@inheritDoc} do nothing.
154      */
155     public void focusGained(FocusEvent arg0) {
156     }
157 
158     /***
159      * {@inheritDoc} On losing focus store the value.
160      */
161     public void focusLost(FocusEvent arg0) {
162         _table.stopEditing(true);
163     }
164 
165     /***
166      * {@inheritDoc}
167      */
168     public void dispose() {
169         super.dispose();
170         if (_text != null && !_text.isDisposed()) {
171             _doubleField.setText(null);
172             _text.removeFocusListener(this);
173             _text.dispose();
174         }
175 
176     }
177 
178     /***
179      * {@inheritDoc}
180      */
181     public void stopEditing(boolean storeInput) {
182         if (storeInput) {
183             storeValue(_row, _column);
184         }
185         _text.setVisible(false);
186     }
187 
188 }