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  
27  import org.jdesktop.swingx.JXTable;
28  
29  import javax.swing.ListSelectionModel;
30  import javax.swing.RowFilter;
31  import javax.swing.event.RowSorterEvent;
32  import javax.swing.table.JTableHeader;
33  import javax.swing.table.TableColumnModel;
34  import javax.swing.table.TableModel;
35  import java.util.Vector;
36  
37  /**
38   * A JXTable that create a SwingTableHeader at init time (useful to prevent NPE on specific context. ex: table in dialog)
39   * <p/>
40   * By default, the SwingTableHeader is set by AbstractTableUIHandler#initTable
41   * <p/>
42   * Created by Ludovic on 13/05/2015.
43   */
44  public class SwingTable extends JXTable {
45  
46      private boolean sorting = false;
47      public static String PROPERTY_SORTING = "sorting";
48      public static String PROPERTY_SORTED = "sorted";
49      public static String PROPERTY_ROW_FILTER = "rowFilter";
50  
51      /**
52       * <p>Constructor for SwingTable.</p>
53       */
54      public SwingTable() {
55          super();
56          init();
57      }
58  
59      /**
60       * <p>Constructor for SwingTable.</p>
61       *
62       * @param dm a {@link TableModel} object.
63       */
64      public SwingTable(TableModel dm) {
65          super(dm);
66          init();
67      }
68  
69      /**
70       * <p>Constructor for SwingTable.</p>
71       *
72       * @param dm a {@link TableModel} object.
73       * @param cm a {@link TableColumnModel} object.
74       */
75      public SwingTable(TableModel dm, TableColumnModel cm) {
76          super(dm, cm);
77          init();
78      }
79  
80      /**
81       * <p>Constructor for SwingTable.</p>
82       *
83       * @param dm a {@link TableModel} object.
84       * @param cm a {@link TableColumnModel} object.
85       * @param sm a {@link ListSelectionModel} object.
86       */
87      public SwingTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
88          super(dm, cm, sm);
89          init();
90      }
91  
92      /**
93       * <p>Constructor for SwingTable.</p>
94       *
95       * @param numRows a int.
96       * @param numColumns a int.
97       */
98      public SwingTable(int numRows, int numColumns) {
99          super(numRows, numColumns);
100         init();
101     }
102 
103     /**
104      * <p>Constructor for SwingTable.</p>
105      *
106      * @param rowData a {@link Vector} object.
107      * @param columnNames a {@link Vector} object.
108      */
109     public SwingTable(Vector<?> rowData, Vector<?> columnNames) {
110         super(rowData, columnNames);
111         init();
112     }
113 
114     /**
115      * <p>Constructor for SwingTable.</p>
116      *
117      * @param rowData an array of {@link Object} objects.
118      * @param columnNames an array of {@link Object} objects.
119      */
120     public SwingTable(Object[][] rowData, Object[] columnNames) {
121         super(rowData, columnNames);
122         init();
123     }
124 
125     protected void init() {
126         setAutoCreateColumnsFromModel(false);
127         setAutoCreateRowSorter(false);
128         setRowSorter(null);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     protected JTableHeader createDefaultTableHeader() {
134         return new SwingTableHeader(columnModel);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public SwingTableHeader getTableHeader() {
140         return (SwingTableHeader) super.getTableHeader();
141     }
142 
143     public AbstractTableModel getTableModel() {
144         return (AbstractTableModel) getModel();
145     }
146 
147     @Override
148     public SwingTableColumnModel getColumnModel() {
149         return (SwingTableColumnModel) super.getColumnModel();
150     }
151 
152     @Override
153     protected TableColumnModel createDefaultColumnModel() {
154         return new SwingTableColumnModel();
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public void setSortable(boolean sortable) {
160         super.setSortable(sortable);
161         if (hasSortController()) {
162             getSortController().setSortable(sortable);
163         }
164     }
165 
166     @Override
167     public void sorterChanged(RowSorterEvent e) {
168         setSorting(true);
169         super.sorterChanged(e);
170         setSorting(false);
171         firePropertyChange(PROPERTY_SORTED, false, true);
172     }
173 
174     private void setSorting(boolean sorting) {
175         boolean oldValue = this.sorting;
176         this.sorting = sorting;
177         firePropertyChange(PROPERTY_SORTING, oldValue, sorting);
178     }
179 
180     public boolean isSorting() {
181         return sorting;
182     }
183 
184     @Override
185     public <R extends TableModel> void setRowFilter(RowFilter<? super R, ? super Integer> filter) {
186         RowFilter oldValue = getRowFilter();
187         super.setRowFilter(filter);
188         firePropertyChange(PROPERTY_ROW_FILTER, oldValue, filter);
189     }
190 
191     public void selectCell(Integer rowIndex, Integer colIndex) {
192 
193         int row = rowIndex != null ? rowIndex : getSelectedRow();
194         row = row != -1 ? row : 0;
195         int col = colIndex != null ? colIndex : getSelectedColumn();
196         col = col != -1 ? col : 0;
197 
198         // Prevent select a non-existing row (Mantis #53717)
199         if (row >= 0 && row < getRowCount()) {
200             setRowSelectionInterval(row, row);
201         }
202         // Prevent select a non-existing column (Mantis #53717)
203         if (col >= 0 && col < getColumnCount()) {
204             setColumnSelectionInterval(col, col);
205         }
206         scrollCellToVisible(row, col);
207     }
208 
209 }