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 / 1OXCP3H (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.8 KB  |  226 lines

  1. package com.sun.java.swing.table;
  2.  
  3. import com.sun.java.swing.JLabel;
  4. import com.sun.java.swing.JTable;
  5. import java.awt.Component;
  6. import java.beans.PropertyChangeListener;
  7. import java.beans.PropertyChangeSupport;
  8. import java.io.Serializable;
  9.  
  10. public class TableColumn implements Serializable {
  11.    public static final String COLUMN_WIDTH_PROPERTY = "columWidth";
  12.    public static final String HEADER_VALUE_PROPERTY = "headerValue";
  13.    public static final String HEADER_RENDERER_PROPERTY = "headerRenderer";
  14.    public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
  15.    protected int modelIndex;
  16.    protected Object identifier;
  17.    protected int width;
  18.    protected int minWidth;
  19.    protected int maxWidth;
  20.    protected TableCellRenderer headerRenderer;
  21.    protected Object headerValue;
  22.    protected TableCellRenderer cellRenderer;
  23.    protected TableCellEditor cellEditor;
  24.    protected boolean isResizable;
  25.    protected transient int resizedPostingDisableCount;
  26.    private PropertyChangeSupport changeSupport;
  27.  
  28.    public TableColumn() {
  29.       this(0);
  30.    }
  31.  
  32.    public TableColumn(int modelIndex) {
  33.       this(modelIndex, 75, (TableCellRenderer)null, (TableCellEditor)null);
  34.    }
  35.  
  36.    public TableColumn(int modelIndex, int width) {
  37.       this(modelIndex, width, (TableCellRenderer)null, (TableCellEditor)null);
  38.    }
  39.  
  40.    public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor) {
  41.       this.modelIndex = modelIndex;
  42.       this.width = width;
  43.       this.cellRenderer = cellRenderer;
  44.       this.cellEditor = cellEditor;
  45.       this.minWidth = 15;
  46.       this.maxWidth = Integer.MAX_VALUE;
  47.       this.isResizable = true;
  48.       this.resizedPostingDisableCount = 0;
  49.       this.setHeaderRenderer(this.createDefaultHeaderRenderer());
  50.       this.headerValue = null;
  51.    }
  52.  
  53.    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
  54.       if (this.changeSupport == null) {
  55.          this.changeSupport = new PropertyChangeSupport(this);
  56.       }
  57.  
  58.       this.changeSupport.addPropertyChangeListener(listener);
  59.    }
  60.  
  61.    protected TableCellRenderer createDefaultHeaderRenderer() {
  62.       DefaultTableCellRenderer label = new 1();
  63.       ((JLabel)label).setHorizontalAlignment(0);
  64.       return label;
  65.    }
  66.  
  67.    public void disableResizedPosting() {
  68.       ++this.resizedPostingDisableCount;
  69.    }
  70.  
  71.    public void enableResizedPosting() {
  72.       --this.resizedPostingDisableCount;
  73.    }
  74.  
  75.    public TableCellEditor getCellEditor() {
  76.       return this.cellEditor;
  77.    }
  78.  
  79.    public TableCellRenderer getCellRenderer() {
  80.       return this.cellRenderer;
  81.    }
  82.  
  83.    public TableCellRenderer getHeaderRenderer() {
  84.       return this.headerRenderer;
  85.    }
  86.  
  87.    public Object getHeaderValue() {
  88.       return this.headerValue;
  89.    }
  90.  
  91.    public Object getIdentifier() {
  92.       return this.identifier != null ? this.identifier : this.getHeaderValue();
  93.    }
  94.  
  95.    public int getMaxWidth() {
  96.       return this.maxWidth;
  97.    }
  98.  
  99.    public int getMinWidth() {
  100.       return this.minWidth;
  101.    }
  102.  
  103.    public int getModelIndex() {
  104.       return this.modelIndex;
  105.    }
  106.  
  107.    public boolean getResizable() {
  108.       return this.isResizable;
  109.    }
  110.  
  111.    public int getWidth() {
  112.       return this.width;
  113.    }
  114.  
  115.    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
  116.       if (this.changeSupport != null) {
  117.          this.changeSupport.removePropertyChangeListener(listener);
  118.       }
  119.  
  120.    }
  121.  
  122.    public void setCellEditor(TableCellEditor anEditor) {
  123.       this.cellEditor = anEditor;
  124.    }
  125.  
  126.    public void setCellRenderer(TableCellRenderer aRenderer) {
  127.       TableCellRenderer oldRenderer = this.cellRenderer;
  128.       this.cellRenderer = aRenderer;
  129.       if (this.changeSupport != null) {
  130.          this.changeSupport.firePropertyChange("cellRenderer", oldRenderer, this.cellRenderer);
  131.       }
  132.  
  133.    }
  134.  
  135.    public void setHeaderRenderer(TableCellRenderer aRenderer) {
  136.       TableCellRenderer oldRenderer = this.headerRenderer;
  137.       if (aRenderer == null) {
  138.          throw new IllegalArgumentException("Object is null");
  139.       } else {
  140.          this.headerRenderer = aRenderer;
  141.          if (this.changeSupport != null) {
  142.             this.changeSupport.firePropertyChange("headerRenderer", oldRenderer, this.headerRenderer);
  143.          }
  144.  
  145.       }
  146.    }
  147.  
  148.    public void setHeaderValue(Object aValue) {
  149.       Object oldValue = this.headerValue;
  150.       this.headerValue = aValue;
  151.       if (this.changeSupport != null) {
  152.          this.changeSupport.firePropertyChange("headerValue", oldValue, this.headerValue);
  153.       }
  154.  
  155.    }
  156.  
  157.    public void setIdentifier(Object anIdentifier) {
  158.       this.identifier = anIdentifier;
  159.    }
  160.  
  161.    public void setMaxWidth(int newMaxWidth) {
  162.       this.maxWidth = newMaxWidth;
  163.       if (this.maxWidth < 0) {
  164.          this.maxWidth = 0;
  165.       } else if (this.maxWidth < this.minWidth) {
  166.          this.maxWidth = this.minWidth;
  167.       }
  168.  
  169.       if (this.width > this.maxWidth) {
  170.          this.setWidth(this.maxWidth);
  171.       }
  172.  
  173.    }
  174.  
  175.    public void setMinWidth(int newMinWidth) {
  176.       this.minWidth = newMinWidth;
  177.       if (this.minWidth < 0) {
  178.          this.minWidth = 0;
  179.       }
  180.  
  181.       if (this.width < this.minWidth) {
  182.          this.setWidth(this.minWidth);
  183.       }
  184.  
  185.    }
  186.  
  187.    public void setModelIndex(int anIndex) {
  188.       this.modelIndex = anIndex;
  189.    }
  190.  
  191.    public void setResizable(boolean flag) {
  192.       this.isResizable = flag;
  193.    }
  194.  
  195.    public void setWidth(int newWidth) {
  196.       int oldWidth = this.width;
  197.       if (this.width != newWidth) {
  198.          this.width = newWidth;
  199.          if (this.width < this.minWidth) {
  200.             this.width = this.minWidth;
  201.          } else if (this.width > this.maxWidth) {
  202.             this.width = this.maxWidth;
  203.          }
  204.  
  205.          if (this.changeSupport != null) {
  206.             this.changeSupport.firePropertyChange("columWidth", new Integer(oldWidth), new Integer(this.width));
  207.          }
  208.  
  209.       }
  210.    }
  211.  
  212.    public void sizeWidthToFit() {
  213.       Component comp = this.getHeaderRenderer().getTableCellRendererComponent((JTable)null, this.getHeaderValue(), false, false, 0, 0);
  214.       int headerWidth = comp.getPreferredSize().width;
  215.       if (headerWidth > this.getMaxWidth()) {
  216.          this.setMaxWidth(headerWidth);
  217.       }
  218.  
  219.       if (headerWidth < this.getMinWidth()) {
  220.          this.setMinWidth(headerWidth);
  221.       }
  222.  
  223.       this.setWidth(headerWidth);
  224.    }
  225. }
  226.