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.LookAndFeel;
  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 extends TableHeaderUI
  61: {
  62: 
  63:   public static ComponentUI createUI(JComponent h)
  64:   {
  65:     return new BasicTableHeaderUI();
  66:   }
  67: 
  68:   protected JTableHeader header;
  69:   protected MouseInputListener mouseInputListener;
  70:   protected CellRendererPane rendererPane;
  71:   protected Border cellBorder;
  72: 
  73:   public class MouseInputHandler implements MouseInputListener
  74:   {
  75:     public void mouseClicked(MouseEvent e)
  76:     {
  77:       // TODO: Implement this properly.
  78:     }
  79: 
  80:     public void mouseDragged(MouseEvent e)
  81:     {
  82:       // TODO: Implement this properly.
  83:     }
  84: 
  85:     public void mouseEntered(MouseEvent e)
  86:     {
  87:       // TODO: Implement this properly.
  88:     }
  89: 
  90:     public void mouseExited(MouseEvent e)
  91:     {
  92:       // TODO: Implement this properly.
  93:     }
  94: 
  95:     public void mouseMoved(MouseEvent e)
  96:     {
  97:       // TODO: Implement this properly.
  98:     }
  99: 
 100:     public void mousePressed(MouseEvent e)
 101:     {
 102:       // TODO: Implement this properly.
 103:     }
 104: 
 105:     public void mouseReleased(MouseEvent e)
 106:     {
 107:       // TODO: Implement this properly.
 108:     }
 109:   }
 110: 
 111:   protected MouseInputListener createMouseInputListener()
 112:   {
 113:     return new MouseInputHandler();
 114:   }
 115: 
 116:   public BasicTableHeaderUI()
 117:   {
 118:     mouseInputListener = createMouseInputListener();
 119:   }
 120: 
 121:   protected void installDefaults()
 122:   {
 123:     LookAndFeel.installColorsAndFont(header, "TableHeader.background",
 124:                                      "TableHeader.foreground",
 125:                                      "TableHeader.font");
 126:     cellBorder = UIManager.getBorder("TableHeader.cellBorder");
 127:   }
 128: 
 129:   protected void installKeyboardActions()
 130:   {
 131:     // TODO: Implement this properly.
 132:   }
 133: 
 134:   protected void installListeners()
 135:   {
 136:     header.addMouseListener(mouseInputListener);
 137:   }
 138: 
 139:   public void installUI(JComponent c)
 140:   {
 141:     header = (JTableHeader) c;
 142:     installDefaults();
 143:     installKeyboardActions();
 144:     installListeners();
 145:   }
 146: 
 147:   protected void uninstallDefaults()
 148:   {
 149:     header.setBackground(null);
 150:     header.setForeground(null);
 151:     header.setFont(null);
 152:   }
 153: 
 154:   protected void uninstallKeyboardActions()
 155:   {
 156:     // TODO: Implement this properly.
 157:   }
 158: 
 159:   protected void uninstallListeners()
 160:   {
 161:     header.removeMouseListener(mouseInputListener);
 162:   }
 163: 
 164:   public void uninstallUI(JComponent c)
 165:   {
 166:     uninstallListeners();
 167:     uninstallKeyboardActions();
 168:     uninstallDefaults();
 169:   }
 170: 
 171:   public void paint(Graphics gfx, JComponent c)
 172:   {
 173:     TableColumnModel cmod = header.getColumnModel();
 174:     int ncols = cmod.getColumnCount();
 175:     if (ncols == 0)
 176:       return;
 177:     
 178:     Rectangle clip = gfx.getClipBounds();
 179:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 180: 
 181:     for (int i = 0; i < ncols; ++i)
 182:       {
 183:         Rectangle bounds = header.getHeaderRect(i);
 184:         if (bounds.intersects(clip))
 185:           {
 186:             Rectangle oldClip = gfx.getClipBounds();
 187:             TableColumn col = cmod.getColumn(i);
 188:             TableCellRenderer rend = col.getHeaderRenderer();
 189:             if (rend == null)
 190:               rend = defaultRend;
 191:             Object val = col.getHeaderValue();
 192:             Component comp = rend.getTableCellRendererComponent(header.getTable(),
 193:                                                                 val,
 194:                                                                 false, // isSelected
 195:                                                                 false, // isFocused
 196:                                                                 -1, i);
 197:             comp.setFont(header.getFont());
 198:             comp.setBackground(header.getBackground());
 199:             comp.setForeground(header.getForeground());
 200:             if (comp instanceof JComponent)
 201:               ((JComponent)comp).setBorder(cellBorder);
 202:             gfx.translate(bounds.x, bounds.y);
 203:             gfx.setClip(0, 0, bounds.width, bounds.height);
 204:             comp.setSize(bounds.width, bounds.height);
 205:             comp.setLocation(0,0);
 206:             comp.paint(gfx);
 207:             gfx.translate(-bounds.x, -bounds.y);
 208:             gfx.setClip(oldClip);
 209:           }
 210:       }
 211: 
 212:   }
 213:   
 214:   public Dimension getPreferredSize(JComponent c)
 215:   {
 216:     TableColumnModel cmod = header.getColumnModel();
 217:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 218:     int ncols = cmod.getColumnCount();    
 219:     Dimension ret = new Dimension(0,0);
 220:     int spacing = 0;
 221: 
 222:     if (header.getTable() != null 
 223:         && header.getTable().getIntercellSpacing() != null)
 224:       spacing = header.getTable().getIntercellSpacing().width;
 225:     
 226:     for (int i = 0; i < ncols; ++i)      
 227:       {
 228:         TableColumn col = cmod.getColumn(i);
 229:         TableCellRenderer rend = col.getHeaderRenderer();
 230:         if (rend == null)
 231:           rend = defaultRend;
 232:         Object val = col.getHeaderValue();
 233:         Component comp = rend.getTableCellRendererComponent(header.getTable(),
 234:                                                             val,
 235:                                                             false, // isSelected
 236:                                                             false, // isFocused
 237:                                                             -1, i);
 238:         comp.setFont(header.getFont());
 239:         comp.setBackground(header.getBackground());
 240:         comp.setForeground(header.getForeground());
 241:         if (comp instanceof JComponent)
 242:           ((JComponent)comp).setBorder(cellBorder);
 243: 
 244:         Dimension d = comp.getPreferredSize();
 245:         ret.width += spacing;
 246:         ret.height = Math.max(d.height, ret.height);        
 247:       }
 248:     ret.width = cmod.getTotalColumnWidth();
 249:     return ret;
 250:   }
 251:   
 252:   
 253: }