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 who create a new row if allowed 33 * <p/> 34 * Created by Ludovic on 04/06/2015. 35 */ 36 public class NextRowSelectionAction extends AbstractCellSelectionAction { 37 38 /** 39 * <p>Constructor for NextRowSelectionAction.</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 NextRowSelectionAction(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 58 // next row 59 currentRow++; 60 61 if (currentRow >= getRowCount()) { 62 63 if (isCreateNewRow()) { 64 65 // create a new row in model 66 addNewRow(); 67 canSelect = true; 68 69 } else { 70 canSelect = false; 71 } 72 } 73 74 // test if cell is editable 75 if (isTableEditing() && isCellEditable(currentRow, currentColumn)) { 76 canSelect = true; 77 } 78 79 } while (!canSelect && currentRow < getRowCount()); 80 81 if (canSelect) { 82 if (isTableEditing()) { 83 editCell(currentRow, currentColumn); 84 } else { 85 selectCell(currentRow, currentColumn); 86 } 87 } 88 } 89 }