View Javadoc

1   /*
2    *  File: ObjectComboEditor.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.List;
14  
15  import org.eclipse.jface.viewers.ILabelProvider;
16  import org.eclipse.jface.viewers.ILabelProviderListener;
17  import org.eclipse.swt.SWT;
18  import org.eclipse.swt.events.FocusEvent;
19  import org.eclipse.swt.events.FocusListener;
20  import org.eclipse.swt.events.KeyEvent;
21  import org.eclipse.swt.events.KeyListener;
22  import org.eclipse.swt.graphics.Image;
23  import org.eclipse.swt.graphics.Point;
24  import org.eclipse.swt.widgets.Combo;
25  import org.eclipse.swt.widgets.Control;
26  
27  import de.jaret.util.ui.table.JaretTable;
28  import de.jaret.util.ui.table.model.IColumn;
29  import de.jaret.util.ui.table.model.IRow;
30  
31  /***
32   * Editor using a ComboBox for selecting one of several objects supplied to the editor at creation time. A label
33   * provider is used for toString conversion.
34   * 
35   * @author Peter Kliem
36   * @version $Id: ObjectComboEditor.java 394 2007-05-01 10:51:25Z olk $
37   */
38  public class ObjectComboEditor extends CellEditorBase implements ICellEditor, FocusListener {
39      /*** combox widget. */
40      protected Combo _combo;
41      /*** old value. */
42      protected Object _oldVal;
43      /*** list of items displayed. */
44      protected String[] _stringItems;
45      /*** label provider used. */
46      protected ILabelProvider _labelProvider;
47      /*** if true allow null as a possible selection. */
48      protected boolean _allowNull = true;
49      /*** the text displayed for <code>null</code>. */
50      protected String _nullText = "";
51      /*** object list for selection. */
52      protected List<? extends Object> _itemList;
53  
54      /***
55       * Construct a new ObjectComboEditor with a list of selectabel Objects and an ILabelprovider.
56       * 
57       * @param list list of Objects that may be selected.
58       * @param labelProvider label provider to be used or <code>null</code>. In the latter case a simple toString
59       * label provider will be used.
60       * @param allowNull if true null will always be a possible value in the comboBox
61       * @param nullText string to be displyed for the null value if allowed
62       */
63      public ObjectComboEditor(List<? extends Object> list, ILabelProvider labelProvider, boolean allowNull,
64              String nullText) {
65          _labelProvider = labelProvider;
66          if (_labelProvider == null) {
67              _labelProvider = new ToStringLabelProvider();
68          }
69          _allowNull = allowNull;
70          _nullText = nullText;
71          _itemList = list;
72  
73          if (_itemList != null) {
74              initItems();
75          }
76  
77      }
78  
79      protected void initItems() {
80          _stringItems = _allowNull ? new String[_itemList.size() + 1] : new String[_itemList.size()];
81  
82          int i = 0;
83          if (_allowNull) {
84              _stringItems[0] = _nullText;
85              i = 1;
86          }
87  
88          for (Object o : _itemList) {
89              _stringItems[i++] = _labelProvider.getText(o);
90          }
91      }
92  
93      /***
94       * {@inheritDoc}
95       */
96      public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
97          super.getEditorControl(table, row, column, typedKey);
98  
99          if (_combo == null) {
100             _combo = new Combo(table, SWT.BORDER | SWT.READ_ONLY);
101             _combo.addKeyListener(new KeyListener() {
102 
103                 public void keyPressed(KeyEvent event) {
104                     if (event.keyCode == SWT.TAB) {
105                         event.doit = false;
106                         stopEditing(true);
107                         _table.forceFocus();
108                         _table.focusRight();
109                     } else if (event.keyCode == SWT.CR) {
110                         event.doit = false;
111                         stopEditing(true);
112                         _table.forceFocus();
113                         _table.focusDown();
114                     } else if (event.keyCode == SWT.ESC) {
115                         event.doit = false;
116                         stopEditing(false);
117                         _column.setValue(_row, _oldVal);
118                         _table.forceFocus();
119                     }
120                 }
121 
122                 public void keyReleased(KeyEvent arg0) {
123                 }
124 
125             });
126             _combo.addFocusListener(this);
127             _combo.setItems(_stringItems);
128         }
129 
130         Object value = column.getValue(row);
131         _oldVal = value;
132 
133         int selIdx = -1;
134         if (_allowNull && value == null) {
135             selIdx = 0;
136         } else {
137             selIdx = _itemList.indexOf(value);
138             selIdx = _allowNull ? selIdx + 1 : selIdx;
139         }
140 
141         if (selIdx != -1) {
142             _combo.select(selIdx);
143         }
144 
145         return _combo;
146     }
147 
148     /***
149      * {@inheritDoc}
150      */
151     public void stopEditing(boolean storeInput) {
152         if (storeInput) {
153             int selIdx = _combo.getSelectionIndex();
154             Object selection = null;
155             if (selIdx != -1) {
156                 if (_allowNull && selIdx == 0) {
157                     selection = null;
158                 } else {
159                     selIdx = _allowNull ? selIdx - 1 : selIdx;
160                     selection = _itemList.get(selIdx);
161                 }
162             }
163             _column.setValue(_row, selection);
164         }
165         _combo.setVisible(false);
166     }
167 
168     /***
169      * {@inheritDoc}
170      */
171     public void dispose() {
172         super.dispose();
173         if (_combo != null && !_combo.isDisposed()) {
174             _combo.dispose();
175         }
176     }
177 
178     /***
179      * {@inheritDoc} Nothing to do on gaining focus.
180      */
181     public void focusGained(FocusEvent arg0) {
182     }
183 
184     /***
185      * {@inheritDoc} Store and end editing when focus is taken away.
186      */
187     public void focusLost(FocusEvent arg0) {
188         stopEditing(true);
189     }
190 
191     /***
192      * {@inheritDoc}
193      */
194     public int getPreferredHeight() {
195         if (_combo != null) {
196             Point size = _combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
197             return size.y;
198         }
199         return -1;
200     }
201 
202     /***
203      * Simple Labelprovider just using the toString method of any supplied object.
204      * 
205      * @author Peter Kliem
206      * @version $Id: ObjectComboEditor.java 394 2007-05-01 10:51:25Z olk $
207      */
208     public class ToStringLabelProvider implements ILabelProvider {
209 
210         /***
211          * {@inheritDoc}
212          */
213         public Image getImage(Object element) {
214             return null;
215         }
216 
217         /***
218          * {@inheritDoc}
219          */
220         public String getText(Object element) {
221             return element.toString();
222         }
223 
224         /***
225          * {@inheritDoc}
226          */
227         public void addListener(ILabelProviderListener listener) {
228         }
229 
230         /***
231          * {@inheritDoc}
232          */
233         public void dispose() {
234         }
235 
236         /***
237          * {@inheritDoc}
238          */
239         public boolean isLabelProperty(Object element, String property) {
240             return false;
241         }
242 
243         /***
244          * {@inheritDoc}
245          */
246         public void removeListener(ILabelProviderListener listener) {
247         }
248 
249     }
250 
251 }