View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.filter.element;
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.quadrige3.ui.core.dto.QuadrigeBean;
27  import fr.ifremer.dali.ui.swing.content.manage.filter.element.menu.ApplyFilterUIModel;
28  import fr.ifremer.dali.ui.swing.content.manage.referential.menu.DefaultReferentialMenuUIModel;
29  import fr.ifremer.dali.ui.swing.content.manage.referential.menu.ReferentialMenuUI;
30  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
31  import fr.ifremer.dali.ui.swing.util.DaliUIs;
32  import fr.ifremer.quadrige3.core.dao.technical.Assert;
33  import jaxx.runtime.SwingUtil;
34  
35  import javax.swing.SwingUtilities;
36  import java.awt.Component;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * Controler.
42   *
43   * @param <E>
44   * @param <UI>
45   */
46  public abstract class AbstractFilterElementUIHandler<E extends QuadrigeBean, UI extends FilterElementUI<E>, MENU extends ReferentialMenuUI>
47          extends AbstractDaliUIHandler<FilterElementUIModel, UI> {
48  
49      private MENU referentialMenuUI;
50  
51      /**
52       * method to override to obtain specific filter element menu
53       *
54       * @return the referential menu
55       */
56      protected abstract MENU createNewReferentialMenuUI();
57  
58      /**
59       * <p>Getter for the field <code>referentialMenuUI</code>.</p>
60       *
61       * @return a MENU object.
62       */
63      public final MENU getReferentialMenuUI() {
64  
65          if (referentialMenuUI == null) {
66              referentialMenuUI = createNewReferentialMenuUI();
67          }
68  
69          return referentialMenuUI;
70  
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public void beforeInit(final UI ui) {
76          super.beforeInit(ui);
77  
78          // Create model and register to the JAXX context directly into ui.model
79          ui.model = new FilterElementUIModel<>();
80          ui.setContextValue(ui.model);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      @SuppressWarnings("unchecked")
86      public void afterInit(UI ui) {
87  
88          initUI(ui);
89  
90          // set color here because jaxx don't recognize 'handler'
91          ui.getListBlockLayer().setBlockingColor(getConfig().getColorBlockingLayer());
92          SwingUtil.setLayerUI(ui.getFilterElementPanel(), ui.getListBlockLayer());
93          ui.applyDataBinding(FilterElementUI.BINDING_LIST_BLOCK_LAYER_BLOCK);
94  
95          // Init double list
96          initBeanList(
97                  getUI().getFilterDoubleList(),
98                  new ArrayList<>(),
99                  new ArrayList<>(),
100                 DaliUIs.DALI_DOUBLE_LIST_SIZE);
101 
102         // Reaffect the model
103         getUI().getFilterDoubleList().setBean(getModel());
104 
105         // init menu UI
106         MENU menuUI = getReferentialMenuUI();
107         Assert.isInstanceOf(Component.class, menuUI);
108         getUI().getFilterElementMenuPanel().add((Component) menuUI);
109         getUI().get$objectMap().put("menuUI", menuUI);
110 
111         // init listener on filtered elements
112         menuUI.getHandler().getApplyFilterUI().getModel().addPropertyChangeListener(ApplyFilterUIModel.PROPERTY_ELEMENTS, evt -> {
113             List<E> elements = (List<E>) evt.getNewValue();
114             loadAvailableElements(elements);
115         });
116 
117         // listen to found results
118         menuUI.getModel().addPropertyChangeListener(DefaultReferentialMenuUIModel.PROPERTY_RESULTS, evt -> loadAvailableElements((List<E>) evt.getNewValue()));
119 
120         disable();
121     }
122 
123     /**
124      * <p>enable.</p>
125      */
126     public void enable() {
127 
128         // Activate search
129         getReferentialMenuUI().getHandler().enableSearch(true);
130 
131         // Activation de la double liste
132         getUI().getFilterDoubleList().setEnabled(true);
133 
134     }
135 
136     /**
137      * <p>disable.</p>
138      */
139     public void disable() {
140 
141         // De activate search components
142         getReferentialMenuUI().getHandler().enableSearch(false);
143 
144         // Deactivate double list
145         getUI().getFilterDoubleList().setEnabled(false);
146 
147     }
148 
149     /**
150      * Load element.
151      * TODO rename
152      *
153      * @param elements Element to load
154      */
155     @SuppressWarnings("unchecked")
156     public void loadSelectedElements(final List<E> elements) {
157 
158         enable();
159 
160         getModel().setElements(elements);
161         getModel().setAdjusting(true);
162         getUI().getFilterDoubleList().getHandler().setSelected(elements != null ? new ArrayList<>(elements) : null);
163         getModel().setAdjusting(false);
164     }
165 
166     /**
167      * Load elements.
168      * TODO rename
169      *
170      * @param elements Element to load
171      */
172     @SuppressWarnings("unchecked")
173     private void loadAvailableElements(final List<E> elements) {
174         if (getModel().isAdjusting()) {
175             return;
176         }
177 
178         // affect new universe list
179         getUI().getFilterDoubleList().getHandler().setUniverse(elements);
180 
181         // re affect select elements by invokeLater because of concurrent access to universe list
182         getModel().setAdjusting(true);
183         SwingUtilities.invokeLater(() -> {
184             getUI().getFilterDoubleList().getHandler().setSelected(getModel().getElements() != null ? new ArrayList<>(getModel().getElements()) : null);
185             getModel().setAdjusting(false);
186         });
187     }
188 
189     /**
190      * Delete filters.
191      */
192     public void clear() {
193 
194         disable();
195 
196         // Clear menu model
197         getReferentialMenuUI().getModel().clear();
198 
199         // Clear list on double list
200         getUI().getFilterDoubleList().getHandler().setUniverse(null);
201         getUI().getFilterDoubleList().getHandler().setSelected(null);
202     }
203 
204 }