001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.ui.grid; 022 023import java.awt.Component; 024import java.util.Arrays; 025 026import javax.swing.JTable; 027import javax.swing.LookAndFeel; 028import javax.swing.UIManager; 029import javax.swing.table.DefaultTableCellRenderer; 030 031import csheets.core.Address; 032 033/** 034 * The table cell renderer used for spreadsheet table headers. 035 * @author Einar Pehrson 036 */ 037@SuppressWarnings("serial") 038public class HeaderRenderer extends DefaultTableCellRenderer { 039 040 /** The axis of the header */ 041 private int axis; 042 043 /** 044 * Creates a new spreadsheet table header renderer. 045 * @param axis the axis of the header, either SwingConstants.HORIZONTAL 046 * or SwingConstants.VERTICAL 047 */ 048 public HeaderRenderer(int axis) { 049 // Checks and stores axis 050 if (axis != HORIZONTAL && axis != VERTICAL) 051 throw new IllegalArgumentException("Axis must be either" 052 + " SwingConstants.HORIZONTAL or SwingConstants.VERTICAL"); 053 this.axis = axis; 054 055 // Uses table header's appearance 056 LookAndFeel.installColorsAndFont(this, "TableHeader.background", 057 "TableHeader.foreground", "TableHeader.font"); 058 LookAndFeel.installProperty(this, "opaque", Boolean.TRUE); 059 setBorder(UIManager.getBorder("TableHeader.cellBorder")); 060 setHorizontalAlignment(CENTER); 061 } 062 063 public Component getTableCellRendererComponent(JTable table, Object value, 064 boolean selected, boolean hasFocus, int row, int column) { 065 // Checks column selection 066 if (axis == HORIZONTAL) 067 selected = Arrays.binarySearch(table.getSelectedColumns(), column) 068 >= 0; 069 070 // Selects and applies color 071 setBackground(UIManager.getColor("TableHeader." 072 + (selected ? "selectionBackground" : "background"))); 073 setForeground(UIManager.getColor("TableHeader." 074 + (selected ? "selectionForeground" : "foreground"))); 075 076 // Updates text and returns 077 if (axis == VERTICAL) 078 setText(Integer.toString(row + 1)); 079 else { 080 String columnStr; 081 int tempColumn = column; 082 for (columnStr = ""; tempColumn >= 0; tempColumn = tempColumn 083 / (Address.HIGHEST_CHAR - Address.LOWEST_CHAR + 1) - 1) 084 columnStr = (char)((char)(tempColumn % (Address.HIGHEST_CHAR 085 - Address.LOWEST_CHAR + 1)) + Address.LOWEST_CHAR) + columnStr; 086 setText(columnStr); 087 } 088 return this; 089 } 090}