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:     rendererPane = new CellRendererPane();
 143:     installDefaults();
 144:     installKeyboardActions();
 145:     installListeners();
 146:   }
 147: 
 148:   protected void uninstallDefaults()
 149:   {
 150:     header.setBackground(null);
 151:     header.setForeground(null);
 152:     header.setFont(null);
 153:   }
 154: 
 155:   protected void uninstallKeyboardActions()
 156:   {
 157:     // TODO: Implement this properly.
 158:   }
 159: 
 160:   protected void uninstallListeners()
 161:   {
 162:     header.removeMouseListener(mouseInputListener);
 163:   }
 164: 
 165:   public void uninstallUI(JComponent c)
 166:   {
 167:     uninstallListeners();
 168:     uninstallKeyboardActions();
 169:     uninstallDefaults();
 170:   }
 171: 
 172:   public void paint(Graphics gfx, JComponent c)
 173:   {
 174:     TableColumnModel cmod = header.getColumnModel();
 175:     int ncols = cmod.getColumnCount();
 176:     if (ncols == 0)
 177:       return;
 178:     
 179:     Rectangle clip = gfx.getClipBounds();
 180:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 181: 
 182:     for (int i = 0; i < ncols; ++i)
 183:       {
 184:         Rectangle bounds = header.getHeaderRect(i);
 185:         if (bounds.intersects(clip))
 186:           {
 187:             Rectangle oldClip = gfx.getClipBounds();
 188:             TableColumn col = cmod.getColumn(i);
 189:             TableCellRenderer rend = col.getHeaderRenderer();
 190:             if (rend == null)
 191:               rend = defaultRend;
 192:             Object val = col.getHeaderValue();
 193:             Component comp = rend.getTableCellRendererComponent(header.getTable(),
 194:                                                                 val,
 195:                                                                 false, // isSelected
 196:                                                                 false, // isFocused
 197:                                                                 -1, i);
 198:             // FIXME: The following settings should be performed in
 199:             // rend.getTableCellRendererComponent().
 200:             comp.setFont(header.getFont());
 201:             comp.setBackground(header.getBackground());
 202:             comp.setForeground(header.getForeground());
 203:             if (comp instanceof JComponent)
 204:               ((JComponent)comp).setBorder(cellBorder);
 205:             rendererPane.paintComponent(gfx, comp, header, bounds.x, bounds.y,
 206:                                         bounds.width, bounds.height);
 207:           }
 208:       }
 209: 
 210:   }
 211:   
 212:   public Dimension getPreferredSize(JComponent c)
 213:   {
 214:     TableColumnModel cmod = header.getColumnModel();
 215:     TableCellRenderer defaultRend = header.getDefaultRenderer();
 216:     int ncols = cmod.getColumnCount();    
 217:     Dimension ret = new Dimension(0,0);
 218:     int spacing = 0;
 219: 
 220:     if (header.getTable() != null 
 221:         && header.getTable().getIntercellSpacing() != null)
 222:       spacing = header.getTable().getIntercellSpacing().width;
 223:     
 224:     for (int i = 0; i < ncols; ++i)      
 225:       {
 226:         TableColumn col = cmod.getColumn(i);
 227:         TableCellRenderer rend = col.getHeaderRenderer();
 228:         if (rend == null)
 229:           rend = defaultRend;
 230:         Object val = col.getHeaderValue();
 231:         Component comp = rend.getTableCellRendererComponent(header.getTable(),
 232:                                                             val,
 233:                                                             false, // isSelected
 234:                                                             false, // isFocused
 235:                                                             -1, i);
 236:         comp.setFont(header.getFont());
 237:         comp.setBackground(header.getBackground());
 238:         comp.setForeground(header.getForeground());
 239:         if (comp instanceof JComponent)
 240:           ((JComponent)comp).setBorder(cellBorder);
 241: 
 242:         Dimension d = comp.getPreferredSize();
 243:         ret.width += spacing;
 244:         ret.height = Math.max(d.height, ret.height);        
 245:       }
 246:     ret.width = cmod.getTotalColumnWidth();
 247:     return ret;
 248:   }
 249:   
 250:   
 251: }