1:
45:
46: package ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
69: public class SortButtonRenderer implements TableCellRenderer {
70:
71:
74: public static final int NONE = 0;
75:
76:
79: public static final int DOWN = 1;
80:
81:
84: public static final int UP = 2;
85:
86:
89: private int pressedColumn = -1;
90:
91:
94: private JButton normalButton;
95:
96:
99: private JButton ascendingButton;
100:
101:
104: private JButton descendingButton;
105:
106:
111: private boolean useLabels;
112:
113:
116: private JLabel normalLabel;
117:
118:
121: private JLabel ascendingLabel;
122:
123:
126: private JLabel descendingLabel;
127:
128:
131: public SortButtonRenderer() {
132:
133: this.pressedColumn = -1;
134: this.useLabels = UIManager.getLookAndFeel().getID().equals("Aqua");
135:
136: final Border border = UIManager.getBorder("TableHeader.cellBorder");
137:
138: if (this.useLabels) {
139: this.normalLabel = new JLabel();
140: this.normalLabel.setHorizontalAlignment(SwingConstants.LEADING);
141:
142: this.ascendingLabel = new JLabel();
143: this.ascendingLabel.setHorizontalAlignment(SwingConstants.LEADING);
144: this.ascendingLabel.setHorizontalTextPosition(SwingConstants.LEFT);
145: this.ascendingLabel.setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
146:
147: this.descendingLabel = new JLabel();
148: this.descendingLabel.setHorizontalAlignment(SwingConstants.LEADING);
149: this.descendingLabel.setHorizontalTextPosition(SwingConstants.LEFT);
150: this.descendingLabel.setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
151:
152: this.normalLabel.setBorder(border);
153: this.ascendingLabel.setBorder(border);
154: this.descendingLabel.setBorder(border);
155: }
156: else {
157: this.normalButton = new JButton();
158: this.normalButton.setMargin(new Insets(0, 0, 0, 0));
159: this.normalButton.setHorizontalAlignment(SwingConstants.LEADING);
160:
161: this.ascendingButton = new JButton();
162: this.ascendingButton.setMargin(new Insets(0, 0, 0, 0));
163: this.ascendingButton.setHorizontalAlignment(SwingConstants.LEADING);
164: this.ascendingButton.setHorizontalTextPosition(SwingConstants.LEFT);
165: this.ascendingButton.setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
166: this.ascendingButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, true));
167:
168: this.descendingButton = new JButton();
169: this.descendingButton.setMargin(new Insets(0, 0, 0, 0));
170: this.descendingButton.setHorizontalAlignment(SwingConstants.LEADING);
171: this.descendingButton.setHorizontalTextPosition(SwingConstants.LEFT);
172: this.descendingButton.setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
173: this.descendingButton.setPressedIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, true));
174:
175: this.normalButton.setBorder(border);
176: this.ascendingButton.setBorder(border);
177: this.descendingButton.setBorder(border);
178:
179: }
180:
181: }
182:
183:
194: public Component getTableCellRendererComponent(final JTable table,
195: final Object value,
196: final boolean isSelected,
197: final boolean hasFocus,
198: final int row, final int column) {
199:
200: if (table == null) {
201: throw new NullPointerException("Table must not be null.");
202: }
203:
204: final JComponent component;
205: final SortableTableModel model = (SortableTableModel) table.getModel();
206: final int cc = table.convertColumnIndexToModel(column);
207: final boolean isSorting = (model.getSortingColumn() == cc);
208: final boolean isAscending = model.isAscending();
209:
210: final JTableHeader header = table.getTableHeader();
211: final boolean isPressed = (cc == this.pressedColumn);
212:
213: if (this.useLabels) {
214: final JLabel label = getRendererLabel(isSorting, isAscending);
215: label.setText((value == null) ? "" : value.toString());
216: component = label;
217: }
218: else {
219: final JButton button = getRendererButton(isSorting, isAscending);
220: button.setText((value == null) ? "" : value.toString());
221: button.getModel().setPressed(isPressed);
222: button.getModel().setArmed(isPressed);
223: component = button;
224: }
225:
226: if (header != null) {
227: component.setForeground(header.getForeground());
228: component.setBackground(header.getBackground());
229: component.setFont(header.getFont());
230: }
231: return component;
232: }
233:
234:
241: private JButton getRendererButton(final boolean isSorting, final boolean isAscending) {
242: if (isSorting) {
243: if (isAscending) {
244: return ascendingButton;
245: }
246: else {
247: return descendingButton;
248: }
249: }
250: else {
251: return normalButton;
252: }
253: }
254:
255:
262: private JLabel getRendererLabel(final boolean isSorting, final boolean isAscending) {
263: if (isSorting) {
264: if (isAscending) {
265: return ascendingLabel;
266: }
267: else {
268: return descendingLabel;
269: }
270: }
271: else {
272: return normalLabel;
273: }
274: }
275:
276:
281: public void setPressedColumn(final int column) {
282: this.pressedColumn = column;
283: }
284:
285: }