View Javadoc

1   /*
2    *  File: IntegerCellEditor.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 org.eclipse.swt.SWT;
14  import org.eclipse.swt.events.FocusEvent;
15  import org.eclipse.swt.events.FocusListener;
16  import org.eclipse.swt.events.KeyEvent;
17  import org.eclipse.swt.events.KeyListener;
18  import org.eclipse.swt.events.TraverseEvent;
19  import org.eclipse.swt.events.TraverseListener;
20  import org.eclipse.swt.graphics.Point;
21  import org.eclipse.swt.widgets.Control;
22  import org.eclipse.swt.widgets.Spinner;
23  
24  import de.jaret.util.ui.table.JaretTable;
25  import de.jaret.util.ui.table.model.IColumn;
26  import de.jaret.util.ui.table.model.IRow;
27  
28  /***
29   * Cell Editor for editing integer values using a spinner widget. Well it seems that the Spinner does not support
30   * negative values ...
31   * 
32   * Key bindings: CR, TAB: accept input and leave, ESC leave and reset to value when starting editing
33   * </p>
34   * 
35   * @author Peter Kliem
36   * @version $Id: IntegerCellEditor.java 394 2007-05-01 10:51:25Z olk $
37   */
38  public class IntegerCellEditor extends CellEditorBase implements ICellEditor, FocusListener {
39      /*** spinner widgrt. */
40      protected Spinner _spinner;
41  
42      /*** old value. */
43      private int _oldVal;
44  
45      /*** min value that can be selected. */
46      private int _min = Integer.MIN_VALUE;
47      /*** max value that can be selected. */
48      private int _max = Integer.MAX_VALUE;
49  
50      /***
51       * Construct an integer cell renderer with given min and max values.
52       * 
53       * @param min minimal value
54       * @param max maximum value
55       */
56      public IntegerCellEditor(int min, int max) {
57          _min = min;
58          _max = max;
59      }
60  
61      /***
62       * Default construcor.
63       * 
64       */
65      public IntegerCellEditor() {
66      }
67  
68      protected int convertValue(IRow row, IColumn column) {
69          Object value = column.getValue(row);
70          return value != null ? (Integer) value : 0;
71      }
72  
73      protected void storeValue(IRow row, IColumn column) {
74          Integer value = _spinner.getSelection();
75          _column.setValue(_row, value);
76      }
77  
78      /***
79       * Create the control.
80       * 
81       * @param table parent table
82       */
83      private void createControl(JaretTable table) {
84          if (_spinner == null) {
85              _table = table;
86              _spinner = new Spinner(table, SWT.BORDER);
87  
88              _spinner.setMaximum(_max);
89              _spinner.setMinimum(_min);
90  
91              _spinner.addTraverseListener(new TraverseListener() {
92                  public void keyTraversed(TraverseEvent e) {
93                      e.doit = false;
94                  }
95              });
96  
97              _spinner.addKeyListener(new KeyListener() {
98                  public void keyPressed(KeyEvent event) {
99                      if (event.keyCode == SWT.TAB) {
100                         event.doit = false;
101                         stopEditing(true);
102                         _table.forceFocus();
103                         _table.focusRight();
104                     } else if (event.keyCode == SWT.CR) {
105                         event.doit = false;
106                         stopEditing(true);
107                         _table.forceFocus();
108                         _table.focusDown();
109                     } else if (event.keyCode == SWT.ESC) {
110                         event.doit = false;
111                         stopEditing(false);
112                         _column.setValue(_row, _oldVal);
113                         _table.forceFocus();
114                     }
115 
116                 }
117 
118                 public void keyReleased(KeyEvent arg0) {
119                 }
120 
121             });
122 
123             _spinner.addFocusListener(this);
124         }
125     }
126 
127     /***
128      * {@inheritDoc}
129      */
130     public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
131         super.getEditorControl(table, row, column, typedKey);
132         createControl(table);
133         _oldVal = (Integer) column.getValue(row);
134         if (false && typedKey != 0) {
135             // _spinner.setsetText("" + typedKey);
136             // _text.setSelection(1);
137         } else {
138             int value = convertValue(row, column);
139             _spinner.setSelection(value);
140         }
141         return _spinner;
142     }
143 
144     /***
145      * {@inheritDoc}
146      */
147     public int getPreferredHeight() {
148         if (_spinner == null) {
149             return -1;
150         }
151         Point size = _spinner.computeSize(SWT.DEFAULT, SWT.DEFAULT);
152         return size.y;
153     }
154 
155     /***
156      * {@inheritDoc} Do nothing on gaining focus.
157      */
158     public void focusGained(FocusEvent arg0) {
159     }
160 
161     /***
162      * {@inheritDoc} Stop and strore when focus leaves.
163      */
164     public void focusLost(FocusEvent arg0) {
165         _table.stopEditing(true);
166     }
167 
168     /***
169      * {@inheritDoc}
170      */
171     public void dispose() {
172         super.dispose();
173         if (_spinner != null && !_spinner.isDisposed()) {
174             _spinner.removeFocusListener(this);
175             _spinner.dispose();
176         }
177 
178     }
179 
180     /***
181      * {@inheritDoc}
182      */
183     public void stopEditing(boolean storeInput) {
184         if (storeInput) {
185             storeValue(_row, _column);
186         }
187         _spinner.setVisible(false);
188     }
189 
190 }