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 * Just an implementation of a AbstractCellSelectionAction
33 * <p/>
34 * Created by Ludovic on 04/06/2015.
35 */
36 public class PreviousCellSelectionAction extends AbstractCellSelectionAction {
37
38 /**
39 * <p>Constructor for PreviousCellSelectionAction.</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 PreviousCellSelectionAction(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 previous cell
58 currentColumn = getPreviousColumn(currentColumn);
59
60 if (currentColumn < 0) {
61
62 // no more cell, so will move to last editable column on previous row
63 currentColumn = getPreviousColumn(getColumnCount());
64 currentRow--;
65 }
66
67 if (currentRow < 0) {
68 canSelect = false;
69 }
70
71 // test if cell is editable
72 if (isTableEditing() && isCellEditable(currentRow, currentColumn)) {
73 canSelect = true;
74 }
75
76 } while (!canSelect && currentRow >= 0);
77
78 if (canSelect) {
79 if (isTableEditing()) {
80 editCell(currentRow, currentColumn);
81 } else {
82 selectCell(currentRow, currentColumn);
83 }
84 } else {
85 selectPreviousComponent();
86 }
87 }
88 }