View Javadoc

1   /*
2    * @(#) SortingTableHeader.java April 15, 2004 Copyright (c) 2002-2005-2004
3    * Delft University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands.
4    * All rights reserved. This software is proprietary information of Delft
5    * University of Technology The code is published under the Lesser General
6    * Public License
7    */
8   package nl.tudelft.simulation.introspection.sortable;
9   
10  import java.awt.event.MouseAdapter;
11  import java.awt.event.MouseEvent;
12  import java.util.ArrayList;
13  import java.util.Arrays;
14  import java.util.List;
15  
16  import javax.swing.JTable;
17  import javax.swing.table.JTableHeader;
18  import javax.swing.table.TableColumn;
19  import javax.swing.table.TableColumnModel;
20  import javax.swing.table.TableModel;
21  
22  /***
23   * The sortingTableHeader class.
24   * <p>
25   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
26   * University of Technology </a>, the Netherlands. <br>
27   * See for project information <a
28   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
30   * General Public License (LGPL) </a>, no warranty.
31   * 
32   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
33   * @version 1.2 Apr 14, 2004
34   * @since 1.5
35   */
36  public class SortingTableHeader extends JTableHeader
37  {
38      /*** the definitions of the tableHeader */
39      protected List<Sortable.Definition> definitions = new ArrayList<Sortable.Definition>(
40              5);
41  
42      /***
43       * constructs a new SortingTableHeader
44       * 
45       * @param defaultSorting the defaultSorting definitions
46       */
47      public SortingTableHeader(final Sortable.Definition[] defaultSorting)
48      {
49          this.definitions.addAll(Arrays.asList(defaultSorting));
50          this.init();
51      }
52  
53      /***
54       * initializes the SortingTableHeader
55       */
56      private void init()
57      {
58          this.addMouseListener(new MouseAdapter()
59          {
60              /***
61               * @see java.awt.event.MouseAdapter#mouseClicked(MouseEvent)
62               */
63              @Override
64              public void mouseClicked(final MouseEvent e)
65              {
66                  int column = columnAtPoint(e.getPoint());
67                  TableColumn col = getColumnModel().getColumn(column);
68                  SortingHeaderCell header = (SortingHeaderCell) col
69                          .getHeaderRenderer();
70                  header.changeSort();
71                  if (!e.isShiftDown())
72                  {
73                      SortingTableHeader.this.definitions.clear();
74                      SortingTableHeader.this.clearHeaders(header);
75                      if (header.isSorted())
76                          SortingTableHeader.this.definitions
77                                  .add(new SortDefinition(column, header
78                                          .isAscendingSorted()));
79                  } else
80                  {
81                      Sortable.Definition def = SortingTableHeader.this
82                              .getMatchingDef(column);
83                      if (def == null)
84                          SortingTableHeader.this.definitions
85                                  .add(new SortDefinition(column, header
86                                          .isAscendingSorted()));
87                      else
88                      {
89                          if (!header.isSorted())
90                              SortingTableHeader.this.definitions.remove(def);
91                          else
92                              def.setAscending(header.isAscendingSorted());
93                      }
94                  }
95                  SortingTableHeader.this.propagateSort();
96              }
97  
98          });
99      }
100 
101     /***
102      * gets the matching definition
103      * 
104      * @param column the columnNumber
105      * @return the definition
106      */
107     protected Sortable.Definition getMatchingDef(final int column)
108     {
109         for (Sortable.Definition def : this.definitions)
110         {
111             if (def.getFieldID() == column)
112             {
113                 return def;
114             }
115         }
116         return null;
117     }
118 
119     /***
120      * clears the header
121      * 
122      * @param butThisOne all but this one
123      */
124     protected void clearHeaders(final SortingHeaderCell butThisOne)
125     {
126         for (int i = 0; i < getColumnModel().getColumnCount(); i++)
127         {
128             SortingHeaderCell current = (SortingHeaderCell) getColumnModel()
129                     .getColumn(i).getHeaderRenderer();
130             if (!current.equals(butThisOne))
131             {
132                 current.setSort(SortingHeaderCell.SORT_NONE);
133             }
134         }
135     }
136 
137     /***
138      * propagates the sort
139      */
140     protected void propagateSort()
141     {
142         TableModel model = this.getTable().getModel();
143         if (model instanceof Sortable)
144         {
145             ((Sortable) model).setDefinitions(this.definitions
146                     .toArray(new SortDefinition[0]));
147             ((Sortable) model).sort();
148         }
149     }
150 
151     /***
152      * @see javax.swing.table.JTableHeader#setColumnModel(TableColumnModel)
153      */
154     @Override
155     public void setColumnModel(final TableColumnModel columnModel)
156     {
157         super.setColumnModel(columnModel);
158         for (int i = 0; i < this.getColumnModel().getColumnCount(); i++)
159         {
160             int sort = getSort(i);
161             this.getColumnModel().getColumn(i).setHeaderRenderer(
162                     new SortingHeaderCell(sort));
163         }
164     }
165 
166     /***
167      * gets the sorting of the column
168      * 
169      * @param column the column number
170      * @return the value
171      */
172     private int getSort(final int column)
173     {
174         for (Sortable.Definition def : this.definitions)
175         {
176             if (def.getFieldID() == column)
177             {
178                 if (def.isAcendingSort())
179                 {
180                     return SortingHeaderCell.SORT_ASCENDING;
181                 }
182                 return SortingHeaderCell.SORT_DESCENDING;
183             }
184         }
185         return SortingHeaderCell.SORT_NONE;
186     }
187 
188     /***
189      * @see javax.swing.table.JTableHeader#setTable(JTable)
190      */
191     @Override
192     public void setTable(final JTable table)
193     {
194         super.setTable(table);
195         this.propagateSort();
196     }
197 }