View Javadoc

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