001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.awt.event.InputEvent;
022: import java.awt.event.MouseAdapter;
023: import java.awt.event.MouseEvent;
024: import java.lang.ref.WeakReference;
025:
026: import javax.swing.JTable;
027: import javax.swing.table.TableColumn;
028: import javax.swing.table.TableColumnModel;
029:
030: /**
031: * This class listens for mouse events on the column headers of a sorted table.
032: * When the user clicks a column, this class invokes the appropriate sort
033: * routine for that column.
034: *
035: * @author Jeff Tassin
036: */
037: class TableSorterHeaderMouseAdapter extends MouseAdapter {
038: private WeakReference m_sorterref;
039: private WeakReference m_tableref;
040:
041: /**
042: * ctor
043: */
044: public TableSorterHeaderMouseAdapter(TableSorter sorter,
045: JTable table) {
046: m_sorterref = new WeakReference(sorter);
047: m_tableref = new WeakReference(table);
048: }
049:
050: /**
051: * @return the underlying table sorter. This will be null if the table has
052: * been gc'ed
053: */
054: TableSorter getSorter() {
055: return (TableSorter) m_sorterref.get();
056: }
057:
058: /**
059: * @return the underlying table. This will be null if the table has been
060: * gc'ed
061: */
062: JTable getTable() {
063: return (JTable) m_tableref.get();
064: }
065:
066: /**
067: * MouseAdapter event
068: */
069: public void mouseClicked(MouseEvent e) {
070: JTable table = getTable();
071: TableSorter sorter = getSorter();
072:
073: // sometimes the controller can be null here even if not garbage
074: // collected
075: if (sorter != null && table != null) {
076: TableColumnModel columnmodel = table.getColumnModel();
077: int viewcolumn = columnmodel.getColumnIndexAtX(e.getX());
078: if (viewcolumn < 0)
079: return;
080:
081: int modelcolumn = columnmodel.getColumn(viewcolumn)
082: .getModelIndex();
083:
084: TableColumn tablecol = columnmodel.getColumn(viewcolumn);
085: SortedColumnHeaderRenderer renderer = null;
086: Object obj = tablecol.getHeaderRenderer();
087: if (obj instanceof SortedColumnHeaderRenderer)
088: renderer = (SortedColumnHeaderRenderer) obj;
089:
090: if (e.getClickCount() == 1 && modelcolumn != -1) {
091: SortMode mode = SortMode.NONE;
092: int shiftpressed = e.getModifiers()
093: & InputEvent.SHIFT_MASK;
094: int ctrlpressed = e.getModifiers()
095: & InputEvent.CTRL_MASK;
096:
097: if (shiftpressed != 0 && ctrlpressed != 0)
098: mode = SortMode.NONE; // user explicitly chose natural
099: // sort
100: else if (shiftpressed != 0)
101: mode = SortMode.DESCENDING; // user explicitly chose
102: // descending sort
103: else if (ctrlpressed != 0)
104: mode = SortMode.ASCENDING; // user explicitly chose
105: // ascending sort
106: else {
107: // then toggle through the sort states
108: if (renderer != null) {
109: mode = renderer.getSortMode();
110: if (mode == SortMode.NONE)
111: mode = SortMode.ASCENDING;
112: else if (mode == SortMode.ASCENDING)
113: mode = SortMode.DESCENDING;
114: else if (mode == SortMode.DESCENDING)
115: mode = SortMode.NONE;
116: else
117: mode = SortMode.ASCENDING;
118: } else
119: mode = SortMode.ASCENDING;
120: }
121: sorter.sortByColumn(modelcolumn, mode);
122:
123: if (renderer != null) {
124: TableUtils.clearColumnHeaders(table);
125: renderer.setSortMode(mode);
126: }
127: }
128: }
129: }
130: }
|