View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.filtercontent;
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.decorator.DecoratorService;
27  import fr.ifremer.quadrige3.ui.core.dto.QuadrigeBean;
28  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
29  import fr.ifremer.dali.dto.referential.TaxonDTO;
30  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
31  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
32  import fr.ifremer.quadrige3.core.dao.technical.decorator.Decorator;
33  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
34  import org.jdesktop.swingx.table.TableColumnExt;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * <p>ManageFilterContentTableUIHandler class.</p>
41   *
42   */
43  public class ManageFilterContentTableUIHandler extends
44          AbstractDaliTableUIHandler<ManageFilterContentTableUIRowModel, ManageFilterContentTableUIModel, ManageFilterContentTableUI> {
45  
46      /** {@inheritDoc} */
47      @Override
48      public void beforeInit(final ManageFilterContentTableUI ui) {
49          super.beforeInit(ui);
50  
51          // create model and register to the JAXX context
52          final ManageFilterContentTableUIModel model = new ManageFilterContentTableUIModel();
53          ui.setContextValue(model);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public void afterInit(final ManageFilterContentTableUI ui) {
59  
60          // Initialize the screen
61          initUI(ui);
62  
63          // Initialisatize the table
64          initializeTable();
65  
66      }
67  
68      /**
69       * Initialisation du tableau.
70       */
71      private void initializeTable() {
72  
73          // the table
74          final SwingTable table = getTable();
75  
76          // label
77          TableColumnExt labelCol = addColumn(ManageFilterContentTableUITableModel.LABEL);
78          labelCol.setSortable(true);
79          labelCol.setEditable(false);
80  
81          ManageFilterContentTableUITableModel tableModel = new ManageFilterContentTableUITableModel(getTable().getColumnModel());
82          table.setModel(tableModel);
83  
84          // Initialisation du tableau
85          initTable(table, true);
86  
87          table.setVisibleRowCount(5);
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public AbstractDaliTableModel<ManageFilterContentTableUIRowModel> getTableModel() {
93          return (ManageFilterContentTableUITableModel) getTable().getModel();
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public SwingTable getTable() {
99          return ui.getManageFilterContentTable();
100     }
101 
102     /**
103      * <p>loadFilterElements.</p>
104      *
105      * @param filterId a {@link java.lang.Integer} object.
106      */
107     public void loadFilterElements(final Integer filterId) {
108         FilterDTO filter = getContext().getContextService().getFilter(filterId);
109 
110         if (filter != null) {
111             getUI().setEnabled(true);
112             loadFilterContent(filter);
113         }
114     }
115 
116     /**
117      * <p>clearTable.</p>
118      */
119     public void clearTable() {
120         loadFilterContent(null);
121     }
122 
123     private void loadFilterContent(FilterDTO filter) {
124 
125         // load filter elements
126         getContext().getContextService().loadFilteredElements(filter);
127 
128         List<String> contentLabels = getFilterContentLabel(filter);
129 
130         List<ManageFilterContentTableUIRowModel> rows = new ArrayList<>(contentLabels.size());
131         for (String label : contentLabels) {
132             ManageFilterContentTableUIRowModel row = getTableModel().createNewRow();
133             row.setValid(true);
134             row.setLabel(label);
135             rows.add(row);
136         }
137         getModel().setRows(rows);
138     }
139 
140     private List<String> getFilterContentLabel(FilterDTO filter) {
141         ArrayList<String> list = new ArrayList<>();
142         if (filter != null && filter.getElements() != null) {
143             for (QuadrigeBean bean : filter.getElements()) {
144                 String context = null;
145                 if (bean instanceof TaxonDTO) {
146                     context = DecoratorService.WITH_CITATION_AND_REFERENT;
147                 }
148                 Decorator decorator = getContext().getDecoratorService().getDecorator(bean, context);
149                 list.add(decorator != null ? decorator.toString(bean) : bean.toString());
150             }
151         }
152         return list;
153     }
154 
155 }