View Javadoc

1   /*
2    * @(#) SortingHeaderCell.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.Color;
13  import java.awt.Component;
14  import java.awt.Graphics;
15  
16  import javax.swing.BorderFactory;
17  import javax.swing.table.DefaultTableCellRenderer;
18  
19  
20  /***
21   * The sorting header cell.
22   * <p>
23   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
24   * University of Technology </a>, the Netherlands. <br>
25   * See for project information <a
26   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
27   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
28   * License (GPL) </a>, no warranty <br>
29   * 
30   * @author <a
31   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
32   *         Lang </a><a
33   *         href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
34   *         Jacobs </a>
35   * @version 1.1 Apr 15, 2004
36   * @since 1.4
37   */
38  public class SortingHeaderCell extends DefaultTableCellRenderer
39  {
40  	/*** SORT_NONE means that sorting is off */
41  	public static final int SORT_NONE = 0;
42  
43  	/*** SORT_NONE means that sorting is ascending */
44  	public static final int SORT_ASCENDING = 1;
45  
46  	/*** SORT_NONE means that sorting is descending */
47  	public static final int SORT_DESCENDING = 2;
48  
49  	/*** counts the number of created instances */
50  	protected static int instanceCounter = 0;
51  
52  	/*** the sortMode */
53  	private int sortMode = SortingHeaderCell.SORT_NONE;
54  
55  	/*** the id of the instance */
56  	private int id = SortingHeaderCell.instanceCounter;
57  
58  	/***
59  	 * constructs a new SortingHeaderCell
60  	 * 
61  	 * @param sort the sort mode (none, ascending, descending).
62  	 */
63  	public SortingHeaderCell(final int sort)
64  	{
65  		this.sortMode = sort;
66  		this.setHorizontalAlignment((int) Component.CENTER_ALIGNMENT);
67  		this.setBackground(Color.LIGHT_GRAY);
68  		this.id = SortingHeaderCell.instanceCounter++;
69  	}
70  
71  	/***
72  	 * constructs a new SortingHeaderCell without sorting.
73  	 */
74  	public SortingHeaderCell()
75  	{
76  		this(SORT_NONE);
77  	}
78  
79  	/***
80  	 * changes the sort modus.
81  	 */
82  	public void changeSort()
83  	{
84  		this.sortMode = (this.sortMode + 1) % 3;
85  		this.repaint();
86  	}
87  
88  	/***
89  	 * sets the sort mode
90  	 * 
91  	 * @param sort the new mode
92  	 */
93  	public void setSort(final int sort)
94  	{
95  		this.sortMode = sort;
96  	}
97  
98  	/***
99  	 * @return returns the sort mode
100 	 */
101 	public int getSort()
102 	{
103 		return this.sortMode;
104 	}
105 
106 	/***
107 	 * is the header cell sorted?
108 	 * 
109 	 * @return whether the header cell is sorted
110 	 */
111 	public boolean isSorted()
112 	{
113 		return (this.sortMode != SORT_NONE);
114 	}
115 
116 	/***
117 	 * is the header cell ascendingly sorted?
118 	 * 
119 	 * @return whether the header cell is ascendingly sorted.
120 	 */
121 	public boolean isAscendingSorted()
122 	{
123 		return this.sortMode == SORT_ASCENDING;
124 	}
125 
126 	/***
127 	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
128 	 */
129 	public void paintComponent(final Graphics g)
130 	{
131 		super.paintComponent(g);
132 		g.setColor(Color.GRAY);
133 		int width = this.getWidth();
134 		int height = this.getHeight();
135 		if (this.sortMode == SortingHeaderCell.SORT_DESCENDING)
136 		{
137 			g.fillPolygon(new int[]{width - 14, width - 9, width - 4},
138 					new int[]{4, height - 4, 4}, 3);
139 		} else if (this.sortMode == SortingHeaderCell.SORT_ASCENDING)
140 		{
141 			g.fillPolygon(new int[]{width - 14, width - 9, width - 4},
142 					new int[]{height - 4, 4, height - 4}, 3);
143 		}
144 	}
145 
146 	/***
147 	 * @see javax.swing.table.DefaultTableCellRenderer#setValue(Object)
148 	 */
149 	protected void setValue(final Object value)
150 	{
151 		super.setValue(value);
152 		this.setBorder(BorderFactory.createRaisedBevelBorder());
153 	}
154 
155 	/***
156 	 * @see java.lang.Object#toString()
157 	 */
158 	public String toString()
159 	{
160 		return "SortingHeaderRenderer: " + this.id;
161 	}
162 }