View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.rule.program;
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.programStrategy.ProgramDTO;
28  import fr.ifremer.dali.dto.enums.FilterTypeValues;
29  import fr.ifremer.dali.ui.swing.content.manage.filter.select.SelectFilterUI;
30  import fr.ifremer.dali.ui.swing.util.AbstractDaliBeanUIModel;
31  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
32  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
33  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
34  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
35  import org.jdesktop.swingx.table.TableColumnExt;
36  
37  import java.awt.Dimension;
38  import java.util.Collection;
39  import java.util.List;
40  import java.util.Set;
41  import java.util.stream.Collectors;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Controller pour le tableau des programmes
47   */
48  public class ControlProgramTableUIHandler extends
49          AbstractDaliTableUIHandler<ControlProgramTableRowModel, ControlProgramTableUIModel, ControlProgramTableUI> {
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public void beforeInit(final ControlProgramTableUI ui) {
56          super.beforeInit(ui);
57  
58          // create model and register to the JAXX context
59          final ControlProgramTableUIModel model = new ControlProgramTableUIModel();
60          ui.setContextValue(model);
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public void afterInit(final ControlProgramTableUI ui) {
68  
69          initUI(ui);
70  
71          // Disable buttons
72          getUI().getAddProgramButton().setEnabled(false);
73          getUI().getRemoveProgramButton().setEnabled(false);
74  
75          // Initialisation du tableau
76          initTable();
77  
78      }
79  
80      /**
81       * Initialisation du tableau.
82       */
83      private void initTable() {
84  
85          // Le tableau
86          final SwingTable table = getTable();
87  
88          // Code
89          TableColumnExt codeCol = addColumn(ControlProgramTableModel.CODE);
90          codeCol.setSortable(true);
91          codeCol.setEditable(false);
92  
93          // Libelle
94          TableColumnExt nameCol = addColumn(ControlProgramTableModel.NAME);
95          nameCol.setSortable(true);
96          nameCol.setEditable(false);
97  
98          // Description
99          TableColumnExt descriptionCol = addColumn(ControlProgramTableModel.COMMENT);
100         descriptionCol.setSortable(true);
101         descriptionCol.setEditable(false);
102 
103         ControlProgramTableModel tableModel = new ControlProgramTableModel(getTable().getColumnModel());
104         table.setModel(tableModel);
105 
106         // Initialisation du tableau
107         initTable(table);
108 
109         // Number rows visible
110         table.setVisibleRowCount(3);
111     }
112 
113     /**
114      * <p>loadProgrammesControle.</p>
115      *
116      * @param programs a {@link Collection} object.
117      * @param readOnly read only ?
118      */
119     public void loadPrograms(final Collection<ProgramDTO> programs, boolean readOnly) {
120 
121         // Load table
122         getModel().setBeans(programs);
123         getTable().setEditable(!readOnly);
124 
125         // Activate add button
126         getUI().getAddProgramButton().setEnabled(!readOnly);
127 
128         recomputeRowsValidState(false);
129     }
130 
131     /**
132      * <p>supprimerProgrammesControle.</p>
133      */
134     public void clearTable() {
135 
136         getModel().setBeans(null);
137 
138         getUI().getAddProgramButton().setEnabled(false);
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public AbstractDaliTableModel<ControlProgramTableRowModel> getTableModel() {
146         return (ControlProgramTableModel) getTable().getModel();
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public SwingTable getTable() {
154         return ui.getControlProgramTable();
155     }
156 
157     /**
158      * <p>openAddDialog.</p>
159      */
160     public void openAddDialog() {
161 
162         SelectFilterUI dialog = new SelectFilterUI(getContext(), FilterTypeValues.PROGRAM.getFilterTypeId());
163         dialog.setTitle(t("dali.filter.program.addDialog.title"));
164         List<ProgramDTO> programs = getModel().getBeans().stream()
165                 .sorted(getDecorator(ProgramDTO.class, null).getCurrentComparator())
166                 // set existing referential as read only to 'fix' these beans on double list
167                 .peek(programDTO -> programDTO.setReadOnly(true))
168                 .collect(Collectors.toList());
169         dialog.getModel().setSelectedElements(programs);
170 
171         openDialog(dialog, new Dimension(1024, 720));
172 
173         if (dialog.getModel().isValid()) {
174 
175             List<ProgramDTO> programsToAdd = dialog.getModel().getSelectedElements().stream()
176                     .map(element -> ((ProgramDTO) element))
177                     .filter(program -> !getModel().getBeans().contains(program))
178                     .collect(Collectors.toList());
179 
180             if (!programsToAdd.isEmpty()) {
181 
182                 Set<String> managedProgramCodes = getContext().getProgramStrategyService().getManagedProgramCodesByQuserId(getContext().getDataContext().getPrincipalUserId());
183                 List<ProgramDTO> rejectedPrograms = programsToAdd.stream()
184                         .filter(programToAdd -> !managedProgramCodes.contains(programToAdd.getCode()))
185                         .collect(Collectors.toList());
186                 if (!rejectedPrograms.isEmpty()) {
187 
188                     // message
189                     getContext().getDialogHelper().showWarningDialog(
190                             t("dali.rule.program.add.invalid.message"),
191                             ApplicationUIUtil.getHtmlString(DaliBeans.transformCollection(rejectedPrograms, this::decorate)),
192                             null,
193                             t("dali.rule.program.add.invalid.title")
194                     );
195 
196                     programsToAdd.removeAll(rejectedPrograms);
197                 }
198 
199                 getModel().addBeans(programsToAdd);
200                 getModel().setModify(!programsToAdd.isEmpty());
201 
202                 saveToParentModel();
203             }
204         }
205     }
206 
207     /**
208      * <p>removePrograms.</p>
209      */
210     public void removePrograms() {
211         getModel().deleteSelectedRows();
212         saveToParentModel();
213     }
214 
215     private void saveToParentModel() {
216         getModel().getParentModel().getRuleListUIModel().getSingleSelectedRow().setPrograms(getModel().getBeans());
217         recomputeRowsValidState(false);
218 
219         getModel().firePropertyChanged(AbstractDaliBeanUIModel.PROPERTY_MODIFY, null, true);
220     }
221 
222 }