View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI;
28  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit.SpeciesBatchRowModel;
29  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit.SpeciesBatchTableModel;
30  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyCellComponent;
31  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyUI;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyUIModel;
33  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
34  import java.util.Map;
35  import jaxx.runtime.swing.CardLayout2Ext;
36  import jaxx.runtime.validator.swing.SwingValidator;
37  import org.jdesktop.swingx.JXTable;
38  
39  import javax.swing.JComponent;
40  import javax.swing.JPanel;
41  import javax.swing.table.TableColumn;
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  import static org.nuiton.i18n.I18n.n;
46  import static org.nuiton.i18n.I18n.t;
47  
48  /**
49   * Created on 18/03/16.
50   *
51   * @author Tony Chemit - chemit@codelutin.com
52   */
53  public class EditSpeciesBatchPanelUIHandler extends AbstractTuttiUIHandler<EditSpeciesBatchPanelUIModel, EditSpeciesBatchPanelUI> {
54  
55          /** Logger. */
56      private static final Log log = LogFactory.getLog(EditSpeciesBatchPanelUIHandler.class);
57  
58      protected SpeciesOrBenthosBatchUISupport speciesOrBenthosBatchUISupport;
59  
60      @Override
61      public void onCloseUI() {
62  
63          closeUI(ui.getEditBatchesUI());
64          closeUI(ui.getEditFrequenciesUI());
65  
66      }
67  
68      @Override
69      public void beforeInit(EditSpeciesBatchPanelUI ui) {
70          super.beforeInit(ui);
71  
72          speciesOrBenthosBatchUISupport = ui.getContextValue(SpeciesOrBenthosBatchUISupport.class, ui.getSpeciesOrBenthosContext());
73  
74          EditSpeciesBatchPanelUIModel model = new EditSpeciesBatchPanelUIModel(speciesOrBenthosBatchUISupport);
75          ui.setContextValue(model);
76  
77      }
78  
79      @Override
80      public void afterInit(EditSpeciesBatchPanelUI editSpeciesBatchPanelUI) {
81  
82          ui.getSplitBatchUI().getModel().setSplitMode(true);
83          ui.getAddSampleCategoryBatch().getModel().setSplitMode(false);
84  
85  
86      }
87  
88      @Override
89      public SwingValidator<EditSpeciesBatchPanelUIModel> getValidator() {
90          return null;
91      }
92  
93      @Override
94      protected JComponent getComponentToFocus() {
95          return null;
96      }
97  
98      public void editSpeciesFrequencies(int rowIndex) {
99          JXTable speciesTable = ui.getEditBatchesUI().getTable();
100 
101         TableColumn frequenciesColumn = speciesTable.getColumn(SpeciesBatchTableModel.COMPUTED_NUMBER);
102         int frequenciesColumnIndex = speciesTable.getColumns(false).indexOf(frequenciesColumn);
103 
104         SpeciesFrequencyCellComponent.FrequencyCellEditor frequencyCellEditor =
105                 (SpeciesFrequencyCellComponent.FrequencyCellEditor) speciesTable.getCellEditor(rowIndex, frequenciesColumnIndex);
106 
107         frequencyCellEditor.initEditor(speciesTable, rowIndex, frequenciesColumnIndex);
108         frequencyCellEditor.startEdit();
109     }
110 
111     public void editSpeciesFrequencies(SpeciesFrequencyCellComponent.FrequencyCellEditor editor) {
112 
113         SpeciesFrequencyUI frequencyEditor = ui.getEditFrequenciesUI();
114 
115         SpeciesBatchRowModel editRow = editor.getEditRow();
116 
117         String editBatchTitle = ui.getEditBatchesUIPanel().getTitle();
118 
119         String frequenciesTitle = buildReminderLabelTitle(editRow.getSpecies(),
120                                                           editRow,
121                                                           editBatchTitle,
122                                                           "",
123                                                           false);
124         frequencyEditor.getHandler().editBatch(editor, frequenciesTitle);
125 
126         SpeciesFrequencyUIModel model = frequencyEditor.getModel();
127         try {
128             int fishingOperationId = model.getFishingOperation().getIdAsInt();
129             int taxonId = model.getBatch().getSpecies().getReferenceTaxonId();
130 
131             Map map = getPersistenceService().getPrevOperationNameAndBatchId(
132                     fishingOperationId, taxonId);
133             model.setCanGoPrevBatch(map != null);
134 
135             map = getPersistenceService().getNextOperationNameAndBatchId(
136                     fishingOperationId, taxonId);
137             model.setCanGoNextBatch(map != null);
138         } catch (Exception eee) {
139             log.warn("Can't check navigation between batch", eee);
140             model.setCanGoPrevBatch(false);
141             model.setCanGoNextBatch(false);
142         }
143 
144         // open frequency editor
145         ui.switchToEditFrequency();
146 
147         // update title
148         String title = buildReminderLabelTitle(editRow.getSpecies(),
149                                                editRow,
150                                                editBatchTitle,
151                                                t("tutti.editSpeciesFrequencies.title"));
152         ui.getEditFrequenciesUIPanel().setTitle(title);
153     }
154 
155     protected void setSpeciesSelectedCard(String card) {
156 
157         CardLayout2Ext layout = (CardLayout2Ext) ui.getLayout();
158 
159         if (!card.equals(layout.getSelected())) {
160 
161             layout.setSelected(card);
162 
163             EditCatchesUI parentContainer = getParentContainer(EditCatchesUI.class);
164             JPanel actionPanel = parentContainer.getCreateFishingOperationActions();
165 
166             actionPanel.setVisible(false);
167 
168             SwingValidator<?> validator = null;
169 
170             switch (card) {
171 
172                 case EditSpeciesBatchPanelUI.EDIT_BATCH_CARD:
173                     actionPanel.setVisible(true);
174                     break;
175 
176                 case EditSpeciesBatchPanelUI.CREATE_BATCH_CARD:
177                     validator = ui.getCreateBatchUI().getValidator();
178                     String title = n("tutti.createSpeciesBatch.title");
179                     ui.getCreateBatchUIPanel().setTitle(ui.getEditBatchesUIPanel().getTitle() + " - " + t(title));
180 
181                     break;
182 
183                 case EditSpeciesBatchPanelUI.SPLIT_BATCH_CARD:
184                     validator = ui.getSplitBatchUI().getValidator();
185                     break;
186 
187                 case EditSpeciesBatchPanelUI.ADD_SAMPLE_CATEGORY_BATCH_CARD:
188                     validator = ui.getAddSampleCategoryBatch().getValidator();
189                     break;
190 
191                 case EditSpeciesBatchPanelUI.EDIT_FREQUENCY_CARD:
192 
193                     validator = ui.getEditFrequenciesUI().getValidator();
194                     break;
195 
196             }
197 
198             if (validator != null) {
199                 registerValidators(validator);
200             }
201 
202         }
203 
204     }
205 }