View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.extensions;
17  
18  import javax.swing.JTable;
19  import javax.swing.table.TableColumnModel;
20  import javax.swing.table.TableModel;
21  
22  /**
23   * The nyx replacement for a table.
24   * Trying to prevent a lot of unnecessary processing of
25   * things we'll never use..
26   *
27   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
28   * @version $Id: NyxJTable.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
29   */
30  public class NyxJTable extends JTable {
31  
32      /**
33       * the sibling table
34       */
35      private NyxJTable siblingTable;
36      /**
37       * is the table changing
38       */
39      private boolean changing;
40  
41      /**
42       *
43       */
44      public NyxJTable() {
45          super();
46      }
47  
48      /**
49       * @param dm the tablemodel
50       * @param cm the columnmodel
51       */
52      public NyxJTable(TableModel dm, TableColumnModel cm) {
53          super(dm, cm);
54      }
55  
56      /**
57       * @see javax.swing.JTable#changeSelection(int, int, boolean, boolean)
58       */
59      public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
60          super.changeSelection(rowIndex, columnIndex, toggle, extend);
61          if (getSiblingTable() != null && !getSiblingTable().isChanging()) {
62              changing = true;
63              getSiblingTable().changeSelection(rowIndex, columnIndex, toggle, extend);
64              changing = false;
65          }
66      }
67  
68      /**
69       * @return the sublingtable or null if non present.
70       */
71      public NyxJTable getSiblingTable() {
72          return siblingTable;
73      }
74  
75      /**
76       * @param siblingTable - the table that should behave exactly like the other..
77       */
78      public void setSiblingTable(NyxJTable siblingTable) {
79          this.siblingTable = siblingTable;
80      }
81  
82      /**
83       * @return if the table is currently changing.
84       */
85      public boolean isChanging() {
86          return changing;
87      }
88  
89      /**
90       * @see javax.swing.JTable#setColumnSelectionInterval(int, int)
91       */
92      public void setColumnSelectionInterval(int index0, int index1) {
93          // siblingtable can have a different number of columns,
94          // so check to see if this is the case and set the index1 to
95          // the current number of columns-1 (starts from 0).
96          if (index1 >= getColumnCount()) {
97              index1 = getColumnCount() - 1;
98          }
99          super.setColumnSelectionInterval(index0, index1);
100         if (getSiblingTable() != null && !getSiblingTable().isChanging()) {
101             changing = true;
102             getSiblingTable().setColumnSelectionInterval(index0, index1);
103             changing = false;
104         }
105     }
106 
107     /**
108      * @see javax.swing.JTable#setRowSelectionInterval(int, int)
109      */
110     public void setRowSelectionInterval(int index0, int index1) {
111         super.setRowSelectionInterval(index0, index1);
112         if (getSiblingTable() != null && !getSiblingTable().isChanging()) {
113             changing = true;
114             getSiblingTable().setRowSelectionInterval(index0, index1);
115             changing = false;
116         }
117     }
118 
119 }