View Javadoc
1   
2   package fr.ifremer.tutti.ui.swing.content.operation.fishing;
3   
4   /*
5    * #%L
6    * Tutti :: UI
7    * %%
8    * Copyright (C) 2012 - 2014 Ifremer
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
24   */
25  
26  import com.google.common.base.Preconditions;
27  import com.google.common.collect.Lists;
28  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
29  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
30  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
31  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
32  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
33  import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor;
34  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
35  import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIHandler;
36  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
37  import jaxx.runtime.validator.swing.SwingValidator;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import javax.swing.JComponent;
42  import java.util.List;
43  
44  /**
45   * @author Kevin Morin - kmorin@codelutin.com
46   * @since 1.0
47   */
48  public abstract class AbstractCaracteristicTabUIHandler
49          <RM extends AbstractCaracteristicRowModel<RM>,
50                  M extends AbstractCaracteristicTabUIModel<RM, M>,
51                  TM extends AbstractApplicationTableModel<RM>,
52                  UI extends TuttiUI<M, ?>>
53          extends AbstractTuttiTableUIHandler<RM, M, UI> {
54  
55      private static final Log log =
56              LogFactory.getLog(AbstractCaracteristicTabUIHandler.class);
57  
58      public AbstractCaracteristicTabUIHandler(String... properties) {
59          super(properties);
60      }
61  
62      //------------------------------------------------------------------------//
63      //-- AbstractTuttiTableUIHandler methods                                --//
64      //------------------------------------------------------------------------//
65  
66      @Override
67      public TM getTableModel() {
68          return (TM) getTable().getModel();
69      }
70  
71      @Override
72      protected boolean isRowValid(RM row) {
73          return row.getValue() != null;
74      }
75  
76      @Override
77      protected void onRowModified(int rowIndex, RM row, String propertyName, Object oldValue, Object newValue) {
78          recomputeRowValidState(row);
79          super.onRowModified(rowIndex, row, propertyName, oldValue, newValue);
80          saveSelectedRowIfNeeded();
81      }
82  
83      @Override
84      protected void saveSelectedRowIfRequired(TuttiBeanMonitor<RM> rowMonitor, RM row) {
85          if (row.isValid()) {
86              // there is a valid bean attached to the monitor
87  
88              if (rowMonitor.wasModified()) {
89  
90                  // monitored bean was modified, save it
91                  if (log.isInfoEnabled()) {
92                      log.info("Row " + row + " was modified, will save it");
93                  }
94  
95                  saveRow(row);
96  
97              }
98          }
99      }
100 
101     //------------------------------------------------------------------------//
102     //-- AbstractTuttiUIHandler methods                                     --//
103     //------------------------------------------------------------------------//
104 
105     @Override
106     public SwingValidator<M> getValidator() {
107         return null;
108     }
109 
110     @Override
111     protected void onAfterSelectedRowChanged(int oldRowIndex,
112                                              RM oldRow,
113                                              int newRowIndex,
114                                              RM newRow) {
115 
116         super.onAfterSelectedRowChanged(oldRowIndex, oldRow, newRowIndex, newRow);
117         getModel().setRemoveCaracteristicEnabled(newRow != null);
118     }
119 
120     @Override
121     public void beforeInit(UI ui) {
122         super.beforeInit(ui);
123         M model = createModel();
124         ui.setContextValue(model);
125     }
126 
127     @Override
128     public void afterInit(UI ui) {
129         initUI(ui);
130 
131         initBeanFilterableComboBox(getKeyCombo(), Lists.<Caracteristic>newArrayList(), null);
132 
133     }
134 
135     @Override
136     protected JComponent getComponentToFocus() {
137         return getKeyCombo();
138     }
139 
140     @Override
141     public void onCloseUI() {
142         if (log.isDebugEnabled()) {
143             log.debug("closing: " + getUI());
144         }
145     }
146 
147     //------------------------------------------------------------------------//
148     //-- Protected methods                                                  --//
149     //------------------------------------------------------------------------//
150 
151     protected void saveRow(RM row) {
152 
153         if (row.isValid()) {
154             CaracteristicMap caracteristics = getModel().getCaracteristicMap();
155             Preconditions.checkNotNull(caracteristics);
156 
157             caracteristics.put(row.getKey(), row.getValue());
158         }
159     }
160 
161     protected abstract M createModel();
162 
163     protected abstract BeanFilterableComboBox<Caracteristic> getKeyCombo();
164 
165     protected abstract CaracteristicMap getCaracteristics(FishingOperation operation);
166 
167     protected abstract List<String> getProtocolPmfmIds();
168 
169     //------------------------------------------------------------------------//
170     //-- Public methods                                                     --//
171     //------------------------------------------------------------------------//
172 
173     /** Adds a row with the parameter selected in the combo box */
174     public void addRow() {
175         BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
176         Caracteristic selectedItem = (Caracteristic) keyCombo.getSelectedItem();
177         TM tableModel = getTableModel();
178 
179         RM row = tableModel.createNewRow();
180         row.setKey(selectedItem);
181 //        tableModel.addNewRow(row);
182         getModel().getRows().add(row);
183 
184         int rowIndex = tableModel.getRowCount() - 1;
185         tableModel.fireTableRowsInserted(rowIndex, rowIndex);
186 
187         keyCombo.getHandler().removeItem(selectedItem);
188 
189         M model = getModel();
190         model.setModify(true);
191         recomputeRowValidState(row);
192     }
193 
194     /** Resets the table with the data from the database */
195     public void reset(FishingOperation fishingOperation) {
196         M model = getModel();
197 
198         CaracteristicMap caracteristicMap = getCaracteristics(fishingOperation);
199         if (caracteristicMap == null) {
200             caracteristicMap = new CaracteristicMap();
201         }
202         model.setCaracteristicMap(caracteristicMap);
203 
204         updateRows();
205         model.setModify(false);
206     }
207 
208     /** Resets the table with the data from the database */
209     public void mergeCaracteristics(FishingOperation fishingOperation) {
210         CaracteristicMap caracteristicMap = getCaracteristics(fishingOperation);
211         if (caracteristicMap != null) {
212             CaracteristicMap thisCaracteristicMap = getModel().getCaracteristicMap();
213             for (Caracteristic caracteristic : caracteristicMap.keySet()) {
214                 thisCaracteristicMap.put(caracteristic, caracteristicMap.get(caracteristic));
215             }
216             getModel().setModify(true);
217 
218             updateRows();
219         }
220     }
221 
222     protected void updateRows() {
223         TM tableModel = getTableModel();
224         M model = getModel();
225         List<String> pmfmIds = Lists.newArrayList();
226 
227         if (getDataContext().isProtocolFilled()) {
228             List<String> protocolPmfmId = getProtocolPmfmIds();
229             if (protocolPmfmId != null) {
230                 pmfmIds.addAll(protocolPmfmId);
231             }
232         }
233 
234         CaracteristicMap caracteristicMap = model.getCaracteristicMap();
235         List<RM> rows = Lists.newArrayList();
236         List<Caracteristic> caracteristics = Lists.newArrayList(caracteristicMap.keySet());
237 
238         List<Caracteristic> availableCaracteristics = model.getAvailableCaracteristics();
239         for (String id : pmfmIds) {
240             Caracteristic caracteristic = TuttiEntities.findById(availableCaracteristics, id);
241             if (!caracteristics.contains(caracteristic)) {
242                 caracteristics.add(caracteristic);
243             }
244         }
245 
246         for (Caracteristic key : caracteristics) {
247             RM newRow = tableModel.createNewRow();
248             newRow.setKey(key);
249             newRow.setValue(caracteristicMap.get(key));
250             rows.add(newRow);
251         }
252 
253         model.setRows(rows);
254 
255         List<Caracteristic> caracteristicList = Lists.newArrayList();
256 
257         for (Caracteristic caracteristic : availableCaracteristics) {
258             if (!caracteristics.contains(caracteristic)) {
259                 caracteristicList.add(caracteristic);
260             }
261         }
262 
263         BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
264         keyCombo.setData(caracteristicList);
265 //        selectFirstInCombo(keyCombo);
266         keyCombo.getHandler().reset();
267     }
268 
269     public void removeCaracteristic() {
270         int rowIndex = getTable().getSelectedRow();
271 
272         Preconditions.checkState(
273                 rowIndex != -1,
274                 "Cant remove caracteristic if no caracteristic selected");
275 
276         RM row = getTableModel().getEntry(rowIndex);
277 
278         CaracteristicMap caracteristicMap = getModel().getCaracteristicMap();
279         if (caracteristicMap != null) {
280             caracteristicMap.remove(row.getKey());
281         }
282 
283         //add the row in the combo
284         BeanFilterableComboBox keyCombo = getKeyCombo();
285         keyCombo.addItem(row.getKey());
286 //        selectFirstInCombo(keyCombo);
287         keyCombo.getHandler().reset();
288 
289         // remove the row from the model
290         getModel().getRows().remove(rowIndex);
291 
292         // refresh all the table
293         getTableModel().fireTableRowsDeleted(rowIndex, rowIndex);
294 
295         getModel().removeRowInError(row);
296     }
297 }