View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: UI Swing Common
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.quadrige3.core.dao.technical.Assert;
25  import fr.ifremer.quadrige3.ui.swing.table.AbstractTableUIHandler;
26  import fr.ifremer.quadrige3.ui.swing.table.RowNumberColumn;
27  import org.jdesktop.swingx.action.AbstractActionExt;
28  import org.nuiton.i18n.I18n;
29  
30  import java.awt.event.ActionEvent;
31  
32  /**
33   * @author peck7 on 23/04/2019.
34   */
35  public class RowNumberColumnAction extends AbstractActionExt {
36  
37      private final AbstractTableUIHandler tableUIHandler;
38      private RowNumberColumn rowNumberColumn;
39  
40      public RowNumberColumnAction(AbstractTableUIHandler tableUIHandler) {
41          super(I18n.t("quadrige3.table.rowNumberColumn"));
42          this.tableUIHandler = tableUIHandler;
43          setSelected(tableUIHandler.hasRowNumberColumn());
44          setStateAction();
45      }
46  
47      @Override
48      public void actionPerformed(ActionEvent e) {
49          if (isSelected()) {
50              addRowNumbers();
51          } else {
52              removeRowNumbers();
53          }
54      }
55  
56      /**
57       * Add the row numbers as row headers
58       */
59      private void addRowNumbers() {
60          if (!tableUIHandler.hasRowNumberColumn())
61              rowNumberColumn = (RowNumberColumn) tableUIHandler.addFixedColumn(RowNumberColumn.class);
62      }
63  
64      private void removeRowNumbers() {
65          Assert.notNull(rowNumberColumn);
66          tableUIHandler.removeFixedColumn(rowNumberColumn);
67      }
68  
69  }