View Javadoc

1   /***
2    * SortableColumnHeaderMouseListener.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing.table;
27  
28  import java.awt.Point;
29  import java.awt.event.MouseAdapter;
30  
31  import javax.swing.SwingUtilities;
32  import javax.swing.table.JTableHeader;
33  import javax.swing.table.TableColumn;
34  
35  import org.jcreme.enumerations.SortOrder;
36  
37  /***
38   * This mouse listener enable to change the order of a column when the user
39   * clicks on the table header.
40   * 
41   * @author $Author: dbregeon $
42   * @version $Revision: 1.1 $
43   */
44  public class SortableColumnHeaderMouseListener extends MouseAdapter {
45      /***
46       * This method is called when the user clicks on the header of a column. If
47       * the column was in order SortOrder.NONE, it becomes in order
48       * SortOrder.ASCENDING. Otherwise it goes in order SortOrder.DESCENDING. All
49       * the other columns are put back to SortOrder.NONE.
50       * 
51       * @param mouseEvent
52       *            the click event to be processed.
53       */
54      public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
55          // Do not take into account double clicks.
56          if ((SwingUtilities.isLeftMouseButton(mouseEvent))
57                  && (mouseEvent.getClickCount() <= 1)
58                  && (mouseEvent.getComponent() instanceof JTableHeader)) {
59              JTableHeader header = (JTableHeader) mouseEvent.getComponent();
60              Point point = mouseEvent.getPoint();
61              TableColumn column = header.getColumnModel().getColumn(
62                      header.columnAtPoint(point));
63              if (column instanceof SortableTableColumn) {
64                  SortableTableColumn c = (SortableTableColumn) column;
65                  if (c.getOrder() == SortOrder.ASCENDING) {
66                      c.setOrder(SortOrder.DESCENDING);
67                  } else {
68                      c.setOrder(SortOrder.ASCENDING);
69                  }
70                  c.setOrderInSort(new Integer(1));
71              }
72              // ensure only one of the columns has a sort order.
73              java.util.Enumeration enum = header.getColumnModel().getColumns();
74              TableColumn current = null;
75              while (enum.hasMoreElements()) {
76                  current = (TableColumn) enum.nextElement();
77                  if ((current != column)
78                          && (current instanceof SortableTableColumn)) {
79                      ((SortableTableColumn) current).setOrder(SortOrder.NONE);
80                      ((SortableTableColumn) current).setOrderInSort(null);
81                  }
82              }
83              header.repaint();
84          }
85      }
86  }