View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.context.menu;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
27  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
28  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import javax.swing.SwingUtilities;
33  
34  /**
35   * Controlleur du menu pour la gestion des lieux au niveau National
36   */
37  public class ManageContextsListMenuUIHandler extends AbstractReefDbUIHandler<ManageContextsListMenuUIModel, ManageContextsListMenuUI> {
38  
39      /**
40       * Logger.
41       */
42      private static final Log LOG = LogFactory.getLog(ManageContextsListMenuUIHandler.class);
43  
44      /** {@inheritDoc} */
45      @Override
46      public void beforeInit(final ManageContextsListMenuUI ui) {
47          super.beforeInit(ui);
48  
49          // create model and register to the JAXX context
50          final ManageContextsListMenuUIModel model = new ManageContextsListMenuUIModel();
51          ui.setContextValue(model);
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public void afterInit(final ManageContextsListMenuUI ui) {
57          initUI(ui);
58  
59          // Initialize combobox
60          initComboBox();
61      }
62  
63      /**
64       * Initialize  combobox
65       */
66      private void initComboBox() {
67  
68          initBeanFilterableComboBox(
69                  getUI().getContextsLabelsCombo(),
70                  getContext().getContextService().getAllContexts(),
71                  null);
72  
73          ReefDbUIs.forceComponentSize(getUI().getContextsLabelsCombo());
74  
75          getUI().getContextsLabelsCombo().getComboBoxModel().addWillChangeSelectedItemListener(event -> {
76              if (getModel().isLoading()) return;
77              if (event.getNextSelectedItem() != null) SwingUtilities.invokeLater(() -> getUI().getSearchButton().getAction().actionPerformed(null));
78          });
79      }
80  
81      /**
82       * <p>reloadComboBox.</p>
83       */
84      @SuppressWarnings("unchecked")
85      public void reloadComboBox() {
86          BeanFilterableComboBox cb = getUI().getContextsLabelsCombo();
87          cb.setData(null);
88          cb.setData(getContext().getContextService().getAllContexts());
89          if (cb.getCombobox().getSelectedIndex() != -1) {
90              int lastSelectedIndex = cb.getCombobox().getSelectedIndex();
91              cb.getCombobox().setSelectedIndex(-1);
92              cb.getCombobox().setSelectedIndex(lastSelectedIndex);
93          }
94  
95      }
96  }