Source for javax.swing.plaf.basic.BasicTableHeaderUI

   1: /* BasicTableHeaderUI.java --
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.plaf.basic;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Dimension;
  43: import java.awt.Graphics;
  44: import java.awt.Rectangle;
  45: import java.awt.event.MouseEvent;
  46: 
  47: import javax.swing.CellRendererPane;
  48: import javax.swing.JComponent;
  49: import javax.swing.UIDefaults;
  50: import javax.swing.UIManager;
  51: import javax.swing.border.Border;
  52: import javax.swing.event.MouseInputListener;
  53: import javax.swing.plaf.ComponentUI;
  54: import javax.swing.plaf.TableHeaderUI;
  55: import javax.swing.table.JTableHeader;
  56: import javax.swing.table.TableCellRenderer;
  57: import javax.swing.table.TableColumn;
  58: import javax.swing.table.TableColumnModel;
  59: 
  60: public class BasicTableHeaderUI
  61:   extends TableHeaderUI
  62: {
  63: 
  64:   public static ComponentUI createUI(JComponent h)
  65:   {
  66:     return new BasicTableHeaderUI();
  67:   }
  68: 
  69:   protected JTableHeader header;
  70:   protected MouseInputListener mouseInputListener;
  71:   protected CellRendererPane rendererPane;
  72:   protected Border cellBorder;
  73: 
  74:   class MouseInputHandler
  75:     implements MouseInputListener
  76:   {
  77:     public void mouseClicked(MouseEvent e) {}
  78:     public void mouseDragged(MouseEvent e) {}
  79:     public void mouseEntered(MouseEvent e) {}
  80:     public void mouseExited(MouseEvent e) {}
  81:     public void mouseMoved(MouseEvent e) {}
  82:     public void mousePressed(MouseEvent e) {}
  83:     public void mouseReleased(MouseEvent e) {}
  84:   }
  85: 
  86:   protected MouseInputListener createMouseInputListener()
  87:   {
  88:     return new MouseInputHandler();
  89:   }
  90: 
  91:   public BasicTableHeaderUI()
  92:   {
  93:     mouseInputListener = createMouseInputListener();
  94:   }
  95: 
  96:   protected void installDefaults()
  97:   {
  98:     UIDefaults defaults = UIManager.getLookAndFeelDefaults();
  99:     header.setBackground(defaults.getColor("TableHeader.background"));
 100:     header.setForeground(defaults.getColor("TableHeader.foreground"));
 101:     header.setFont(defaults.getFont("TableHeader.font"));
 102:     cellBorder = defaults.getBorder("TableHeader.cellBorder");
 103:   }
 104: 
 105:   protected void installKeyboardActions()
 106:   {
 107:   }
 108: 
 109:   protected void installListeners()
 110:   {
 111:     header.addMouseListener(mouseInputListener);
 112:   }
 113: 
 114:   public void installUI(JComponent c)
 115:   {
 116:     header = (JTableHeader) c;
 117:     installDefaults();
 118:     installKeyboardActions();
 119:     installListeners();
 120:   }
 121: 
 122:   protected void uninstallDefaults()
 123:   {
 124:     header.setBackground(null);
 125:     header.setForeground(null);
 126:     header.setFont(null);
 127:   }
 128: 
 129:   protected void uninstallKeyboardActions()
 130:   {
 131:   }
 132: 
 133:   protected void uninstallListeners()
 134:   {
 135:     header.removeMouseListener(mouseInputListener);
 136:   }
 137: 
 138:   public void uninstallUI(JComponent c)
 139:   {
 140:     uninstallListeners();
 141:     uninstallKeyboardActions();
 142:     uninstallDefaults();
 143:   }
 144: 
 145:   public void paint(Graphics gfx, JComponent c)
 146:   {
 147:     TableColumnModel cmod = header.getColumnModel();
 148:     int ncols = cmod.getColumnCount();
 149:     if (ncols == 0)
 150:       return;
 151:     
 152:     Rectangle clip = gfx.getClipBounds();
 153:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 154: 
 155:     for (int i = 0; i < ncols; ++i)
 156:       {
 157:         Rectangle bounds = header.getHeaderRect(i);
 158:         if (bounds.intersects(clip))
 159:           {
 160:             TableColumn col = cmod.getColumn(i);
 161:             TableCellRenderer rend = col.getHeaderRenderer();
 162:             if (rend == null)
 163:               rend = defaultRend;
 164:             Object val = col.getHeaderValue();
 165:             Component comp = rend.getTableCellRendererComponent(header.getTable(),
 166:                                                                 val,
 167:                                                                 false, // isSelected
 168:                                                                 false, // isFocused
 169:                                                                 -1, i);
 170:             comp.setFont(header.getFont());
 171:             comp.setBackground(header.getBackground());
 172:             comp.setForeground(header.getForeground());
 173:             if (comp instanceof JComponent)
 174:               ((JComponent)comp).setBorder(cellBorder);
 175:             gfx.translate(bounds.x, bounds.y);
 176:             comp.setSize(bounds.width, bounds.height);
 177:             comp.setLocation(0,0);
 178:             comp.paint(gfx);
 179:             gfx.translate(-bounds.x, -bounds.y);
 180:           }
 181:       }
 182: 
 183:   }
 184:   
 185:   public Dimension getPreferredSize(JComponent c)
 186:   {
 187:     TableColumnModel cmod = header.getColumnModel();
 188:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 189:     int ncols = cmod.getColumnCount();    
 190:     Dimension ret = new Dimension(0,0);
 191:     int spacing = 0;
 192: 
 193:     if (header.getTable() != null 
 194:         && header.getTable().getIntercellSpacing() != null)
 195:       spacing = header.getTable().getIntercellSpacing().width;
 196:     
 197:     for (int i = 0; i < ncols; ++i)      
 198:       {
 199:         TableColumn col = cmod.getColumn(i);
 200:         TableCellRenderer rend = col.getHeaderRenderer();
 201:         if (rend == null)
 202:           rend = defaultRend;
 203:         Object val = col.getHeaderValue();
 204:         Component comp = rend.getTableCellRendererComponent(header.getTable(),
 205:                                                             val,
 206:                                                             false, // isSelected
 207:                                                             false, // isFocused
 208:                                                             -1, i);
 209:         comp.setFont(header.getFont());
 210:         comp.setBackground(header.getBackground());
 211:         comp.setForeground(header.getForeground());
 212:         if (comp instanceof JComponent)
 213:           ((JComponent)comp).setBorder(cellBorder);
 214: 
 215:         Dimension d = comp.getPreferredSize();
 216:         ret.width += spacing;
 217:         ret.height = Math.max(d.height, ret.height);        
 218:       }
 219:     ret.width = cmod.getTotalColumnWidth();
 220:     return ret;
 221:   }
 222:   
 223:   
 224: }