home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 7AIAX4 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.2 KB  |  248 lines

  1. package com.sun.java.swing.table;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JTable;
  7. import com.sun.java.swing.ToolTipManager;
  8. import com.sun.java.swing.UIManager;
  9. import com.sun.java.swing.event.ChangeEvent;
  10. import com.sun.java.swing.event.ListSelectionEvent;
  11. import com.sun.java.swing.event.TableColumnModelEvent;
  12. import com.sun.java.swing.event.TableColumnModelListener;
  13. import com.sun.java.swing.plaf.TableHeaderUI;
  14. import java.awt.AWTEvent;
  15. import java.awt.Component;
  16. import java.awt.Container;
  17. import java.awt.Point;
  18. import java.awt.Rectangle;
  19. import java.awt.event.InputEvent;
  20. import java.awt.event.MouseEvent;
  21. import java.util.Enumeration;
  22.  
  23. public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible {
  24.    protected JTable table;
  25.    protected TableColumnModel columnModel;
  26.    protected boolean reorderingAllowed;
  27.    protected boolean resizingAllowed;
  28.    protected boolean updateTableInRealTime;
  29.    protected transient TableColumn resizingColumn;
  30.    protected transient TableColumn draggedColumn;
  31.    protected transient int draggedDistance;
  32.  
  33.    public JTableHeader() {
  34.       this((TableColumnModel)null);
  35.    }
  36.  
  37.    public JTableHeader(TableColumnModel cm) {
  38.       if (cm == null) {
  39.          cm = this.createDefaultColumnModel();
  40.       }
  41.  
  42.       this.setColumnModel(cm);
  43.       this.initializeLocalVars();
  44.       this.updateUI();
  45.    }
  46.  
  47.    public void columnAdded(TableColumnModelEvent e) {
  48.       this.resizeAndRepaint();
  49.    }
  50.  
  51.    public int columnAtPoint(Point point) {
  52.       return this.getColumnModel().getColumnIndexAtX(point.x);
  53.    }
  54.  
  55.    public void columnMarginChanged(ChangeEvent e) {
  56.       this.resizeAndRepaint();
  57.    }
  58.  
  59.    public void columnMoved(TableColumnModelEvent e) {
  60.       ((Component)this).repaint();
  61.    }
  62.  
  63.    public void columnRemoved(TableColumnModelEvent e) {
  64.       this.resizeAndRepaint();
  65.    }
  66.  
  67.    public void columnSelectionChanged(ListSelectionEvent e) {
  68.    }
  69.  
  70.    protected TableColumnModel createDefaultColumnModel() {
  71.       return new DefaultTableColumnModel();
  72.    }
  73.  
  74.    public AccessibleContext getAccessibleContext() {
  75.       if (super.accessibleContext == null) {
  76.          super.accessibleContext = new AccessibleJTableHeader(this);
  77.       }
  78.  
  79.       return super.accessibleContext;
  80.    }
  81.  
  82.    public TableColumnModel getColumnModel() {
  83.       return this.columnModel;
  84.    }
  85.  
  86.    public TableColumn getDraggedColumn() {
  87.       return this.draggedColumn;
  88.    }
  89.  
  90.    public int getDraggedDistance() {
  91.       return this.draggedDistance;
  92.    }
  93.  
  94.    public Rectangle getHeaderRect(int columnIndex) {
  95.       TableColumnModel columnModel = this.getColumnModel();
  96.       if (columnIndex >= 0 && columnIndex < columnModel.getColumnCount()) {
  97.          int rectX = 0;
  98.          int column = 0;
  99.          int columnMargin = this.getColumnModel().getColumnMargin();
  100.  
  101.          for(Enumeration enumeration = this.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
  102.             TableColumn aColumn = (TableColumn)enumeration.nextElement();
  103.             if (column == columnIndex) {
  104.                return new Rectangle(rectX, 0, aColumn.getWidth() + columnMargin, ((Component)this).getSize().height);
  105.             }
  106.  
  107.             rectX += aColumn.getWidth() + columnMargin;
  108.          }
  109.  
  110.          return new Rectangle();
  111.       } else {
  112.          throw new IllegalArgumentException("Column index out of range");
  113.       }
  114.    }
  115.  
  116.    public boolean getReorderingAllowed() {
  117.       return this.reorderingAllowed;
  118.    }
  119.  
  120.    public boolean getResizingAllowed() {
  121.       return this.resizingAllowed;
  122.    }
  123.  
  124.    public TableColumn getResizingColumn() {
  125.       return this.resizingColumn;
  126.    }
  127.  
  128.    public JTable getTable() {
  129.       return this.table;
  130.    }
  131.  
  132.    public String getToolTipText(MouseEvent event) {
  133.       String tip = null;
  134.       Point p = event.getPoint();
  135.       int column;
  136.       if ((column = this.columnModel.getColumnIndexAtX(p.x)) != -1) {
  137.          TableColumn aColumn = this.columnModel.getColumn(column);
  138.          TableCellRenderer renderer = aColumn.getHeaderRenderer();
  139.          Component component = renderer.getTableCellRendererComponent(this.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
  140.          if (component instanceof JComponent) {
  141.             Rectangle cellRect = this.getHeaderRect(column);
  142.             p.translate(-cellRect.x, -cellRect.y);
  143.             MouseEvent newEvent = new MouseEvent(component, ((AWTEvent)event).getID(), ((InputEvent)event).getWhen(), ((InputEvent)event).getModifiers(), p.x, p.y, event.getClickCount(), event.isPopupTrigger());
  144.             tip = ((JComponent)component).getToolTipText(newEvent);
  145.          }
  146.       }
  147.  
  148.       if (tip == null) {
  149.          tip = ((JComponent)this).getToolTipText();
  150.       }
  151.  
  152.       return tip;
  153.    }
  154.  
  155.    public TableHeaderUI getUI() {
  156.       return (TableHeaderUI)super.ui;
  157.    }
  158.  
  159.    public String getUIClassID() {
  160.       return "TableHeaderUI";
  161.    }
  162.  
  163.    public boolean getUpdateTableInRealTime() {
  164.       return this.updateTableInRealTime;
  165.    }
  166.  
  167.    protected void initializeLocalVars() {
  168.       this.table = null;
  169.       this.reorderingAllowed = true;
  170.       this.resizingAllowed = true;
  171.       this.draggedColumn = null;
  172.       this.draggedDistance = 0;
  173.       this.resizingColumn = null;
  174.       this.updateTableInRealTime = true;
  175.       ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
  176.       toolTipManager.registerComponent(this);
  177.    }
  178.  
  179.    public boolean isOpaque() {
  180.       return true;
  181.    }
  182.  
  183.    public void resizeAndRepaint() {
  184.       ((JComponent)this).revalidate();
  185.       ((Component)this).repaint();
  186.    }
  187.  
  188.    public void setColumnModel(TableColumnModel newModel) {
  189.       if (newModel == null) {
  190.          throw new IllegalArgumentException("Cannot set a null ColumnModel");
  191.       } else {
  192.          TableColumnModel oldModel = this.columnModel;
  193.          if (newModel != oldModel) {
  194.             if (oldModel != null) {
  195.                oldModel.removeColumnModelListener(this);
  196.             }
  197.  
  198.             this.columnModel = newModel;
  199.             newModel.addColumnModelListener(this);
  200.             this.resizeAndRepaint();
  201.          }
  202.  
  203.       }
  204.    }
  205.  
  206.    public void setDraggedColumn(TableColumn aColumn) {
  207.       this.draggedColumn = aColumn;
  208.    }
  209.  
  210.    public void setDraggedDistance(int distance) {
  211.       this.draggedDistance = distance;
  212.    }
  213.  
  214.    public void setReorderingAllowed(boolean b) {
  215.       this.reorderingAllowed = b;
  216.    }
  217.  
  218.    public void setResizingAllowed(boolean b) {
  219.       this.resizingAllowed = b;
  220.    }
  221.  
  222.    public void setResizingColumn(TableColumn aColumn) {
  223.       this.resizingColumn = aColumn;
  224.    }
  225.  
  226.    public void setTable(JTable aTable) {
  227.       this.table = aTable;
  228.    }
  229.  
  230.    public void setUI(TableHeaderUI ui) {
  231.       if (super.ui != ui) {
  232.          super.setUI(ui);
  233.          ((Component)this).repaint();
  234.       }
  235.  
  236.    }
  237.  
  238.    public void setUpdateTableInRealTime(boolean flag) {
  239.       this.updateTableInRealTime = flag;
  240.    }
  241.  
  242.    public void updateUI() {
  243.       this.setUI((TableHeaderUI)UIManager.getUI(this));
  244.       this.resizeAndRepaint();
  245.       ((Container)this).invalidate();
  246.    }
  247. }
  248.