View Javadoc

1   /*
2    *  File: DateCellEditor.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.util.Calendar;
14  import java.util.Date;
15  
16  import org.eclipse.swt.SWT;
17  import org.eclipse.swt.events.FocusEvent;
18  import org.eclipse.swt.events.FocusListener;
19  import org.eclipse.swt.events.KeyEvent;
20  import org.eclipse.swt.events.KeyListener;
21  import org.eclipse.swt.events.TraverseEvent;
22  import org.eclipse.swt.events.TraverseListener;
23  import org.eclipse.swt.widgets.Control;
24  import org.eclipse.swt.widgets.Display;
25  
26  import de.jaret.util.date.JaretDate;
27  import de.jaret.util.ui.datechooser.DateChooser;
28  import de.jaret.util.ui.datechooser.IDateChooserListener;
29  import de.jaret.util.ui.datechooser.IFieldIdentifier;
30  import de.jaret.util.ui.datechooser.SimpleFieldIdentifier;
31  import de.jaret.util.ui.table.JaretTable;
32  import de.jaret.util.ui.table.model.IColumn;
33  import de.jaret.util.ui.table.model.IRow;
34  
35  /***
36   * Cell editor for editing dates using the jaret datechooser. Supports java.util.date and JaretDate. The fieldidentifier
37   * used for the datechooser (see Javadoc there) is not locale dependant (Day/Month/year) have to be changed when used in
38   * another country (or removed!).
39   * <p>
40   * Key bindings: TAB and CR will leave the datechooser (positive). ESC will leave the chooser resetting the date to the
41   * value present when editing started.
42   * </p>
43   * 
44   * @author Peter Kliem
45   * @version $Id: DateCellEditor.java 602 2007-10-19 05:42:14Z olk $
46   */
47  public class DateCellEditor extends CellEditorBase implements ICellEditor, IDateChooserListener, FocusListener {
48      /*** chooser component. */
49      protected DateChooser _chooser;
50  
51      /*** old java.util.Date val if present. */
52      protected Date _oldVal;
53      /*** old JaretDate value if present. */
54      protected JaretDate _oldJaretDateVal;
55  
56      /*** true if jaretdate is used. */
57      private boolean _jaretDate;
58  
59      /***
60       * Create the chooser control.
61       * 
62       * @param table parent table.
63       */
64      private void createControl(JaretTable table) {
65          _table = table;
66          if (_chooser == null) {
67              _chooser = new DateChooser(table, SWT.NULL);
68              // TODO locale dependent
69              IFieldIdentifier fi = new SimpleFieldIdentifier(".", new int[] {Calendar.DAY_OF_MONTH, Calendar.MONTH,
70                      Calendar.YEAR});
71              _chooser.setFieldIdentifier(fi);
72              _chooser.setSelectAllOnFocusGained(false);
73              _chooser.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
74              _chooser.addFocusListener(this);
75              _chooser.addDateChooserListener(this);
76  
77              _chooser.getTextField().addKeyListener(new KeyListener() {
78                  public void keyPressed(KeyEvent event) {
79                      if (event.keyCode == SWT.TAB) {
80                          _chooser.validateInput();
81                          stopEditing(true);
82                          event.doit = false; // do not further process
83                          _table.forceFocus();
84                          _table.focusRight();
85                      } else if (event.keyCode == SWT.CR) {
86                          _chooser.validateInput();
87                          stopEditing(true);
88                          event.doit = false; // do not further process
89                          _table.forceFocus();
90                          _table.focusDown();
91                      } else if (event.keyCode == SWT.ESC) {
92                          stopEditing(false);
93                          restoreOldVal();
94                          event.doit = false; // do not further process
95                          _table.forceFocus();
96                      }
97                  }
98  
99                  public void keyReleased(KeyEvent e) {
100                 }
101             });
102 
103             // add a traverse listener so the TAB-key won't traverse the focus out of the table
104             _chooser.getTextField().addTraverseListener(new TraverseListener() {
105                 public void keyTraversed(TraverseEvent e) {
106                     e.doit = false;
107                 }
108 
109             });
110         }
111     }
112 
113     /***
114      * {@inheritDoc}
115      */
116     public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
117         super.getEditorControl(table, row, column, typedKey);
118         createControl(table);
119         if (column.getValue(row) instanceof Date) {
120             _oldVal = (Date) column.getValue(row);
121             _jaretDate = false;
122         } else if (column.getValue(row) instanceof JaretDate) {
123             _oldVal = ((JaretDate) column.getValue(row)).getDate();
124             _oldJaretDateVal = (JaretDate) column.getValue(row);
125             _jaretDate = true;
126         }
127         if (typedKey != 0) {
128             _chooser.setText("" + typedKey);
129             _chooser.setSelection(1);
130         } else {
131             _chooser.setDate(_oldVal);
132         }
133         _row = row;
134         _column = column;
135 
136         return _chooser;
137     }
138 
139     /***
140      * Restore date from the the beginning of the edit action.
141      * 
142      */
143     private void restoreOldVal() {
144         if (!_jaretDate) {
145             _column.setValue(_row, _oldVal);
146         } else {
147             _column.setValue(_row, _oldJaretDateVal);
148         }
149     }
150 
151     /***
152      * {@inheritDoc}
153      */
154     public void stopEditing(boolean storeInput) {
155         if (storeInput) {
156             storeValue();
157         }
158         _chooser.setDropped(false);
159         _chooser.setVisible(false);
160     }
161 
162     /***
163      * Store the value in the model.
164      * 
165      */
166     private void storeValue() {
167         if (!_jaretDate) {
168             _column.setValue(_row, _chooser.getDate());
169         } else {
170             _column.setValue(_row, new JaretDate(_chooser.getDate()));
171         }
172     }
173 
174     /***
175      * {@inheritDoc}
176      */
177     public void dispose() {
178         if (_chooser != null && !_chooser.isDisposed()) {
179             _chooser.removeFocusListener(this);
180             _chooser.dispose();
181         }
182         // help the garbage collector
183         _table = null;
184         _column = null;
185         _row = null;
186     }
187 
188     /***
189      * {@inheritDoc} If the users choses a date, stop editing an store the chosen date.
190      */
191     public void dateChosen(Date date) {
192         // if a date has been chosen in the datechooser stop editing immediately
193         stopEditing(true);
194         _table.forceFocus();
195     }
196 
197     /***
198      * {@inheritDoc} No Action on intermediate changes in the chooser.
199      */
200     public void dateIntermediateChange(Date date) {
201     }
202 
203     /***
204      * {@inheritDoc} When the chooser tells us the user canceled the editing, restore the old date.
205      */
206     public void choosingCanceled() {
207         _chooser.setDate(_oldVal);
208     }
209 
210     /***
211      * {@inheritDoc} nothing to do.
212      */
213     public void inputInvalid() {
214     }
215     
216     /***
217      * {@inheritDoc} Do nothing on focus gained.
218      */
219     public void focusGained(FocusEvent e) {
220     }
221 
222     /***
223      * {@inheritDoc} When loosing focus, stop the editing and store the value.
224      */
225     public void focusLost(FocusEvent e) {
226         _table.stopEditing(true);
227     }
228 
229 }