View Javadoc

1   /*
2    *  File: EnumComboEditor.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.graphics.Point;
19  import org.eclipse.swt.widgets.Combo;
20  import org.eclipse.swt.widgets.Control;
21  
22  import de.jaret.util.ui.table.JaretTable;
23  import de.jaret.util.ui.table.model.IColumn;
24  import de.jaret.util.ui.table.model.IRow;
25  
26  /***
27   * Editor for a field with an enum as type. Naturally uses a combobox.
28   * 
29   * @author Peter Kliem
30   * @version $Id: EnumComboEditor.java 394 2007-05-01 10:51:25Z olk $
31   */
32  public class EnumComboEditor extends CellEditorBase implements ICellEditor, FocusListener {
33      /*** combobox widget. */
34      protected Combo _combo;
35      /*** old value. */
36      protected Object _oldVal;
37      /*** list of selectable items in the combobox. */
38      protected Object[] _items;
39  
40      /***
41       * {@inheritDoc}
42       */
43      public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
44          super.getEditorControl(table, row, column, typedKey);
45  
46          _items = new Object[] {};
47  
48          if (_combo == null) {
49              _combo = new Combo(table, SWT.BORDER | SWT.READ_ONLY);
50              _combo.addKeyListener(new KeyListener() {
51  
52                  public void keyPressed(KeyEvent event) {
53                      if (event.keyCode == SWT.TAB) {
54                          event.doit = false;
55                          stopEditing(true);
56                          _table.forceFocus();
57                          _table.focusRight();
58                      } else if (event.keyCode == SWT.CR) {
59                          event.doit = false;
60                          stopEditing(true);
61                          _table.forceFocus();
62                          _table.focusDown();
63                      } else if (event.keyCode == SWT.ESC) {
64                          event.doit = false;
65                          stopEditing(false);
66                          _column.setValue(_row, _oldVal);
67                          _table.forceFocus();
68                      }
69                  }
70  
71                  public void keyReleased(KeyEvent arg0) {
72                  }
73  
74              });
75              _combo.addFocusListener(this);
76  
77          }
78          Class<?> clazz = column.getContentClass(row);
79  
80          if (clazz != null && Enum.class.isAssignableFrom(clazz)) {
81              _items = clazz.getEnumConstants();
82          } else {
83              _items = new Object[] {};
84          }
85  
86          Object value = column.getValue(row);
87          _oldVal = value;
88  
89          int selIdx = -1;
90          String[] stringItems = new String[_items.length];
91          for (int i = 0; i < _items.length; i++) {
92              stringItems[i] = _items[i].toString();
93              if (value != null && value.equals(_items[i])) {
94                  selIdx = i;
95              }
96          }
97          _combo.setItems(stringItems);
98  
99          if (selIdx != -1) {
100             _combo.select(selIdx);
101         }
102 
103         return _combo;
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     public void stopEditing(boolean storeInput) {
110         if (storeInput) {
111             int selIdx = _combo.getSelectionIndex();
112             Object selection = null;
113             if (selIdx != -1) {
114                 selection = _items[selIdx];
115             }
116             _column.setValue(_row, selection);
117         }
118         _combo.setVisible(false);
119     }
120 
121     /***
122      * {@inheritDoc}
123      */
124     public void dispose() {
125         super.dispose();
126         if (_combo != null && !_combo.isDisposed()) {
127             _combo.dispose();
128         }
129     }
130 
131     /***
132      * {@inheritDoc} Do nothing on gaining focus.
133      */
134     public void focusGained(FocusEvent arg0) {
135     }
136 
137     /***
138      * {@inheritDoc} Stop editing and store the value.
139      */
140     public void focusLost(FocusEvent arg0) {
141         stopEditing(true);
142     }
143 
144     /***
145      * {@inheritDoc}
146      */
147     public int getPreferredHeight() {
148         if (_combo != null) {
149             Point size = _combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
150             return size.y;
151         }
152         return -1;
153     }
154 }