View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.table.action;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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  
27  import fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableUIHandler;
28  
29  import java.awt.event.ActionEvent;
30  
31  /**
32   * An implementation of a AbstractCellSelectionAction that creates a new row if allowed
33   * <p/>
34   * Created by Ludovic on 04/06/2015.
35   */
36  public class NextCellSelectionAction extends AbstractCellSelectionAction {
37  
38      /**
39       * <p>Constructor for NextCellSelectionAction.</p>
40       *
41       * @param name a {@link String} object.
42       * @param handler a {@link fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableUIHandler} object.
43       */
44      public NextCellSelectionAction(String name, AbstractTableUIHandler handler) {
45          super(name, handler);
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public void actionPerformed(ActionEvent e) {
51  
52          int currentRow = getSelectedRow();
53          int currentColumn = getSelectedColumn();
54  
55          boolean canSelect = !isTableEditing();
56          do {
57              // go to next cell
58              currentColumn = getNextColumn(currentColumn);
59  
60              if (currentColumn >= getColumnCount()) {
61  
62                  // no more cell, so will move to next editable column on next row
63                  currentColumn = getNextColumn(-1);
64                  currentRow++;
65  
66                  if (currentRow == getRowCount()) {
67  
68                      if (isCreateNewRow()) {
69  
70                          // create a new row in model
71                          addNewRow();
72                          canSelect = true;
73                      } else {
74                          canSelect = false;
75                      }
76                  }
77              }
78  
79              // test if cell is editable
80              if (isTableEditing() && isCellEditable(currentRow, currentColumn)) {
81                  canSelect = true;
82              }
83  
84          } while (!canSelect && currentRow < getRowCount());
85  
86          if (canSelect) {
87              if (isTableEditing()) {
88                  editCell(currentRow, currentColumn);
89              } else {
90                  selectCell(currentRow, currentColumn);
91              }
92          } else {
93              selectNextComponent();
94          }
95      }
96  
97  }