View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.rule.menu;
2   
3   /*
4    * #%L
5    * Dali :: 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.dali.dto.configuration.control.RuleListDTO;
27  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
28  import fr.ifremer.dali.ui.swing.content.manage.filter.element.menu.ApplyFilterUI;
29  import fr.ifremer.dali.ui.swing.content.manage.referential.menu.ReferentialMenuUIHandler;
30  import fr.ifremer.dali.ui.swing.content.manage.rule.RulesUI;
31  import fr.ifremer.dali.ui.swing.util.DaliUIs;
32  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
33  
34  import javax.swing.SwingUtilities;
35  import java.util.List;
36  
37  /**
38   * Controlleur du menu de sélection des listes de règles
39   */
40  public class RulesMenuUIHandler extends ReferentialMenuUIHandler<RulesMenuUIModel, RulesMenuUI> {
41  
42      /** {@inheritDoc} */
43      @Override
44      public void beforeInit(final RulesMenuUI ui) {
45          super.beforeInit(ui);
46          
47          // create model and register to the JAXX context
48          final RulesMenuUIModel model = new RulesMenuUIModel();
49          ui.setContextValue(model);
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public void afterInit(final RulesMenuUI ui) {
55          initUI(ui);
56          
57          initComboBox();
58          initListeners();
59      }
60  
61  	/**
62  	 * Initialisation des combobox
63  	 */
64  	private void initComboBox() {
65  		
66  		initBeanFilterableComboBox(
67  				getUI().getRuleListComboBox(),
68  				getContext().getRuleListService().getAllRuleLists(),
69  				null);
70  
71          initBeanFilterableComboBox(
72                  getUI().getProgramComboBox(),
73                  getContext().getProgramStrategyService().getManagedPrograms(),
74                  null
75          );
76  
77          DaliUIs.forceComponentSize(getUI().getRuleListComboBox());
78          DaliUIs.forceComponentSize(getUI().getProgramComboBox());
79  
80  	}
81  
82      private void initListeners() {
83  
84          getModel().addPropertyChangeListener(RulesMenuUIModel.PROPERTY_RULE_LIST, evt -> {
85              if (getModel().isLoading()) return;
86              if (getModel().getRuleList() != null) {
87                  getModel().setProgram(null);
88                  SwingUtilities.invokeLater(() -> getUI().getSearchButton().getAction().actionPerformed(null));
89              }
90          });
91      }
92  
93      @Override
94      public void enableSearch(boolean enabled) {
95      }
96  
97      @Override
98      public List<FilterDTO> getFilters() {
99          return null;
100     }
101 
102     @Override
103     public ApplyFilterUI getApplyFilterUI() {
104         return null;
105     }
106 
107     /**
108      * <p>reloadComboBox.</p>
109      */
110     public void reloadComboBox() {
111         getModel().setLoading(true);
112         BeanFilterableComboBox<RuleListDTO> cb = getUI().getRuleListComboBox();
113         cb.setData(null);
114         List<RuleListDTO> ruleLists = getContext().getRuleListService().getAllRuleLists();
115         cb.setData(ruleLists);
116 
117         // clear selected item if not present in list (Mantis #50537)
118         if (cb.getSelectedItem() instanceof RuleListDTO && !ruleLists.contains(cb.getSelectedItem())) {
119             cb.setSelectedItem(null);
120         }
121 
122         getModel().setLoading(false);
123     }
124 
125     /**
126      * <p>getRulesUI.</p>
127      *
128      * @return a {@link fr.ifremer.dali.ui.swing.content.manage.rule.RulesUI} object.
129      */
130     RulesUI getRulesUI() {
131         return getUI().getParentContainer(RulesUI.class);
132     }
133 }