View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.table;
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 org.jdesktop.swingx.table.DefaultTableColumnModelExt;
28  
29  import javax.swing.table.TableColumn;
30  
31  /**
32   * The default table column model used for application
33   * <p/>
34   * The CheckTableColumn is not draggable
35   * <p/>
36   * Created by Ludovic on 13/05/2015.
37   */
38  public class SwingTableColumnModel extends DefaultTableColumnModelExt {
39  
40      boolean hasCheckTableColumn;
41  
42      /** {@inheritDoc} */
43      @Override
44      public void moveColumn(int columnIndex, int newIndex) {
45  
46          // Prevent the CheckTableColumn to be moved
47          if (getColumn(columnIndex) instanceof CheckTableColumn) {
48              return;
49          }
50  
51          // Prevent other column to be move before CheckTableColumn (if exists)
52          if (newIndex == CheckTableColumn.FIXED_VIEW_POSITION && hasCheckTableColumn) {
53              return;
54          }
55  
56          super.moveColumn(columnIndex, newIndex);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public void addColumn(TableColumn aColumn) {
62          super.addColumn(aColumn);
63  
64          // move CheckTableColumn to the first position
65          if (aColumn instanceof CheckTableColumn) {
66              super.moveColumn(aColumn.getModelIndex(), CheckTableColumn.FIXED_VIEW_POSITION);
67              hasCheckTableColumn = true;
68          }
69      }
70  }