View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.caracteristics.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 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 com.google.common.base.Preconditions;
28  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
29  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
30  import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
31  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapEditorRowModel;
32  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapEditorTableModel;
33  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapEditorUI;
34  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapEditorUIModel;
35  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * Created on 3/7/15.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.15
44   */
45  public class CaracteristicEditorRemoveRowAction extends SimpleActionSupport<CaracteristicMapEditorUI> {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(CaracteristicEditorRemoveRowAction.class);
49  
50      private static final long serialVersionUID = 1L;
51  
52      public CaracteristicEditorRemoveRowAction(CaracteristicMapEditorUI ui) {
53          super(ui);
54      }
55  
56      @Override
57      protected void onActionPerformed(CaracteristicMapEditorUI ui) {
58  
59          int rowIndex = ui.getCaracteristicMapEditorTable().getSelectedRow();
60  
61          Preconditions.checkState(
62                  rowIndex != -1,
63                  "Cant remove caracteristic if no caracteristic selected");
64          CaracteristicMapEditorTableModel tableModel = ui.getHandler().getTableModel();
65          CaracteristicMapEditorRowModel row = tableModel.getEntry(rowIndex);
66  
67          if (log.isInfoEnabled()) {
68              log.info("Remove caracteristic row: " + row);
69          }
70  
71          CaracteristicMapEditorUIModel model = ui.getModel();
72          CaracteristicMap caracteristicMap = model.getCaracteristicMap();
73          if (caracteristicMap != null) {
74              caracteristicMap.remove(row.getKey());
75          }
76  
77          //add the row in the combo
78          BeanFilterableComboBox<Caracteristic> keyCombo = ui.getNewRowKey();
79          keyCombo.addItem(row.getKey());
80          keyCombo.reset();
81  
82          // remove the row from the model
83          model.getRows().remove(rowIndex);
84  
85          // refresh all the table
86          tableModel.fireTableRowsDeleted(rowIndex, rowIndex);
87  
88          model.removeRowInError(row);
89  
90      }
91  
92  }