1 package fr.ifremer.quadrige2.ui.swing.common.component.bean;
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 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
28 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBoxHandler;
29 import org.apache.commons.lang3.StringUtils;
30 import org.nuiton.decorator.JXPathDecorator;
31
32 import javax.swing.text.JTextComponent;
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37 import java.util.List;
38
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46
47
48
49
50
51 public class ExtendedComboBoxHandler<O> extends BeanFilterableComboBoxHandler<O> implements FocusListener {
52
53
54
55
56 protected final ExtendedComboBox<O> ui;
57
58
59
60
61 ActionListener actionListener;
62
63
64
65
66
67
68 @SuppressWarnings("unchecked")
69 public ExtendedComboBoxHandler(BeanFilterableComboBox ui) {
70 super(ui);
71 this.ui = (ExtendedComboBox<O>) ui;
72 }
73
74
75 @Override
76 @SuppressWarnings("unchecked")
77 public void init(JXPathDecorator decorator, List data) {
78
79
80 ui.getCombobox().setModel(new ExtendedComboBoxModel<O>());
81
82 super.init(decorator, data);
83
84 ui.getCombobox().getEditor().getEditorComponent().setFocusTraversalKeysEnabled(false);
85 ui.getCombobox().getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
86
87 @Override
88 public void keyPressed(final KeyEvent e) {
89 if (KeyEvent.VK_TAB == e.getKeyCode()) {
90
91
92 Component editor = ui.getCombobox().getEditor().getEditorComponent();
93 KeyEvent enterEvent = new KeyEvent(editor, KeyEvent.KEY_PRESSED, e.getWhen() + 1, 0, KeyEvent.VK_ENTER, '\r');
94 editor.dispatchEvent(enterEvent);
95 enterEvent = new KeyEvent(editor, KeyEvent.KEY_RELEASED, e.getWhen() + 2, 0, KeyEvent.VK_ENTER, '\r');
96 editor.dispatchEvent(enterEvent);
97
98 if (e.isShiftDown()) {
99 KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
100 } else {
101 KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
102 }
103
104 return;
105 }
106 super.keyPressed(e);
107 }
108
109 @Override
110 public void keyReleased(KeyEvent e) {
111 if (KeyEvent.VK_TAB == e.getKeyCode()) {
112 e.consume();
113 return;
114 }
115 super.keyReleased(e);
116 }
117 });
118 ui.getCombobox().addFocusListener(this);
119 ui.getCombobox().getEditor().getEditorComponent().addFocusListener(this);
120
121 ui.addPropertyChangeListener(BeanFilterableComboBox.PROPERTY_SELECTED_ITEM, new PropertyChangeListener() {
122 @Override
123 public void propertyChange(PropertyChangeEvent evt) {
124 resetCaretPosition();
125 }
126 });
127
128 ui.removeDataBinding(BeanFilterableComboBox.BINDING_TOOLBAR_RIGHT_VISIBLE);
129 ui.getToolbarRight().setVisible(true);
130
131 initActionButton();
132 }
133
134
135 @Override
136 protected void setSelectedItem(O oldValue, O newValue) {
137 super.setSelectedItem(oldValue, newValue);
138 resetCaretPosition();
139 }
140
141
142 @Override
143 public void focusGained(FocusEvent e) {
144 }
145
146
147 @Override
148 public void focusLost(FocusEvent e) {
149 resetCaretPosition();
150 }
151
152
153
154
155 protected void resetCaretPosition() {
156 JTextComponent editor = (JTextComponent) ui.getCombobox().getEditor().getEditorComponent();
157 editor.moveCaretPosition(0);
158 }
159
160 private void initActionButton() {
161 if (ui.showActionButton) {
162 if (ui.getIcon() != null) {
163 ui.getActionButton().setIcon(ui.getIcon());
164 }
165 if (StringUtils.isNotBlank(ui.getActionToolTipI18n())) {
166 ui.getActionButton().setToolTipText(t(ui.getActionToolTipI18n()));
167 }
168 }
169 }
170
171
172
173
174 public void doAction() {
175 if (actionListener != null) {
176 actionListener.actionPerformed(null);
177 }
178 }
179
180
181
182
183
184
185 public void setActionListener(ActionListener actionListener) {
186 this.actionListener = actionListener;
187 }
188
189 }