View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.table.action;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  import fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableModel;
27  import fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableUIHandler;
28  import fr.ifremer.quadrige2.ui.swing.common.table.CheckTableColumn;
29  import org.jdesktop.swingx.JXTable;
30  
31  import javax.swing.AbstractAction;
32  import javax.swing.table.TableCellEditor;
33  import java.awt.Component;
34  
35  /**
36   * Abstract Action performed on a table responsive on key bindings
37   * <p/>
38   * Created by Ludovic on 01/06/2015.
39   */
40  public abstract class AbstractCellSelectionAction extends AbstractAction {
41  
42      private final AbstractTableUIHandler handler;
43  
44      /**
45       * <p>Constructor for AbstractCellSelectionAction.</p>
46       *
47       * @param name a {@link String} object.
48       * @param handler a {@link fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableUIHandler} object.
49       */
50      public AbstractCellSelectionAction(String name, AbstractTableUIHandler handler) {
51          super(name);
52          this.handler = handler;
53      }
54  
55      private AbstractTableModel getTableModel() {
56          return handler.getTableModel();
57      }
58  
59      private JXTable getTable() {
60          return handler.getTable();
61      }
62  
63      /**
64       * <p>isTableEditing.</p>
65       *
66       * @return a boolean.
67       */
68      protected boolean isTableEditing() {
69          return getTable().isEditing();
70      }
71  
72      /**
73       * <p>isCreateNewRow.</p>
74       *
75       * @return a boolean.
76       */
77      protected boolean isCreateNewRow() {
78          return getTableModel().isCreateNewRow();
79      }
80  
81      /**
82       * <p>addNewRow.</p>
83       */
84      protected void addNewRow() {
85          // stop previous cell editing first
86          if (stopActiveEdition()) {
87              // add a new row in model
88              getTableModel().addNewRow();
89          }
90      }
91  
92      /**
93       * <p>getRowCount.</p>
94       *
95       * @return a int.
96       */
97      protected int getRowCount() {
98          return getTable().getRowCount();
99      }
100 
101     /**
102      * <p>getColumnCount.</p>
103      *
104      * @return a int.
105      */
106     protected int getColumnCount() {
107         return getTable().getColumnCount();
108     }
109 
110     /**
111      * <p>getSelectedRow.</p>
112      *
113      * @return a int.
114      */
115     protected int getSelectedRow() {
116         return getTable().getSelectedRow();
117     }
118 
119     /**
120      * <p>getSelectedColumn.</p>
121      *
122      * @return a int.
123      */
124     protected int getSelectedColumn() {
125         return getTable().getSelectedColumn();
126     }
127 
128     /**
129      * <p>getNextColumn.</p>
130      *
131      * @param column a int.
132      * @return a int.
133      */
134     protected int getNextColumn(int column) {
135         int nextColumn = column;
136         do {
137             nextColumn++;
138         } while (nextColumn < getColumnCount() && getTable().getColumn(nextColumn) instanceof CheckTableColumn);
139         return nextColumn;
140     }
141 
142     /**
143      * <p>getPreviousColumn.</p>
144      *
145      * @param column a int.
146      * @return a int.
147      */
148     protected int getPreviousColumn(int column) {
149         int nextColumn = column;
150         do {
151             nextColumn--;
152         } while (nextColumn >= 0 && getTable().getColumn(nextColumn) instanceof CheckTableColumn);
153         return nextColumn;
154     }
155 
156     /**
157      * <p>isCellValid.</p>
158      *
159      * @param row a int.
160      * @param column a int.
161      * @return a boolean.
162      */
163     protected boolean isCellValid(int row, int column) {
164         return row > -1 && column > -1 && row < getRowCount() && column < getColumnCount();
165     }
166 
167     /**
168      * <p>isCellEditable.</p>
169      *
170      * @param row a int.
171      * @param column a int.
172      * @return a boolean.
173      */
174     protected boolean isCellEditable(int row, int column) {
175         return isCellValid(row, column) && getTable().isCellEditable(row, column) && !(getTable().getColumn(column) instanceof CheckTableColumn);
176     }
177 
178     /**
179      * <p>selectCell.</p>
180      *
181      * @param row a int.
182      * @param column a int.
183      * @return a boolean.
184      */
185     protected boolean selectCell(int row, int column) {
186 
187         if (!isCellValid(row, column)) return false;
188 
189         // stop previous cell editing first
190         if (!stopActiveEdition()) {
191             return false;
192         }
193 
194         getTable().setColumnSelectionInterval(column, column);
195         getTable().setRowSelectionInterval(row, row);
196         getTable().scrollCellToVisible(row, column);
197         return true;
198     }
199 
200     /**
201      * <p>editCell.</p>
202      *
203      * @param row a int.
204      * @param column a int.
205      */
206     protected void editCell(int row, int column) {
207         if (selectCell(row, column)) getTable().editCellAt(row, column);
208     }
209 
210     /**
211      * <p>stopActiveEdition.</p>
212      *
213      * @return a boolean.
214      */
215     protected boolean stopActiveEdition() {
216         TableCellEditor editor = getTable().getCellEditor();
217         return editor == null || editor.stopCellEditing();
218     }
219 
220     /**
221      * <p>selectNextComponent.</p>
222      */
223     protected void selectNextComponent() {
224         Component next = handler.getNextComponentToFocus();
225         if (next != null) {
226             next.requestFocus();
227         }
228     }
229 
230     /**
231      * <p>selectPreviousComponent.</p>
232      */
233     protected void selectPreviousComponent() {
234         Component previous = handler.getPreviousComponentToFocus();
235         if (previous != null) {
236             previous.requestFocus();
237         }
238     }
239 
240     public enum Direction {
241         NEXT_CELL, PREVIOUS_CELL, NEXT_ROW, PREVIOUS_ROW
242     }
243 }
244