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