View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.component.bean;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Le handler d'un {@link ExtendedComboBox}.
42   * <p/>
43   * Note: ce handler n'est pas stateless et n'est donc pas partageable entre
44   * plusieurs ui.
45   *
46   * @param <O> le type des objet contenus dans le modèle du composant.
47   * @author Ludovic
48   * @see ExtendedComboBox
49   */
50  public class ExtendedComboBoxHandler<O> extends BeanFilterableComboBoxHandler<O> implements FocusListener {
51  
52      /**
53       * ui if the handler
54       */
55      protected final ExtendedComboBox<O> ui;
56  
57      /**
58       * delegate action listener executed when action button is pressed
59       */
60      private ActionListener actionListener;
61  
62      /**
63       * <p>Constructor for ExtendedComboBoxHandler.</p>
64       *
65       * @param ui a {@link jaxx.runtime.swing.editor.bean.BeanFilterableComboBox} object.
66       */
67      @SuppressWarnings("unchecked")
68      public ExtendedComboBoxHandler(BeanFilterableComboBox ui) {
69          super(ui);
70          this.ui = (ExtendedComboBox<O>) ui;
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      @SuppressWarnings("unchecked")
76      public void init(JXPathDecorator decorator, List data) {
77  
78          // Override default model
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                      // simulate ENTER key action when TAB key pressed
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     /** {@inheritDoc} */
129     @Override
130     protected void setSelectedItem(O oldValue, O newValue) {
131 
132         super.setSelectedItem(oldValue, newValue);
133 
134         if (newValue == null) {
135             // Force reset the selected item in combobox model (Mantis #51731)
136             getEditorComponent().setText("");
137         }
138 
139         resetCaretPosition();
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public void focusGained(FocusEvent e) {
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public void focusLost(FocusEvent e) {
150         resetCaretPosition();
151     }
152 
153     /**
154      * <p>resetCaretPosition.</p>
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      * <p>doAction.</p>
177      */
178     public void doAction() {
179         if (actionListener != null) {
180             actionListener.actionPerformed(null);
181         }
182     }
183 
184     /**
185      * <p>Setter for the field <code>actionListener</code>.</p>
186      *
187      * @param actionListener a {@link java.awt.event.ActionListener} object.
188      */
189     public void setActionListener(ActionListener actionListener) {
190         this.actionListener = actionListener;
191     }
192 
193 }