View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.filterslist;
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.DaliBeans;
27  import fr.ifremer.dali.dto.configuration.context.ContextDTO;
28  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
29  import fr.ifremer.dali.dto.enums.FilterTypeValues;
30  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUI;
31  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
32  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
33  import fr.ifremer.dali.ui.swing.util.table.DaliColumnIdentifier;
34  import fr.ifremer.quadrige3.core.dao.technical.Assert;
35  import fr.ifremer.quadrige3.core.dao.technical.decorator.Decorator;
36  import fr.ifremer.quadrige3.ui.swing.component.bean.ExtendedComboBox;
37  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
38  import fr.ifremer.quadrige3.ui.swing.table.editor.FilterableComboBoxCellEditor;
39  import org.apache.commons.collections4.CollectionUtils;
40  import org.jdesktop.swingx.table.TableColumnExt;
41  
42  import javax.swing.SortOrder;
43  import javax.swing.event.CellEditorListener;
44  import javax.swing.event.ChangeEvent;
45  import java.awt.event.ItemEvent;
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  /**
50   * <p>ManageFiltersListTableUIHandler class.</p>
51   */
52  public class ManageFiltersListTableUIHandler extends
53          AbstractDaliTableUIHandler<ManageFiltersListTableUIRowModel, ManageFiltersListTableUIModel, ManageFiltersListTableUI> {
54  
55      /**
56       * {@inheritDoc}
57       *
58       * Logger.
59       */
60  //    private static final Log LOG = LogFactory.getLog(ManageFiltersListTableUIHandler.class);
61      @Override
62      public void beforeInit(final ManageFiltersListTableUI ui) {
63          super.beforeInit(ui);
64  
65          // create model and register to the JAXX context
66          final ManageFiltersListTableUIModel model = new ManageFiltersListTableUIModel();
67          ui.setContextValue(model);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void afterInit(final ManageFiltersListTableUI ui) {
73  
74          // Initialize the screen
75          initUI(ui);
76  
77          // Initialize the table
78          initTable();
79  
80          // Initialize the listeners
81          initListeners();
82      }
83  
84      /**
85       * Initialisation du tableau.
86       */
87      private void initTable() {
88  
89          // the table
90          final SwingTable table = getTable();
91  
92          // type
93          TableColumnExt typeCol = addColumn(ManageFiltersListTableUITableModel.TYPE);
94          typeCol.setSortable(true);
95          typeCol.setEditable(false);
96  
97          // Filter column
98          final TableColumnExt filterCol = addFilterComboDataColumnToModel();
99          filterCol.setEditable(true);
100 
101         ManageFiltersListTableUITableModel tableModel = new ManageFiltersListTableUITableModel(getTable().getColumnModel());
102         table.setModel(tableModel);
103 
104         // Initialisation du tableau
105         initTable(table, true);
106 
107         table.setSortOrder(ManageFiltersListTableUITableModel.TYPE, SortOrder.ASCENDING);
108 
109         table.setVisibleRowCount(FilterTypeValues.values().length);
110     }
111 
112     /**
113      * Add caracteristique controle column into model.
114      *
115      * @return Column
116      */
117     private TableColumnExt addFilterComboDataColumnToModel() {
118 
119         // Column identifier
120         final DaliColumnIdentifier<ManageFiltersListTableUIRowModel> identifier = ManageFiltersListTableUITableModel.FILTER;
121 
122         // Decorator name
123         final String decoratorName = identifier.getDecoratorName();
124 
125         // Specific editor
126         final FilterableComboBoxCellEditor<FilterDTO> editor = getFilterComboCellEditor(decoratorName);
127 
128         editor.addCellEditorListener(new CellEditorListener() {
129 
130             @Override
131             public void editingStopped(ChangeEvent e) {
132                 FilterComboCellEditor comboCellEditor = (FilterComboCellEditor) e.getSource();
133                 FilterDTO filter = (FilterDTO) comboCellEditor.getCellEditorValue();
134                 int filterTypeId = getModel().getSingleSelectedRow().getFilterTypeId();
135 
136                 // ensure filterTypeId are equals
137                 if (filter != null) {
138                     Assert.equals(filterTypeId, filter.getFilterTypeId());
139                 }
140 
141                 replaceFilterType(filterTypeId, filter);
142             }
143 
144             @Override
145             public void editingCanceled(ChangeEvent e) {
146             }
147         });
148 
149         // Add column to model
150         return addColumn(editor, newTableCellRender(FilterDTO.class, decoratorName), identifier);
151     }
152 
153     private void replaceFilterType(int filterTypeId, FilterDTO filter) {
154 
155         // remove old filter
156         FilterDTO oldFilter = DaliBeans.findByProperty(getModel().getLocalContext().getFilters(), FilterDTO.PROPERTY_FILTER_TYPE_ID, filterTypeId);
157         if (oldFilter != null) {
158             getModel().getLocalContext().removeFilters(oldFilter);
159             getModel().setModify(true);
160         }
161 
162         // add new filter
163         if (filter != null) {
164             getModel().getLocalContext().addFilters(filter);
165             getModel().setModify(true);
166         }
167     }
168 
169     /**
170      * Caracteristique Controle Editor.
171      *
172      * @param decoratorName Decorator name
173      * @return Editor
174      */
175     private FilterComboCellEditor getFilterComboCellEditor(final String decoratorName) {
176 
177         // Combobox
178         final ExtendedComboBox<FilterDTO> comboBox = new ExtendedComboBox<>(ui);
179         comboBox.setFilterable(true);
180         comboBox.setShowReset(false);
181         comboBox.setShowDecorator(false);
182         comboBox.setBeanType(FilterDTO.class);
183         comboBox.setAutoFocus(false);
184 
185         // Decorator
186         final Decorator<FilterDTO> decorator = getContext().getDecoratorService().getDecoratorByType(
187                 FilterDTO.class, decoratorName);
188 
189         // add listener to filter combo change
190         comboBox.getCombobox().addItemListener(event -> {
191             if (event.getStateChange() == ItemEvent.SELECTED) {
192                 FilterDTO filter = null;
193                 if (event.getItem() instanceof FilterDTO) {
194                     filter = (FilterDTO) event.getItem();
195                 }
196                 // updates element list
197                 getUI().getParentContainer(ManageContextsUI.class).getHandler().loadFilterElements(filter);
198 
199             } else if (event.getStateChange() == ItemEvent.DESELECTED) {
200 
201                 getUI().getParentContainer(ManageContextsUI.class).getHandler().loadFilterElements(null);
202             }
203         });
204 
205         // Editor
206         return new FilterComboCellEditor(getModel(), comboBox, decorator, getContext().getContextService());
207     }
208 
209     private void initListeners() {
210 
211         getModel().addPropertyChangeListener(ManageFiltersListTableUIModel.PROPERTY_SINGLE_ROW_SELECTED, evt -> {
212 
213             if (getTable().getSelectedRow() > -1) {
214                 final ManageFiltersListTableUIRowModel rowModel = getModel().getSingleSelectedRow();
215                 getUI().getParentContainer(ManageContextsUI.class).getHandler().loadFilterElements(rowModel.getFilter());
216             }
217         });
218 
219     }
220 
221     /** {@inheritDoc} */
222     @Override
223     public AbstractDaliTableModel<ManageFiltersListTableUIRowModel> getTableModel() {
224         return (ManageFiltersListTableUITableModel) getTable().getModel();
225     }
226 
227     /** {@inheritDoc} */
228     @Override
229     public SwingTable getTable() {
230         return ui.getManageFiltersListTable();
231     }
232 
233     /**
234      * <p>disable.</p>
235      */
236     public void disable() {
237         getTable().setEnabled(false);
238     }
239 
240     /**
241      * <p>clearTable.</p>
242      */
243     public void clearTable() {
244         loadContext(null);
245     }
246 
247     /**
248      * <p>loadContext.</p>
249      *
250      * @param context a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
251      */
252     public void loadContext(final ContextDTO context) {
253         // Load context
254         getModel().setLocalContext(context);
255 
256         if (context != null) {
257             getTable().setEnabled(true);
258             List<ManageFiltersListTableUIRowModel> rows = new ArrayList<>(FilterTypeValues.values().length);
259 
260             for (FilterTypeValues contextFilter : FilterTypeValues.values()) {
261                 ManageFiltersListTableUIRowModel row = getTableModel().createNewRow();
262                 row.setValid(true);
263                 row.setType(contextFilter.getLabel());
264                 row.setFilterTypeId(contextFilter.getFilterTypeId());
265 
266                 // find a filter in context
267                 if (CollectionUtils.isNotEmpty(context.getFilters())) {
268                     row.setFilter(DaliBeans.findByProperty(context.getFilters(), FilterDTO.PROPERTY_FILTER_TYPE_ID, contextFilter.getFilterTypeId()));
269                 }
270 
271                 rows.add(row);
272             }
273             getModel().setRows(rows);
274         } else {
275             getModel().setRows(null);
276         }
277     }
278 
279 }