View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.table;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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  import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
27  import org.jdesktop.swingx.table.TableColumnExt;
28  
29  import javax.swing.table.TableColumn;
30  import java.util.HashSet;
31  import java.util.Objects;
32  import java.util.Set;
33  import java.util.function.Predicate;
34  
35  /**
36   * The default table column model used for application
37   * <p/>
38   * The CheckTableColumn is not draggable
39   * <p/>
40   * Created by Ludovic on 13/05/2015.
41   */
42  public class SwingTableColumnModel extends DefaultTableColumnModelExt {
43  
44      /*
45          Map holding model and view indexes of fixed column
46          key id model index, value is view index
47       */
48      private Set<TableColumn> fixedColumns = new HashSet<>();
49  
50      @Override
51      public TableColumnExt getColumnExt(Object identifier) {
52  
53          TableColumnExt column = super.getColumnExt(identifier);
54  
55          if (column == null && identifier instanceof ColumnIdentifier) {
56              ColumnIdentifier<?> columnIdentifier = (ColumnIdentifier<?>) identifier;
57  
58              // try to
59              column = (TableColumnExt) getColumns(true).stream()
60                  .filter(tableColumn -> tableColumn.getIdentifier() instanceof ColumnIdentifier)
61                  .filter(tableColumn -> Objects.equals(((ColumnIdentifier<?>) tableColumn.getIdentifier()).getPropertyName(), columnIdentifier.getPropertyName()))
62                  .filter(tableColumn -> {
63                      if (identifier instanceof PmfmColumnIdentifier && tableColumn.getIdentifier() instanceof PmfmColumnIdentifier) {
64                          return ((PmfmColumnIdentifier) identifier).getPmfmId() == ((PmfmColumnIdentifier) tableColumn.getIdentifier()).getPmfmId();
65                      }
66                      return false;
67                  })
68                  .findFirst()
69                  .orElse(null);
70          }
71  
72          return column;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public void moveColumn(int columnIndex, int newIndex) {
80  
81          // prevent a fixed column to be moved or a column to be moved before fixed columns
82          if (fixedColumns.contains(getColumn(columnIndex)) || fixedColumns.contains(getColumn(newIndex))) {
83              return;
84          }
85  
86          super.moveColumn(columnIndex, newIndex);
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void addColumn(TableColumn column) {
94          super.addColumn(column);
95  
96          if (column instanceof FixedColumn) {
97              Integer fixedPosition = ((FixedColumn) column).getFixedPosition();
98              if (fixedPosition != null) {
99                  setFixedColumn(column, fixedPosition);
100             }
101         }
102     }
103 
104     @Override
105     public void removeColumn(TableColumn column) {
106         super.removeColumn(column);
107 
108         fixedColumns.remove(column);
109     }
110 
111     private void setFixedColumn(TableColumn column, int fixedViewIndex) {
112 
113         // adjust fixedViewIndex if > to fixed column count
114         fixedViewIndex = Math.min(fixedViewIndex, fixedColumns.size());
115 
116         fixedColumns.add(column);
117 
118         // move column to target view index
119         super.moveColumn(getColumnIndex(column.getIdentifier()), fixedViewIndex);
120     }
121 
122     public boolean isFixedColumn(TableColumn column) {
123         return column != null && fixedColumns.contains(column);
124     }
125 
126     public boolean hasFixedColumn(Predicate<TableColumn> predicate) {
127         return fixedColumns.stream().anyMatch(predicate);
128     }
129 }