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