blob: 2affc2de59fae923a4314576c3135ce60d31a233 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.core.ui.internal.components;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
/**
* A TextComboBoxCellEditor to overcome the limitation of the standard ComboBoxCellEditor, which does not allow to edit
* plain text values.
*
* @author Dmitry Platonoff
*/
public class TextComboBoxCellEditor extends CellEditor {
protected String[] items;
protected CCombo combo;
public TextComboBoxCellEditor(Composite parent, int style) {
super(parent, style);
}
protected Control createControl(Composite parent) {
combo = new CCombo(parent, getStyle());
combo.setFont(parent.getFont());
combo.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
keyReleaseOccured(e);
}
});
combo.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if(e.detail == SWT.TRAVERSE_ESCAPE || e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false;
}
}
});
loadItems();
return combo;
}
protected Object doGetValue() {
Assert.isNotNull(combo);
return combo.getText();
}
protected void doSetFocus() {
Assert.isNotNull(combo);
combo.setFocus();
}
protected void doSetValue(Object value) {
Assert.isNotNull(combo);
combo.setText(String.valueOf(value));
}
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
loadItems();
}
protected void loadItems() {
if(combo != null && items != null) {
combo.setItems(items);
}
}
protected void keyReleaseOccured(KeyEvent keyEvent) {
if(keyEvent.character == SWT.ESC) {
fireCancelEditor();
} else if(keyEvent.character == SWT.TAB || keyEvent.character == SWT.CR) {
focusLost();
}
}
}
|