001 package nl.cwi.sen1.visplugin.table.model;
002
003 import java.awt.Component;
004
005 import java.util.Hashtable;
006
007 import javax.swing.ImageIcon;
008 import javax.swing.JButton;
009 import javax.swing.JTable;
010 import javax.swing.table.TableCellRenderer;
011 /**
012 * Sort button renderer. Class responsible for the button in the header.
013 * These buttons will show the order in which the current sort applies.
014 * This will be either descending or ascending, the initial state is no sorting.
015 *
016 * @author Anton Gerdessen
017 * @date 12-03-2007
018 */
019 public class SortButtonRenderer extends JButton implements TableCellRenderer {
020
021 private static final long serialVersionUID = 1L;
022
023 // Define the button states.
024 public static final int NONE = 0;
025 public static final int ASC = 1;
026 public static final int DEC = 2;
027
028 private Hashtable<Integer, Integer> state;
029 private JButton ascButton;
030 private JButton decButton;
031
032 /**
033 * Constructor.
034 *
035 * @author Anton Gerdessen
036 * @date 12-03-2007
037 */
038 public SortButtonRenderer() {
039 state = new Hashtable<Integer, Integer>();
040
041 // Create the ascending button with icon.
042 ascButton = new JButton();
043 ascButton.setHorizontalTextPosition(LEFT);
044 ImageIcon aicon = new ImageIcon(getClass().getResource(
045 "/resources/images/sortAsc.gif"));
046 ascButton.setIcon(aicon);
047
048 // Create the descending button with icon.
049 decButton = new JButton();
050 decButton.setHorizontalTextPosition(LEFT);
051 ImageIcon bicon = new ImageIcon(getClass().getResource(
052 "/resources/images/sortDec.gif"));
053 decButton.setIcon(bicon);
054
055 }
056
057 /**
058 * Method inherited from the TableCellRenderer
059 * Only the column and value attributes are used.
060 *
061 * @param table <not used>
062 * @param value The header buttons text
063 * @param isSelected <not used>
064 * @param hasFocus <not used>
065 * @param row <not used>
066 * @param column The column for which to change the header.
067 * @author Anton Gerdessen
068 * @date 12-03-2007
069 */
070 public Component getTableCellRendererComponent(JTable table, Object value,
071 boolean isSelected, boolean hasFocus, int row, int column) {
072
073 JButton button = this;
074
075 // Determine the state for sorting.
076 Object obj = state.get(new Integer(column));
077 if (obj != null) {
078 if (((Integer) obj).intValue() == ASC) {
079 button = ascButton;
080 } else {
081 button = decButton;
082 }
083 }
084
085 // Copy the text from the 'old' to on the 'new' button.
086 button.setText((value == null) ? "" : value.toString());
087
088 return button;
089 }
090
091 /**
092 * Set the column on which the current sort is based.
093 *
094 * @param column The column for which sort will be based
095 * @author Anton Gerdessen
096 * @date 12-03-2007
097 */
098 @SuppressWarnings("unchecked")
099 public void setSelectedColumn(int column) {
100
101 if (column < 0)
102 return;
103
104 // Inial state will be ascending. if a sorting order is already
105 // 'active', swap the sorting.
106 Integer value = null;
107 Object obj = state.get(new Integer(column));
108 if (obj == null) {
109 value = new Integer(ASC);
110 } else {
111 if (((Integer) obj).intValue() == ASC) {
112 value = new Integer(DEC);
113 } else {
114 value = new Integer(ASC);
115 }
116 }
117
118 // Clear the state and add the current column and its order to state.
119 state.clear();
120 state.put(new Integer(column), value);
121 }
122
123 /**
124 * Retrieve the current state for a given column.
125 *
126 * @param col The column for which to retrieve the sort order.
127 * @author Anton Gerdessen
128 * @date 12-03-2007
129 */
130 public int getState(int col) {
131 // If no sort order is given return none, else return the sort order.
132 int retValue;
133 Object obj = state.get(new Integer(col));
134
135 if (obj == null) {
136 retValue = NONE;
137 } else {
138 if (((Integer) obj).intValue() == ASC) {
139 retValue = ASC;
140 } else {
141 retValue = DEC;
142 }
143 }
144 return retValue;
145 }
146
147 }