001    /* CellRendererPane.java --
002       Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing;
040    
041    import java.awt.Component;
042    import java.awt.Container;
043    import java.awt.Graphics;
044    import java.awt.Rectangle;
045    
046    import javax.accessibility.Accessible;
047    import javax.accessibility.AccessibleContext;
048    import javax.accessibility.AccessibleRole;
049    
050    /**
051     * Paints the cells of JList, JTable and JTree.
052     * It intercepts the usual paint tree, so that we don't walk up and
053     * repaint everything.
054     *
055     * @author Andrew Selkirk
056     */
057    public class CellRendererPane extends Container implements Accessible
058    {
059      private static final long serialVersionUID = -7642183829532984273L;
060    
061      /**
062       * Provides accessibility support for CellRendererPanes.
063       */
064      protected class AccessibleCellRendererPane extends AccessibleAWTContainer
065      {
066        private static final long serialVersionUID = -8981090083147391074L;
067    
068        /**
069         * Constructor AccessibleCellRendererPane
070         */
071        protected AccessibleCellRendererPane()
072        {
073          // Nothing to do here.
074        }
075    
076        /**
077         * getAccessibleRole
078         * @return AccessibleRole
079         */
080        public AccessibleRole getAccessibleRole()
081        {
082          return AccessibleRole.PANEL;
083        }
084      }
085    
086      /**
087       * accessibleContext
088       */
089      protected AccessibleContext accessibleContext = null;
090    
091      /**
092       * Constructs a new CellRendererPane.
093       */
094      public CellRendererPane()
095      {
096        setVisible(false);
097      }
098    
099      /**
100       * Should not be called.
101       *
102       * @param graphics not used here
103       */
104      public void update(Graphics graphics)
105      {
106        //Nothing to do here.
107      }
108    
109      /**
110       * Despite normal behaviour this does <em>not</em> cause the container
111       * to be invalidated. This prevents propagating up the paint tree.
112       */
113      public void invalidate()
114      {
115        // Overridden to do nothing.
116      }
117    
118      /**
119       * Should not be called.
120       *
121       * @param graphics not used here
122       */
123      public void paint(Graphics graphics)
124      {
125        // Overridden to do nothing.
126      }
127    
128      /**
129       * Overridden to check if a component is already a child of this Container.
130       * If it's already a child, nothing is done. Otherwise we pass this to
131       * <code>super.addImpl()</code>.
132       *
133       * @param c the component to add
134       * @param constraints not used here
135       * @param index not used here
136       */
137      protected void addImpl(Component c, Object constraints, int index)
138      {
139        if (!isAncestorOf(c))
140          {
141            super.addImpl(c, constraints, index);
142          }
143      }
144    
145      /**
146       * Paints the specified component <code>c</code> on the {@link Graphics}
147       * context <code>graphics</code>. The Graphics context is tranlated to
148       * (x,y) and the components bounds are set to (w,h). If
149       * <code>shouldValidate</code>
150       * is set to true, then the component is validated before painting.
151       *
152       * @param graphics the graphics context to paint on
153       * @param c the component to be painted
154       * @param p the parent of the component
155       * @param x the X coordinate of the upper left corner where c should
156                be painted
157       * @param y the Y coordinate of the upper left corner where c should
158                be painted
159       * @param w the width of the components drawing area
160       * @param h the height of the components drawing area
161       * @param shouldValidate if <code>c</code> should be validated before
162       *        painting
163       */
164      public void paintComponent(Graphics graphics, Component c,
165                                 Container p, int x, int y, int w, int h,
166                                 boolean shouldValidate)
167      {
168        // reparent c
169        addImpl(c, null, 0);
170    
171        Rectangle oldClip = graphics.getClipBounds();
172        boolean translated = false;
173        try
174          {
175            // translate to (x,y)
176            graphics.translate(x, y);
177            translated = true;
178            graphics.clipRect(0, 0, w, h);
179            // set bounds of c
180            c.setBounds(0, 0, w, h);
181    
182            // validate if necessary
183            if (shouldValidate)
184              {
185                c.validate();
186              }
187    
188            // paint component
189            c.paint(graphics);
190          }
191        finally
192          {
193            // untranslate g
194            if (translated)
195              graphics.translate(-x, -y);
196            graphics.setClip(oldClip);
197          }
198      }
199    
200      /**
201       * Paints the specified component <code>c</code> on the {@link Graphics}
202       * context <code>graphics</code>. The Graphics context is tranlated to (x,y)
203       * and the components bounds are set to (w,h). The component is <em>not</em>
204       * validated before painting.
205       *
206       * @param graphics the graphics context to paint on
207       * @param c the component to be painted
208       * @param p the parent of the component
209       * @param x the X coordinate of the upper left corner where c should
210                be painted
211       * @param y the Y coordinate of the upper left corner where c should
212                be painted
213       * @param w the width of the components drawing area
214       * @param h the height of the components drawing area
215       */
216      public void paintComponent(Graphics graphics, Component c,
217                                 Container p, int x, int y, int w, int h)
218      {
219        paintComponent(graphics, c, p, x, y, w, h, false);
220      }
221    
222      /**
223       * Paints the specified component <code>c</code> on the {@link Graphics}
224       * context <code>g</code>. The Graphics context is tranlated to (r.x,r.y) and
225       * the components bounds are set to (r.width,r.height).
226       * The component is <em>not</em>
227       * validated before painting.
228       *
229       * @param graphics the graphics context to paint on
230       * @param c the component to be painted
231       * @param p the component on which we paint
232       * @param r the bounding rectangle of c
233       */
234      public void paintComponent(Graphics graphics, Component c,
235                                 Container p, Rectangle r)
236      {
237        paintComponent(graphics, c, p, r.x, r.y, r.width, r.height);
238      }
239    
240      /**
241       * getAccessibleContext <em>TODO</em>
242       * @return AccessibleContext
243       */
244      public AccessibleContext getAccessibleContext()
245      {
246        if (accessibleContext == null)
247          accessibleContext = new AccessibleCellRendererPane();
248    
249        return accessibleContext;
250      }
251    }