home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / CellRendererPane.java < prev    next >
Text File  |  1998-02-26  |  18KB  |  577 lines

  1. /*
  2.  * @(#)CellRendererPane.java    1.19 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.io.*;
  25. import com.sun.java.swing.*;
  26. import java.beans.PropertyChangeListener;
  27. import java.util.Locale;
  28. import java.util.Vector;
  29.  
  30. import com.sun.java.accessibility.*;
  31.  
  32. /** 
  33.  * This class is inserted in between cell renderers and the components that 
  34.  * use them.  It just exists to thwart the repaint() and invalidate() methods 
  35.  * which would otherwise propogate up the tree when the renderer was configured.
  36.  * It's used by the implementations of JTable, JTree, and JList.  For example,
  37.  * here's how CellRendererPane is used in the code the paints each row
  38.  * in a JList:
  39.  * <pre>
  40.  *   cellRendererPane = new CellRendererPane();
  41.  *   ...
  42.  *   Component rendererComponent = renderer.getListCellRendererComponent();
  43.  *   renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
  44.  *   cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
  45.  * </pre>
  46.  * <p>
  47.  * A renderer component must override isShowing() and unconditionally return
  48.  * true to work correctly because the Swing paint does nothing for components
  49.  * with isShowing false.  
  50.  * <p>
  51.  * Warning: serialized objects of this class will not be compatible with
  52.  * future swing releases.  The current serialization support is appropriate 
  53.  * for short term storage or RMI between Swing1.0 applications.  It will
  54.  * not be possible to load serialized Swing1.0 objects with future releases
  55.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  56.  * baseline for the serialized form of Swing objects.
  57.  *
  58.  * @version 1.19 02/02/98
  59.  * @author unknown
  60.  */
  61. public class CellRendererPane extends Container implements Accessible
  62. {
  63.     public CellRendererPane() {
  64.     super();
  65.     setLayout(null);
  66.     setVisible(false);
  67.     }
  68.  
  69.     /** 
  70.      * Overridden to avoid propogating a invalidate up the tree when the
  71.      * cell renderer child is configured.
  72.      */
  73.     public void invalidate() { }
  74.  
  75.  
  76.     /** 
  77.      * Shouldn't be called.
  78.      */
  79.     public void paint(Graphics g) { }
  80.  
  81.   
  82.     /**
  83.      * Shouldn't be called.
  84.      */
  85.     public void update(Graphics g) { }
  86.  
  87.  
  88.     /** 
  89.      * If the specified component is already a child of this then we don't
  90.      * bother doing anything - stacking order doesn't matter for cell
  91.      * renderer components (CellRendererPane doesn't paint anyway).<
  92.      */
  93.     protected void addImpl(Component x, Object constraints, int index) {
  94.     if (x.getParent() == this) {
  95.         return;
  96.     }
  97.     else {
  98.         super.addImpl(x, constraints, index);
  99.     }
  100.     }
  101.  
  102.  
  103.     /** 
  104.      * Paint a cell renderer component c on graphics object g.  Before the component
  105.      * is drawn it's reparented to this (if that's neccessary), it's bounds 
  106.      * are set to w,h and the graphics object is (effectively) translated to x,y.  
  107.      * If it's a JComponent, double buffering is temporarily turned off. After 
  108.      * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if 
  109.      * it's the last renderer component painted, it will not start consuming input.  
  110.      * The Container p is the component we're actually drawing on, typically it's 
  111.      * equal to this.getParent(). If shouldValidate is true the component c will be 
  112.      * validated before painted.
  113.      */
  114.     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
  115.     if (c == null) {
  116.         if (p != null) {
  117.         Color oldColor = g.getColor();
  118.         g.setColor(p.getBackground());
  119.         g.fillRect(x, y, w, h);
  120.         g.setColor(oldColor);
  121.         }
  122.         return;
  123.     }
  124.  
  125.     if (c.getParent() != this) {
  126.         this.add(c);
  127.     }
  128.  
  129.     c.setBounds(x, y, w, h);
  130.  
  131.     if(shouldValidate) {
  132.         c.validate();
  133.     }
  134.  
  135.     boolean wasDoubleBuffered = false;
  136.     if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
  137.         wasDoubleBuffered = true;
  138.         ((JComponent)c).setDoubleBuffered(false);
  139.     }
  140.  
  141.     Graphics cg = SwingGraphics.createGraphics(g, x, y, w, h);
  142.     try {
  143.         c.paint(cg);
  144.     }
  145.     finally {
  146.         cg.dispose();
  147.     }
  148.  
  149.     if ((c instanceof JComponent) && wasDoubleBuffered) {
  150.         ((JComponent)c).setDoubleBuffered(true);
  151.     }
  152.  
  153.     if (c instanceof JComponent) {
  154.         JComponent jc = (JComponent)c;
  155.         jc.setDoubleBuffered(wasDoubleBuffered);
  156.     }
  157.  
  158.     c.setBounds(-w, -h, 0, 0);
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Calls this.paintComponent(g, c, p, x, y, w, h, false).
  164.      */
  165.     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
  166.     paintComponent(g, c, p, x, y, w, h, false);
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Calls this.paintComponent() with the rectangles x,y,width,height fields.
  172.      */
  173.     public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
  174.     paintComponent(g, c, p, r.x, r.y, r.width, r.height);
  175.     }
  176.  
  177.  
  178.     private void writeObject(ObjectOutputStream s) throws IOException {
  179.     removeAll();
  180.     s.defaultWriteObject();
  181.     }
  182.  
  183.  
  184. /////////////////
  185. // Accessibility support
  186. ////////////////
  187.  
  188.     protected AccessibleContext accessibleContext = null;
  189.  
  190.     /**
  191.      * Get the AccessibleContext associated with this CellRendererPane
  192.      *
  193.      * @return the AccessibleContext of this CellRendererPane
  194.      */
  195.     public AccessibleContext getAccessibleContext() {
  196.     if (accessibleContext == null) {
  197.         accessibleContext = new AccessibleCellRendererPane();
  198.     }
  199.     return accessibleContext;
  200.     }
  201.  
  202.  
  203.     protected class AccessibleCellRendererPane extends AccessibleContext
  204.     implements Serializable, AccessibleComponent 
  205.     {
  206.  
  207.         // AccessibleContext methods
  208.         //
  209.         /**
  210.          * Get the role of this object.
  211.          *
  212.          * @return an instance of AccessibleRole describing the role of the 
  213.      * object
  214.          * @see AccessibleRole
  215.          */
  216.         public AccessibleRole getAccessibleRole() {
  217.         return AccessibleRole.PANEL;
  218.         }
  219.  
  220.         /**
  221.          * Get the state of this object.
  222.          *
  223.          * @return an instance of AccessibleStateSet containing the current 
  224.      * state set of the object
  225.          * @see AccessibleState
  226.          */
  227.         public AccessibleStateSet getAccessibleStateSet() {
  228.         return SwingUtilities.getAccessibleStateSet(CellRendererPane.this);
  229.         }
  230.  
  231.         /**
  232.          * Get the Accessible parent of this object.  If the parent of this
  233.          * object implements Accessible, this method should simply return
  234.          * getParent().
  235.          *
  236.          * @return the Accessible parent of this object -- can be null if this
  237.          * object does not have an Accessible parent
  238.          */
  239.         public Accessible getAccessibleParent() {
  240.             Container parent = getParent();
  241.             if (parent instanceof Accessible) {
  242.                 return (Accessible) parent;
  243.             } else {
  244.                 return null;
  245.             }
  246.         }
  247.  
  248.         /**
  249.          * Get the index of this object in its accessible parent. 
  250.          *
  251.          * @return the index of this object in its parent; -1 if this 
  252.          * object does not have an accessible parent.
  253.          * @see #getAccessibleParent
  254.          */
  255.         public int getAccessibleIndexInParent() {
  256.         return SwingUtilities.getAccessibleIndexInParent(CellRendererPane.this);
  257.         }
  258.  
  259.         /**
  260.          * Returns the number of accessible children in the object.  If all
  261.          * of the children of this object implement Accessible, than this
  262.          * method should return the number of children of this object.
  263.          *
  264.          * @return the number of accessible children in the object.
  265.          */
  266.         public int getAccessibleChildrenCount() {
  267.         return SwingUtilities.getAccessibleChildrenCount(CellRendererPane.this);
  268.         }
  269.  
  270.         /**
  271.          * Return the nth Accessible child of the object.  
  272.          *
  273.          * @param i zero-based index of child
  274.          * @return the nth Accessible child of the object
  275.          */
  276.         public Accessible getAccessibleChild(int i) {
  277.             return SwingUtilities.getAccessibleChild(CellRendererPane.this,i);
  278.         }
  279.  
  280.         /**
  281.          * Return the locale of this object.
  282.      *
  283.          * @return the locale of this object
  284.          */
  285.         public Locale getLocale() {
  286.             return CellRendererPane.this.getLocale();
  287.         }
  288.  
  289.         /**
  290.          * Get the AccessibleComponent associated with this object if one
  291.          * exists.  Otherwise return null.
  292.          */
  293.     public AccessibleComponent getAccessibleComponent() {
  294.         return this;
  295.     }
  296.  
  297.  
  298.         // AccessibleComponent methods
  299.         //
  300.         /**
  301.          * Get the background color of this object.
  302.          *
  303.          * @return the background color, if supported, of the object; 
  304.          * otherwise, null
  305.          */
  306.         public Color getBackground() {
  307.         return CellRendererPane.this.getBackground();
  308.     }
  309.  
  310.         /**
  311.          * Set the background color of this object.
  312.          *
  313.          * @param c the new Color for the background
  314.          */
  315.         public void setBackground(Color c) {
  316.         CellRendererPane.this.setBackground(c);
  317.     }
  318.  
  319.         /**
  320.          * Get the foreground color of this object.
  321.          *
  322.          * @return the foreground color, if supported, of the object; 
  323.          * otherwise, null
  324.          */
  325.         public Color getForeground() {
  326.         return CellRendererPane.this.getForeground();
  327.     }
  328.  
  329.         /**
  330.          * Set the foreground color of this object.
  331.          *
  332.          * @param c the new Color for the foreground
  333.          */
  334.         public void setForeground(Color c) {
  335.         CellRendererPane.this.setForeground(c);
  336.     }
  337.  
  338.         /**
  339.          * Get the Cursor of this object.
  340.          *
  341.          * @return the Cursor, if supported, of the object; otherwise, null
  342.          */
  343.         public Cursor getCursor() {
  344.         return CellRendererPane.this.getCursor();
  345.     }
  346.  
  347.         /**
  348.          * Set the Cursor of this object.
  349.          *
  350.          * @param c the new Cursor for the object
  351.          */
  352.         public void setCursor(Cursor cursor) {
  353.         CellRendererPane.this.setCursor(cursor);
  354.     }
  355.  
  356.         /**
  357.          * Get the Font of this object.
  358.          *
  359.          * @return the Font,if supported, for the object; otherwise, null
  360.          */
  361.         public Font getFont() {
  362.         return CellRendererPane.this.getFont();
  363.     }
  364.  
  365.         /**
  366.          * Set the Font of this object.
  367.          *
  368.          * @param f the new Font for the object
  369.          */
  370.         public void setFont(Font f) {
  371.         CellRendererPane.this.setFont(f);
  372.     }
  373.  
  374.         /**
  375.          * Get the FontMetrics of this object.
  376.          *
  377.          * @param f the Font
  378.          * @return the FontMetrics, if supported, the object; otherwise, null
  379.          * @see getFont
  380.          */
  381.         public FontMetrics getFontMetrics(Font f) {
  382.         return CellRendererPane.this.getFontMetrics(f);
  383.     }
  384.  
  385.         /**
  386.          * Determine if the object is enabled.
  387.          *
  388.          * @return true if object is enabled; otherwise, false
  389.          */
  390.         public boolean isEnabled() {
  391.         return CellRendererPane.this.isEnabled();
  392.     }
  393.  
  394.         /**
  395.          * Set the enabled state of the object.
  396.          *
  397.          * @param b if true, enables this object; otherwise, disables it 
  398.          */
  399.         public void setEnabled(boolean b) {
  400.         CellRendererPane.this.setEnabled(b);
  401.     }
  402.     
  403.         /**
  404.          * Determine if the object is visible.  Note: this means that the
  405.          * object intends to be visible; however, it may not in fact be
  406.          * showing on the screen because one of the objects that this object
  407.          * is contained by is not visible.  To determine if an object is
  408.          * showing on the screen, use isShowing().
  409.          *
  410.          * @return true if object is visible; otherwise, false
  411.          */
  412.         public boolean isVisible() {
  413.         return CellRendererPane.this.isVisible();
  414.     }
  415.  
  416.         /**
  417.          * Set the visible state of the object.
  418.          *
  419.          * @param b if true, shows this object; otherwise, hides it 
  420.          */
  421.         public void setVisible(boolean b) {
  422.         CellRendererPane.this.setVisible(b);
  423.     }
  424.  
  425.         /**
  426.          * Determine if the object is showing.  This is determined by checking
  427.          * the visibility of the object and ancestors of the object.  Note: 
  428.      * this will return true even if the object is obscured by another 
  429.      * (for example, it happens to be underneath a menu that was pulled 
  430.      * down).
  431.          *
  432.          * @return true if object is showing; otherwise, false
  433.          */
  434.         public boolean isShowing() {
  435.         return CellRendererPane.this.isShowing();
  436.     }
  437.  
  438.         /** 
  439.          * Checks whether the specified point is within this object's bounds,
  440.          * where the point's x and y coordinates are defined to be relative to 
  441.      * the coordinate system of the object. 
  442.          *
  443.          * @param p the Point relative to the coordinate system of the object
  444.          * @return true if object contains Point; otherwise false
  445.          */
  446.         public boolean contains(Point p) {
  447.         return CellRendererPane.this.contains(p);
  448.     }
  449.     
  450.         /** 
  451.          * Returns the location of the object on the screen.
  452.          *
  453.          * @return location of object on screen -- can be null if this object
  454.          * is not on the screen
  455.          */
  456.         public Point getLocationOnScreen() {
  457.         return CellRendererPane.this.getLocationOnScreen();
  458.     }
  459.  
  460.         /** 
  461.          * Gets the location of the object relative to the parent in the form 
  462.          * of a point specifying the object's top-left corner in the screen's 
  463.          * coordinate space.
  464.          *
  465.          * @return An instance of Point representing the top-left corner of 
  466.      * the objects's bounds in the coordinate space of the screen; null if
  467.          * this object or its parent are not on the screen
  468.          */
  469.     public Point getLocation() {
  470.         return CellRendererPane.this.getLocation();
  471.     }
  472.  
  473.         /** 
  474.          * Sets the location of the object relative to the parent.
  475.          */
  476.         public void setLocation(Point p) {
  477.         CellRendererPane.this.setLocation(p);
  478.     }
  479.  
  480.         /** 
  481.          * Gets the bounds of this object in the form of a Rectangle object. 
  482.          * The bounds specify this object's width, height, and location
  483.          * relative to its parent. 
  484.          *
  485.          * @return A rectangle indicating this component's bounds; null if 
  486.      * this object is not on the screen.
  487.          */
  488.         public Rectangle getBounds() {
  489.         return CellRendererPane.this.getBounds();
  490.     }
  491.  
  492.         /** 
  493.          * Sets the bounds of this object in the form of a Rectangle object. 
  494.          * The bounds specify this object's width, height, and location
  495.          * relative to its parent.
  496.          *    
  497.          * @param A rectangle indicating this component's bounds
  498.          */
  499.         public void setBounds(Rectangle r) {
  500.         CellRendererPane.this.setBounds(r);
  501.     }
  502.  
  503.         /** 
  504.          * Returns the size of this object in the form of a Dimension object. 
  505.          * The height field of the Dimension object contains this objects's
  506.          * height, and the width field of the Dimension object contains this 
  507.      * object's width. 
  508.          *
  509.          * @return A Dimension object that indicates the size of this 
  510.      * component; null if this object is not on the screen
  511.          */
  512.         public Dimension getSize() {
  513.         return CellRendererPane.this.getSize();
  514.     }
  515.  
  516.         /** 
  517.          * Resizes this object so that it has width width and height. 
  518.          *    
  519.          * @param d - The dimension specifying the new size of the object. 
  520.          */
  521.         public void setSize(Dimension d) {
  522.         CellRendererPane.this.setSize(d);
  523.     }
  524.  
  525.         /**
  526.          * Returns the Accessible child, if one exists, contained at the local
  527.      * coordinate Point.
  528.          *
  529.          * @param p The point defining the top-left corner of the Accessible, 
  530.      * given in the coordinate space of the object's parent. 
  531.          * @return the Accessible, if it exists, at the specified location; 
  532.      * else null
  533.          */
  534.         public Accessible getAccessibleAt(Point p) {
  535.         return SwingUtilities.getAccessibleAt(CellRendererPane.this,p);
  536.     }
  537.  
  538.         /**
  539.          * Returns whether this object can accept focus or not.
  540.          *
  541.          * @return true if object can accept focus; otherwise false
  542.          */
  543.         public boolean isFocusTraversable() {
  544.         return CellRendererPane.this.isFocusTraversable();
  545.     }
  546.  
  547.         /**
  548.          * Requests focus for this object.
  549.          */
  550.         public void requestFocus() {
  551.         CellRendererPane.this.requestFocus();
  552.         }
  553.  
  554.         /**
  555.          * Adds the specified focus listener to receive focus events from this 
  556.          * component. 
  557.          *
  558.          * @param l the focus listener
  559.          */
  560.         public void addFocusListener(FocusListener l) {
  561.         CellRendererPane.this.addFocusListener(l);
  562.     }
  563.  
  564.         /**
  565.          * Removes the specified focus listener so it no longer receives focus 
  566.          * events from this component.
  567.          *
  568.          * @param l the focus listener
  569.          */
  570.         public void removeFocusListener(FocusListener l) {
  571.         CellRendererPane.this.removeFocusListener(l);
  572.     }
  573.     } // inner class AccessibleCellRendererPane
  574. }
  575.  
  576.  
  577.